Skip to content

Commit

Permalink
set the parameters for spans in JSON object with arguments and metada…
Browse files Browse the repository at this point in the history
…ta fields
  • Loading branch information
anmarchenko committed Jan 11, 2024
1 parent 3e0d74e commit fd793ad
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
12 changes: 5 additions & 7 deletions lib/datadog/ci/contrib/cucumber/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "json"

require_relative "../../ext/test"
require_relative "../../utils/git"
require_relative "ext"
Expand Down Expand Up @@ -66,18 +64,18 @@ def on_test_case_started(event)
CI::Ext::Test::TAG_SOURCE_START => event.test_case.location.line.to_s
}

if (parameters = extract_parameters_hash(event.test_case))
tags[CI::Ext::Test::TAG_PARAMETERS] = JSON.generate(parameters)
end

start_test_suite(test_suite_name) unless same_test_suite_as_current?(test_suite_name)

CI.start_test(
test_span = CI.start_test(
event.test_case.name,
test_suite_name,
tags: tags,
service: configuration[:service_name]
)

if (parameters = extract_parameters_hash(event.test_case))
test_span.set_parameters(parameters)
end
end

def on_test_case_finished(event)
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/ci/contrib/cucumber/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ module InstanceMethods
attr_reader :datadog_formatter

def formatters
existing_formatters = super
@datadog_formatter ||= CI::Contrib::Cucumber::Formatter.new(@configuration)
[@datadog_formatter] + super
[@datadog_formatter] + existing_formatters
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/ci/null_span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def set_environment_runtime_tags
def set_default_tags
end

def set_parameters(arguments, metadata = {})
end

def to_s
self.class.to_s
end
Expand Down
19 changes: 19 additions & 0 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ def set_default_tags
tracer_span.set_tag(Ext::Test::TAG_SPAN_KIND, Ext::AppTypes::TYPE_TEST)
end

# Sets the parameters for this span for parametrized tests (e.g. Cucumber examples or RSpec shared specs).
# Parameters are needed to compute test fingerprint to distinguish between different tests having same names.
# @param [Hash] arguments the arguments that test accepts as key-value hash
# @param [Hash] metadata optional metadata
# @return [void]
def set_parameters(arguments, metadata = {})
return if arguments.nil? || arguments.empty?

set_tag(
Ext::Test::TAG_PARAMETERS,
JSON.generate(
{
arguments: arguments,
metadata: metadata
}
)
)
end

def to_s
"#{self.class}(name:#{name},tracer_span:#{@tracer_span})"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/datadog/ci/test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "json"

require_relative "span"

module Datadog
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module Datadog
def set_environment_runtime_tags: () -> void

def set_default_tags: () -> void

def set_parameters: (Hash[String, Object] arguments, ?Hash[String, Object] metadata) -> void
end
end
end
2 changes: 1 addition & 1 deletion spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq("scenario with examples")

expect(span.get_tag(Datadog::CI::Ext::Test::TAG_PARAMETERS)).to eq(
"{\"num1\":\"#{index}\",\"num2\":\"#{index + 1}\",\"total\":\"#{index + index + 1}\"}"
"{\"arguments\":{\"num1\":\"#{index}\",\"num2\":\"#{index + 1}\",\"total\":\"#{index + index + 1}\"},\"metadata\":{}}"
)
else
expect(span.get_tag(Datadog::CI::Ext::Test::TAG_NAME)).to eq(
Expand Down
12 changes: 12 additions & 0 deletions spec/datadog/ci/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,16 @@
expect(span.span_type).to eq("test")
end
end

describe "#set_parameters" do
let(:parameters) { {"foo" => "bar", "baz" => "qux"} }

it "sets the parameters" do
expect(tracer_span).to receive(:set_tag).with(
"test.parameters", JSON.generate({arguments: parameters, metadata: {}})
)

span.set_parameters(parameters)
end
end
end

0 comments on commit fd793ad

Please sign in to comment.