Skip to content

Commit

Permalink
factory method for Datadog::CI::Span
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 7, 2023
1 parent 7ceca4a commit 8059258
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 77 deletions.
14 changes: 12 additions & 2 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ def trace(span_type, span_name, tags: {}, &block)
# create_datadog_span(span_name, span_options: span_options, tags: tags, &block)
if block
Datadog::Tracing.trace(span_name, **span_options) do |tracer_span, trace|
block.call(Span.new(tracer_span, tags))
block.call(build_span(tracer_span, tags))
end
else
tracer_span = Datadog::Tracing.trace(span_name, **span_options)

Span.new(tracer_span, tags)
build_span(tracer_span, tags)
end
end

Expand Down Expand Up @@ -105,6 +105,16 @@ def build_test(tracer_span, tags)

test
end

def build_span(tracer_span, tags)
span = Span.new(tracer_span)

span.set_default_tags
span.set_environment_runtime_tags
span.set_tags(tags)

span
end
end
end
end
12 changes: 1 addition & 11 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ module CI
class Span
attr_reader :tracer_span

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

init_span!(tags) unless tags.nil?
end

def name
Expand Down Expand Up @@ -74,14 +72,6 @@ def set_environment_runtime_tags
def set_default_tags
tracer_span.set_tag(Ext::Test::TAG_SPAN_KIND, Ext::AppTypes::TYPE_TEST)
end

private

def init_span!(tags)
set_tags(tags)
set_default_tags
set_environment_runtime_tags
end
end
end
end
2 changes: 2 additions & 0 deletions sig/datadog/ci/recorder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Datadog
private

def build_test: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::Test

def build_span: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::Span
end
end
end
6 changes: 1 addition & 5 deletions sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Datadog

attr_reader tracer_span: Datadog::Tracing::SpanOperation

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

def name: () -> String

Expand All @@ -30,10 +30,6 @@ module Datadog
def set_environment_runtime_tags: () -> void

def set_default_tags: () -> void

private

def init_span!: (Hash[untyped, untyped] tags) -> void
end
end
end
11 changes: 9 additions & 2 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def expect_initialized_test
end

describe "#trace" do
def expect_initialized_span
allow(Datadog::CI::Span).to receive(:new).with(span_op).and_return(ci_span)
expect(ci_span).to receive(:set_default_tags)
expect(ci_span).to receive(:set_environment_runtime_tags)
expect(ci_span).to receive(:set_tags).with(tags)
end

let(:tags) { {"my_tag" => "my_value"} }
let(:span_type) { "step" }
let(:span_name) { "span name" }
Expand Down Expand Up @@ -147,7 +154,7 @@ def expect_initialized_test
trace_block.call(span_op, trace_op)
end

allow(Datadog::CI::Span).to receive(:new).with(span_op, expected_tags).and_return(ci_span)
expect_initialized_span

trace
end
Expand Down Expand Up @@ -180,7 +187,7 @@ def expect_initialized_test
)
.and_return(span_op)

allow(Datadog::CI::Span).to receive(:new).with(span_op, expected_tags).and_return(ci_span)
expect_initialized_span

trace
end
Expand Down
87 changes: 30 additions & 57 deletions spec/datadog/ci/span_spec.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
RSpec.describe Datadog::CI::Span do
describe "#initialize" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span, tags) }

context "when tags are nil" do
let(:tags) { nil }

it "doesn't set any tags" do
expect(tracer_span).to_not receive(:set_tag)
span
end
end

context "when tags are provided" do
let(:tags) { {"foo" => "bar"} }

it "sets provided tags along with default tag and environment tag" do
# default tags
expect(tracer_span).to receive(:set_tag).with("span.kind", "test")

# runtime tags
expect(tracer_span).to receive(:set_tag).with("os.architecture", ::RbConfig::CONFIG["host_cpu"])
expect(tracer_span).to receive(:set_tag).with("os.platform", ::RbConfig::CONFIG["host_os"])
expect(tracer_span).to receive(:set_tag).with("runtime.name", Datadog::Core::Environment::Ext::LANG_ENGINE)
expect(tracer_span).to receive(:set_tag).with("runtime.version", Datadog::Core::Environment::Ext::ENGINE_VERSION)

# client-supplied tags
expect(tracer_span).to receive(:set_tag).with("foo", "bar")

span
end
end
end
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, name: "span_name", type: "test") }
subject(:span) { described_class.new(tracer_span) }

describe "#name" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, name: "span_name") }
let(:span) { described_class.new(tracer_span) }

it "returns the span name" do
expect(span.name).to eq("span_name")
end
end

describe "#passed!" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

it "sets the status to PASS" do
expect(tracer_span).to receive(:set_tag).with("test.status", "pass")

Expand All @@ -54,9 +17,6 @@
end

describe "#failed!" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

context "when exception is nil" do
it "sets the status to FAIL" do
expect(tracer_span).to receive(:status=).with(1)
Expand All @@ -78,9 +38,6 @@
end

describe "#skipped!" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

context "when exception is nil" do
it "sets the status to SKIP" do
expect(tracer_span).to receive(:set_tag).with("test.status", "skip")
Expand Down Expand Up @@ -121,31 +78,50 @@
end

describe "#set_tag" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }

it "sets the tag" do
expect(tracer_span).to receive(:set_tag).with("foo", "bar")

span.set_tag("foo", "bar")
end
end

describe "#set_metric" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }
describe "#set_tags" do
it "sets the tags" do
expect(tracer_span).to receive(:set_tag).with("foo", "bar")
expect(tracer_span).to receive(:set_tag).with("baz", "qux")

span.set_tags("foo" => "bar", "baz" => "qux")
end
end

describe "#set_metric" do
it "sets the metric" do
expect(tracer_span).to receive(:set_metric).with("foo", "bar")

span.set_metric("foo", "bar")
end
end

describe "#finish" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation) }
let(:span) { described_class.new(tracer_span) }
describe "#set_default_tags" do
it "sets the default tags" do
expect(tracer_span).to receive(:set_tag).with("span.kind", "test")

span.set_default_tags
end
end

describe "#set_environment_runtime_tags" do
it "sets the environment runtime tags" do
expect(tracer_span).to receive(:set_tag).with("os.architecture", ::RbConfig::CONFIG["host_cpu"])
expect(tracer_span).to receive(:set_tag).with("os.platform", ::RbConfig::CONFIG["host_os"])
expect(tracer_span).to receive(:set_tag).with("runtime.name", Datadog::Core::Environment::Ext::LANG_ENGINE)
expect(tracer_span).to receive(:set_tag).with("runtime.version", Datadog::Core::Environment::Ext::ENGINE_VERSION)

span.set_environment_runtime_tags
end
end

describe "#finish" do
it "finishes the span" do
expect(tracer_span).to receive(:finish)

Expand All @@ -154,9 +130,6 @@
end

describe "#span_type" do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, type: "test") }
let(:span) { described_class.new(tracer_span) }

it "returns 'test'" do
expect(span.span_type).to eq("test")
end
Expand Down

0 comments on commit 8059258

Please sign in to comment.