Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2292 Fix race condition on Add in redis profiler #2303

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading