Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add SentrySdk.SetTag #4228
  • Loading branch information
KnapSac committed May 30, 2025
commit a83370d84f6b81f70e9e542ca3a69a93bc58e438
14 changes: 14 additions & 0 deletions src/Sentry/Extensibility/DisabledHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public void ConfigureScope(Action<Scope> configureScope)
/// </summary>
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope) => Task.CompletedTask;

/// <summary>
/// No-Op.
/// </summary>
public void SetTag(string key, string value)
{
}

/// <summary>
/// No-Op.
/// </summary>
public void UnsetTag(string key)
{
}

/// <summary>
/// No-Op.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Sentry/Extensibility/HubAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void ConfigureScope(Action<Scope> configureScope)
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
=> SentrySdk.ConfigureScopeAsync(configureScope);

/// <summary>
/// Forwards the call to <see cref="SentrySdk"/>.
/// </summary>
[DebuggerStepThrough]
public void SetTag(string key, string value)
=> SentrySdk.SetTag(key, value);

/// <summary>
/// Forwards the call to <see cref="SentrySdk"/>.
/// </summary>
[DebuggerStepThrough]
public void UnsetTag(string key)
=> SentrySdk.UnsetTag(key);

/// <summary>
/// Forwards the call to <see cref="SentrySdk"/>.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry/ISentryScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public interface ISentryScopeManager
/// <returns>A task that completes when the callback is done or a completed task if the SDK is disabled.</returns>
public Task ConfigureScopeAsync(Func<Scope, Task> configureScope);

/// <summary>
/// Sets a tag on the current scope.
/// </summary>
public void SetTag(string key, string value);

/// <summary>
/// Removes a tag from the current scope.
/// </summary>
public void UnsetTag(string key);

/// <summary>
/// Binds the client to the current scope.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public async Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
}
}

public void SetTag(string key, string value) => ScopeManager.SetTag(key, value);

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

public IDisposable PushScope() => ScopeManager.PushScope();

public IDisposable PushScope<TState>(TState state) => ScopeManager.PushScope(state);
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry/Internal/SentryScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public Task ConfigureScopeAsync(Func<Scope, Task>? configureScope)
return configureScope?.Invoke(scope) ?? Task.CompletedTask;
}

public void SetTag(string key, string value)
{
var (scope, _) = GetCurrent();
scope.SetTag(key, value);
}

public void UnsetTag(string key)
{
var (scope, _) = GetCurrent();
scope.UnsetTag(key);
}

public IDisposable PushScope() => PushScope<object>(null);

public IDisposable PushScope<TState>(TState? state)
Expand Down
14 changes: 14 additions & 0 deletions src/Sentry/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ public static void ConfigureScope(Action<Scope> configureScope)
public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
=> CurrentHub.ConfigureScopeAsync(configureScope);

/// <summary>
/// Sets a tag on the current scope.
/// </summary>
[DebuggerStepThrough]
public static void SetTag(string key, string value)
=> CurrentHub.SetTag(key, value);

/// <summary>
/// Removes a tag from the current scope.
/// </summary>
[DebuggerStepThrough]
public static void UnsetTag(string key)
=> CurrentHub.UnsetTag(key);

