From 805925869388c055aa6a1d32e022274853957702 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 7 Nov 2023 16:14:50 +0100 Subject: [PATCH] factory method for Datadog::CI::Span --- lib/datadog/ci/recorder.rb | 14 ++++- lib/datadog/ci/span.rb | 12 +---- sig/datadog/ci/recorder.rbs | 2 + sig/datadog/ci/span.rbs | 6 +-- spec/datadog/ci/recorder_spec.rb | 11 +++- spec/datadog/ci/span_spec.rb | 87 +++++++++++--------------------- 6 files changed, 55 insertions(+), 77 deletions(-) diff --git a/lib/datadog/ci/recorder.rb b/lib/datadog/ci/recorder.rb index 4f6db41f..830bbcfe 100644 --- a/lib/datadog/ci/recorder.rb +++ b/lib/datadog/ci/recorder.rb @@ -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 @@ -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 diff --git a/lib/datadog/ci/span.rb b/lib/datadog/ci/span.rb index e6f24d14..d349a200 100644 --- a/lib/datadog/ci/span.rb +++ b/lib/datadog/ci/span.rb @@ -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 @@ -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 diff --git a/sig/datadog/ci/recorder.rbs b/sig/datadog/ci/recorder.rbs index da74f54c..fd84a055 100644 --- a/sig/datadog/ci/recorder.rbs +++ b/sig/datadog/ci/recorder.rbs @@ -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 diff --git a/sig/datadog/ci/span.rbs b/sig/datadog/ci/span.rbs index de1c21e0..32ae69a9 100644 --- a/sig/datadog/ci/span.rbs +++ b/sig/datadog/ci/span.rbs @@ -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 @@ -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 diff --git a/spec/datadog/ci/recorder_spec.rb b/spec/datadog/ci/recorder_spec.rb index c2ec50da..7f4ee337 100644 --- a/spec/datadog/ci/recorder_spec.rb +++ b/spec/datadog/ci/recorder_spec.rb @@ -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" } @@ -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 @@ -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 diff --git a/spec/datadog/ci/span_spec.rb b/spec/datadog/ci/span_spec.rb index ebe69567..fe4983c3 100644 --- a/spec/datadog/ci/span_spec.rb +++ b/spec/datadog/ci/span_spec.rb @@ -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") @@ -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) @@ -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") @@ -121,9 +78,6 @@ 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") @@ -131,10 +85,16 @@ 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") @@ -142,10 +102,26 @@ 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) @@ -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