Skip to content

Commit

Permalink
serializable span event
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 20, 2023
1 parent cd2d070 commit 0ce035b
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 63 deletions.
48 changes: 48 additions & 0 deletions lib/datadog/ci/test_visibility/serializer/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/datadog/ci/test_visibility/serializer/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
114 changes: 57 additions & 57 deletions lib/datadog/ci/test_visibility/transport.rb
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions spec/datadog/ci/test_visibility/serializer/span_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions spec/datadog/ci/test_visibility/serializer/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand All @@ -45,7 +45,8 @@
"span_id" => span.id,
"name" => "rspec.test",
"service" => "rspec-test-suite",
"type" => "test"
"type" => "test",
"resource" => "calculator_tests.test_add"
}
)

Expand Down

0 comments on commit 0ce035b

Please sign in to comment.