Skip to content

Commit b3239bb

Browse files
committed
Use non allocating ConfigureScope overload
1 parent a04c3a6 commit b3239bb

9 files changed

Lines changed: 27 additions & 33 deletions

File tree

src/Sentry.AspNetCore/SentryMiddleware.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
118118
context.Items.TryAdd(BaggageHeaderItemKey, baggageHeader);
119119
context.Items.TryAdd(TransactionContextItemKey, transactionContext);
120120

121-
hub.ConfigureScope(scope =>
121+
hub.ConfigureScope(static (scope, arg) =>
122122
{
123123
// At the point lots of stuff from the request are not yet filled
124124
// Identity for example is added later on in the pipeline
@@ -131,16 +131,16 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
131131
// when the event fires. Use `activeScope`, not `scope` or `hub`.
132132
scope.OnEvaluating += (_, activeScope) =>
133133
{
134-
SyncOptionsScope(activeScope);
135-
PopulateScope(context, activeScope);
134+
arg.middleware.SyncOptionsScope(activeScope);
135+
arg.middleware.PopulateScope(arg.context, activeScope);
136136
};
137-
});
137+
}, (middleware: this, context));
138138

139139
// Pre-create the Sentry Event ID and save it on the scope it so it's available throughout the pipeline,
140140
// even if there's no event actually being sent to Sentry. This allows for things like a custom exception
141141
// handler page to access the event ID, enabling user feedback, etc.
142142
var eventId = SentryId.Create();
143-
hub.ConfigureScope(scope => scope.LastEventId = eventId);
143+
hub.ConfigureScope(static (scope, eventId) => scope.LastEventId = eventId, eventId);
144144

145145
try
146146
{
@@ -167,7 +167,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
167167
{
168168
// The middleware pipeline finishes up before the Otel Activity.OnEnd callback is invoked so we need
169169
// so save a copy of the scope that can be restored by our SentrySpanProcessor
170-
hub.ConfigureScope(scope => activity.SetFused(scope));
170+
hub.ConfigureScope(static (scope, activity) => activity.SetFused(scope), activity);
171171
}
172172

173173
// When an exception was handled by other component (i.e: UseExceptionHandler feature).
@@ -179,12 +179,12 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
179179
"The web server likely returned a customized error page as a result of this exception.";
180180

181181
#if NET6_0_OR_GREATER
182-
hub.ConfigureScope(scope =>
182+
hub.ConfigureScope(static (scope, arg) =>
183183
{
184184
scope.ExceptionProcessors.Add(
185-
new ExceptionHandlerFeatureProcessor(originalMethod, exceptionFeature)
185+
new ExceptionHandlerFeatureProcessor(arg.originalMethod, arg.exceptionFeature)
186186
);
187-
});
187+
}, (originalMethod, exceptionFeature));
188188
#endif
189189
CaptureException(exceptionFeature.Error, eventId, "IExceptionHandlerFeature", description);
190190
}

src/Sentry.AspNetCore/SentryTracingMiddleware.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ public async Task InvokeAsync(HttpContext context)
130130

131131
// Expose the transaction on the scope so that the user
132132
// can retrieve it and start child spans off of it.
133-
hub.ConfigureScope(scope =>
134-
{
135-
scope.Transaction = transaction;
136-
});
133+
hub.ConfigureScope(static (scope, transaction) => scope.Transaction = transaction, transaction);
137134

138135
Exception? exception = null;
139136
try

