Skip to content

Commit

Permalink
ScopeManagerShim - do not store not recorded spans
Browse files Browse the repository at this point in the history
  • Loading branch information
konczykl committed Feb 4, 2025
1 parent 36b777b commit 3f13bcd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/OpenTelemetry.Shims.OpenTracing/ScopeManagerShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ public IScope Activate(ISpan span, bool finishSpanOnDispose)
scope!.Dispose();
});

SpanScopeTable.Add(shim.Span, instrumentation);
// not recorded span is static TelemetrySpan.NoopInstance
// we do not return the instrumentation if the span is not recorded anyway
if (shim.Span.IsRecording)
{
SpanScopeTable.Add(shim.Span, instrumentation);
#if DEBUG
Interlocked.Increment(ref this.spanScopeTableCount);
Interlocked.Increment(ref this.spanScopeTableCount);
#endif
}

return instrumentation;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Shims.OpenTracing.Tests;

public class ScopeManagerShimNotRecordedTests
{
[Fact]
public void MultipleNotRecordedSpans_ReturnNoopInstance_DoNotThrow()
{
var tracer = TracerProvider.Default.GetTracer("TracerName");
var scopeManagerShim = new ScopeManagerShim();
var span1 = new SpanShim(tracer.StartSpan("Span-1"));
var span2 = new SpanShim(tracer.StartSpan("Span-2"));

var exception = Record.Exception(() =>
{
using var scope1 = scopeManagerShim.Activate(span1, true);
using var scope2 = scopeManagerShim.Activate(span1, true);
});

Assert.Same(TelemetrySpan.NoopInstance, span1.Span);
Assert.Same(TelemetrySpan.NoopInstance, span2.Span);
Assert.False(span1.Span.IsRecording);
Assert.False(span2.Span.IsRecording);
Assert.Null(exception);
}
}

0 comments on commit 3f13bcd

Please sign in to comment.