From b55d93a359b1330fdef2816d339b0daca7903590 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Mon, 30 Oct 2023 16:30:42 +0100 Subject: [PATCH] trace_test public API method --- lib/datadog/ci.rb | 24 ++++++++++++++++++ lib/datadog/ci/contrib/cucumber/formatter.rb | 17 ++++++------- lib/datadog/ci/contrib/minitest/hooks.rb | 26 +++++++++----------- lib/datadog/ci/contrib/rspec/example.rb | 19 ++++++-------- lib/datadog/ci/recorder.rb | 3 ++- lib/datadog/ci/span.rb | 10 +++++--- 6 files changed, 59 insertions(+), 40 deletions(-) diff --git a/lib/datadog/ci.rb b/lib/datadog/ci.rb index 02905110..dca2306b 100644 --- a/lib/datadog/ci.rb +++ b/lib/datadog/ci.rb @@ -5,11 +5,35 @@ require "datadog/core" require "datadog/tracing" +require_relative "ci/recorder" +require_relative "ci/span" +require_relative "ci/test" + module Datadog # Public API for Datadog CI visibility module CI module_function + def trace_test(test_name, test_suite, service_name, operation_name, tags = {}) + span_options = { + resource: test_name, + service: service_name + } + + tags[:test_name] = test_name + tags[:test_suite] = test_suite + tags[:span_options] = span_options + + if block_given? + Recorder.trace(operation_name, tags) do |span| + yield Test.new(span) + end + else + tracer_span = Recorder.trace(operation_name, tags) + Test.new(tracer_span) + end + end + def trace(span_type, span_name, span_options = {}) span_options[:resource] = span_name span_options[:span_type] = span_type diff --git a/lib/datadog/ci/contrib/cucumber/formatter.rb b/lib/datadog/ci/contrib/cucumber/formatter.rb index 8fbd07a4..b347407b 100644 --- a/lib/datadog/ci/contrib/cucumber/formatter.rb +++ b/lib/datadog/ci/contrib/cucumber/formatter.rb @@ -28,17 +28,14 @@ def bind_events(config) end def on_test_case_started(event) - @current_feature_span = CI::Recorder.trace( + @current_feature_span = CI.trace_test( + event.test_case.name, + event.test_case.location.file, + configuration[:service_name], configuration[:operation_name], { - span_options: { - resource: event.test_case.name, - service: configuration[:service_name] - }, framework: Ext::FRAMEWORK, framework_version: CI::Contrib::Cucumber::Integration.version.to_s, - test_name: event.test_case.name, - test_suite: event.test_case.location.file, test_type: Ext::TEST_TYPE } ) @@ -48,11 +45,11 @@ def on_test_case_finished(event) return if @current_feature_span.nil? if event.result.skipped? - CI::Recorder.skipped!(@current_feature_span) + @current_feature_span.skipped! elsif event.result.ok? - CI::Recorder.passed!(@current_feature_span) + @current_feature_span.passed! elsif event.result.failed? - CI::Recorder.failed!(@current_feature_span) + @current_feature_span.failed! end @current_feature_span.finish diff --git a/lib/datadog/ci/contrib/minitest/hooks.rb b/lib/datadog/ci/contrib/minitest/hooks.rb index 41253cde..9b317317 100644 --- a/lib/datadog/ci/contrib/minitest/hooks.rb +++ b/lib/datadog/ci/contrib/minitest/hooks.rb @@ -19,41 +19,37 @@ def before_setup path, = method(name).source_location test_suite = Pathname.new(path.to_s).relative_path_from(Pathname.pwd).to_s - span = CI::Recorder.trace( + test_span = CI.trace_test( + test_name, + test_suite, + configuration[:service_name], configuration[:operation_name], { - span_options: { - resource: test_name, - service: configuration[:service_name] - }, framework: Ext::FRAMEWORK, framework_version: CI::Contrib::Minitest::Integration.version.to_s, - test_name: test_name, - test_suite: test_suite, test_type: Ext::TEST_TYPE } ) - Thread.current[:_datadog_test_span] = span + @current_test_span = test_span end def after_teardown - span = Thread.current[:_datadog_test_span] - return super unless span + return super unless @current_test_span Thread.current[:_datadog_test_span] = nil case result_code when "." - CI::Recorder.passed!(span) + @current_test_span.passed! when "E", "F" - CI::Recorder.failed!(span, failure) + @current_test_span.failed!(failure) when "S" - CI::Recorder.skipped!(span) - span.set_tag(CI::Ext::Test::TAG_SKIP_REASON, failure.message) + @current_test_span.skipped!(nil, failure.message) end - span.finish + @current_test_span.finish + @current_test_span = nil super end diff --git a/lib/datadog/ci/contrib/rspec/example.rb b/lib/datadog/ci/contrib/rspec/example.rb index e695cd0a..e59b6c44 100644 --- a/lib/datadog/ci/contrib/rspec/example.rb +++ b/lib/datadog/ci/contrib/rspec/example.rb @@ -25,29 +25,26 @@ def run(example_group_instance, reporter) test_name += " #{description}" end - CI::Recorder.trace( + CI.trace_test( + test_name, + metadata[:example_group][:file_path], + configuration[:service_name], configuration[:operation_name], { - span_options: { - resource: test_name, - service: configuration[:service_name] - }, framework: Ext::FRAMEWORK, framework_version: CI::Contrib::RSpec::Integration.version.to_s, - test_name: test_name, - test_suite: metadata[:example_group][:file_path], test_type: Ext::TEST_TYPE } - ) do |span| + ) do |test_span| result = super case execution_result.status when :passed - CI::Recorder.passed!(span) + test_span.passed! when :failed - CI::Recorder.failed!(span, execution_result.exception) + test_span.failed!(execution_result.exception) else - CI::Recorder.skipped!(span, execution_result.exception) if execution_result.example_skipped? + test_span.skipped!(execution_result.exception) if execution_result.example_skipped? end result diff --git a/lib/datadog/ci/recorder.rb b/lib/datadog/ci/recorder.rb index a298f2ad..4f5867ad 100644 --- a/lib/datadog/ci/recorder.rb +++ b/lib/datadog/ci/recorder.rb @@ -67,9 +67,10 @@ def self.failed!(span, exception = nil) span.set_error(exception) unless exception.nil? end - def self.skipped!(span, exception = nil) + def self.skipped!(span, exception = nil, reason = nil) span.set_tag(Ext::Test::TAG_STATUS, Ext::Test::Status::SKIP) span.set_error(exception) unless exception.nil? + span.set_tag(CI::Ext::Test::TAG_SKIP_REASON, reason) unless reason.nil? end private_class_method def self.set_environment_runtime_tags!(span) diff --git a/lib/datadog/ci/span.rb b/lib/datadog/ci/span.rb index 0074eed2..b7874edb 100644 --- a/lib/datadog/ci/span.rb +++ b/lib/datadog/ci/span.rb @@ -15,11 +15,15 @@ def passed! end def failed!(exception = nil) - CI::Recorder.failed!(@current_step_span, exception) + CI::Recorder.failed!(@tracer_span, exception) end - def skipped!(exception = nil) - CI::Recorder.skipped!(@current_step_span, exception) + def skipped!(exception = nil, reason = nil) + CI::Recorder.skipped!(@tracer_span, exception, reason) + end + + def finish + tracer_span.finish end end end