Skip to content

Commit bc1e4f1

Browse files
authored
Add SentrySdk.SetTag #4228 (#4232)
* Add SentrySdk.SetTag #4228 * Update CHANGELOG.md
1 parent daaa5ca commit bc1e4f1

File tree

11 files changed

+144
-0
lines changed

11 files changed

+144
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Reduced memory pressure when sampling less than 100% of traces/transactions ([#4212](https://github.com/getsentry/sentry-dotnet/pull/4212))
8+
- Add SentrySdk.SetTag ([#4232](https://github.com/getsentry/sentry-dotnet/pull/4232))
89

910
### Fixes
1011

src/Sentry/Extensibility/DisabledHub.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ public void ConfigureScope(Action<Scope> configureScope)
3535
/// </summary>
3636
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope) => Task.CompletedTask;
3737

38+
/// <summary>
39+
/// No-Op.
40+
/// </summary>
41+
public void SetTag(string key, string value)
42+
{
43+
}
44+
45+
/// <summary>
46+
/// No-Op.
47+
/// </summary>
48+
public void UnsetTag(string key)
49+
{
50+
}
51+
3852
/// <summary>
3953
/// No-Op.
4054
/// </summary>

src/Sentry/Extensibility/HubAdapter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ public void ConfigureScope(Action<Scope> configureScope)
4646
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
4747
=> SentrySdk.ConfigureScopeAsync(configureScope);
4848

49+
/// <summary>
50+
/// Forwards the call to <see cref="SentrySdk"/>.
51+
/// </summary>
52+
[DebuggerStepThrough]
53+
public void SetTag(string key, string value)
54+
=> SentrySdk.SetTag(key, value);
55+
56+
/// <summary>
57+
/// Forwards the call to <see cref="SentrySdk"/>.
58+
/// </summary>
59+
[DebuggerStepThrough]
60+
public void UnsetTag(string key)
61+
=> SentrySdk.UnsetTag(key);
62+
4963
/// <summary>
5064
/// Forwards the call to <see cref="SentrySdk"/>.
5165
/// </summary>

src/Sentry/ISentryScopeManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public interface ISentryScopeManager
2222
/// <returns>A task that completes when the callback is done or a completed task if the SDK is disabled.</returns>
2323
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope);
2424

25+
/// <summary>
26+
/// Sets a tag on the current scope.
27+
/// </summary>
28+
public void SetTag(string key, string value);
29+
30+
/// <summary>
31+
/// Removes a tag from the current scope.
32+
/// </summary>
33+
public void UnsetTag(string key);
34+
2535
/// <summary>
2636
/// Binds the client to the current scope.
2737
/// </summary>

src/Sentry/Internal/Hub.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ public async Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
111111
}
112112
}
113113

114+
public void SetTag(string key, string value) => ScopeManager.SetTag(key, value);
115+
116+
public void UnsetTag(string key) => ScopeManager.UnsetTag(key);
117+
114118
public IDisposable PushScope() => ScopeManager.PushScope();
115119

116120
public IDisposable PushScope<TState>(TState state) => ScopeManager.PushScope(state);

src/Sentry/Internal/SentryScopeManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public Task ConfigureScopeAsync(Func<Scope, Task>? configureScope)
4444
return configureScope?.Invoke(scope) ?? Task.CompletedTask;
4545
}
4646

47+
public void SetTag(string key, string value)
48+
{
49+
var (scope, _) = GetCurrent();
50+
scope.SetTag(key, value);
51+
}
52+
53+
public void UnsetTag(string key)
54+
{
55+
var (scope, _) = GetCurrent();
56+
scope.UnsetTag(key);
57+
}
58+
4759
public IDisposable PushScope() => PushScope<object>(null);
4860

4961
public IDisposable PushScope<TState>(TState? state)

src/Sentry/SentrySdk.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ public static void ConfigureScope(Action<Scope> configureScope)
389389
public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
390390
=> CurrentHub.ConfigureScopeAsync(configureScope);
391391

392+
/// <summary>
393+
/// Sets a tag on the current scope.
394+
/// </summary>
395+
[DebuggerStepThrough]
396+
public static void SetTag(string key, string value)
397+
=> CurrentHub.SetTag(key, value);
398+
399+
/// <summary>
400+
/// Removes a tag from the current scope.
401+
/// </summary>
402+
[DebuggerStepThrough]
403+
public static void UnsetTag(string key)
404+
=> CurrentHub.UnsetTag(key);
405+
392406
/// <inheritdoc cref="ISentryClient.CaptureEnvelope"/>
393407
[DebuggerStepThrough]
394408
[EditorBrowsable(EditorBrowsableState.Never)]

