datadog-ci 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/README.md +26 -3
  4. data/lib/datadog/ci/concurrent_span.rb +59 -0
  5. data/lib/datadog/ci/configuration/components.rb +34 -4
  6. data/lib/datadog/ci/configuration/extensions.rb +21 -0
  7. data/lib/datadog/ci/configuration/settings.rb +7 -1
  8. data/lib/datadog/ci/contrib/cucumber/configuration/settings.rb +12 -1
  9. data/lib/datadog/ci/contrib/cucumber/formatter.rb +3 -4
  10. data/lib/datadog/ci/contrib/minitest/configuration/settings.rb +10 -1
  11. data/lib/datadog/ci/contrib/minitest/hooks.rb +3 -4
  12. data/lib/datadog/ci/contrib/rspec/configuration/settings.rb +10 -1
  13. data/lib/datadog/ci/contrib/rspec/example.rb +3 -4
  14. data/lib/datadog/ci/contrib/settings.rb +2 -0
  15. data/lib/datadog/ci/ext/app_types.rb +5 -0
  16. data/lib/datadog/ci/ext/settings.rb +11 -0
  17. data/lib/datadog/ci/ext/test.rb +12 -1
  18. data/lib/datadog/ci/null_span.rb +63 -0
  19. data/lib/datadog/ci/span.rb +13 -3
  20. data/lib/datadog/ci/test.rb +0 -1
  21. data/lib/datadog/ci/test_module.rb +23 -0
  22. data/lib/datadog/ci/test_session.rb +36 -0
  23. data/lib/datadog/ci/test_suite.rb +25 -0
  24. data/lib/datadog/ci/test_visibility/context/global.rb +82 -0
  25. data/lib/datadog/ci/test_visibility/context/local.rb +52 -0
  26. data/lib/datadog/ci/test_visibility/recorder.rb +293 -0
  27. data/lib/datadog/ci/test_visibility/serializers/base.rb +100 -12
  28. data/lib/datadog/ci/test_visibility/serializers/factories/test_suite_level.rb +37 -0
  29. data/lib/datadog/ci/test_visibility/serializers/span.rb +2 -16
  30. data/lib/datadog/ci/test_visibility/serializers/test_module.rb +46 -0
  31. data/lib/datadog/ci/test_visibility/serializers/test_session.rb +46 -0
  32. data/lib/datadog/ci/test_visibility/serializers/test_suite.rb +46 -0
  33. data/lib/datadog/ci/test_visibility/serializers/test_v1.rb +3 -17
  34. data/lib/datadog/ci/test_visibility/serializers/test_v2.rb +38 -0
  35. data/lib/datadog/ci/test_visibility/transport.rb +4 -4
  36. data/lib/datadog/ci/utils/test_run.rb +15 -0
  37. data/lib/datadog/ci/version.rb +1 -1
  38. data/lib/datadog/ci.rb +217 -35
  39. data/sig/datadog/ci/concurrent_span.rbs +23 -0
  40. data/sig/datadog/ci/configuration/components.rbs +4 -2
  41. data/sig/datadog/ci/configuration/extensions.rbs +9 -0
  42. data/sig/datadog/ci/ext/app_types.rbs +6 -1
  43. data/sig/datadog/ci/ext/settings.rbs +3 -0
  44. data/sig/datadog/ci/ext/test.rbs +15 -0
  45. data/sig/datadog/ci/null_span.rbs +37 -0
  46. data/sig/datadog/ci/span.rbs +4 -0
  47. data/sig/datadog/ci/test_module.rbs +6 -0
  48. data/sig/datadog/ci/test_session.rbs +9 -0
  49. data/sig/datadog/ci/test_suite.rbs +6 -0
  50. data/sig/datadog/ci/test_visibility/context/global.rbs +39 -0
  51. data/sig/datadog/ci/test_visibility/context/local.rbs +23 -0
  52. data/sig/datadog/ci/test_visibility/recorder.rbs +85 -0
  53. data/sig/datadog/ci/test_visibility/serializers/base.rbs +26 -5
  54. data/sig/datadog/ci/test_visibility/serializers/factories/test_suite_level.rbs +13 -0
  55. data/sig/datadog/ci/test_visibility/serializers/test_module.rbs +26 -0
  56. data/sig/datadog/ci/test_visibility/serializers/test_session.rbs +26 -0
  57. data/sig/datadog/ci/test_visibility/serializers/test_suite.rbs +26 -0
  58. data/sig/datadog/ci/test_visibility/serializers/test_v1.rbs +1 -1
  59. data/sig/datadog/ci/test_visibility/serializers/test_v2.rbs +25 -0
  60. data/sig/datadog/ci/test_visibility/transport.rbs +3 -3
  61. data/sig/datadog/ci/utils/test_run.rbs +11 -0
  62. data/sig/datadog/ci.rbs +19 -3
  63. metadata +32 -8
  64. data/lib/datadog/ci/context/local.rb +0 -50
  65. data/lib/datadog/ci/extensions.rb +0 -19
  66. data/lib/datadog/ci/recorder.rb +0 -119
  67. data/sig/datadog/ci/context/local.rbs +0 -21
  68. data/sig/datadog/ci/extensions.rbs +0 -7
  69. data/sig/datadog/ci/recorder.rbs +0 -30
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "set"
4
+
5
+ require_relative "../../ext/test"
6
+
3
7
  module Datadog
