Skip to content

Commit

Permalink
TestSession and TestV2 serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 23, 2023
1 parent 0cae355 commit 443f856
Show file tree
Hide file tree
Showing 17 changed files with 447 additions and 26 deletions.
26 changes: 14 additions & 12 deletions .standard_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
# Remove from this list as you refactor files.
---
ignore:
- lib/datadog/ci/contrib/minitest/integration.rb:
- Style/SafeNavigation
- lib/datadog/ci/contrib/cucumber/integration.rb:
- Style/SafeNavigation
- lib/datadog/ci/contrib/rspec/integration.rb:
- Style/SafeNavigation
- lib/datadog/ci/ext/environment.rb:
- Style/SafeNavigation
- spec/support/log_helpers.rb:
- Performance/UnfreezeString
- Appraisals:
- Style/Alias
- lib/datadog/ci/test_visibility/serializers/base.rb:
- Style/HashExcept
- lib/datadog/ci/contrib/minitest/integration.rb:
- Style/SafeNavigation
- lib/datadog/ci/contrib/cucumber/integration.rb:
- Style/SafeNavigation
- lib/datadog/ci/contrib/rspec/integration.rb:
- Style/SafeNavigation
- lib/datadog/ci/ext/environment.rb:
- Style/SafeNavigation
- spec/support/log_helpers.rb:
- Performance/UnfreezeString
- Appraisals:
- Style/Alias
2 changes: 2 additions & 0 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ module Test
TAG_SUITE = "test.suite"
TAG_TRAITS = "test.traits"
TAG_TYPE = "test.type"
TAG_COMMAND = "test.command"

# those tags are special and they are used to conrrelate tests with the test sessions, suites, and modules
TAG_TEST_SESSION_ID = "_test.session_id"
SPECIAL_TAGS = [TAG_TEST_SESSION_ID].freeze

# tags that can be inherited from the test session
INHERITABLE_TAGS = [TAG_FRAMEWORK, TAG_FRAMEWORK_VERSION, TAG_TYPE].freeze
Expand Down
2 changes: 2 additions & 0 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def start_test_session(service_name: nil, tags: {})

set_trace_origin(trace)

tags[Ext::Test::TAG_TEST_SESSION_ID] = tracer_span.id

test_session = build_test_session(tracer_span, tags)
@global_context.activate_test_session!(test_session)

Expand Down
14 changes: 9 additions & 5 deletions lib/datadog/ci/test_visibility/serializers/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

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

module Datadog
module CI
module TestVisibility
Expand All @@ -9,11 +11,13 @@ class Base
MINIMUM_DURATION_NANO = 0
MAXIMUM_DURATION_NANO = 9223372036854775807

attr_reader :trace, :span
attr_reader :trace, :span, :meta

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

@meta = @span.meta.reject { |key, _| Ext::Test::SPECIAL_TAGS.include?(key) }
end

def to_msgpack(packer = nil)
Expand Down Expand Up @@ -67,6 +71,10 @@ def parent_id
@span.parent_id
end

def test_session_id
@span.get_tag(Ext::Test::TAG_TEST_SESSION_ID)
end

def type
end

Expand Down Expand Up @@ -98,10 +106,6 @@ def duration
@duration ||= duration_nano(@span.duration)
end

def meta
@span.meta
end

def metrics
@span.metrics
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require_relative "../test_v2"
require_relative "../test_session"
require_relative "../span"

module Datadog
module CI
module TestVisibility
module Serializers
module Factories
# This factory takes care of creating citestcycle serializers when test-suite-level visibility is enabled
module TestSuiteLevel
module_function

def serializer(trace, span)
case span.type
when Datadog::CI::Ext::AppTypes::TYPE_TEST
Serializers::TestV2.new(trace, span)
when Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION
Serializers::TestSession.new(trace, span)
else
Serializers::Span.new(trace, span)
end
end
end
end
end
end
end
end
59 changes: 59 additions & 0 deletions lib/datadog/ci/test_visibility/serializers/test_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

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

module Datadog
module CI
module TestVisibility
module Serializers
class TestSession < Base
CONTENT_FIELDS = [
"test_session_id",
"name", "resource", "service",
"error", "start", "duration",
"meta", "metrics",
"type" => "span_type"
].freeze

CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)

REQUIRED_FIELDS = [
"test_session_id",
"error",
"name",
"resource",
"start",
"duration"
].freeze

def content_fields
CONTENT_FIELDS
end