test/Sentry.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ namespace Sentry
259259
System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope);
260260
System.IDisposable PushScope();
261261
System.IDisposable PushScope<TState>(TState state);
262+
void SetTag(string key, string value);
263+
void UnsetTag(string key);
262264
}
263265
public interface ISentryScopeStateProcessor
264266
{
@@ -863,12 +865,14 @@ namespace Sentry
863865
public static System.IDisposable PushScope() { }
864866
public static System.IDisposable PushScope<TState>(TState state) { }
865867
public static void ResumeSession() { }
868+
public static void SetTag(string key, string value) { }
866869
public static void StartSession() { }
867870
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context) { }
868871
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
869872
public static Sentry.ITransactionTracer StartTransaction(string name, string operation) { }
870873
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, Sentry.SentryTraceHeader traceHeader) { }
871874
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, string? description) { }
875+
public static void UnsetTag(string key) { }
872876
}
873877
public class SentrySession : Sentry.ISentrySession
874878
{
@@ -1367,8 +1371,10 @@ namespace Sentry.Extensibility
13671371
public System.IDisposable PushScope() { }
13681372
public System.IDisposable PushScope<TState>(TState state) { }
13691373
public void ResumeSession() { }
1374+
public void SetTag(string key, string value) { }
13701375
public void StartSession() { }
13711376
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
1377+
public void UnsetTag(string key) { }
13721378
}
13731379
public class FormRequestPayloadExtractor : Sentry.Extensibility.BaseRequestPayloadExtractor
13741380
{
@@ -1413,8 +1419,10 @@ namespace Sentry.Extensibility
14131419
public System.IDisposable PushScope() { }
14141420
public System.IDisposable PushScope<TState>(TState state) { }
14151421
public void ResumeSession() { }
1422+
public void SetTag(string key, string value) { }
14161423
public void StartSession() { }
14171424
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
1425+
public void UnsetTag(string key) { }
14181426
}
14191427
public interface IBackgroundWorker
14201428
{

test/Sentry.Tests/ApiApprovalTests.Run.DotNet9_0.verified.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ namespace Sentry
259259
System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope);
260260
System.IDisposable PushScope();
261261
System.IDisposable PushScope<TState>(TState state);
262+
void SetTag(string key, string value);
263+
void UnsetTag(string key);
262264
}
263265
public interface ISentryScopeStateProcessor
264266
{
@@ -863,12 +865,14 @@ namespace Sentry
863865
public static System.IDisposable PushScope() { }
864866
public static System.IDisposable PushScope<TState>(TState state) { }
865867
public static void ResumeSession() { }
868+
public static void SetTag(string key, string value) { }
866869
public static void StartSession() { }
867870
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context) { }
868871
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
869872
public static Sentry.ITransactionTracer StartTransaction(string name, string operation) { }
870873
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, Sentry.SentryTraceHeader traceHeader) { }
871874
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, string? description) { }
875+
public static void UnsetTag(string key) { }
872876
}
873877
public class SentrySession : Sentry.ISentrySession
874878
{
@@ -1367,8 +1371,10 @@ namespace Sentry.Extensibility
13671371
public System.IDisposable PushScope() { }
13681372
public System.IDisposable PushScope<TState>(TState state) { }
13691373
public void ResumeSession() { }
1374+
public void SetTag(string key, string value) { }
13701375
public void StartSession() { }
13711376
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
1377+
public void UnsetTag(string key) { }
13721378
}
13731379
public class FormRequestPayloadExtractor : Sentry.Extensibility.BaseRequestPayloadExtractor
13741380
{
@@ -1413,8 +1419,10 @@ namespace Sentry.Extensibility
14131419
public System.IDisposable PushScope() { }
14141420
public System.IDisposable PushScope<TState>(TState state) { }
14151421
public void ResumeSession() { }
1422+
public void SetTag(string key, string value) { }
14161423
public void StartSession() { }
14171424
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
1425+
public void UnsetTag(string key) { }
14181426
}
14191427
public interface IBackgroundWorker
14201428
{

test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ namespace Sentry
247247
System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope);
248248
System.IDisposable PushScope();
249249
System.IDisposable PushScope<TState>(TState state);
250+
void SetTag(string key, string value);
251+
void UnsetTag(string key);
250252
}
251253
public interface ISentryScopeStateProcessor
252254
{
@@ -844,12 +846,14 @@ namespace Sentry
844846
public static System.IDisposable PushScope() { }
845847
public static System.IDisposable PushScope<TState>(TState state) { }
846848
public static void ResumeSession() { }
849+
public static void SetTag(string key, string value) { }
847850
public static void StartSession() { }
848851
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context) { }
849852
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
850853
public static Sentry.ITransactionTracer StartTransaction(string name, string operation) { }
851854
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, Sentry.SentryTraceHeader traceHeader) { }
852855
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, string? description) { }
856+
public static void UnsetTag(string key) { }
853857
}
854858
public class SentrySession : Sentry.ISentrySession
855859
{
@@ -1348,8 +1352,10 @@ namespace Sentry.Extensibility
13481352
public System.IDisposable PushScope() { }
13491353
public System.IDisposable PushScope<TState>(TState state) { }
13501354
public void ResumeSession() { }
1355+
public void SetTag(string key, string value) { }
13511356
public void StartSession() { }
13521357
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
1358+
public void UnsetTag(string key) { }
13531359
}
13541360
public class FormRequestPayloadExtractor : Sentry.Extensibility.BaseRequestPayloadExtractor
13551361
{
@@ -1394,8 +1400,10 @@ namespace Sentry.Extensibility
13941400
public System.IDisposable PushScope() { }
13951401
public System.IDisposable PushScope<TState>(TState state) { }
13961402
public void ResumeSession() { }
1403+
public void SetTag(string key, string value) { }
13971404
public void StartSession() { }
13981405
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
1406+
public void UnsetTag(string key) { }
13991407
}
14001408
public interface IBackgroundWorker
14011409
{

0 commit comments

Comments
 (0)