forked from Cysharp/MagicOnion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenTelemetryFilterAttributes.cs
More file actions
149 lines (127 loc) · 8.08 KB
/
Copy pathOpenTelemetryFilterAttributes.cs
File metadata and controls
149 lines (127 loc) · 8.08 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;
using MagicOnion.Server;
using MagicOnion.Server.Hubs;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Trace;
namespace MagicOnion.Server.OpenTelemetry
{
/// <summary>
/// Collect OpenTelemetry Tracing for Global filter. Handle Unary and most outside logging.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class OpenTelemetryCollectorFilterFactoryAttribute : Attribute, IMagicOnionFilterFactory<MagicOnionFilterAttribute>
{
public int Order { get; set; }
MagicOnionFilterAttribute IMagicOnionFilterFactory<MagicOnionFilterAttribute>.CreateInstance(IServiceProvider serviceProvider)
{
return new OpenTelemetryCollectorFilterAttribute(serviceProvider.GetService<ActivitySource>(), serviceProvider.GetService<MagicOnionOpenTelemetryOptions>());
}
}
internal class OpenTelemetryCollectorFilterAttribute : MagicOnionFilterAttribute
{
readonly ActivitySource source;
readonly MagicOnionOpenTelemetryOptions telemetryOption;
public OpenTelemetryCollectorFilterAttribute(ActivitySource activitySource, MagicOnionOpenTelemetryOptions telemetryOption)
{
this.source = activitySource;
this.telemetryOption = telemetryOption;
}
public override async ValueTask Invoke(ServiceContext context, Func<ServiceContext, ValueTask> next)
{
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc
// Client -> Server incoming filter
// span name must be `$package.$service/$method` but MagicOnion has no $package.
using var activity = source.StartActivity($"{context.MethodType}:{context.CallContext.Method}", ActivityKind.Server);
// add trace context to service context. it allows user to add their span directly to this context.
context.SetTraceContext(activity.Context);
try
{
activity.SetTag("grpc.method", context.MethodType.ToString());
activity.SetTag("rpc.system", "grpc");
activity.SetTag("rpc.service", context.ServiceType.Name);
activity.SetTag("rpc.method", context.CallContext.Method);
// todo: context.CallContext.Peer/Host format is https://github.com/grpc/grpc/blob/master/doc/naming.md and not uri standard.
activity.SetTag("net.peer.name", context.CallContext.Peer);
activity.SetTag("net.host.name", context.CallContext.Host);
activity.SetTag("message.type", "RECIEVED");
activity.SetTag("message.id", context.ContextId.ToString());
activity.SetTag("message.uncompressed_size", context.GetRawRequest()?.LongLength.ToString() ?? "0");
// todo: net.peer.name not report on tracer. use custom tag
activity.SetTag("magiconion.peer.ip", context.CallContext.Peer);
activity.SetTag("magiconion.auth.enabled", (!string.IsNullOrEmpty(context.CallContext.AuthContext.PeerIdentityPropertyName)).ToString());
activity.SetTag("magiconion.auth.peer.authenticated", context.CallContext.AuthContext.IsPeerAuthenticated.ToString());
await next(context);
activity.SetTag("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
activity.SetStatus(OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode));
}
catch (Exception ex)
{
activity.SetTag("exception", ex.ToString());
activity.SetTag("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
activity.SetTag("grpc.status_detail", context.CallContext.Status.Detail);
activity.SetStatus(OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode));
throw;
}
}
}
/// <summary>
/// Collect OpenTelemetry Tracing for StreamingHub Filter. Handle Streaming Hub logging.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class OpenTelemetryHubCollectorFilterFactoryAttribute : Attribute, IMagicOnionFilterFactory<StreamingHubFilterAttribute>
{
public int Order { get; set; }
StreamingHubFilterAttribute IMagicOnionFilterFactory<StreamingHubFilterAttribute>.CreateInstance(IServiceProvider serviceProvider)
{
return new OpenTelemetryHubCollectorFilterAttribute(serviceProvider.GetService<ActivitySource>(), serviceProvider.GetService<MagicOnionOpenTelemetryOptions>());
}
}
internal class OpenTelemetryHubCollectorFilterAttribute : StreamingHubFilterAttribute
{
readonly ActivitySource source;
readonly MagicOnionOpenTelemetryOptions telemetryOption;
public OpenTelemetryHubCollectorFilterAttribute(ActivitySource activitySource, MagicOnionOpenTelemetryOptions telemetryOption)
{
this.source = activitySource;
this.telemetryOption = telemetryOption;
}
public override async ValueTask Invoke(StreamingHubContext context, Func<StreamingHubContext, ValueTask> next)
{
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc
using var activity = source.StartActivity($"{context.ServiceContext.MethodType}:/{context.Path}", ActivityKind.Server);
// add trace context to service context. it allows user to add their span directly to this hub
context.SetTraceContext(activity.Context);
try
{
activity.SetTag("grpc.method", context.ServiceContext.MethodType.ToString());
activity.SetTag("rpc.system", "grpc");
activity.SetTag("rpc.service", context.ServiceContext.ServiceType.Name);
activity.SetTag("rpc.method", $"/{context.Path}");
// todo: context.CallContext.Peer/Host format is https://github.com/grpc/grpc/blob/master/doc/naming.md and not uri standard.
activity.SetTag("net.peer.ip", context.ServiceContext.CallContext.Peer);
activity.SetTag("net.host.name", context.ServiceContext.CallContext.Host);
activity.SetTag("message.type", "RECIEVED");
activity.SetTag("message.id", context.ServiceContext.ContextId.ToString());
activity.SetTag("message.uncompressed_size", context.Request.Length.ToString());
// todo: net.peer.name not report on tracer. use custom tag
activity.SetTag("magiconion.peer.ip", context.ServiceContext.CallContext.Peer);
activity.SetTag("magiconion.auth.enabled", (!string.IsNullOrEmpty(context.ServiceContext.CallContext.AuthContext.PeerIdentityPropertyName)).ToString());
activity.SetTag("magiconion.auth.peer.authenticated", context.ServiceContext.CallContext.AuthContext.IsPeerAuthenticated.ToString());
await next(context);
activity.SetTag("grpc.status_code", ((long)context.ServiceContext.CallContext.Status.StatusCode).ToString());
activity.SetStatus(OpenTelemetrygRpcStatusHelper.ConvertStatus(context.ServiceContext.CallContext.Status.StatusCode));
}
catch (Exception ex)
{
activity.SetTag("exception", ex.ToString());
activity.SetTag("grpc.status_code", ((long)context.ServiceContext.CallContext.Status.StatusCode).ToString());
activity.SetTag("grpc.status_detail", context.ServiceContext.CallContext.Status.Detail);
activity.SetStatus(OpenTelemetrygRpcStatusHelper.ConvertStatus(context.ServiceContext.CallContext.Status.StatusCode));
throw;
}
}
}
}