4
8
  module CI
5
9
  module TestVisibility
@@ -9,11 +13,31 @@ module Datadog
9
13
  MINIMUM_DURATION_NANO = 0
10
14
  MAXIMUM_DURATION_NANO = 9223372036854775807
11
15
 
12
- attr_reader :trace, :span
16
+ CONTENT_FIELDS = [
17
+ "name", "resource", "service",
18
+ "error", "start", "duration",
19
+ "meta", "metrics",
20
+ "type" => "span_type"
21
+ ].freeze
22
+
23
+ REQUIRED_FIELDS = [
24
+ "error",
25
+ "name",
26
+ "resource",
27
+ "start",
28
+ "duration"
29
+ ].freeze
30
+
31
+ attr_reader :trace, :span, :meta
13
32
 
14
33
  def initialize(trace, span)
15
34
  @trace = trace
16
35
  @span = span
36
+
37
+ @meta = @span.meta.reject { |key, _| Ext::Test::SPECIAL_TAGS.include?(key) }
38
+
39
+ @errors = {}
40
+ @validated = false
17
41
  end
18
42
 
19
43
  def to_msgpack(packer = nil)
@@ -40,7 +64,23 @@ module Datadog
40
64
 
41
65
  # validates according to citestcycle json schema
42
66
  def valid?
43
- required_fields_present? && valid_start_time? && valid_duration?
67
+ validate! unless @validated
68
+
69
+ @errors.empty?
70
+ end
71
+
72
+ def validate!
73
+ @errors.clear
74
+
75
+ validate_required_fields!
76
+ validate_start_time!
77
+ validate_duration!
78
+
79
+ @validated = true
80
+ end
81
+
82
+ def validation_errors
83
+ @errors
44
84
  end
45
85
 
46
86
  def content_fields
@@ -67,6 +107,18 @@ module Datadog
67
107
  @span.parent_id
68
108
  end
69
109
 
110
+ def test_session_id
111
+ to_integer(@span.get_tag(Ext::Test::TAG_TEST_SESSION_ID))
112
+ end
113
+
114
+ def test_module_id
115
+ to_integer(@span.get_tag(Ext::Test::TAG_TEST_MODULE_ID))
116
+ end
117
+
118
+ def test_suite_id
119
+ to_integer(@span.get_tag(Ext::Test::TAG_TEST_SUITE_ID))
120
+ end
121
+
70
122
  def type
71
123
  end
72
124
 
@@ -98,10 +150,6 @@ module Datadog
98
150
  @duration ||= duration_nano(@span.duration)
