Skip to content

Commit

Permalink
move contrib tracer helpers to common helpers;
Browse files Browse the repository at this point in the history
serialize test event to msgpack
  • Loading branch information
anmarchenko committed Sep 20, 2023
1 parent 2a854ff commit cd2d070
Show file tree
Hide file tree
Showing 24 changed files with 333 additions and 296 deletions.
29 changes: 0 additions & 29 deletions lib/datadog/ci/test_visibility/events.rb

This file was deleted.

17 changes: 0 additions & 17 deletions lib/datadog/ci/test_visibility/events/event.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/datadog/ci/test_visibility/events/span.rb

This file was deleted.

16 changes: 0 additions & 16 deletions lib/datadog/ci/test_visibility/events/test.rb

This file was deleted.

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

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

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

def runtime_id
@trace.runtime_id
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
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/datadog/ci/test_visibility/serializer/span.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative "base"

module Datadog
module CI
module TestVisibility
module Serializer
class Span < Base
def to_msgpack(packer = nil)
packer ||= MessagePack::Packer.new
end
end
end
end
end
end
61 changes: 61 additions & 0 deletions lib/datadog/ci/test_visibility/serializer/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

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

module Datadog
module CI
module TestVisibility
module Serializer
class Test < Base
def to_msgpack(packer = nil)
packer ||= MessagePack::Packer.new unless defined?(@packer)

packer.write_map_header(3)

packer.write("type")
packer.write("test")

packer.write("version")
packer.write(1)

packer.write("content")

packer.write_map_header(10)

packer.write("trace_id")
packer.write(@trace.id)

packer.write("span_id")
packer.write(@span.id)

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

packer.write("resource")
packer.write("#{@span.get_tag(Ext::Test::TAG_SUITE)}.#{@span.get_tag(Ext::Test::TAG_NAME)}")

packer.write("service")
packer.write(@span.service)

packer.write("type")
packer.write("test")

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)

# metrics have the same value as meta
packer.write("metrics")
packer.write({})
end
end
end
end
end
end
27 changes: 27 additions & 0 deletions lib/datadog/ci/test_visibility/something_that_converts_traces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative "serializer/test"
require_relative "serializer/span"

module Datadog
module CI
module TestVisibility
module SomethingThatConvertsTraces
module_function

def convert(trace)
trace.spans.map { |span| convert_span(trace, span) }
end

def convert_span(trace, span)
case span.type
when Datadog::CI::Ext::AppTypes::TYPE_TEST
Serializer::Test.new(trace, span)
else
Serializer::Span.new(trace, span)
end
end
end
end
end
end
28 changes: 25 additions & 3 deletions lib/datadog/ci/test_visibility/transport.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 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"
Expand All @@ -14,8 +15,8 @@ def initialize

def send_traces(traces)
# convert traces to events and construct payload
events = traces.map { |trace| PayloadEvents.convert_from_trace(trace) }
# payload = Payload.new(events)
events = traces.flat_map { |trace| SomethingThatConvertsTraces.convert(trace) }
payload = Payload.new(events)
# @encoder.encode(payload)
end

Expand All @@ -24,9 +25,30 @@ def send_traces(traces)
# 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
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
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

require "stringio"
require "cucumber"

Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/cucumber/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

RSpec.describe Datadog::CI::Contrib::Cucumber::Integration do
extend ConfigurationHelpers

Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/cucumber/patcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

RSpec.describe Datadog::CI::Contrib::Cucumber::Patcher do
describe ".patch" do
subject!(:patch) { described_class.patch }
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require "minitest"
require "minitest/spec"

require_relative "../support/spec_helper"

RSpec.describe "Minitest hooks" do
include_context "CI mode activated" do
let(:integration_name) { :minitest }
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/minitest/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

RSpec.describe Datadog::CI::Contrib::Minitest::Integration do
extend ConfigurationHelpers

Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/minitest/patcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

require "minitest"

RSpec.describe Datadog::CI::Contrib::Minitest::Patcher do
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "time"

require_relative "../support/spec_helper"

RSpec.describe "RSpec hooks" do
include_context "CI mode activated" do
let(:integration_name) { :rspec }
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/rspec/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

RSpec.describe Datadog::CI::Contrib::RSpec::Integration do
extend ConfigurationHelpers

Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/ci/contrib/rspec/patcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require_relative "../support/spec_helper"

RSpec.describe Datadog::CI::Contrib::RSpec::Patcher do
describe ".patch" do
subject!(:patch) { described_class.patch }
Expand Down
32 changes: 0 additions & 32 deletions spec/datadog/ci/contrib/support/spec_helper.rb

This file was deleted.

Loading

0 comments on commit cd2d070

Please sign in to comment.