From 831ae0301ee06c042053b019dc9263442cfb82a7 Mon Sep 17 00:00:00 2001 From: Robin Schroer Date: Tue, 10 Dec 2024 09:38:18 +0900 Subject: [PATCH] fix: Fix a bug breaking all span tags Somehow I brain-farted and mixed up if and unless, and then also didn't run the test suite which would've told me that this isn't working. Also adding more test coverage in this, just to be safe. --- lib/degica_datadog/tracing.rb | 4 ++-- spec/tracing_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/degica_datadog/tracing.rb b/lib/degica_datadog/tracing.rb index f6b851f..f2598ea 100644 --- a/lib/degica_datadog/tracing.rb +++ b/lib/degica_datadog/tracing.rb @@ -111,14 +111,14 @@ def error!(e) # Returns the current span. def current_span - Datadog::Tracing.active_span unless Config.enabled? + Datadog::Tracing.active_span if Config.enabled? end # Returns the current root span. Root here meaning within the service, not necessarily the # actual trace root span if that is from a different service. def root_span # forgive me my friends - Datadog::Tracing.active_trace.instance_variable_get(:@root_span) unless Config.enabled? + Datadog::Tracing.active_trace.instance_variable_get(:@root_span) if Config.enabled? end # Please don't use this. It's just a temporary thing until we can get the diff --git a/spec/tracing_spec.rb b/spec/tracing_spec.rb index 0d57f3a..0da264a 100644 --- a/spec/tracing_spec.rb +++ b/spec/tracing_spec.rb @@ -13,6 +13,38 @@ end end + describe ".current_span" do + it "does nothing when disabled" do + allow(DegicaDatadog::Config).to receive(:enabled?).and_return(false) + described_class.span!("test") do + expect(described_class.current_span).to be_nil + end + end + + it "returns the current span" do + allow(DegicaDatadog::Config).to receive(:enabled?).and_return(true) + described_class.span!("test") do + expect(described_class.current_span).to_not be_nil + end + end + end + + describe ".root_span" do + it "does nothing when disabled" do + allow(DegicaDatadog::Config).to receive(:enabled?).and_return(false) + described_class.span!("test") do + expect(described_class.root_span).to be_nil + end + end + + it "returns the current span" do + allow(DegicaDatadog::Config).to receive(:enabled?).and_return(true) + described_class.span!("test") do + expect(described_class.root_span).to_not be_nil + end + end + end + describe ".span!" do it "starts a new span" do expect { described_class.span!("test") }.to change { Datadog::Tracing.active_span }.from(nil)