diff --git a/lib/datadog/ci/test_visibility/serializer/span.rb b/lib/datadog/ci/test_visibility/serializer/span.rb index 96535baa..9da993d6 100644 --- a/lib/datadog/ci/test_visibility/serializer/span.rb +++ b/lib/datadog/ci/test_visibility/serializer/span.rb @@ -9,6 +9,54 @@ module Serializer class Span < Base def to_msgpack(packer = nil) packer ||= MessagePack::Packer.new + + packer.write_map_header(3) + + packer.write("type") + packer.write("span") + + packer.write("version") + packer.write(1) + + packer.write("content") + + packer.write_map_header(12) + + packer.write("trace_id") + packer.write(@trace.id) + + packer.write("span_id") + packer.write(@span.id) + + packer.write("parent_id") + packer.write(@span.parent_id) + + packer.write("name") + packer.write(@span.name) + + packer.write("resource") + packer.write(@span.resource) + + packer.write("service") + packer.write(@span.service) + + packer.write("type") + packer.write(@span.type) + + packer.write("error") + packer.write(@span.status) + + packer.write("start") + packer.write(time_nano(@span.start_time)) + + packer.write("duration") + packer.write(duration_nano(@span.duration)) + + packer.write("meta") + packer.write(@span.meta) + + packer.write("metrics") + packer.write(@span.metrics) end end end diff --git a/lib/datadog/ci/test_visibility/serializer/test.rb b/lib/datadog/ci/test_visibility/serializer/test.rb index 586f327e..48d54c9e 100644 --- a/lib/datadog/ci/test_visibility/serializer/test.rb +++ b/lib/datadog/ci/test_visibility/serializer/test.rb @@ -9,7 +9,7 @@ module TestVisibility module Serializer class Test < Base def to_msgpack(packer = nil) - packer ||= MessagePack::Packer.new unless defined?(@packer) + packer ||= MessagePack::Packer.new packer.write_map_header(3) @@ -50,9 +50,8 @@ def to_msgpack(packer = nil) packer.write("meta") packer.write(@span.meta) - # metrics have the same value as meta packer.write("metrics") - packer.write({}) + packer.write(@span.metrics) end end end diff --git a/lib/datadog/ci/test_visibility/transport.rb b/lib/datadog/ci/test_visibility/transport.rb index 33b17a84..6712753f 100644 --- a/lib/datadog/ci/test_visibility/transport.rb +++ b/lib/datadog/ci/test_visibility/transport.rb @@ -1,57 +1,57 @@ -# frozen_string_literal: true - -require_relative "something_that_converts_traces" -require "datadog/core/encoding" -# use it to chunk payloads by size -# require "datadog/core/chunker" - -module Datadog - module CI - module TestVisibility - class Transport - def initialize - @encoder = Datadog::Core::Encoding::MsgpackEncoder - end - - def send_traces(traces) - # convert traces to events and construct payload - events = traces.flat_map { |trace| SomethingThatConvertsTraces.convert(trace) } - payload = Payload.new(events) - # @encoder.encode(payload) - end - - private - - # represents payload with some subset of serializable events to be sent to CI-APP intake - class Payload - def initialize(events) - @events = events - end - - def to_msgpack(packer) - packer ||= MessagePack::Packer.new - - packer.write_map_header(3) # Set header with how many elements in the map - packer.write("version") - packer.write(1) - - packer.write("metadata") - packer.write_map_header(3) - - packer.write("runtime-id") - packer.write(@events.first.runtime_id) - - packer.write("language") - packer.write("ruby") - - packer.write("library_version") - packer.write(Datadog::CI::VERSION::STRING) - - packer.write_array_header(@events.size) - packer.write(@events) - end - end - end - end - end -end +# # frozen_string_literal: true + +# require_relative "something_that_converts_traces" +# require "datadog/core/encoding" +# # use it to chunk payloads by size +# # require "datadog/core/chunker" + +# module Datadog +# module CI +# module TestVisibility +# class Transport +# def initialize +# @encoder = Datadog::Core::Encoding::MsgpackEncoder +# end + +# def send_traces(traces) +# # convert traces to events and construct payload +# events = traces.flat_map { |trace| SomethingThatConvertsTraces.convert(trace) } +# payload = Payload.new(events) +# # @encoder.encode(payload) +# end + +# private + +# # represents payload with some subset of serializable events to be sent to CI-APP intake +# class Payload +# def initialize(events) +# @events = events +# end + +# def to_msgpack(packer) +# packer ||= MessagePack::Packer.new + +# packer.write_map_header(3) # Set header with how many elements in the map +# packer.write("version") +# packer.write(1) + +# packer.write("metadata") +# packer.write_map_header(3) + +# packer.write("runtime-id") +# packer.write(@events.first.runtime_id) + +# packer.write("language") +# packer.write("ruby") + +# packer.write("library_version") +# packer.write(Datadog::CI::VERSION::STRING) + +# packer.write_array_header(@events.size) +# packer.write(@events) +# end +# end +# end +# end +# end +# end diff --git a/spec/datadog/ci/test_visibility/serializer/span_spec.rb b/spec/datadog/ci/test_visibility/serializer/span_spec.rb new file mode 100644 index 00000000..dfa572c9 --- /dev/null +++ b/spec/datadog/ci/test_visibility/serializer/span_spec.rb @@ -0,0 +1,80 @@ +require_relative "../../../../../lib/datadog/ci/test_visibility/serializer/span" +require_relative "../../../../../lib/datadog/ci/recorder" + +RSpec.describe Datadog::CI::TestVisibility::Serializer::Span do + include_context "CI mode activated" do + let(:integration_name) { :rspec } + end + + let(:test_span) do + spans.find { |span| span.type == "test" } + end + + let(:tracer_span) do + spans.find { |span| span.type != "test" } + end + subject { described_class.new(trace, tracer_span) } + + describe "#to_msgpack" do + context "traced a single test execution with Recorder" do + before do + Datadog::CI::Recorder.trace( + "rspec.example", + { + span_options: { + resource: "test_add", + service: "rspec-test-suite" + }, + framework: "rspec", + framework_version: "3.0.0", + test_name: "test_add", + test_suite: "calculator_tests.rb", + test_type: "test" + } + ) do |span| + Datadog::Tracing.trace("http-call", type: "http", service: "net-http") do |span, trace| + span.set_tag("custom_tag", "custom_tag_value") + end + + Datadog::CI::Recorder.passed!(span) + end + end + + let(:payload) { MessagePack.unpack(MessagePack.pack(subject)) } + + it "serializes test event to messagepack" do + expect(payload).to include( + { + "version" => 1, + "type" => "span" + } + ) + content = payload["content"] + expect(content).to include( + { + "trace_id" => trace.id, + "span_id" => tracer_span.id, + "parent_id" => test_span.id, + "name" => "http-call", + "service" => "net-http", + "type" => "http", + "error" => 0, + "resource" => "http-call" + } + ) + + tags = content["meta"] + expect(tags).to include( + { + "custom_tag" => "custom_tag_value", + "_dd.origin" => "ciapp-test" + } + ) + # TODO: test start and duration with timecop + # expect(content["start"]).to eq(1) + # expect(content["duration"]).to eq(1) + # + end + end + end +end diff --git a/spec/datadog/ci/test_visibility/serializer/test_spec.rb b/spec/datadog/ci/test_visibility/serializer/test_spec.rb index 3890e3fe..d06f81ee 100644 --- a/spec/datadog/ci/test_visibility/serializer/test_spec.rb +++ b/spec/datadog/ci/test_visibility/serializer/test_spec.rb @@ -6,7 +6,7 @@ let(:integration_name) { :rspec } end - subject { Datadog::CI::TestVisibility::Serializer::Test.new(trace, span) } + subject { described_class.new(trace, span) } describe "#to_msgpack" do context "traced a single test execution with Recorder" do @@ -21,7 +21,7 @@ framework: "rspec", framework_version: "3.0.0", test_name: "test_add", - test_suite: "calculator_tests.rb", + test_suite: "calculator_tests", test_type: "test" } ) do |span| @@ -45,7 +45,8 @@ "span_id" => span.id, "name" => "rspec.test", "service" => "rspec-test-suite", - "type" => "test" + "type" => "test", + "resource" => "calculator_tests.test_add" } )