src/Sentry.Extensions.Logging/SentryLoggerProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal SentryLoggerProvider(
4848
if (hub.IsEnabled)
4949
{
5050
_scope = hub.PushScope();
51-
hub.ConfigureScope(s =>
51+
hub.ConfigureScope(static s =>
5252
{
5353
if (s.Sdk is { } sdk)
5454
{

src/Sentry.OpenTelemetry/AspNetCoreEnricher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public void Enrich(ISpan span, Activity activity, IHub hub, SentryOptions? optio
1010
{
1111
if (options?.SendDefaultPii is true)
1212
{
13-
hub.ConfigureScope(scope =>
13+
hub.ConfigureScope(static (scope, enricher) =>
1414
{
15-
if (!scope.HasUser() && _userFactory.Create() is { } user)
15+
if (!scope.HasUser() && enricher._userFactory.Create() is { } user)
1616
{
1717
scope.User = user;
1818
}
19-
});
19+
}, this);
2020
}
2121
}
2222
}

src/Sentry.OpenTelemetry/SentrySpanProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private void CreateRootSpan(Activity data)
166166
);
167167
transaction.Contexts.Trace.Origin = OpenTelemetryOrigin;
168168
transaction.StartTimestamp = data.StartTimeUtc;
169-
_hub.ConfigureScope(scope => scope.Transaction = transaction);
169+
_hub.ConfigureScope(static (scope, transaction) => scope.Transaction = transaction, transaction);
170170
transaction.SetFused(data);
171171
_map[data.SpanId] = transaction;
172172
}

src/Sentry/HubExtensions.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,11 @@ public static ITransactionTracer StartTransaction(
5656
/// </summary>
5757
public static ISpan StartSpan(this IHub hub, string operation, string description)
5858
{
59-
ITransactionTracer? currentTransaction = null;
60-
hub.ConfigureScope(s => currentTransaction = s.Transaction);
61-
return currentTransaction is { } transaction
59+
return hub.GetTransaction() is { } transaction
6260
? transaction.StartChild(operation, description)
6361
: hub.StartTransaction(operation, description); // this is actually in the wrong order but changing it may break other things
6462
}
6563

66-
6764
/// <summary>
6865
/// Adds a breadcrumb to the current scope.
6966
/// </summary>
@@ -158,8 +155,8 @@ public static void AddBreadcrumb(
158155
}
159156

160157
hub.ConfigureScope(
161-
s => s.AddBreadcrumb(breadcrumb, hint ?? new SentryHint())
162-
);
158+
static(s, arg) => s.AddBreadcrumb(arg.breadcrumb, arg.hint ?? new SentryHint()),
159+
(breadcrumb, hint));
163160
}
164161

165162
/// <summary>
@@ -175,13 +172,13 @@ public static void AddBreadcrumb(
175172
/// like Loggers which guarantee log messages are not lost.
176173
/// </remarks>
177174
[EditorBrowsable(EditorBrowsableState.Never)]
178-
public static void LockScope(this IHub hub) => hub.ConfigureScope(c => c.Locked = true);
175+
public static void LockScope(this IHub hub) => hub.ConfigureScope(static s => s.Locked = true);
179176

180177
/// <summary>
181178
/// Unlocks the current scope to allow subsequent calls to <see cref="ISentryScopeManager.PushScope"/> create new scopes.
182179
/// </summary>
183180
[EditorBrowsable(EditorBrowsableState.Never)]
184-
public static void UnlockScope(this IHub hub) => hub.ConfigureScope(c => c.Locked = false);
181+
public static void UnlockScope(this IHub hub) => hub.ConfigureScope(static s => s.Locked = false);
185182

186183
private sealed class LockedScope : IDisposable
187184
{

src/Sentry/Internal/Hub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public TransactionContext ContinueTrace(
296296
string? operation = null)
297297
{
298298
var propagationContext = SentryPropagationContext.CreateFromHeaders(_options.DiagnosticLogger, traceHeader, baggageHeader, _replaySession);
299-
ConfigureScope(scope => scope.SetPropagationContext(propagationContext));
299+
ConfigureScope(static (scope, propagationContext) => scope.SetPropagationContext(propagationContext), propagationContext);
300300

301301
return new TransactionContext(
302302
name: name ?? string.Empty,

src/Sentry/Internal/UnsampledTransaction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public override void Finish()
6464

6565
// Clear the transaction from the scope and regenerate the Propagation Context, so new events don't have a
6666
// trace context that is "older" than the transaction that just finished
67-
_hub.ConfigureScope(scope =>
67+
_hub.ConfigureScope(static (scope, transactionTracer) =>
6868
{
69-
scope.ResetTransaction(this);
69+
scope.ResetTransaction(transactionTracer);
7070
scope.SetPropagationContext(new SentryPropagationContext());
71-
});
71+
}, this);
7272

7373
// Record the discarded events
7474
var spanCount = Spans.Count + 1; // 1 for each span + 1 for the transaction itself

src/Sentry/TransactionTracer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,11 @@ public void Finish()
386386

387387
// Clear the transaction from the scope and regenerate the Propagation Context
388388
// We do this so new events don't have a trace context that is "older" than the transaction that just finished
389-
_hub.ConfigureScope(scope =>
389+
_hub.ConfigureScope(static (scope, transactionTracer) =>
390390
{
391-
scope.ResetTransaction(this);
391+
scope.ResetTransaction(transactionTracer);
392392
scope.SetPropagationContext(new SentryPropagationContext());
393-
});
393+
}, this);
394394

395395
// Client decides whether to discard this transaction based on sampling
396396
_hub.CaptureTransaction(new SentryTransaction(this));

0 commit comments

Comments
 (0)