-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract serialization logic to the base serializer
- Loading branch information
1 parent
0ce035b
commit 040773c
Showing
16 changed files
with
310 additions
and
332 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.