Skip to content

Commit

Permalink
trace_test public API method
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Oct 30, 2023
1 parent 8c8ef2a commit b55d93a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
24 changes: 24 additions & 0 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 7 additions & 10 deletions lib/datadog/ci/contrib/cucumber/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
Expand All @@ -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
Expand Down
26 changes: 11 additions & 15 deletions lib/datadog/ci/contrib/minitest/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 8 additions & 11 deletions lib/datadog/ci/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b55d93a

Please sign in to comment.