Skip to content

Commit 27267e6

Browse files
committed
add the scope apply hook
1 parent 89083f1 commit 27267e6

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Sentry.Protocol
6+
{
7+
public class DefaultSentryScopeProcessor : ISentryScopeProcessor
8+
{
9+
public void Apply(BaseScope scope, object state)
10+
{
11+
switch (state)
12+
{
13+
case string scopeString:
14+
// TODO: find unique key to support multiple single-string scopes
15+
scope.SetTag("scope", scopeString);
16+
break;
17+
case IEnumerable<KeyValuePair<string, string>> keyValStringString:
18+
scope.SetTags(keyValStringString
19+
.Where(kv => !string.IsNullOrEmpty(kv.Value)));
20+
break;
21+
case IEnumerable<KeyValuePair<string, object>> keyValStringObject:
22+
{
23+
scope.SetTags(keyValStringObject
24+
.Select(k => new KeyValuePair<string, string>(
25+
k.Key,
26+
k.Value?.ToString()))
27+
.Where(kv => !string.IsNullOrEmpty(kv.Value)));
28+
29+
break;
30+
}
31+
#if HAS_VALUE_TUPLE
32+
case ValueTuple<string, string> tupleStringString:
33+
if (!string.IsNullOrEmpty(tupleStringString.Item2))
34+
{
35+
scope.SetTag(tupleStringString.Item1, tupleStringString.Item2);
36+
}
37+
break;
38+
#endif
39+
default:
40+
scope.SetExtra("state", state);
41+
break;
42+
}
43+
}
44+
}
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Sentry.Protocol
2+
{
3+
public interface ISentryScopeProcessor
4+
{
5+
void Apply(IScope scope, object state);
6+
}
7+
}

src/Sentry/ScopeExtensions.cs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -354,39 +354,8 @@ public static void Apply(this IScope from, IScope to)
354354
/// <param name="state">The state object to apply.</param>
355355
public static void Apply(this IScope scope, object state)
356356
{
357-
switch (state)
358-
{
359-
case string scopeString:
360-
// TODO: find unique key to support multiple single-string scopes
361-
scope.SetTag("scope", scopeString);
362-
break;
363-
case IEnumerable<KeyValuePair<string, string>> keyValStringString:
364-
scope.SetTags(keyValStringString
365-
.Where(kv => !string.IsNullOrEmpty(kv.Value)));
366-
break;
367-
case IEnumerable<KeyValuePair<string, object>> keyValStringObject:
368-
{
369-
scope.SetTags(keyValStringObject
370-
.Select(k => new KeyValuePair<string, string>(
371-
k.Key,
372-
k.Value?.ToString()!))
373-
.Where(kv => !string.IsNullOrEmpty(kv.Value)));
374-
375-
break;
376-
}
377-
#if HAS_VALUE_TUPLE
378-
case ValueTuple<string, string> tupleStringString:
379-
if (!string.IsNullOrEmpty(tupleStringString.Item2))
380-
{
381-
scope.SetTag(tupleStringString.Item1, tupleStringString.Item2);
382-
}
383-
384-
break;
385-
#endif
386-
default:
387-
scope.SetExtra("state", state);
388-
break;
389-
}
357+
var processor = scope.ScopeOptions.SentryScopeProcessor ?? new DefaultSentryScopeProcessor();
358+
processor.Apply(scope, state);
390359
}
391360

392361
/// <summary>

0 commit comments

Comments
 (0)