forked from Cysharp/MagicOnion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceContextTelemetryExtensions.cs
57 lines (54 loc) · 2.19 KB
/
ServiceContextTelemetryExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using MagicOnion.Server.Hubs;
using MagicOnion.Server.OpenTelemetry.Internal;
namespace MagicOnion.Server.OpenTelemetry
{
public static class ServiceContextTelemetryExtensions
{
/// <summary>
/// Set the trace scope with this service context.
/// </summary>
/// <param name="context"></param>
/// <param name="scope"></param>
internal static void SetTraceScope(this StreamingHubContext context, IRpcScope scope)
{
context.ServiceContext.Items[MagicOnionTelemetryConstants.ServiceContextItemKeyTrace + "." + context.Path] = scope;
}
/// <summary>
/// Set the trace scope with this service context. This allows user to add their tag directly to this activity.
/// </summary>
/// <param name="context"></param>
/// <param name="scope"></param>
internal static void SetTraceScope(this ServiceContext context, IRpcScope scope)
{
context.Items[MagicOnionTelemetryConstants.ServiceContextItemKeyTrace] = scope;
}
/// <summary>
/// Gets the trace scope associated with this service context.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static IRpcScope GetTraceScope(this ServiceContext context)
{
if (context.Items.TryGetValue(MagicOnionTelemetryConstants.ServiceContextItemKeyTrace, out var scope))
{
return (IRpcScope)scope;
}
return default;
}
/// <summary>
/// Gets the trace scope associated with this service context.
/// </summary>
/// <remarks>Add custom tag directly to this activity.</remarks>
/// <param name="context"></param>
/// <param name="hubPath">IHubClass/MethodName</param>
/// <returns></returns>
public static IRpcScope GetTraceScope(this ServiceContext context, string hubPath)
{
if (context.Items.TryGetValue(MagicOnionTelemetryConstants.ServiceContextItemKeyTrace + "." + hubPath, out var scope))
{
return (IRpcScope)scope;
}
return default;
}
}
}