Skip to content

Commit 558753a

Browse files
committed
Add non allocating ConfigureScope overload
1 parent 0e11682 commit 558753a

File tree

7 files changed

+173
-6
lines changed

7 files changed

+173
-6
lines changed

src/Sentry/Extensibility/DisabledHub.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,23 @@ public void ConfigureScope(Action<Scope> configureScope)
3030
{
3131
}
3232

33+
/// <summary>
34+
/// No-Op.
35+
/// </summary>
36+
public void ConfigureScope<TArg>(Action<Scope, TArg> configureScope, TArg arg)
37+
{
38+
}
39+
3340
/// <summary>
3441
/// No-Op.
3542
/// </summary>
3643
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope) => Task.CompletedTask;
3744

45+
/// <summary>
46+
/// No-Op.
47+
/// </summary>
48+
public Task ConfigureScopeAsync<TArg>(Func<Scope, TArg, Task> configureScope, TArg arg) => Task.CompletedTask;
49+
3850
/// <summary>
3951
/// No-Op.
4052
/// </summary>

src/Sentry/Extensibility/HubAdapter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,27 @@ private HubAdapter() { }
3939
public void ConfigureScope(Action<Scope> configureScope)
4040
=> SentrySdk.ConfigureScope(configureScope);
4141

42+
/// <summary>
43+
/// Forwards the call to <see cref="SentrySdk"/>.
44+
/// </summary>
45+
[DebuggerStepThrough]
46+
public void ConfigureScope<TArg>(Action<Scope, TArg> configureScope, TArg arg)
47+
=> SentrySdk.ConfigureScope(configureScope, arg);
48+
4249
/// <summary>
4350
/// Forwards the call to <see cref="SentrySdk"/>.
4451
/// </summary>
4552
[DebuggerStepThrough]
4653
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
4754
=> SentrySdk.ConfigureScopeAsync(configureScope);
4855

56+
/// <summary>
57+
/// Forwards the call to <see cref="SentrySdk"/>.
58+
/// </summary>
59+
[DebuggerStepThrough]
60+
public Task ConfigureScopeAsync<TArg>(Func<Scope, TArg, Task> configureScope, TArg arg)
61+
=> SentrySdk.ConfigureScopeAsync(configureScope, arg);
62+
4963
/// <summary>
5064
/// Forwards the call to <see cref="SentrySdk"/>.
5165
/// </summary>

src/Sentry/ISentryScopeManager.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,33 @@ namespace Sentry;
1010
public interface ISentryScopeManager
1111
{
1212
/// <summary>
13-
/// Configures the current scope.
13+
/// Configures the current scope through the callback.
1414
/// </summary>
15-
/// <param name="configureScope">The configure scope.</param>
15+
/// <param name="configureScope">The configure scope callback.</param>
1616
public void ConfigureScope(Action<Scope> configureScope);
1717

1818
/// <summary>
19-
/// Asynchronously configure the current scope.
19+
/// Configures the current scope through the callback.
2020
/// </summary>
21-
/// <param name="configureScope">The configure scope.</param>
21+
/// <param name="configureScope">The configure scope callback.</param>
22+
/// <param name="arg">The argument to pass to the configure scope callback.</param>
23+
internal void ConfigureScope<TArg>(Action<Scope, TArg> configureScope, TArg arg);
24+
25+
/// <summary>
26+
/// Configures the current scope through the callback asynchronously.
27+
/// </summary>
28+
/// <param name="configureScope">The configure scope callback.</param>
2229
/// <returns>A task that completes when the callback is done or a completed task if the SDK is disabled.</returns>
2330
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope);
2431

32+
/// <summary>
33+
/// Configures the current scope through the callback asynchronously.
34+
/// </summary>
35+
/// <param name="configureScope">The configure scope callback.</param>
36+
/// <param name="arg">The argument to pass to the configure scope callback.</param>
37+
/// <returns>A task that completes when the callback is done or a completed task if the SDK is disabled.</returns>
38+
internal Task ConfigureScopeAsync<TArg>(Func<Scope, TArg, Task> configureScope, TArg arg);
39+
2540
/// <summary>
2641
/// Sets a tag on the current scope.
2742
/// </summary>

src/Sentry/Internal/Hub.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ public void ConfigureScope(Action<Scope> configureScope)
9999
}
100100
}
101101

102+
public void ConfigureScope<TArg>(Action<Scope, TArg> configureScope, TArg arg)
103+
{
104+
try
105+
{
106+
ScopeManager.ConfigureScope(configureScope, arg);
107+
}
108+
catch (Exception e)
109+
{
110+
_options.LogError(e, "Failure to ConfigureScope");
111+
}
112+
}
113+
102114
public async Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
103115
{
104116
try
@@ -111,6 +123,18 @@ public async Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
111123
}
112124
}
113125

126+
public async Task ConfigureScopeAsync<TArg>(Func<Scope, TArg, Task> configureScope, TArg arg)
127+
{
128+
try
129+
{
130+
await ScopeManager.ConfigureScopeAsync(configureScope, arg).ConfigureAwait(false);
131+
}
132+
catch (Exception e)
133+
{
134+
_options.LogError(e, "Failure to ConfigureScopeAsync");
135+
}
136+
}
137+
114138
public void SetTag(string key, string value) => ScopeManager.SetTag(key, value);
115139

116140
public void UnsetTag(string key) => ScopeManager.UnsetTag(key);

src/Sentry/Internal/SentryScopeManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ public void ConfigureScope(Action<Scope>? configureScope)
3838
configureScope?.Invoke(scope);
3939
}
4040