99
151
  end
100
152
 
101
- def meta
102
- @span.meta
103
- end
104
-
105
153
  def metrics
106
154
  @span.metrics
107
155
  end
@@ -122,16 +170,48 @@ module Datadog
122
170
 
123
171
  private
124
172
 
125
- def valid_start_time?
126
- !start.nil? && start >= MINIMUM_TIMESTAMP_NANO
173
+ def validate_start_time!
174
+ validate_required!("start")
175
+ validate_greater_than_or_equal!("start", MINIMUM_TIMESTAMP_NANO)
176
+ end
177
+
178
+ def validate_duration!
179
+ validate_required!("duration")
180
+ validate_greater_than_or_equal!("duration", MINIMUM_DURATION_NANO)
181
+ validate_less_than_or_equal!("duration", MAXIMUM_DURATION_NANO)
182
+ end
183
+
184
+ def validate_required_fields!
185
+ required_fields.each do |field|
186
+ validate_required!(field)
187
+ end
127
188
  end
128
189
 
129
- def valid_duration?
130
- !duration.nil? && duration >= MINIMUM_DURATION_NANO && duration <= MAXIMUM_DURATION_NANO
190
+ def validate_required!(field)
191
+ if send(field).nil?
192
+ add_error(field, "is required")
193
+ end
131
194
  end
132
195
 
133
- def required_fields_present?
134
- required_fields.all? { |field| !send(field).nil? }
196
+ def validate_greater_than_or_equal!(field, value)
197
+ return if send(field).nil?
198
+
199
+ if send(field) < value
200
+ add_error(field, "must be greater than or equal to #{value}")
201
+ end
202
+ end
203
+
204
+ def validate_less_than_or_equal!(field, value)
205
+ return if send(field).nil?
206
+
207
+ if send(field) > value
208
+ add_error(field, "must be less than or equal to #{value}")
209
+ end
210
+ end
211
+
212
+ def add_error(field, message)
213
+ @errors[field] ||= Set.new
214
+ @errors[field] << message
135
215
  end
136
216
 
137
217
  def required_fields
@@ -154,6 +234,14 @@ module Datadog
154
234
  def duration_nano(duration)
155
235
  (duration * 1e9).to_i
156
236
  end
237
+
238
+ def to_s
239
+ "#{self.class.name}(id:#{span_id},name:#{name})"
240
+ end
241
+
242
+ def to_integer(value)
243
+ value.to_i if value
244
+ end
157
245
  end
158
246
  end
159
247
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../test_v2"
4
+ require_relative "../test_session"
5
+ require_relative "../test_module"
6
+ require_relative "../test_suite"
7
+ require_relative "../span"
8
+
9
+ module Datadog
10
+ module CI
11
+ module TestVisibility
12
+ module Serializers
13
+ module Factories
14
+ # This factory takes care of creating citestcycle serializers when test-suite-level visibility is enabled
15
+ module TestSuiteLevel
16
+ module_function
17
+
18
+ def serializer(trace, span)
19
+ case span.type
20
+ when Datadog::CI::Ext::AppTypes::TYPE_TEST
21
+ Serializers::TestV2.new(trace, span)
22
+ when Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION
23
+ Serializers::TestSession.new(trace, span)
24
+ when Datadog::CI::Ext::AppTypes::TYPE_TEST_MODULE
25
+ Serializers::TestModule.new(trace, span)
26
+ when Datadog::CI::Ext::AppTypes::TYPE_TEST_SUITE
27
+ Serializers::TestSuite.new(trace, span)
28
+ else
29
+ Serializers::Span.new(trace, span)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -7,25 +7,11 @@ module Datadog
7
7
  module TestVisibility
8
8
  module Serializers
9
9
  class Span < Base
