| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Extensions.DependencyInjection; |
| | 4 | | using System.Buffers; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using ZeroC.Slice; |
| | 7 | |
|
| | 8 | | namespace IceRpc.Telemetry; |
| | 9 | |
|
| | 10 | | /// <summary>A middleware that starts an <see cref="Activity" /> per request, following |
| | 11 | | /// <see href="https://opentelemetry.io/">OpenTelemetry</see> conventions. The middleware restores the parent invocation |
| | 12 | | /// activity from the request <see cref="RequestFieldKey.TraceContext" /> field before starting the dispatch activity. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks>The activities are only created for requests using the icerpc protocol.</remarks> |
| | 15 | | /// <seealso cref="TelemetryRouterExtensions" /> |
| | 16 | | /// <seealso cref="TelemetryDispatcherBuilderExtensions"/> |
| | 17 | | public class TelemetryMiddleware : IDispatcher |
| | 18 | | { |
| | 19 | | private readonly IDispatcher _next; |
| | 20 | | private readonly ActivitySource _activitySource; |
| | 21 | |
|
| | 22 | | /// <summary>Constructs a telemetry middleware.</summary> |
| | 23 | | /// <param name="next">The next dispatcher in the dispatch pipeline.</param> |
| | 24 | | /// <param name="activitySource">The <see cref="ActivitySource" /> is used to start the request activity.</param> |
| 3 | 25 | | public TelemetryMiddleware(IDispatcher next, ActivitySource activitySource) |
| 3 | 26 | | { |
| 3 | 27 | | _next = next; |
| 3 | 28 | | _activitySource = activitySource; |
| 3 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc/> |
| | 32 | | public async ValueTask<OutgoingResponse> DispatchAsync(IncomingRequest request, CancellationToken cancellationToken) |
| 3 | 33 | | { |
| 3 | 34 | | if (request.Protocol.HasFields) |
| 3 | 35 | | { |
| 3 | 36 | | string name = $"{request.Path}/{request.Operation}"; |
| 3 | 37 | | using Activity activity = _activitySource.CreateActivity(name, ActivityKind.Server) ?? new Activity(name); |
| 3 | 38 | | activity.AddTag("rpc.system", "icerpc"); |
| 3 | 39 | | activity.AddTag("rpc.service", request.Path); |
| 3 | 40 | | activity.AddTag("rpc.method", request.Operation); |
| 3 | 41 | | if (request.Fields.TryGetValue(RequestFieldKey.TraceContext, out ReadOnlySequence<byte> buffer)) |
| 2 | 42 | | { |
| 2 | 43 | | RestoreActivityContext(buffer, activity); |
| 1 | 44 | | } |
| 2 | 45 | | activity.Start(); |
| 2 | 46 | | return await _next.DispatchAsync(request, cancellationToken).ConfigureAwait(false); |
| | 47 | | } |
| | 48 | | else |
| 0 | 49 | | { |
| 0 | 50 | | return await _next.DispatchAsync(request, cancellationToken).ConfigureAwait(false); |
| | 51 | | } |
| 2 | 52 | | } |
| | 53 | |
|
| | 54 | | internal static void RestoreActivityContext(ReadOnlySequence<byte> buffer, Activity activity) |
| 3 | 55 | | { |
| 3 | 56 | | var decoder = new SliceDecoder(buffer, SliceEncoding.Slice2); |
| | 57 | |
|
| | 58 | | // Read W3C traceparent binary encoding (1 byte version, 16 bytes trace-ID, 8 bytes span-ID, |
| | 59 | | // 1 byte flags) https://www.w3.org/TR/trace-context/#traceparent-header-field-values |
| | 60 | |
|
| 3 | 61 | | byte traceIdVersion = decoder.DecodeUInt8(); |
| | 62 | |
|
| 2 | 63 | | using IMemoryOwner<byte> memoryOwner = MemoryPool<byte>.Shared.Rent(16); |
| 2 | 64 | | Span<byte> traceIdSpan = memoryOwner.Memory.Span[0..16]; |
| 2 | 65 | | decoder.CopyTo(traceIdSpan); |
| 2 | 66 | | var traceId = ActivityTraceId.CreateFromBytes(traceIdSpan); |
| | 67 | |
|
| 2 | 68 | | Span<byte> spanIdSpan = memoryOwner.Memory.Span[0..8]; |
| 2 | 69 | | decoder.CopyTo(spanIdSpan); |
| 2 | 70 | | var spanId = ActivitySpanId.CreateFromBytes(spanIdSpan); |
| | 71 | |
|
| 2 | 72 | | var traceFlags = (ActivityTraceFlags)decoder.DecodeUInt8(); |
| | 73 | |
|
| 2 | 74 | | activity.SetParentId(traceId, spanId, traceFlags); |
| | 75 | |
|
| | 76 | | // Read TraceState encoded as a string |
| 2 | 77 | | activity.TraceStateString = decoder.DecodeString(); |
| | 78 | |
|
| 2 | 79 | | IEnumerable<(string Key, string Value)> baggage = decoder.DecodeSequence( |
| 2 | 80 | | (ref SliceDecoder decoder) => |
| 2 | 81 | | { |
| 2 | 82 | | string key = decoder.DecodeString(); |
| 2 | 83 | | string value = decoder.DecodeString(); |
| 2 | 84 | | return (key, value); |
| 4 | 85 | | }); |
| | 86 | |
|
| | 87 | | // Restore in reverse order to keep the order in witch the peer add baggage entries, |
| | 88 | | // this is important when there are duplicate keys. |
| 10 | 89 | | foreach ((string key, string value) in baggage.Reverse()) |
| 2 | 90 | | { |
| 2 | 91 | | activity.AddBaggage(key, value); |
| 2 | 92 | | } |
| 4 | 93 | | } |
| | 94 | | } |