/// <inheritdoc cref="ISentryClient.CaptureEnvelope"/>
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ namespace Sentry
System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope);
System.IDisposable PushScope();
System.IDisposable PushScope<TState>(TState state);
void SetTag(string key, string value);
void UnsetTag(string key);
}
public interface ISentryScopeStateProcessor
{
Expand Down Expand Up @@ -863,12 +865,14 @@ namespace Sentry
public static System.IDisposable PushScope() { }
public static System.IDisposable PushScope<TState>(TState state) { }
public static void ResumeSession() { }
public static void SetTag(string key, string value) { }
public static void StartSession() { }
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context) { }
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, Sentry.SentryTraceHeader traceHeader) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, string? description) { }
public static void UnsetTag(string key) { }
}
public class SentrySession : Sentry.ISentrySession
{
Expand Down Expand Up @@ -1367,8 +1371,10 @@ namespace Sentry.Extensibility
public System.IDisposable PushScope() { }
public System.IDisposable PushScope<TState>(TState state) { }
public void ResumeSession() { }
public void SetTag(string key, string value) { }
public void StartSession() { }
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public void UnsetTag(string key) { }
}
public class FormRequestPayloadExtractor : Sentry.Extensibility.BaseRequestPayloadExtractor
{
Expand Down Expand Up @@ -1413,8 +1419,10 @@ namespace Sentry.Extensibility
public System.IDisposable PushScope() { }
public System.IDisposable PushScope<TState>(TState state) { }
public void ResumeSession() { }
public void SetTag(string key, string value) { }
public void StartSession() { }
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public void UnsetTag(string key) { }
}
public interface IBackgroundWorker
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ namespace Sentry
System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope);
System.IDisposable PushScope();
System.IDisposable PushScope<TState>(TState state);
void SetTag(string key, string value);
void UnsetTag(string key);
}
public interface ISentryScopeStateProcessor
{
Expand Down Expand Up @@ -863,12 +865,14 @@ namespace Sentry
public static System.IDisposable PushScope() { }
public static System.IDisposable PushScope<TState>(TState state) { }
public static void ResumeSession() { }
public static void SetTag(string key, string value) { }
public static void StartSession() { }
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context) { }
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, Sentry.SentryTraceHeader traceHeader) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, string? description) { }
public static void UnsetTag(string key) { }
}
public class SentrySession : Sentry.ISentrySession
{
Expand Down Expand Up @@ -1367,8 +1371,10 @@ namespace Sentry.Extensibility
public System.IDisposable PushScope() { }
public System.IDisposable PushScope<TState>(TState state) { }
public void ResumeSession() { }
public void SetTag(string key, string value) { }
public void StartSession() { }
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public void UnsetTag(string key) { }
}
public class FormRequestPayloadExtractor : Sentry.Extensibility.BaseRequestPayloadExtractor
{
Expand Down Expand Up @@ -1413,8 +1419,10 @@ namespace Sentry.Extensibility
public System.IDisposable PushScope() { }
public System.IDisposable PushScope<TState>(TState state) { }
public void ResumeSession() { }
public void SetTag(string key, string value) { }
public void StartSession() { }
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public void UnsetTag(string key) { }
}
public interface IBackgroundWorker
{
Expand Down
8 changes: 8 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ namespace Sentry
System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope);
System.IDisposable PushScope();
System.IDisposable PushScope<TState>(TState state);
void SetTag(string key, string value);
void UnsetTag(string key);
}
public interface ISentryScopeStateProcessor
{
Expand Down Expand Up @@ -844,12 +846,14 @@ namespace Sentry
public static System.IDisposable PushScope() { }
public static System.IDisposable PushScope<TState>(TState state) { }
public static void ResumeSession() { }
public static void SetTag(string key, string value) { }
public static void StartSession() { }
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context) { }
public static Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, Sentry.SentryTraceHeader traceHeader) { }
public static Sentry.ITransactionTracer StartTransaction(string name, string operation, string? description) { }
public static void UnsetTag(string key) { }
}
public class SentrySession : Sentry.ISentrySession
{
Expand Down Expand Up @@ -1348,8 +1352,10 @@ namespace Sentry.Extensibility
public System.IDisposable PushScope() { }
public System.IDisposable PushScope<TState>(TState state) { }
public void ResumeSession() { }
public void SetTag(string key, string value) { }
public void StartSession() { }
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public void UnsetTag(string key) { }
}
public class FormRequestPayloadExtractor : Sentry.Extensibility.BaseRequestPayloadExtractor
{
Expand Down Expand Up @@ -1394,8 +1400,10 @@ namespace Sentry.Extensibility
public System.IDisposable PushScope() { }
public System.IDisposable PushScope<TState>(TState state) { }
public void ResumeSession() { }
public void SetTag(string key, string value) { }
public void StartSession() { }
public Sentry.ITransactionTracer StartTransaction(Sentry.ITransactionContext context, System.Collections.Generic.IReadOnlyDictionary<string, object?> customSamplingContext) { }
public void UnsetTag(string key) { }
}
public interface IBackgroundWorker
{
Expand Down
51 changes: 51 additions & 0 deletions test/Sentry.Tests/SentrySdkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,57 @@ async Task ModifyScope()
}
}

[Fact]
public void SetTag_SetsTagOnCurrentScope()
{
using var _ = SentrySdk.Init(o =>
{
o.Dsn = ValidDsn;
o.AutoSessionTracking = false;
o.BackgroundWorker = Substitute.For<IBackgroundWorker>();
o.InitNativeSdks = false;
});

const string key = "key";
const string value = "value";

SentrySdk.SetTag(key, value);

string actual = null;
SentrySdk.ConfigureScope(s => actual = s.Tags[key]);

Assert.Equal(value, actual);
}

[Fact]
public void SetTag_NotInit_NoOp() => SentrySdk.SetTag("key", "value");

[Fact]
public void UnsetTag_UnsetsTagOnCurrentScope()
{
using var _ = SentrySdk.Init(o =>
{
o.Dsn = ValidDsn;
o.AutoSessionTracking = false;
o.BackgroundWorker = Substitute.For<IBackgroundWorker>();
o.InitNativeSdks = false;
});

const string key = "key";
const string value = "value";

SentrySdk.SetTag(key, value);
SentrySdk.UnsetTag(key);

bool? containsKey = null;
SentrySdk.ConfigureScope(s => containsKey = s.Tags.ContainsKey(key));

Assert.True(containsKey is false);
}

[Fact]
public void UnsetTag_NotInit_NoOp() => SentrySdk.UnsetTag("key");

[Fact]
public void CaptureEvent_WithConfiguredScope_ScopeAppliesToEvent()
{
Expand Down
Loading