41+
public void ConfigureScope<TArg>(Action<Scope, TArg>? configureScope, TArg arg)
42+
{
43+
var (scope, _) = GetCurrent();
44+
configureScope?.Invoke(scope, arg);
45+
}
46+
4147
public Task ConfigureScopeAsync(Func<Scope, Task>? configureScope)
4248
{
4349
var (scope, _) = GetCurrent();
4450
return configureScope?.Invoke(scope) ?? Task.CompletedTask;
4551
}
4652

53+
public Task ConfigureScopeAsync<TArg>(Func<Scope, TArg, Task>? configureScope, TArg arg)
54+
{
55+
var (scope, _) = GetCurrent();
56+
return configureScope?.Invoke(scope, arg) ?? Task.CompletedTask;
57+
}
58+
4759
public void SetTag(string key, string value)
4860
{
4961
var (scope, _) = GetCurrent();

src/Sentry/SentrySdk.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,32 @@ public static void ConfigureScope(Action<Scope> configureScope)
381381
=> CurrentHub.ConfigureScope(configureScope);
382382

383383
/// <summary>
384-
/// Configures the scope asynchronously.
384+
/// Configures the scope through the callback.
385385
/// </summary>
386386
/// <param name="configureScope">The configure scope callback.</param>
387-
/// <returns>The Id of the event.</returns>
387+
/// <param name="arg">The argument to pass to the configure scope callback.</param>
388+
internal static void ConfigureScope<TArg>(Action<Scope, TArg> configureScope, TArg arg)
389+
=> CurrentHub.ConfigureScope(configureScope, arg);
390+
391+
/// <summary>
392+
/// Configures the scope through the callback asynchronously.
393+
/// </summary>
394+
/// <param name="configureScope">The configure scope callback.</param>
395+
/// <returns>A task that completes when the callback is done or a completed task if the SDK is disabled.</returns>
388396
[DebuggerStepThrough]
389397
public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
390398
=> CurrentHub.ConfigureScopeAsync(configureScope);
391399

400+
/// <summary>
401+
/// Configures the scope through the callback asynchronously.
402+
/// </summary>
403+
/// <param name="configureScope">The configure scope callback.</param>
404+
/// <param name="arg">The argument to pass to the configure scope callback.</param>
405+
/// <returns>A task that completes when the callback is done or a completed task if the SDK is disabled.</returns>
406+
[DebuggerStepThrough]
407+
internal static Task ConfigureScopeAsync<TArg>(Func<Scope, TArg, Task> configureScope, TArg arg)
408+
=> CurrentHub.ConfigureScopeAsync(configureScope, arg);
409+
392410
/// <summary>
393411
/// Sets a tag on the current scope.
394412
/// </summary>

test/Sentry.Tests/SentrySdkTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,36 @@ public void ConfigureScope_Sync_CallbackNeverInvoked()
436436
Assert.False(invoked);
437437
}
438438

439+
[Fact]
440+
public void ConfigureScope_SyncWithArg_CallbackNeverInvoked()
441+
{
442+
var invoked = false;
443+
SentrySdk.ConfigureScope((_,_) => invoked = true, "arg");
444+
Assert.False(invoked);
445+
}
446+
447+
[Fact]
448+
public void ConfigureScope_SyncWithArg_ArgIsUsed()
449+
{
450+
using var _ = SentrySdk.Init(o =>
451+
{
452+
o.Dsn = ValidDsn;
453+
o.AutoSessionTracking = false;
454+
o.BackgroundWorker = Substitute.For<IBackgroundWorker>();
455+
o.InitNativeSdks = false;
456+
});
457+
458+
const string key = "key";
459+
const string arg = "arg";
460+
461+
SentrySdk.ConfigureScope((s,a) => s.SetTag(key, a), arg);
462+
463+
string actual = null;
464+
SentrySdk.ConfigureScope(s => actual = s.Tags[key]);
465+
466+
Assert.Equal(arg, actual);
467+
}
468+
439469
[SkippableFact]
440470
public async Task ConfigureScope_OnTask_PropagatedToCaller()
441471
{
@@ -668,6 +698,48 @@ await SentrySdk.ConfigureScopeAsync(_ =>
668698
Assert.False(invoked);
669699
}
670700

701+
[Fact]
702+
public async Task ConfigureScope_AsyncWithArg_CallbackNeverInvoked()
703+
{
704+
var invoked = false;
705+
await SentrySdk.ConfigureScopeAsync((_,_) =>
706+
{
707+
invoked = true;
708+
return Task.CompletedTask;
709+
}, "arg");
710+
Assert.False(invoked);
711+
}
712+
713+
[Fact]
714+
public async Task ConfigureScope_AsyncWithArg_ArgIsUsed()
715+
{
716+
using var _ = SentrySdk.Init(o =>
717+
{
718+
o.Dsn = ValidDsn;
719+
o.AutoSessionTracking = false;
720+
o.BackgroundWorker = Substitute.For<IBackgroundWorker>();
721+
o.InitNativeSdks = false;
722+
});
723+
724+
const string key = "key";
725+
const string arg = "arg";
726+
727+
await SentrySdk.ConfigureScopeAsync((s, a) =>
728+
{
729+
s.SetTag(key, a);
730+
return Task.CompletedTask;
731+
}, arg);
732+
733+
string actual = null;
734+
await SentrySdk.ConfigureScopeAsync(s =>
735+
{
736+
actual = s.Tags[key];
737+
return Task.CompletedTask;
738+
});
739+
740+
Assert.Equal(arg, actual);
741+
}
742+
671743
[Fact]
672744
public void CaptureEvent_Instance_NoOp() => SentrySdk.CaptureEvent(new SentryEvent());
673745

0 commit comments

Comments
 (0)