10
- CONTENT_FIELDS = [
11
- "trace_id", "span_id", "parent_id",
12
- "name", "resource", "service",
13
- "error", "start", "duration",
14
- "meta", "metrics",
15
- "type" => "span_type"
16
- ].freeze
10
+ CONTENT_FIELDS = (["trace_id", "span_id", "parent_id"] + Base::CONTENT_FIELDS).freeze
17
11
 
18
12
  CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)
19
13
 
20
- REQUIRED_FIELDS = [
21
- "trace_id",
22
- "span_id",
23
- "error",
24
- "name",
25
- "resource",
26
- "start",
27
- "duration"
28
- ].freeze
14
+ REQUIRED_FIELDS = (["trace_id", "span_id"] + Base::REQUIRED_FIELDS).freeze
29
15
 
30
16
  def content_fields
31
17
  CONTENT_FIELDS
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "../../ext/test"
5
+
6
+ module Datadog
7
+ module CI
8
+ module TestVisibility
9
+ module Serializers
10
+ class TestModule < Base
11
+ CONTENT_FIELDS = (["test_session_id", "test_module_id"] + Base::CONTENT_FIELDS).freeze
12
+
13
+ CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)
14
+
15
+ REQUIRED_FIELDS = (["test_session_id", "test_module_id"] + Base::REQUIRED_FIELDS).freeze
16
+
17
+ def content_fields
18
+ CONTENT_FIELDS
19
+ end
20
+
21
+ def content_map_size
22
+ CONTENT_MAP_SIZE
23
+ end
24
+
25
+ def type
26
+ Ext::AppTypes::TYPE_TEST_MODULE
27
+ end
28
+
29
+ def name
30
+ "#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_module"
31
+ end
32
+
33
+ def resource
34
+ "#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_module.#{@span.get_tag(Ext::Test::TAG_MODULE)}"
35
+ end
36
+
37
+ private
38
+
39
+ def required_fields
40
+ REQUIRED_FIELDS
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "../../ext/test"
5
+
6
+ module Datadog
7
+ module CI
8
+ module TestVisibility
9
+ module Serializers
10
+ class TestSession < Base
11
+ CONTENT_FIELDS = (["test_session_id"] + Base::CONTENT_FIELDS).freeze
12
+
13
+ CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)
14
+
15
+ REQUIRED_FIELDS = (["test_session_id"] + Base::REQUIRED_FIELDS).freeze
16
+
17
+ def content_fields
18
+ CONTENT_FIELDS
19
+ end
20
+
21
+ def content_map_size
22
+ CONTENT_MAP_SIZE
23
+ end
24
+
25
+ def type
26
+ Ext::AppTypes::TYPE_TEST_SESSION
27
+ end
28
+
29
+ def name
30
+ "#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_session"
31
+ end
32
+
33
+ def resource
34
+ "#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_session.#{@span.get_tag(Ext::Test::TAG_COMMAND)}"
35
+ end
36
+
37
+ private
38
+
39
+ def required_fields
40
+ REQUIRED_FIELDS
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "../../ext/test"
5
+
6
+ module Datadog
7
+ module CI
8
+ module TestVisibility
9
+ module Serializers
10
+ class TestSuite < Base
11
+ CONTENT_FIELDS = (["test_session_id", "test_module_id", "test_suite_id"] + Base::CONTENT_FIELDS).freeze
12
+
13
+ CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)
14
+
15
+ REQUIRED_FIELDS = (["test_session_id", "test_module_id", "test_suite_id"] + Base::REQUIRED_FIELDS).freeze
16
+
17
+ def content_fields
18
+ CONTENT_FIELDS
19
+ end
20
+
21
+ def content_map_size
22
+ CONTENT_MAP_SIZE
23
+ end
24
+
25
+ def type
26
+ Ext::AppTypes::TYPE_TEST_SUITE
27
+ end
28
+
29
+ def name
30
+ "#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_suite"
31
+ end
32
+
33
+ def resource
34
+ "#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_suite.#{@span.get_tag(Ext::Test::TAG_SUITE)}"
35
+ end
36
+
37
+ private
38
+
39
+ def required_fields
40
+ REQUIRED_FIELDS
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -8,25 +8,11 @@ module Datadog
8
8
  module TestVisibility
