Class: Datadog::CI::TestVisibility::Serializers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/test_visibility/serializers/base.rb

Direct Known Subclasses

Span, TestModule, TestSession, TestSuite, TestV1

Constant Summary collapse

MINIMUM_TIMESTAMP_NANO =
946684800000000000
MINIMUM_DURATION_NANO =
0
MAXIMUM_DURATION_NANO =
9223372036854775807
CONTENT_FIELDS =
[
  "name", "resource", "service",
  "error", "start", "duration",
  "meta", "metrics",
  "type" => "span_type"
].freeze
REQUIRED_FIELDS =
%w[error name resource start duration].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trace, span, options: {}) ⇒ Base

Returns a new instance of Base.



27
28
29
30
31
32
33
34
35
36
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 27

def initialize(trace, span, options: {})
  @trace = trace
  @span = span
  @options = options

  @meta = @span.meta.reject { |key, _| Ext::Test::TRANSIENT_TAGS.include?(key) }

  @errors = {}
  @validated = false
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



25
26
27
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 25

def meta
  @meta
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 25

def options
  @options
end

#spanObject (readonly)

Returns the value of attribute span.



25
26
27
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 25

def span
  @span
end

#traceObject (readonly)

Returns the value of attribute trace.



25
26
27
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 25

def trace
  @trace
end

Class Method Details

.calculate_content_map_size(fields_list) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 157

def self.calculate_content_map_size(fields_list)
  fields_list.reduce(0) do |size, field|
    if field.is_a?(Hash)
      size + field.size
    else
      size + 1
    end
  end
end

Instance Method Details

#content_fieldsObject



81
82
83
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 81

def content_fields
  []
end

#content_map_sizeObject



85
86
87
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 85

def content_map_size
  0
end

#durationObject



145
146
147
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 145

def duration
  @duration ||= duration_nano(@span.duration)
end

#errorObject



153
154
155
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 153

def error
  @span.status
end

#event_typeObject



125
126
127
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 125

def event_type
  "span"
end

#metricsObject



149
150
151
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 149

def metrics
  @span.metrics
end

#nameObject



129
130
131
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 129

def name
  @span.name
end

#parent_idObject



101
102
103
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 101

def parent_id
  @span.parent_id
end

#resourceObject



133
134
135
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 133

def resource
  @span.resource
end

#runtime_idObject



89
90
91
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 89

def runtime_id
  @trace.runtime_id
end

#serviceObject



137
138
139
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 137

def service
  @span.service
end

#span_idObject



97
98
99
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 97

def span_id
  @span.id
end

#span_typeObject



121
122
123
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 121

def span_type
  @span.type
end

#startObject



141
142
143
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 141

def start
  @start ||= time_nano(@span.start_time)
end

#test_module_idObject



109
110
111
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 109

def test_module_id
  to_integer(@span.get_tag(Ext::Test::TAG_TEST_MODULE_ID))
end

#test_session_idObject



105
106
107
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 105

def test_session_id
  to_integer(@span.get_tag(Ext::Test::TAG_TEST_SESSION_ID))
end

#test_suite_idObject



113
114
115
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 113

def test_suite_id
  to_integer(@span.get_tag(Ext::Test::TAG_TEST_SUITE_ID))
end

#to_msgpack(packer = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 38

def to_msgpack(packer = nil)
  packer ||= MessagePack::Packer.new

  packer.write_map_header(3)

  write_field(packer, "type", "event_type")
  write_field(packer, "version")

  packer.write("content")
  packer.write_map_header(content_map_size)

  content_fields.each do |field|
    if field.is_a?(Hash)
      field.each do |field_name, method|
        write_field(packer, field_name, method)
      end
    else
      write_field(packer, field)
    end
  end
end

#trace_idObject



93
94
95
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 93

def trace_id
  @trace.id
end

#valid?Boolean

validates according to citestcycle json schema

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 61

def valid?
  validate! unless @validated

  @errors.empty?
end

#validate!Object



67
68
69
70
71
72
73
74
75
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 67

def validate!
  @errors.clear

  validate_required_fields!
  validate_start_time!
  validate_duration!

  @validated = true
end

#validation_errorsObject



77
78
79
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 77

def validation_errors
  @errors
end

#versionObject



117
118
119
# File 'lib/datadog/ci/test_visibility/serializers/base.rb', line 117

def version
  1
end