Skip to content

Commit

Permalink
Hook for BaseScope.Apply (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Nov 20, 2020
1 parent fc37c7a commit f2abba1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
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();
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

0 comments on commit f2abba1

Please sign in to comment.