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)