9
9
  module Serializers
10
10
  class TestV1 < Base
11
- CONTENT_FIELDS = [
12
- "trace_id", "span_id",
13
- "name", "resource", "service",
14
- "error", "start", "duration",
15
- "meta", "metrics",
16
- "type" => "span_type"
17
- ].freeze
11
+ CONTENT_FIELDS = (["trace_id", "span_id"] + Base::CONTENT_FIELDS).freeze
18
12
 
19
13
  CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)
20
14
 
21
- REQUIRED_FIELDS = [
22
- "trace_id",
23
- "span_id",
24
- "error",
25
- "name",
26
- "resource",
27
- "start",
28
- "duration"
29
- ].freeze
15
+ REQUIRED_FIELDS = (["trace_id", "span_id"] + Base::REQUIRED_FIELDS).freeze
30
16
 
31
17
  def content_fields
32
18
  CONTENT_FIELDS
@@ -37,7 +23,7 @@ module Datadog
37
23
  end
38
24
 
39
25
  def type
40
- "test"
26
+ Ext::AppTypes::TYPE_TEST
41
27
  end
42
28
 
43
29
  def name
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_v1"
4
+ require_relative "../../ext/test"
5
+
6
+ module Datadog
7
+ module CI
8
+ module TestVisibility
9
+ module Serializers
10
+ class TestV2 < TestV1
11
+ CONTENT_FIELDS = (["test_session_id", "test_module_id", "test_suite_id"] + TestV1::CONTENT_FIELDS).freeze
12
+
13
+ CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)
14
+
15
+ REQUIRED_FIELDS = (["test_session_id", "test_module_id", "test_suite_id"] + TestV1::REQUIRED_FIELDS).freeze
16
+
17
+ def content_fields
18
+ CONTENT_FIELDS
19
+ end
20
+
21
+ def content_map_size
22
+ CONTENT_MAP_SIZE
23
+ end
24
+
25
+ def version
26
+ 2
27
+ end
28
+
29
+ private
30
+
31
+ def required_fields
32
+ REQUIRED_FIELDS
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -81,7 +81,7 @@ module Datadog
81
81
  if spans.respond_to?(:filter_map)
82
82
  spans.filter_map { |span| encode_span(trace, span) }
83
83
  else
84
- trace.spans.map { |span| encode_span(trace, span) }.reject(&:nil?)
84
+ spans.map { |span| encode_span(trace, span) }.reject(&:nil?)
85
85
  end
86
86
  end
87
87
  end
@@ -94,15 +94,15 @@ module Datadog
94
94
 
95
95
  if encoded.size > max_payload_size
96
96
  # This single event is too large, we can't flush it
97
- Datadog.logger.debug { "Dropping test event. Payload too large: '#{span.inspect}'" }
98
- Datadog.logger.debug { encoded }
97
+ Datadog.logger.warn("Dropping test event. Payload too large: '#{span.inspect}'")
98
+ Datadog.logger.warn(encoded)
99
99
 
100
100
  return nil
101
101
  end
102
102
 
103
103
  encoded
104
104
  else
105
- Datadog.logger.debug { "Invalid span skipped: #{span}" }
105
+ Datadog.logger.warn("Invalid event skipped: #{serializer} Errors: #{serializer.validation_errors}")
106
106
  nil
107
107
  end
108
108
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module CI
5
+ module Utils
6
+ module TestRun
7
+ def self.command
8
+ return @command if defined?(@command)
9
+
10
+ @command = "#{$0} #{ARGV.join(" ")}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,7 +4,7 @@ module Datadog
4
4
  module CI
5
5
  module VERSION
6
6
  MAJOR = "0"
7
- MINOR = "4"
7
+ MINOR = "5"
8
8
  PATCH = "1"
9
9
  PRE = nil
10
10
  BUILD = nil