def content_map_size
CONTENT_MAP_SIZE
end

def type
Ext::AppTypes::TYPE_TEST_SESSION
end

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

def resource
"#{@span.get_tag(Ext::Test::TAG_FRAMEWORK)}.test_session.#{@span.get_tag(Ext::Test::TAG_COMMAND)}"
end

private

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

require_relative "test_v1"
require_relative "../../ext/test"

module Datadog
module CI
module TestVisibility
module Serializers
class TestV2 < TestV1
CONTENT_FIELDS = [
"trace_id", "span_id",
"name", "resource", "service",
"error", "start", "duration",
"meta", "metrics", "test_session_id",
"type" => "span_type"
].freeze

CONTENT_MAP_SIZE = calculate_content_map_size(CONTENT_FIELDS)

REQUIRED_FIELDS = [
"test_session_id",
"trace_id",
"span_id",
"error",
"name",
"resource",
"start",
"duration"
].freeze

def content_fields
CONTENT_FIELDS
end

def content_map_size
CONTENT_MAP_SIZE
end

def version
2
end

private

def required_fields
REQUIRED_FIELDS
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ module Datadog

TAG_TYPE: String

TAG_COMMAND: String

TAG_TEST_SESSION_ID: String

SPECIAL_TAGS: Array[String]

INHERITABLE_TAGS: Array[String]

TAG_OS_ARCHITECTURE: String
Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/test_visibility/serializers/base.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Datadog
@content_fields_count: Integer
@start: Integer
@duration: Integer
@meta: Hash[untyped, untyped]

attr_reader trace: Datadog::Tracing::TraceSegment
attr_reader span: Datadog::Tracing::Span
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Datadog
module CI
module TestVisibility
module Serializers
module Factories
module TestSuiteLevel
def self?.serializer: (Datadog::Tracing::TraceSegment trace, Datadog::Tracing::Span span) -> Datadog::CI::TestVisibility::Serializers::Base
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions sig/datadog/ci/test_visibility/serializers/test_session.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Datadog
module CI
module TestVisibility
module Serializers
class TestSession < Base
CONTENT_FIELDS: Array[String | Hash[String, String]]
CONTENT_MAP_SIZE: Integer
REQUIRED_FIELDS: Array[String]

def content_fields: () -> Array[String | Hash[String, String]]
def content_map_size: () -> Integer

def type: () -> ::String

def name: () -> ::String

def resource: () -> ::String

private

def required_fields: () -> Array[String]
end
end
end
end
end
25 changes: 25 additions & 0 deletions sig/datadog/ci/test_visibility/serializers/test_v2.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Datadog
module CI
module TestVisibility
module Serializers
class TestV2 < TestV1
CONTENT_FIELDS: ::Array[String | ::Hash[::String, String]]

CONTENT_MAP_SIZE: Integer

REQUIRED_FIELDS: ::Array[String]

def content_fields: () -> ::Array[String | ::Hash[::String, String]]

def content_map_size: () -> Integer

def version: () -> 2

private

def required_fields: () -> Array[String]
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@
subject(:start_test_session) { recorder.start_test_session(service_name: service) }

let(:session_operation_name) { "test.session" }

let(:span_op) { Datadog::Tracing::SpanOperation.new(session_operation_name) }
let(:expected_tags) { {"_test.session_id" => span_op.id} }

before do
allow(Datadog::Tracing)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative "../../../../../../lib/datadog/ci/test_visibility/serializers/factories/test_suite_level"
require_relative "../../../../../../lib/datadog/ci/test_visibility/serializers/test_v2"
require_relative "../../../../../../lib/datadog/ci/test_visibility/serializers/test_session"
require_relative "../../../../../../lib/datadog/ci/recorder"

RSpec.describe Datadog::CI::TestVisibility::Serializers::Factories::TestSuiteLevel do
include_context "CI mode activated" do
let(:integration_name) { :rspec }
end

before do
produce_test_session_trace
end

subject { described_class.serializer(trace, ci_span) }

describe ".convert_trace_to_serializable_events" do
context "with a session span" do
let(:ci_span) { test_session_span }
it { is_expected.to be_kind_of(Datadog::CI::TestVisibility::Serializers::TestSession) }
end

context "with a test span" do
let(:ci_span) { first_test_span }
it { is_expected.to be_kind_of(Datadog::CI::TestVisibility::Serializers::TestV2) }
end
end
end
Loading

0 comments on commit 443f856

Please sign in to comment.