datadog-ci 0.5.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +60 -1
  3. data/README.md +1 -1
  4. data/lib/datadog/ci/codeowners/matcher.rb +102 -0
  5. data/lib/datadog/ci/codeowners/parser.rb +42 -0
  6. data/lib/datadog/ci/codeowners/rule.rb +33 -0
  7. data/lib/datadog/ci/concurrent_span.rb +2 -1
  8. data/lib/datadog/ci/configuration/components.rb +15 -11
  9. data/lib/datadog/ci/configuration/settings.rb +15 -0
  10. data/lib/datadog/ci/contrib/cucumber/configuration/settings.rb +4 -1
  11. data/lib/datadog/ci/contrib/cucumber/ext.rb +7 -5
  12. data/lib/datadog/ci/contrib/cucumber/formatter.rb +138 -27
  13. data/lib/datadog/ci/contrib/cucumber/instrumentation.rb +2 -1
  14. data/lib/datadog/ci/contrib/minitest/configuration/settings.rb +4 -1
  15. data/lib/datadog/ci/contrib/minitest/ext.rb +6 -4
  16. data/lib/datadog/ci/contrib/minitest/helpers.rb +23 -0
  17. data/lib/datadog/ci/contrib/minitest/hooks.rb +34 -19
  18. data/lib/datadog/ci/contrib/minitest/patcher.rb +10 -0
  19. data/lib/datadog/ci/contrib/minitest/reporter.rb +50 -0
  20. data/lib/datadog/ci/contrib/minitest/runnable.rb +42 -0
  21. data/lib/datadog/ci/contrib/minitest/runner.rb +41 -0
  22. data/lib/datadog/ci/contrib/rspec/configuration/settings.rb +4 -1
  23. data/lib/datadog/ci/contrib/rspec/example.rb +55 -14
  24. data/lib/datadog/ci/contrib/rspec/example_group.rb +51 -0
  25. data/lib/datadog/ci/contrib/rspec/ext.rb +5 -4
  26. data/lib/datadog/ci/contrib/rspec/integration.rb +1 -1
  27. data/lib/datadog/ci/contrib/rspec/patcher.rb +14 -0
  28. data/lib/datadog/ci/contrib/rspec/runner.rb +57 -0
  29. data/lib/datadog/ci/contrib/settings.rb +1 -1
  30. data/lib/datadog/ci/ext/app_types.rb +2 -0
  31. data/lib/datadog/ci/ext/environment/providers/local_git.rb +8 -29
  32. data/lib/datadog/ci/ext/settings.rb +1 -0
  33. data/lib/datadog/ci/ext/test.rb +19 -6
  34. data/lib/datadog/ci/span.rb +33 -2
  35. data/lib/datadog/ci/test.rb +96 -1
  36. data/lib/datadog/ci/test_module.rb +1 -1
  37. data/lib/datadog/ci/test_session.rb +10 -2
  38. data/lib/datadog/ci/test_suite.rb +53 -2
  39. data/lib/datadog/ci/test_visibility/context/local.rb +3 -9
  40. data/lib/datadog/ci/test_visibility/null_recorder.rb +53 -0
  41. data/lib/datadog/ci/test_visibility/recorder.rb +52 -25
  42. data/lib/datadog/ci/test_visibility/serializers/base.rb +6 -5
  43. data/lib/datadog/ci/test_visibility/serializers/span.rb +1 -1
  44. data/lib/datadog/ci/test_visibility/serializers/test_module.rb +1 -1
  45. data/lib/datadog/ci/test_visibility/serializers/test_session.rb +1 -1
  46. data/lib/datadog/ci/test_visibility/serializers/test_suite.rb +1 -1
  47. data/lib/datadog/ci/test_visibility/serializers/test_v1.rb +1 -1
  48. data/lib/datadog/ci/utils/configuration.rb +15 -0
  49. data/lib/datadog/ci/utils/git.rb +70 -0
  50. data/lib/datadog/ci/version.rb +2 -2
  51. data/lib/datadog/ci.rb +40 -56
  52. metadata +14 -82
  53. data/lib/datadog/ci/null_span.rb +0 -63
  54. data/sig/datadog/ci/concurrent_span.rbs +0 -23
  55. data/sig/datadog/ci/configuration/components.rbs +0 -21
  56. data/sig/datadog/ci/configuration/extensions.rbs +0 -9
  57. data/sig/datadog/ci/configuration/settings.rbs +0 -16
  58. data/sig/datadog/ci/contrib/cucumber/configuration/settings.rbs +0 -12
  59. data/sig/datadog/ci/contrib/cucumber/ext.rbs +0 -25
  60. data/sig/datadog/ci/contrib/cucumber/formatter.rbs +0 -35
  61. data/sig/datadog/ci/contrib/cucumber/instrumentation.rbs +0 -16
  62. data/sig/datadog/ci/contrib/cucumber/integration.rbs +0 -26
  63. data/sig/datadog/ci/contrib/cucumber/patcher.rbs +0 -15
  64. data/sig/datadog/ci/contrib/integration.rbs +0 -44
  65. data/sig/datadog/ci/contrib/minitest/configuration/settings.rbs +0 -12
  66. data/sig/datadog/ci/contrib/minitest/ext.rbs +0 -23
  67. data/sig/datadog/ci/contrib/minitest/hooks.rbs +0 -19
  68. data/sig/datadog/ci/contrib/minitest/integration.rbs +0 -26
  69. data/sig/datadog/ci/contrib/minitest/patcher.rbs +0 -15
  70. data/sig/datadog/ci/contrib/rspec/configuration/settings.rbs +0 -12
  71. data/sig/datadog/ci/contrib/rspec/example.rbs +0 -20
  72. data/sig/datadog/ci/contrib/rspec/ext.rbs +0 -23
  73. data/sig/datadog/ci/contrib/rspec/integration.rbs +0 -26
  74. data/sig/datadog/ci/contrib/rspec/patcher.rbs +0 -15
  75. data/sig/datadog/ci/contrib/settings.rbs +0 -25
  76. data/sig/datadog/ci/ext/app_types.rbs +0 -14
  77. data/sig/datadog/ci/ext/environment/extractor.rbs +0 -25
  78. data/sig/datadog/ci/ext/environment/providers/appveyor.rbs +0 -48
  79. data/sig/datadog/ci/ext/environment/providers/aws_code_pipeline.rbs +0 -19
  80. data/sig/datadog/ci/ext/environment/providers/azure.rbs +0 -56
  81. data/sig/datadog/ci/ext/environment/providers/base.rbs +0 -71
  82. data/sig/datadog/ci/ext/environment/providers/bitbucket.rbs +0 -37
  83. data/sig/datadog/ci/ext/environment/providers/bitrise.rbs +0 -41
  84. data/sig/datadog/ci/ext/environment/providers/buddy.rbs +0 -37
  85. data/sig/datadog/ci/ext/environment/providers/buildkite.rbs +0 -45
  86. data/sig/datadog/ci/ext/environment/providers/circleci.rbs +0 -41
  87. data/sig/datadog/ci/ext/environment/providers/codefresh.rbs +0 -25
  88. data/sig/datadog/ci/ext/environment/providers/github_actions.rbs +0 -42
  89. data/sig/datadog/ci/ext/environment/providers/gitlab.rbs +0 -57
  90. data/sig/datadog/ci/ext/environment/providers/jenkins.rbs +0 -35
  91. data/sig/datadog/ci/ext/environment/providers/local_git.rbs +0 -66
  92. data/sig/datadog/ci/ext/environment/providers/teamcity.rbs +0 -17
  93. data/sig/datadog/ci/ext/environment/providers/travis.rbs +0 -35
  94. data/sig/datadog/ci/ext/environment/providers/user_defined_tags.rbs +0 -33
  95. data/sig/datadog/ci/ext/environment/providers.rbs +0 -13
  96. data/sig/datadog/ci/ext/environment.rbs +0 -44
  97. data/sig/datadog/ci/ext/git.rbs +0 -53
  98. data/sig/datadog/ci/ext/settings.rbs +0 -14
  99. data/sig/datadog/ci/ext/test.rbs +0 -58
  100. data/sig/datadog/ci/ext/transport.rbs +0 -29
  101. data/sig/datadog/ci/null_span.rbs +0 -37
  102. data/sig/datadog/ci/span.rbs +0 -39
  103. data/sig/datadog/ci/test.rbs +0 -7
  104. data/sig/datadog/ci/test_module.rbs +0 -6
  105. data/sig/datadog/ci/test_session.rbs +0 -9
  106. data/sig/datadog/ci/test_suite.rbs +0 -6
  107. data/sig/datadog/ci/test_visibility/context/global.rbs +0 -39
  108. data/sig/datadog/ci/test_visibility/context/local.rbs +0 -23
  109. data/sig/datadog/ci/test_visibility/flush.rbs +0 -17
  110. data/sig/datadog/ci/test_visibility/recorder.rbs +0 -85
  111. data/sig/datadog/ci/test_visibility/serializers/base.rbs +0 -94
  112. data/sig/datadog/ci/test_visibility/serializers/factories/test_level.rbs +0 -13
  113. data/sig/datadog/ci/test_visibility/serializers/factories/test_suite_level.rbs +0 -13
  114. data/sig/datadog/ci/test_visibility/serializers/span.rbs +0 -18
  115. data/sig/datadog/ci/test_visibility/serializers/test_module.rbs +0 -26
  116. data/sig/datadog/ci/test_visibility/serializers/test_session.rbs +0 -26
  117. data/sig/datadog/ci/test_visibility/serializers/test_suite.rbs +0 -26
  118. data/sig/datadog/ci/test_visibility/serializers/test_v1.rbs +0 -23
  119. data/sig/datadog/ci/test_visibility/serializers/test_v2.rbs +0 -25
  120. data/sig/datadog/ci/test_visibility/transport.rbs +0 -35
  121. data/sig/datadog/ci/transport/api/base.rbs +0 -21
  122. data/sig/datadog/ci/transport/api/builder.rbs +0 -12
  123. data/sig/datadog/ci/transport/api/ci_test_cycle.rbs +0 -21
  124. data/sig/datadog/ci/transport/api/evp_proxy.rbs +0 -19
  125. data/sig/datadog/ci/transport/gzip.rbs +0 -9
  126. data/sig/datadog/ci/transport/http.rbs +0 -36
  127. data/sig/datadog/ci/utils/git.rbs +0 -11
  128. data/sig/datadog/ci/utils/test_run.rbs +0 -11
  129. data/sig/datadog/ci/utils/url.rbs +0 -9
  130. data/sig/datadog/ci/version.rbs +0 -16
  131. data/sig/datadog/ci.rbs +0 -35
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85bb3584dc8081e8f6e8531d2a600e3948f4f9ea23d551dd99df0eab9daf2a3c
4
- data.tar.gz: b6f686d49d3dbb1695c0f6e20b434b77648a118c32e1691f4a0f30b13d38dc62
3
+ metadata.gz: 14828e8c64b8eeb9c04d4ae2563e568b3b920b587f8e97e628896b143219daf0
4
+ data.tar.gz: b538d180acd0c18e1d8070adfdcd9f44dad65331048619de40db04cd876dfe01
5
5
  SHA512:
