| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Extensions.DependencyInjection; |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using ZeroC.Slice.Codec; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Telemetry; |
| | | 9 | | |
| | | 10 | | /// <summary>An interceptor that starts an <see cref="Activity" /> per request, following |
| | | 11 | | /// <see href="https://opentelemetry.io/">OpenTelemetry</see> conventions. The activity context is written in the |
| | | 12 | | /// request <see cref="RequestFieldKey.TraceContext" /> field and can be restored on the server-side by installing the |
| | | 13 | | /// <see cref="TelemetryMiddleware" />.</summary> |
| | | 14 | | /// <remarks>The activities are only created for requests using the icerpc protocol.</remarks> |
| | | 15 | | /// <seealso cref="TelemetryPipelineExtensions"/> |
| | | 16 | | /// <seealso cref="TelemetryDispatcherBuilderExtensions"/> |
| | | 17 | | public class TelemetryInterceptor : IInvoker |
| | | 18 | | { |
| | | 19 | | // The W3C Baggage spec guarantees propagation of all entries only when the baggage has at most 64 |
| | | 20 | | // list-members and fits in 8192 bytes. We clip at 64 entries — the spec-mandated floor — so a |
| | | 21 | | // strictly spec-conforming peer in any language can always round-trip the baggage we send. Clipping |
| | | 22 | | // here also prevents amplification of entry count across forwarded hops. |
| | | 23 | | internal const int MaxBaggageEntries = 64; |
| | | 24 | | |
| | | 25 | | private readonly IInvoker _next; |
| | | 26 | | private readonly ActivitySource _activitySource; |
| | | 27 | | |
| | | 28 | | /// <summary>Constructs a telemetry interceptor.</summary> |
| | | 29 | | /// <param name="next">The next invoker in the invocation pipeline.</param> |
| | | 30 | | /// <param name="activitySource">The <see cref="ActivitySource" /> used to start the request activity.</param> |
| | 3 | 31 | | public TelemetryInterceptor(IInvoker next, ActivitySource activitySource) |
| | 3 | 32 | | { |
| | 3 | 33 | | _next = next; |
| | 3 | 34 | | _activitySource = activitySource; |
| | 3 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public async Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) |
| | 3 | 39 | | { |
| | 3 | 40 | | if (request.Protocol.HasFields) |
| | 3 | 41 | | { |
| | 3 | 42 | | string name = $"{request.ServiceAddress.Path}/{request.Operation}"; |
| | 3 | 43 | | using Activity activity = _activitySource.CreateActivity(name, ActivityKind.Client) ?? new Activity(name); |
| | 3 | 44 | | activity.SetIdFormat(ActivityIdFormat.W3C); |
| | 3 | 45 | | activity.AddTag("rpc.system", "icerpc"); |
| | 3 | 46 | | activity.AddTag("rpc.service", request.ServiceAddress.Path); |
| | 3 | 47 | | activity.AddTag("rpc.method", request.Operation); |
| | 3 | 48 | | activity.Start(); |
| | 3 | 49 | | request.Fields = request.Fields.With(RequestFieldKey.TraceContext, activity, WriteActivityContext); |
| | 3 | 50 | | return await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 51 | | } |
| | | 52 | | else |
| | 0 | 53 | | { |
| | 0 | 54 | | return await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 55 | | } |
| | 3 | 56 | | } |
| | | 57 | | |
| | | 58 | | internal static void WriteActivityContext(ref SliceEncoder encoder, Activity activity) |
| | 4 | 59 | | { |
| | 4 | 60 | | Debug.Assert(activity.IdFormat == ActivityIdFormat.W3C); |
| | | 61 | | |
| | 4 | 62 | | if (activity.Id is null) |
| | 0 | 63 | | { |
| | 0 | 64 | | throw new ArgumentException("The activity ID property cannot be null.", nameof(activity.Id)); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // The activity context is written to the field value, as if it has the following Slice definition |
| | | 68 | | // |
| | | 69 | | // compact struct BaggageEntry |
| | | 70 | | // { |
| | | 71 | | // string key; |
| | | 72 | | // string value; |
| | | 73 | | // } |
| | | 74 | | // |
| | | 75 | | // Sequence<BaggageEntry> Baggage; |
| | | 76 | | // |
| | | 77 | | // compact struct ActivityContext |
| | | 78 | | // { |
| | | 79 | | // // ActivityID version 1 byte |
| | | 80 | | // uint8 version; |
| | | 81 | | // // ActivityTraceId 16 bytes |
| | | 82 | | // uint64 activityTraceId0; |
| | | 83 | | // uint64 activityTraceId1; |
| | | 84 | | // // ActivitySpanId 8 bytes |
| | | 85 | | // uint64 activitySpanId |
| | | 86 | | // // ActivityTraceFlags 1 byte |
| | | 87 | | // uint8 ActivityTraceFlags; |
| | | 88 | | // string traceStateString; |
| | | 89 | | // Baggage baggage; |
| | | 90 | | // } |
| | | 91 | | // |
| | | 92 | | // Baggage is modeled as a sequence rather than a dictionary because Activity.Baggage allows |
| | | 93 | | // duplicate keys: encoding as a Slice dictionary could produce bytes that a strict dictionary |
| | | 94 | | // decoder in another language would reject (see #4518). |
| | | 95 | | |
| | | 96 | | // W3C traceparent binary encoding (1 byte version, 16 bytes trace-ID, 8 bytes span-ID, |
| | | 97 | | // 1 byte flags) https://www.w3.org/TR/trace-context/#traceparent-header-field-values |
| | 4 | 98 | | encoder.EncodeUInt8(0); |
| | | 99 | | |
| | | 100 | | // Unfortunately we can't use stackalloc. |
| | 4 | 101 | | using IMemoryOwner<byte> memoryOwner = MemoryPool<byte>.Shared.Rent(16); |
| | 4 | 102 | | Span<byte> buffer = memoryOwner.Memory.Span[0..16]; |
| | 4 | 103 | | activity.TraceId.CopyTo(buffer); |
| | 4 | 104 | | encoder.WriteByteSpan(buffer); |
| | 4 | 105 | | activity.SpanId.CopyTo(buffer[0..8]); |
| | 4 | 106 | | encoder.WriteByteSpan(buffer[0..8]); |
| | 4 | 107 | | encoder.EncodeUInt8((byte)activity.ActivityTraceFlags); |
| | | 108 | | |
| | | 109 | | // TraceState encoded as a string |
| | 4 | 110 | | encoder.EncodeString(activity.TraceStateString ?? ""); |
| | | 111 | | |
| | | 112 | | // Baggage encoded as a Sequence<BaggageEntry>, clipped to MaxBaggageEntries. Activity.Baggage has |
| | | 113 | | // no documented iteration order, so which entries we retain when clipping is unspecified; the W3C |
| | | 114 | | // Baggage spec permits dropping list-members in any order. |
| | 4 | 115 | | KeyValuePair<string, string?>[] baggage = activity.Baggage.Take(MaxBaggageEntries).ToArray(); |
| | 4 | 116 | | encoder.EncodeSequence( |
| | 4 | 117 | | baggage, |
| | 4 | 118 | | (ref SliceEncoder encoder, KeyValuePair<string, string?> entry) => |
| | 66 | 119 | | { |
| | 66 | 120 | | encoder.EncodeString(entry.Key); |
| | 66 | 121 | | encoder.EncodeString(entry.Value ?? ""); |
| | 70 | 122 | | }); |
| | 8 | 123 | | } |
| | | 124 | | } |