Skip to content

Commit

Permalink
Fix #2292 Fix race condition on Add in redis profiler (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Feb 23, 2024
1 parent 2f7d592 commit 1c51317
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public ElasticApmProfiler(Func<IApmAgent> agentGetter)
_logger = new Lazy<IApmLogger>(() => _agent.Value.Logger.Scoped(nameof(ElasticApmProfiler)));
}

private readonly object _lock = new();

/// <summary>
/// Gets a profiling session for StackExchange.Redis to add redis commands to.
/// Creates a profiling session per span or transaction
Expand Down Expand Up @@ -63,12 +65,18 @@ public ProfilingSession GetProfilingSession()
if (_executionSegmentSessions.TryGetValue(executionSegment, out var session))
return session;

_logger.Value.Trace()?.Log("Creating profiling session for {ExecutionSegment} {Id}",
isSpan ? "span" : "transaction", executionSegment.Id);
lock (_lock)
{
if (_executionSegmentSessions.TryGetValue(executionSegment, out session))
return session;

_logger.Value.Trace()?.Log("Creating profiling session for {ExecutionSegment} {Id}",
isSpan ? "span" : "transaction", executionSegment.Id);

session = new ProfilingSession();
session = new ProfilingSession();

_executionSegmentSessions.Add(executionSegment, session);
_executionSegmentSessions.Add(executionSegment, session);
}

if (isSpan)
realSpan.Ended += (sender, _) => EndProfilingSession(sender, session);
Expand Down

0 comments on commit 1c51317

Please sign in to comment.