Skip to content

Commit

Permalink
extract serialization logic to the base serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 20, 2023
1 parent 0ce035b commit 040773c
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 332 deletions.
34 changes: 0 additions & 34 deletions lib/datadog/ci/test_visibility/serializer/base.rb

This file was deleted.

65 changes: 0 additions & 65 deletions lib/datadog/ci/test_visibility/serializer/span.rb

This file was deleted.

60 changes: 0 additions & 60 deletions lib/datadog/ci/test_visibility/serializer/test.rb

This file was deleted.

28 changes: 28 additions & 0 deletions lib/datadog/ci/test_visibility/serializers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative "serializers/test_v1"
require_relative "serializers/span"

module Datadog
module CI
module TestVisibility
module Serializers
module_function

def convert_trace_to_serializable_events(trace)
trace.spans.map { |span| convert_span_to_serializable_event(trace, span) }
end

# for test suite visibility we might need to make it configurable
def convert_span_to_serializable_event(trace, span)
case span.type
when Datadog::CI::Ext::AppTypes::TYPE_TEST
Serializers::TestV1.new(trace, span)
else
Serializers::Span.new(trace, span)
end
end
end
end
end
end
136 changes: 136 additions & 0 deletions lib/datadog/ci/test_visibility/serializers/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# frozen_string_literal: true

module Datadog
module CI
module TestVisibility
module Serializers
class Base
attr_reader :trace, :span

def initialize(trace, span)
@trace = trace
@span = span
end

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

packer.write_map_header(3)

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

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

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

def content_fields
[]
end

def runtime_id
@trace.runtime_id
end

def trace_id
@trace.id
end

def span_id
@span.id
end

def parent_id
@span.parent_id
end

def type
end

def version
1
end

def span_type
@span.type
end

def name
@span.name
end

def resource
@span.resource
end

def service
@span.service
end

def start
time_nano(@span.start_time)
end

def duration
duration_nano(@span.duration)
end

def meta
@span.meta
end

def metrics
@span.metrics
end

def error
@span.status
end

private

def write_field(packer, field_name, method = nil)
method ||= field_name

packer.write(field_name)
packer.write(send(method))
end

# Used for serialization
# @return [Integer] in nanoseconds since Epoch
def time_nano(time)
time.to_i * 1000000000 + time.nsec
end

# Used for serialization
# @return [Integer] in nanoseconds since Epoch
def duration_nano(duration)
(duration * 1e9).to_i
end

def content_fields_count
res = 0
content_fields.each do |field|
res += if field.is_a?(Hash)
field.size
else
1
end
end
res
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/datadog/ci/test_visibility/serializers/span.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative "base"

module Datadog
module CI
module TestVisibility
module Serializers
class Span < Base
def content_fields
@content_fields ||= [
"trace_id", "span_id", "parent_id", "name",
"resource", "service", "error", "start",
"duration", "meta", "metrics",
"type" => "span_type"
]
end

def type
"span"
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/datadog/ci/test_visibility/serializers/test_v1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative "base"
require_relative "../../ext/test"

module Datadog
module CI
module TestVisibility
module Serializers
class TestV1 < Base
def content_fields
@content_fields ||= [
"trace_id", "span_id", "name", "resource", "service",
"start", "duration", "meta", "metrics", "error",
"type" => "span_type"
]
end

def type
"test"
end

def name
"#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test"
end

def resource
"#{@span.get_tag(Ext::Test::TAG_SUITE)}.#{@span.get_tag(Ext::Test::TAG_NAME)}"
end
end
end
end
end
end
Loading

0 comments on commit 040773c

Please sign in to comment.