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

Hook for BaseScope.Apply #596

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
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
35 changes: 2 additions & 33 deletions src/Sentry.Protocol/BaseScopeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Sentry.Protocol;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -308,38 +307,8 @@ public static void Apply(this BaseScope from, BaseScope to)
/// <param name="state">The state object to apply.</param>
public static void Apply(this BaseScope scope, object state)
{
switch (state)
{
case string scopeString:
// TODO: find unique key to support multiple single-string scopes
scope.SetTag("scope", scopeString);
break;
case IEnumerable<KeyValuePair<string, string>> keyValStringString:
scope.SetTags(keyValStringString
.Where(kv => !string.IsNullOrEmpty(kv.Value)));
break;
case IEnumerable<KeyValuePair<string, object>> keyValStringObject:
{
scope.SetTags(keyValStringObject
.Select(k => new KeyValuePair<string, string>(
k.Key,
k.Value?.ToString()))
.Where(kv => !string.IsNullOrEmpty(kv.Value)));

break;
}
#if HAS_VALUE_TUPLE
case ValueTuple<string, string> tupleStringString:
if (!string.IsNullOrEmpty(tupleStringString.Item2))
{
scope.SetTag(tupleStringString.Item1, tupleStringString.Item2);
}
break;
#endif
default:
scope.SetExtra("state", state);
break;
}
var processor = scope.ScopeOptions.SentryScopeProcessor ?? new DefaultSentryScopeProcessor();
bruno-garcia marked this conversation as resolved.
Show resolved Hide resolved
processor.Apply(scope, state);
}
}
}
45 changes: 45 additions & 0 deletions src/Sentry.Protocol/DefaultSentryScopeProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sentry.Protocol
{
public class DefaultSentryScopeProcessor : ISentryScopeProcessor
{
public void Apply(BaseScope scope, object state)
{
switch (state)
{
case string scopeString:
// TODO: find unique key to support multiple single-string scopes
scope.SetTag("scope", scopeString);
break;
case IEnumerable<KeyValuePair<string, string>> keyValStringString:
scope.SetTags(keyValStringString
.Where(kv => !string.IsNullOrEmpty(kv.Value)));
break;
case IEnumerable<KeyValuePair<string, object>> keyValStringObject:
{
scope.SetTags(keyValStringObject
.Select(k => new KeyValuePair<string, string>(
k.Key,
k.Value?.ToString()))
.Where(kv => !string.IsNullOrEmpty(kv.Value)));

break;
}
#if HAS_VALUE_TUPLE
case ValueTuple<string, string> tupleStringString:
if (!string.IsNullOrEmpty(tupleStringString.Item2))
{
scope.SetTag(tupleStringString.Item1, tupleStringString.Item2);
}
break;
#endif
default:
scope.SetExtra("state", state);
break;
}
}
}
}
5 changes: 5 additions & 0 deletions src/Sentry.Protocol/IScopeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Sentry.Protocol
/// </summary>
public interface IScopeOptions
{
/// <summary>
/// Configured scope processor.
/// </summary>
ISentryScopeProcessor SentryScopeProcessor { get; set; }

/// <summary>
/// Gets or sets the maximum breadcrumbs.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Sentry.Protocol/ISentryScopeProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Sentry.Protocol
{
public interface ISentryScopeProcessor
{
void Apply(BaseScope scope, object state);
}
}
10 changes: 8 additions & 2 deletions src/Sentry/Internal/SentryScopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ public IDisposable PushScope<TState>(TState state)

if (scope.Key.Locked)
{
// TODO: keep state on current scope?
_options?.DiagnosticLogger?.LogDebug("Locked scope. No new scope pushed.");
return DisabledHub.Instance;

// Apply to current scope
if (state != null)
{
scope.Key.Apply(state);
}

return new ScopeSnapshot(_options, currentScopeAndClientStack, this);
}

var clonedScope = scope.Key.Clone();
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class SentryOptions : IScopeOptions

internal ISentryHttpClientFactory SentryHttpClientFactory { get; set; }

/// <inheritdoc />
public ISentryScopeProcessor SentryScopeProcessor { get; set; } = new DefaultSentryScopeProcessor();

/// <summary>
/// A list of namespaces (or prefixes) considered not part of application code
/// </summary>
Expand Down