Skip to content

Commit

Permalink
ref: Instantiating _tags for Spans lazily (#2636)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes authored Sep 19, 2023
1 parent e6bb733 commit a667e7c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Reduced the memory footprint of `SpanTracer` by initializing the tags lazily ([2636](https://github.com/getsentry/sentry-dotnet/pull/2636))
- Added distributed tracing without performance for Azure Function Workers ([#2630](https://github.com/getsentry/sentry-dotnet/pull/2630))
- The SDK now provides and overload of `ContinueTrace` that accepts headers as `string` ([#2601](https://github.com/getsentry/sentry-dotnet/pull/2601))
- Sentry tracing middleware now gets configured automatically ([#2602](https://github.com/getsentry/sentry-dotnet/pull/2602))
Expand Down
34 changes: 34 additions & 0 deletions benchmarks/Sentry.Benchmarks/TransactionBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BenchmarkDotNet.Attributes;

namespace Sentry.Benchmarks;

public class TransactionBenchmarks
{
private const string Operation = "Operation";
private const string Name = "Name";

[Params(1, 10, 100, 1000)]
public int SpanCount;

private IDisposable _sdk;

[GlobalSetup(Target = nameof(CreateTransaction))]
public void EnabledSdk() => _sdk = SentrySdk.Init(Constants.ValidDsn);

[GlobalCleanup(Target = nameof(CreateTransaction))]
public void DisableDsk() => _sdk.Dispose();

[Benchmark(Description = "Creates a Transaction")]
public void CreateTransaction()
{
var transaction = SentrySdk.StartTransaction(Name, Operation);

for (var i = 0; i < SpanCount; i++)
{
var span = transaction.StartChild(Operation);
span.Finish();
}

transaction.Finish();
}
}
2 changes: 1 addition & 1 deletion src/Sentry/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Span(ISpan tracer)
Status = tracer.Status;
IsSampled = tracer.IsSampled;
_extra = tracer.Extra.ToDictionary();
_tags = tracer.Tags.ToDictionary();
_tags = tracer is SpanTracer s ? s.InternalTags?.ToDictionary() : tracer.Tags.ToDictionary();
}

/// <inheritdoc />
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry/SpanTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class SpanTracer : ISpan

private ConcurrentDictionary<string, string>? _tags;

internal ConcurrentDictionary<string, string>? InternalTags => _tags;

/// <inheritdoc />
public IReadOnlyDictionary<string, string> Tags => _tags ??= new ConcurrentDictionary<string, string>();

Expand Down

0 comments on commit a667e7c

Please sign in to comment.