6
- metadata.gz: 8e8ae716ea7c7408c59c11efa5a3bc30e0767a02c659e231f1cec2a02d5494d451c1e2b43c7c16240cbe7c9418f004ead32faa09b08c83bcd878156fa1062ae9
7
- data.tar.gz: 15bdfb45e32660356fcc4c5726c4a640c78cfc2d5007807623f532a0b7c321f0b11a32d0a75f571a64ae263159d4b86b0a750dda5d7e8a5c33f9c110bf7682a0
6
+ metadata.gz: 60f8fda5f530404027669902b194c7e056af834c882adbc2b7d350eb62f7f75c2d63dae23bf6e98a8bebea0cc8ec34d4f5416fe667d1c30dff1fdb7ebb42854d
7
+ data.tar.gz: 1b2ad405dee8a1dd2ce24c3d5738ddcbc751f85d5aa5c29a287b614dcfc0a4764230a3b53e59967247fd0f64711fd81fc3863e49895098e7664aa81a3bf1b8e6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.7.0] - 2024-01-26
4
+
5
+ ### Added
6
+
7
+ * Source code integration ([#95][])
8
+ * CODEOWNERS support ([#98][])
9
+ * Cucumber scenarios with examples are treated as parametrized tests ([#100][])
10
+ * Deduplicate dynamically generated RSpec examples using test.parameters ([#101][])
11
+ * Repository name is used as default test service name ([#104][])
12
+ * Cucumber v9 support ([#99][])
13
+ * ci-queue runner support for minitest ([#110][])
14
+ * ci-queue support for rspec ([#112][])
15
+
16
+ ### Fixed
17
+
18
+ * do not publish sig folder when publishing this gem to prevent steep errors in client applications ([#114][])
19
+ * minitest: fix rails parallel test runner ([#115][])
20
+ * Test suites and tests skipped by frameworks are correctly reported as skipped to Datadog ([#113][])
21
+
22
+ ### Changed
23
+
24
+ * Enable test suite level visibility by default (with killswitch) ([#109][])
25
+ * Test suite names are more human-readable now ([#105][])
26
+ * Remove span_type method in tracer-related models ([#107][])
27
+ * Manual tracing API: convert type parameter to keyword in Datadog::CI.trace, remove internal-only methods from public API ([#108][])
28
+
29
+ ## [0.6.0] - 2024-01-03
30
+
31
+ ### Added
32
+
33
+ * Test suite level visibility instrumentation for RSpec ([#86][])
34
+ * Test suite level visibility instrumentation for Cucumber ([#90][])
35
+ * Test suite level visibility instrumentation for Minitest framework ([#92][])
36
+
37
+ ### Fixed
38
+
39
+ * Do not instantiate TestVisibility::Recorder unless CI visibility is enabled ([#89][])
40
+
3
41
  ## [0.5.1] - 2023-12-11
4
42
 
5
43
  ### Fixed
@@ -112,7 +150,9 @@ Currently test suite level visibility is not used by our instrumentation: it wil
112
150
 
113
151
  * Ruby versions < 2.7 no longer supported ([#8][])
114
152
 
115
- [Unreleased]: https://github.com/DataDog/datadog-ci-rb/compare/v0.5.1...main
153
+ [Unreleased]: https://github.com/DataDog/datadog-ci-rb/compare/v0.7.0...main
154
+ [0.7.0]: https://github.com/DataDog/datadog-ci-rb/compare/v0.6.0...v0.7.0
155
+ [0.6.0]: https://github.com/DataDog/datadog-ci-rb/compare/v0.5.1...v0.6.0
116
156
  [0.5.1]: https://github.com/DataDog/datadog-ci-rb/compare/v0.5.0...v0.5.1
117
157
  [0.5.0]: https://github.com/DataDog/datadog-ci-rb/compare/v0.4.1...v0.5.0
118
158
  [0.4.1]: https://github.com/DataDog/datadog-ci-rb/compare/v0.4.0...v0.4.1
@@ -153,4 +193,23 @@ Currently test suite level visibility is not used by our instrumentation: it wil
153
193
  [#82]: https://github.com/DataDog/datadog-ci-rb/issues/82
154
194
  [#84]: https://github.com/DataDog/datadog-ci-rb/issues/84
155
195
  [#85]: https://github.com/DataDog/datadog-ci-rb/issues/85
196
+ [#86]: https://github.com/DataDog/datadog-ci-rb/issues/86
156
197
  [#87]: https://github.com/DataDog/datadog-ci-rb/issues/87
198
+ [#89]: https://github.com/DataDog/datadog-ci-rb/issues/89
199
+ [#90]: https://github.com/DataDog/datadog-ci-rb/issues/90
200
+ [#92]: https://github.com/DataDog/datadog-ci-rb/issues/92
201
+ [#95]: https://github.com/DataDog/datadog-ci-rb/issues/95
202
+ [#98]: https://github.com/DataDog/datadog-ci-rb/issues/98
203
+ [#99]: https://github.com/DataDog/datadog-ci-rb/issues/99
204
+ [#100]: https://github.com/DataDog/datadog-ci-rb/issues/100
205
+ [#101]: https://github.com/DataDog/datadog-ci-rb/issues/101
206
+ [#104]: https://github.com/DataDog/datadog-ci-rb/issues/104
207
+ [#105]: https://github.com/DataDog/datadog-ci-rb/issues/105
208
+ [#107]: https://github.com/DataDog/datadog-ci-rb/issues/107
209
+ [#108]: https://github.com/DataDog/datadog-ci-rb/issues/108
210
+ [#109]: https://github.com/DataDog/datadog-ci-rb/issues/109
211
+ [#110]: https://github.com/DataDog/datadog-ci-rb/issues/110
212
+ [#112]: https://github.com/DataDog/datadog-ci-rb/issues/112
213
+ [#113]: https://github.com/DataDog/datadog-ci-rb/issues/113
214
+ [#114]: https://github.com/DataDog/datadog-ci-rb/issues/114
215
+ [#115]: https://github.com/DataDog/datadog-ci-rb/issues/115
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![CircleCI](https://dl.circleci.com/status-badge/img/gh/DataDog/datadog-ci-rb/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/DataDog/datadog-ci-rb/tree/main)
7
7
 
8
8
  Datadog's Ruby Library for instrumenting your test and continuous integration pipeline.
9
- Learn more on our [official website](https://docs.datadoghq.com/continuous_integration/tests/ruby/).
9
+ Learn more on our [official website](https://docs.datadoghq.com/tests/) and check out our [documentation for this library](https://docs.datadoghq.com/tests/setup/ruby/?tab=cloudciprovideragentless).
10
10
 
11
11
  > [!IMPORTANT]
12
12
  > The `datadog-ci` gem is currently a component of [`ddtrace`](https://github.com/datadog/dd-trace-rb) and should not be used without it.
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rule"
4
+
5
+ module Datadog
6
+ module CI
7
+ module Codeowners
8
+ # Responsible for matching a test source file path to a list of owners
9
+ class Matcher
10
+ def initialize(codeowners_file_path)
11
+ @rules = parse(codeowners_file_path)
12
+ @rules.reverse!
13
+ end
14
+
15
+ def list_owners(file_path)
16
+ # treat all file paths that we check as absolute from the repository root
17
+ file_path = "/#{file_path}" unless file_path.start_with?("/")
18
+
19
+ Datadog.logger.debug { "Matching file path #{file_path} to CODEOWNERS rules" }
20
+
21
+ @rules.each do |rule|
22
+ if rule.match?(file_path)
23
+ Datadog.logger.debug { "Matched rule [#{rule.pattern}] with owners #{rule.owners}" }
24
+ return rule.owners
25
+ end
26
+ end
27
+
28
+ Datadog.logger.debug { "CODEOWNERS rule not matched" }
29
+ nil
30
+ end
31
+
32
+ private
33
+
34
+ def parse(codeowners_file_path)
35
+ unless File.exist?(codeowners_file_path)
36
+ Datadog.logger.debug { "CODEOWNERS file not found at #{codeowners_file_path}" }
37
+ return []
38
+ end
39
+
40
+ result = []
41
+ section_default_owners = []
42
+
43
+ File.open(codeowners_file_path, "r") do |f|
44
+ f.each_line do |line|
45
+ line.strip!
46
+
47
+ next if line.empty?
48
+ next if comment?(line)
49
+
50
+ pattern, *line_owners = line.strip.split(/\s+/)
51
+ next if pattern.nil? || pattern.empty?
52
+
53
+ # if the current line starts with section record the default owners for this section
54
+ if section?(pattern)
55
+ section_default_owners = line_owners
56
+ next
57
+ end
58
+
59
+ pattern = expand_pattern(pattern)
60
+ # if the current line doesn't have any owners then use the default owners for this section
61
+ if line_owners.empty? && !section_default_owners.empty?
62
+ line_owners = section_default_owners
63
+ end
64
+
65
+ result << Rule.new(pattern, line_owners)
66
+ end
67
+ end
68
+
69
+ result
70
+ rescue => e
71
+ Datadog.logger.warn(
72
+ "Failed to parse codeowners file at #{codeowners_file_path}: " \
73
+ "#{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
74
+ )
75
+ []
76
+ end
77
+
78
+ def comment?(line)
79
+ line.start_with?("#")
80
+ end
81
+
82
+ def section?(line)
83
+ line.start_with?("[", "^[") && line.end_with?("]")
84
+ end
85
+
86
+ def expand_pattern(pattern)
87
+ return pattern if pattern == "*"
88
+
89
+ # if pattern ends with a slash then it matches everything deeply nested in this directory
90
+ pattern += "**" if pattern.end_with?(::File::SEPARATOR)
91
+
92
+ # if pattern doesn't start with a slash then it matches anywhere in the repository
93
+ if !pattern.start_with?(::File::SEPARATOR, "**#{::File::SEPARATOR}", "*#{::File::SEPARATOR}")
94
+ pattern = "**#{::File::SEPARATOR}#{pattern}"
95
+ end
96
+
97
+ pattern
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "matcher"
4
+
5
+ module Datadog
6
+ module CI
7
+ module Codeowners
8
+ # Responsible for parsing a CODEOWNERS file
9
+ class Parser
10
+ DEFAULT_LOCATION = "CODEOWNERS"
11
+ POSSIBLE_CODEOWNERS_LOCATIONS = [
12
+ "CODEOWNERS",
13
+ ".github/CODEOWNERS",
14
+ ".gitlab/CODEOWNERS",
15
+ "docs/CODEOWNERS"
16
+ ].freeze
17
+
18
+ def initialize(root_file_path)
19
+ @root_file_path = root_file_path || Dir.pwd
20
+ end
21
+
22
+ def parse
23
+ default_path = File.join(@root_file_path, DEFAULT_LOCATION)
24
+ # We are using the first codeowners file that we find or
25
+ # default location if nothing is found
26
+ #
27
+ # Matcher handles it internally and creates a class with
28
+ # an empty list of rules if the file is not found
29
+ codeowners_file_path = POSSIBLE_CODEOWNERS_LOCATIONS.map do |codeowners_location|
30
+ File.join(@root_file_path, codeowners_location)
31
+ end.find do |path|
32
+ File.exist?(path)
33
+ end || default_path
34
+
35
+ ::Datadog.logger.debug { "Using CODEOWNERS file from: #{codeowners_file_path}" }
36
+
37
+ Matcher.new(codeowners_file_path)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,33 @@
1
+ module Datadog
2
+ module CI
3
+ module Codeowners
4
+ class Rule
5
+ attr_reader :pattern, :owners
6
+
7
+ def initialize(pattern, owners)
8
+ @pattern = pattern
9
+ @owners = owners
10
+ end
11
+
12
+ def match?(file_path)
13
+ res = false
14
+ # if pattern does not end with a separator or a wildcard, it could be either a directory or a file
15
+ if !pattern.end_with?(::File::SEPARATOR, "*")
16
+ directory_pattern = "#{pattern}#{::File::SEPARATOR}*"
17
+ res ||= File.fnmatch?(directory_pattern, file_path, flags)
18
+ end
19
+
20
+ res ||= File.fnmatch?(pattern, file_path, flags)
21
+ res
22
+ end
23
+
24
+ private
25
+
26
+ def flags
27
+ return ::File::FNM_PATHNAME if pattern.end_with?("#{::File::SEPARATOR}*")
28
+ 0
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -12,7 +12,8 @@ module Datadog
12
12
  def initialize(tracer_span)
13
13
  super
14
14
 
15
- @mutex = Mutex.new
15
+ # we use Monitor instead of Mutex because it is reentrant
16
+ @mutex = Monitor.new
16
17
  end
17
18
 
18
19
  # Gets tag value by key. This method is thread-safe.
@@ -7,6 +7,7 @@ require_relative "../ext/transport"
7
7
  require_relative "../ext/settings"
8
8
  require_relative "../test_visibility/flush"
9
9
  require_relative "../test_visibility/recorder"
10
+ require_relative "../test_visibility/null_recorder"
10
11
  require_relative "../test_visibility/serializers/factories/test_level"
11
12
  require_relative "../test_visibility/serializers/factories/test_suite_level"
12
13
  require_relative "../test_visibility/transport"
@@ -21,14 +22,12 @@ module Datadog
21
22
 
22
23
  def initialize(settings)
23
24
  # Activate CI mode if enabled
24
- activate_ci!(settings) if settings.ci.enabled
25
-
26
- @ci_recorder = TestVisibility::Recorder.new(
27
- enabled: settings.ci.enabled,
28
- test_suite_level_visibility_enabled: settings.ci.experimental_test_suite_level_visibility_enabled
29
- )
25
+ if settings.ci.enabled
26
+ activate_ci!(settings)
27
+ else
28
+ @ci_recorder = TestVisibility::NullRecorder.new
29
+ end
30
30
 
31
- # Initialize normally
32
31
  super
33
32
  end
34
33
 
@@ -42,7 +41,7 @@ module Datadog
42
41
  elsif can_use_evp_proxy?(settings, agent_settings)
43
42
  test_visibility_transport = build_evp_proxy_transport(settings, agent_settings)
44
43
  else
45
- settings.ci.experimental_test_suite_level_visibility_enabled = false
44
+ settings.ci.force_test_level_visibility = true
46
45
  end
47
46
 
48
47
  # Deactivate telemetry
@@ -65,11 +64,16 @@ module Datadog
65
64
  if test_visibility_transport
66
65
  writer_options[:transport] = test_visibility_transport
67
66
  writer_options[:shutdown_timeout] = 60
67
+ writer_options[:buffer_size] = 10_000
68
68
 
69
69
  settings.tracing.test_mode.async = true
70
70
  end
71
71
 
72
72
  settings.tracing.test_mode.writer_options = writer_options
73
+
74
+ @ci_recorder = TestVisibility::Recorder.new(
75
+ test_suite_level_visibility_enabled: !settings.ci.force_test_level_visibility
76
+ )
73
77
  end
74
78
 
75
79
  def can_use_evp_proxy?(settings, agent_settings)
@@ -115,10 +119,10 @@ module Datadog
115
119
  end
116
120
 
117
121
  def serializers_factory(settings)
118
- if settings.ci.experimental_test_suite_level_visibility_enabled
119
- Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel
120
- else
122
+ if settings.ci.force_test_level_visibility
121
123
  Datadog::CI::TestVisibility::Serializers::Factories::TestLevel
124
+ else
125
+ Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel
122
126
  end
123
127
  end
124
128
 
@@ -34,10 +34,25 @@ module Datadog
34
34
  o.env CI::Ext::Settings::ENV_AGENTLESS_URL
35
35
  end
36
36
 
37
+ option :force_test_level_visibility do |o|
38
+ o.type :bool
39
+ o.env CI::Ext::Settings::ENV_FORCE_TEST_LEVEL_VISIBILITY
40
+ o.default false
41
+ end
42
+
37
43
  option :experimental_test_suite_level_visibility_enabled do |o|
38
44
  o.type :bool
39
45
  o.env CI::Ext::Settings::ENV_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED
40
46
  o.default false
47
+ o.after_set do |value|
48
+ if value
49
+ Datadog::Core.log_deprecation do
50
+ "The experimental_test_suite_level_visibility_enabled setting has no effect and will be removed in 1.0. " \
51
+ "Test suite level visibility is now enabled by default. " \
52
+ "If you want to disable test suite level visibility set configuration.ci.force_test_level_visibility = true."
53
+ end
54
+ end
55
+ end
41
56
  end
42
57
 
43
58
  define_method(:instrument) do |integration_name, options = {}, &block|
@@ -4,6 +4,7 @@ require "datadog/core"
4
4
 
5
5
  require_relative "../ext"
6
6
  require_relative "../../settings"
7
+ require_relative "../../../utils/configuration"
7
8
 
8
9
  module Datadog
9
10
  module CI
@@ -21,7 +22,9 @@ module Datadog
21
22
 
22
23
  option :service_name do |o|
23
24
  o.type :string
24
- o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME }
25
+ o.default do
26
+ Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
27
+ end
25
28
  end
26
29
 
27
30
  # @deprecated Will be removed in 1.0
@@ -7,14 +7,16 @@ module Datadog
7
7
  # Cucumber integration constants
8
8
  # TODO: mark as `@public_api` when GA, to protect from resource and tag name changes.
9
9
  module Ext
10
- APP = "cucumber"
11
10
  ENV_ENABLED = "DD_TRACE_CUCUMBER_ENABLED"
12
- ENV_OPERATION_NAME = "DD_TRACE_CUCUMBER_OPERATION_NAME"
11
+ DEFAULT_SERVICE_NAME = "cucumber"
12
+
13
13
  FRAMEWORK = "cucumber"
14
- OPERATION_NAME = "cucumber.test"
15
- SERVICE_NAME = "cucumber"
14
+
16
15
  STEP_SPAN_TYPE = "step"
17
- TEST_TYPE = "test"
16
+
17
+ # TODO: remove in 1.0
18
+ ENV_OPERATION_NAME = "DD_TRACE_CUCUMBER_OPERATION_NAME"
19
+ OPERATION_NAME = "cucumber.test"
18
20
  end
19
21
  end
20
22
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../../ext/test"
4
+ require_relative "../../utils/git"
4
5
  require_relative "ext"
5
6
 
6
7
  module Datadog
@@ -9,71 +10,181 @@ module Datadog
9
10
  module Cucumber
10
11
  # Defines collection of instrumented Cucumber events
11
12
  class Formatter
12
- attr_reader :config, :current_feature_span, :current_step_span
13
+ attr_reader :config
13
14
  private :config
14
- private :current_feature_span, :current_step_span
15
15
 
16
16
  def initialize(config)
17
+ @ast_lookup = ::Cucumber::Formatter::AstLookup.new(config) if defined?(::Cucumber::Formatter::AstLookup)
17
18
  @config = config
18
19
 
20
+ @current_test_suite = nil
21
+
22
+ @failed_tests_count = 0
23
+
19
24
  bind_events(config)
20
25
  end
21
26
 
22
27
  def bind_events(config)
28
+ config.on_event :test_run_started, &method(:on_test_run_started)
29
+ config.on_event :test_run_finished, &method(:on_test_run_finished)
23
30
  config.on_event :test_case_started, &method(:on_test_case_started)
24
31
  config.on_event :test_case_finished, &method(:on_test_case_finished)
25
32
  config.on_event :test_step_started, &method(:on_test_step_started)
26
33
  config.on_event :test_step_finished, &method(:on_test_step_finished)
27
34
  end
28
35
 
29
- def on_test_case_started(event)
30
- CI.start_test(
31
- event.test_case.name,
32
- event.test_case.location.file,
36
+ def on_test_run_started(event)
37
+ test_session = CI.start_test_session(
33
38
  tags: {
34
39
  CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
35
- CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Cucumber::Integration.version.to_s,
36
- CI::Ext::Test::TAG_TYPE => Ext::TEST_TYPE
40
+ CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Cucumber::Integration.version.to_s
37
41
  },
38
42
  service: configuration[:service_name]
39
43
  )
44
+ CI.start_test_module(test_session.name) if test_session
45
+ end
46
+
47
+ def on_test_run_finished(event)
48
+ if event.respond_to?(:success)
49
+ finish_session(event.success)
50
+ else
51
+ finish_session(@failed_tests_count.zero?)
52
+ end
53
+ end
54
+
55
+ def on_test_case_started(event)
56
+ test_suite_name = test_suite_name(event.test_case)
57
+
58
+ tags = {
59
+ CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
60
+ CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Cucumber::Integration.version.to_s,
61
+ CI::Ext::Test::TAG_SOURCE_FILE => Utils::Git.relative_to_root(event.test_case.location.file),
62
+ CI::Ext::Test::TAG_SOURCE_START => event.test_case.location.line.to_s
63
+ }
64
+
65
+ start_test_suite(test_suite_name) unless same_test_suite_as_current?(test_suite_name)
66
+
67
+ test_span = CI.start_test(
68
+ event.test_case.name,
69
+ test_suite_name,
70
+ tags: tags,
71
+ service: configuration[:service_name]
72
+ )
73
+
74
+ if test_span && (parameters = extract_parameters_hash(event.test_case))
75
+ test_span.set_parameters(parameters)
76
+ end
40
77
  end
41
78
 
42
79
  def on_test_case_finished(event)
43
80
  test_span = CI.active_test
44
81
  return if test_span.nil?
45
82
 
46
- if event.result.skipped?
47
- test_span.skipped!
48
- elsif event.result.ok?
49
- test_span.passed!
50
- elsif event.result.failed?
51
- test_span.failed!
52
- end
53
-
54
- test_span.finish
83
+ finish_span(test_span, event.result)
84
+ @failed_tests_count += 1 if test_span.failed?
55
85
  end
56
86
 
57
87
  def on_test_step_started(event)
58
- CI.trace(Ext::STEP_SPAN_TYPE, event.test_step.to_s)
88
+ CI.trace(event.test_step.to_s, type: Ext::STEP_SPAN_TYPE)
59
89
  end
60
90
 
61
91
  def on_test_step_finished(event)
62
- current_step_span = CI.active_span(Ext::STEP_SPAN_TYPE)
92
+ current_step_span = CI.active_span
63
93
  return if current_step_span.nil?
64
94
 
65
- if event.result.skipped?
66
- current_step_span.skipped!
67
- elsif event.result.ok?
68
- current_step_span.passed!
69
- elsif event.result.failed?
70
- current_step_span.failed!(exception: event.result.exception)
95
+ finish_span(current_step_span, event.result)
96
+ end
97
+
98
+ private
99
+
100
+ def test_suite_name(test_case)
101
+ feature = if test_case.respond_to?(:feature)
102
+ test_case.feature
103
+ elsif @ast_lookup
104
+ gherkin_doc = @ast_lookup.gherkin_document(test_case.location.file)
105
+ gherkin_doc.feature if gherkin_doc
71
106
  end
72
107
 
73
- current_step_span.finish
108
+ if feature
109
+ "#{feature.name} at #{test_case.location.file}"
110
+ else
111
+ test_case.location.file
112
+ end
74
113
  end
75
114
 
76
- private
115
+ def finish_span(span, result)
116
+ if !result.passed? && result.ok?(@config.strict)
117
+ span.skipped!(reason: result.message)
118
+ elsif result.passed?
119
+ span.passed!
120
+ else
121
+ span.failed!(exception: result.exception)
122
+ end
123
+ span.finish
124
+ end
125
+
126
+ def finish_session(result)
127
+ finish_current_test_suite
128
+
129
+ test_session = CI.active_test_session
130
+ test_module = CI.active_test_module
131
+
132
+ return unless test_session && test_module
133
+
134
+ if result
135
+ test_module.passed!
136
+ test_session.passed!
137
+ else
138
+ test_module.failed!
139
+ test_session.failed!
140
+ end
141
+
142
+ test_module.finish
143
+ test_session.finish
144
+ end
145
+
146
+ def start_test_suite(test_suite_name)
147
+ finish_current_test_suite
148
+
149
+ @current_test_suite = CI.start_test_suite(test_suite_name)
150
+ end
151
+
152
+ def finish_current_test_suite
153
+ test_suite = @current_test_suite
154
+ return unless test_suite
155
+
156
+ test_suite.finish
157
+
158
+ @current_test_suite = nil
159
+ end
160
+
161
+ def same_test_suite_as_current?(test_suite_name)
162
+ test_suite = @current_test_suite
163
+ return false unless test_suite
164
+
165
+ test_suite.name == test_suite_name
166
+ end
167
+
168
+ def extract_parameters_hash(test_case)
169
+ # not supported in cucumber < 4.0
170
+ return nil unless @ast_lookup
171
+
172
+ scenario_source = @ast_lookup.scenario_source(test_case)
173
+
174
+ # cucumber examples are only supported for scenario outlines
175
+ return nil unless scenario_source.type == :ScenarioOutline
176
+
177
+ scenario_source.examples.table_header.cells.map(&:value).zip(
178
+ scenario_source.row.cells.map(&:value)
179
+ ).to_h
180
+ rescue => e
181
+ Datadog.logger.warn do
182
+ "Unable to extract parameters from test case #{test_case.name}: " \
183
+ "#{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
184
+ end
185
+
186
+ nil
187
+ end
77
188
 
78
189
  def configuration
79
190
  Datadog.configuration.ci[:cucumber]
@@ -17,8 +17,9 @@ module Datadog
17
17
  attr_reader :datadog_formatter
18
18
 
19
19
  def formatters
20
+ existing_formatters = super
20
21
  @datadog_formatter ||= CI::Contrib::Cucumber::Formatter.new(@configuration)
21
- [@datadog_formatter] + super
22
+ [@datadog_formatter] + existing_formatters
22
23
  end
23
24
  end
24
25
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../ext"
4
4
  require_relative "../../settings"
5
+ require_relative "../../../utils/configuration"
5
6
 
6
7
  module Datadog
7
8
  module CI
@@ -19,7 +20,9 @@ module Datadog
19
20
 
20
21
  option :service_name do |o|
21
22
  o.type :string
22
- o.default { Datadog.configuration.service_without_fallback || Ext::SERVICE_NAME }
23
+ o.default do
24
+ Utils::Configuration.fetch_service_name(Ext::DEFAULT_SERVICE_NAME)
25
+ end
23
26
  end
24
27
 
25
28
  # @deprecated Will be removed in 1.0