Skip to content

Commit

Permalink
move tagging logic to Datadog::CI::Span
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 1, 2023
1 parent 9b39765 commit 9a32937
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 216 deletions.
6 changes: 2 additions & 4 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
require "datadog/tracing"

require_relative "ci/recorder"
require_relative "ci/span"
require_relative "ci/test"

module Datadog
# Public API for Datadog CI visibility
Expand All @@ -16,8 +14,8 @@ def self.trace_test(test_name, service_name: nil, operation_name: "test", tags:
Recorder.trace_test(test_name, service_name: service_name, operation_name: operation_name, tags: tags, &block)
end

def self.trace(span_type, span_name, &block)
Recorder.trace(span_type, span_name, &block)
def self.trace(span_type, span_name, tags: {}, &block)
Recorder.trace(span_type, span_name, tags: tags, &block)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/ci/contrib/cucumber/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def on_test_case_started(event)
@current_feature_span = CI.trace_test(
event.test_case.name,
tags: {
framework: Ext::FRAMEWORK,
framework_version: CI::Contrib::Cucumber::Integration.version.to_s,
test_type: Ext::TEST_TYPE,
test_suite: event.test_case.location.file
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Cucumber::Integration.version.to_s,
CI::Ext::Test::TAG_TYPE => Ext::TEST_TYPE,
CI::Ext::Test::TAG_SUITE => event.test_case.location.file
},
service_name: configuration[:service_name],
operation_name: configuration[:operation_name]
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/ci/contrib/minitest/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def before_setup
test_span = CI.trace_test(
test_name,
tags: {
framework: Ext::FRAMEWORK,
framework_version: CI::Contrib::Minitest::Integration.version.to_s,
test_type: Ext::TEST_TYPE,
test_suite: test_suite
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::Minitest::Integration.version.to_s,
CI::Ext::Test::TAG_TYPE => Ext::TEST_TYPE,
CI::Ext::Test::TAG_SUITE => test_suite
},
service_name: configuration[:service_name],
operation_name: configuration[:operation_name]
Expand Down
10 changes: 5 additions & 5 deletions lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "../../recorder"
require_relative "../../../ci"
require_relative "../../ext/test"
require_relative "ext"

Expand Down Expand Up @@ -28,10 +28,10 @@ def run(example_group_instance, reporter)
CI.trace_test(
test_name,
tags: {
framework: Ext::FRAMEWORK,
framework_version: CI::Contrib::RSpec::Integration.version.to_s,
test_type: Ext::TEST_TYPE,
test_suite: metadata[:example_group][:file_path]
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::RSpec::Integration.version.to_s,
CI::Ext::Test::TAG_TYPE => Ext::TEST_TYPE,
CI::Ext::Test::TAG_SUITE => metadata[:example_group][:file_path]
},
service_name: configuration[:service_name],
operation_name: configuration[:operation_name]
Expand Down
53 changes: 17 additions & 36 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
require_relative "ext/test"
require_relative "ext/environment"

require_relative "span"

require "rbconfig"

module Datadog
Expand All @@ -21,68 +23,47 @@ def self.trace_test(test_name, service_name: nil, operation_name: "test", tags:
span_type: Ext::AppTypes::TYPE_TEST
}

tags[:test_name] = test_name
tags[Ext::Test::TAG_NAME] = test_name
tags.merge!(environment_tags)

if block
::Datadog::Tracing.trace(operation_name, **span_options) do |span, trace|
set_tags!(trace, span, tags)

block.call(Test.new(span))
::Datadog::Tracing.trace(operation_name, **span_options) do |tracer_span, trace|
set_internal_tracing_context!(trace, tracer_span)
block.call(Span.new(tracer_span, tags))
end
else
span = ::Datadog::Tracing.trace(operation_name, **span_options)
tracer_span = ::Datadog::Tracing.trace(operation_name, **span_options)
trace = ::Datadog::Tracing.active_trace
set_tags!(trace, span, tags)
Test.new(span)

set_internal_tracing_context!(trace, tracer_span)
Span.new(tracer_span, tags)
end
end

def self.trace(span_type, span_name, &block)
def self.trace(span_type, span_name, tags: {}, &block)
span_options = {
resource: span_name,
span_type: span_type
}

if block
::Datadog::Tracing.trace(span_name, **span_options) do |tracer_span|
block.call(Span.new(tracer_span))
block.call(Span.new(tracer_span, tags))
end
else
tracer_span = Datadog::Tracing.trace(span_name, **span_options)
Span.new(tracer_span)
Span.new(tracer_span, tags)
end
end

# Adds tags to a CI test span.
def self.set_tags!(trace, span, tags = {})
tags ||= {}

def self.set_internal_tracing_context!(trace, span)
# Set default tags
trace.origin = Ext::Test::CONTEXT_ORIGIN if trace
::Datadog::Tracing::Contrib::Analytics.set_measured(span)
span.set_tag(Ext::Test::TAG_SPAN_KIND, Ext::AppTypes::TYPE_TEST)

# Set environment tags
@environment_tags ||= Ext::Environment.tags(ENV)
@environment_tags.each { |k, v| span.set_tag(k, v) }

# Set contextual tags
span.set_tag(Ext::Test::TAG_FRAMEWORK, tags[:framework]) if tags[:framework]
span.set_tag(Ext::Test::TAG_FRAMEWORK_VERSION, tags[:framework_version]) if tags[:framework_version]
span.set_tag(Ext::Test::TAG_NAME, tags[:test_name]) if tags[:test_name]
span.set_tag(Ext::Test::TAG_SUITE, tags[:test_suite]) if tags[:test_suite]
span.set_tag(Ext::Test::TAG_TYPE, tags[:test_type]) if tags[:test_type]

set_environment_runtime_tags!(span)

span
end

private_class_method def self.set_environment_runtime_tags!(span)
span.set_tag(Ext::Test::TAG_OS_ARCHITECTURE, ::RbConfig::CONFIG["host_cpu"])
span.set_tag(Ext::Test::TAG_OS_PLATFORM, ::RbConfig::CONFIG["host_os"])
span.set_tag(Ext::Test::TAG_RUNTIME_NAME, Core::Environment::Ext::LANG_ENGINE)
span.set_tag(Ext::Test::TAG_RUNTIME_VERSION, Core::Environment::Ext::ENGINE_VERSION)
def self.environment_tags
@environment_tags ||= Ext::Environment.tags(ENV)
end
end
end
Expand Down
34 changes: 33 additions & 1 deletion lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# frozen_string_literal: true

require_relative "ext/test"

module Datadog
# Public API for Datadog CI visibility
module CI
class Span
attr_reader :tracer_span

def initialize(tracer_span)
def initialize(tracer_span, tags = nil)
@tracer_span = tracer_span

set_tags!(tags) unless tags.nil?
end

def passed!
Expand All @@ -26,9 +30,37 @@ def skipped!(exception = nil, reason = nil)
tracer_span.set_tag(CI::Ext::Test::TAG_SKIP_REASON, reason) unless reason.nil?
end

def set_tag(key, value)
tracer_span.set_tag(key, value)
end

def set_metric(key, value)
tracer_span.set_metric(key, value)
end

def finish
tracer_span.finish
end

private

def set_tags!(tags)
# set default tags
tracer_span.set_tag(Ext::Test::TAG_SPAN_KIND, Ext::AppTypes::TYPE_TEST)

tags.each do |key, value|
tracer_span.set_tag(key, value)
end

set_environment_runtime_tags!
end

def set_environment_runtime_tags!
tracer_span.set_tag(Ext::Test::TAG_OS_ARCHITECTURE, ::RbConfig::CONFIG["host_cpu"])
tracer_span.set_tag(Ext::Test::TAG_OS_PLATFORM, ::RbConfig::CONFIG["host_os"])
tracer_span.set_tag(Ext::Test::TAG_RUNTIME_NAME, Core::Environment::Ext::LANG_ENGINE)
tracer_span.set_tag(Ext::Test::TAG_RUNTIME_VERSION, Core::Environment::Ext::ENGINE_VERSION)
end
end
end
end
9 changes: 0 additions & 9 deletions lib/datadog/ci/test.rb

This file was deleted.

4 changes: 2 additions & 2 deletions sig/datadog/ci.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Datadog
module CI
def self.trace_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Test span) -> untyped } -> untyped
def self.trace_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def self.trace: (String span_type, String span_name) ?{ (Datadog::CI::Span span) -> untyped } -> untyped
def self.trace: (String span_type, String span_name, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped
end
end
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/minitest/hooks.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Datadog
module Hooks : ::Minitest::Test
include ::Minitest::Test::LifecycleHooks

@current_test_span: Datadog::CI::Test?
@current_test_span: Datadog::CI::Span?

def before_setup: () -> (nil | untyped)

Expand Down
10 changes: 6 additions & 4 deletions sig/datadog/ci/recorder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module Datadog
module Recorder
self.@environment_tags: Hash[String, String]

def self.trace_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Test span) -> untyped } -> untyped
def self.trace_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def self.trace: (String span_type, String span_name) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def self.set_tags!: (Datadog::Tracing::TraceOperation trace, Datadog::Tracing::SpanOperation span, ?::Hash[untyped, untyped] tags) -> untyped
def self.trace: (String span_type, String span_name, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def self.set_environment_runtime_tags!: (Datadog::Tracing::SpanOperation span) -> untyped

def self.set_internal_tracing_context!: (Datadog::Tracing::TraceOperation trace, Datadog::Tracing::SpanOperation span) -> untyped

def self.environment_tags: -> Hash[String, String]
end
end
end
12 changes: 11 additions & 1 deletion sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ module Datadog

attr_reader tracer_span: Datadog::Tracing::SpanOperation

def initialize: (Datadog::Tracing::SpanOperation tracer_span) -> void
def initialize: (Datadog::Tracing::SpanOperation tracer_span, ?Hash[untyped, untyped]? tags) -> void

def passed!: () -> void

def failed!: (?untyped? exception) -> void

def skipped!: (?untyped? exception, ?String? reason) -> void

def set_tag: (String key, untyped? value) -> untyped

def set_metric: (String key, untyped value) -> untyped

def finish: () -> untyped

private

def set_tags!: (Hash[untyped, untyped] tags) -> void

def set_environment_runtime_tags!: () -> void
end
end
end
Loading

0 comments on commit 9a32937

Please sign in to comment.