| | 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>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 | | private readonly IInvoker _next; |
| | 20 | | private readonly ActivitySource _activitySource; |
| | 21 | |
|
| | 22 | | /// <summary>Constructs a telemetry interceptor.</summary> |
| | 23 | | /// <param name="next">The next invoker in the invocation pipeline.</param> |
| | 24 | | /// <param name="activitySource">The <see cref="ActivitySource" /> used to start the request activity.</param> |
| 2 | 25 | | public TelemetryInterceptor(IInvoker next, ActivitySource activitySource) |
| 2 | 26 | | { |
| 2 | 27 | | _next = next; |
| 2 | 28 | | _activitySource = activitySource; |
| 2 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc/> |
| | 32 | | public async Task<IncomingResponse> InvokeAsync(OutgoingRequest request, CancellationToken cancellationToken) |
| 2 | 33 | | { |
| 2 | 34 | | if (request.Protocol.HasFields) |
| 2 | 35 | | { |
| 2 | 36 | | string name = $"{request.ServiceAddress.Path}/{request.Operation}"; |
| 2 | 37 | | using Activity activity = _activitySource?.CreateActivity(name, ActivityKind.Client) ?? new Activity(name); |
| 2 | 38 | | activity.AddTag("rpc.system", "icerpc"); |
| 2 | 39 | | activity.AddTag("rpc.service", request.ServiceAddress.Path); |
| 2 | 40 | | activity.AddTag("rpc.method", request.Operation); |
| 2 | 41 | | activity.Start(); |
| 2 | 42 | | request.Fields = request.Fields.With(RequestFieldKey.TraceContext, activity, WriteActivityContext); |
| 2 | 43 | | return await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | 44 | | } |
| | 45 | | else |
| 0 | 46 | | { |
| 0 | 47 | | return await _next.InvokeAsync(request, cancellationToken).ConfigureAwait(false); |
| | 48 | | } |
| 2 | 49 | | } |
| | 50 | |
|
| | 51 | | internal static void WriteActivityContext(ref SliceEncoder encoder, Activity activity) |
| 2 | 52 | | { |
| 2 | 53 | | if (activity.IdFormat != ActivityIdFormat.W3C) |
| 0 | 54 | | { |
| 0 | 55 | | throw new NotSupportedException( |
| 0 | 56 | | $"The activity ID format '{activity.IdFormat}' is not supported, the only supported activity ID format i |
| | 57 | | } |
| | 58 | |
|
| 2 | 59 | | if (activity.Id is null) |
| 0 | 60 | | { |
| 0 | 61 | | throw new ArgumentException("The activity ID property cannot be null.", nameof(activity.Id)); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // The activity context is written to the field value, as if it has the following Slice definition |
| | 65 | | // |
| | 66 | | // struct BaggageEntry |
| | 67 | | // { |
| | 68 | | // string key; |
| | 69 | | // string value; |
| | 70 | | // } |
| | 71 | | // Sequence<BaggageEntry> Baggage; |
| | 72 | | // |
| | 73 | | // struct ActivityContext |
| | 74 | | // { |
| | 75 | | // // ActivityID version 1 byte |
| | 76 | | // uint8 version; |
| | 77 | | // // ActivityTraceId 16 bytes |
| | 78 | | // uint64 activityTraceId0; |
| | 79 | | // uint64 activityTraceId1; |
| | 80 | | // // ActivitySpanId 8 bytes |
| | 81 | | // uint64 activitySpanId |
| | 82 | | // // ActivityTraceFlags 1 byte |
| | 83 | | // uint8 ActivityTraceFlags; |
| | 84 | | // string traceStateString; |
| | 85 | | // Baggage baggage; |
| | 86 | | // } |
| | 87 | |
|
| | 88 | | // W3C traceparent binary encoding (1 byte version, 16 bytes trace-ID, 8 bytes span-ID, |
| | 89 | | // 1 byte flags) https://www.w3.org/TR/trace-context/#traceparent-header-field-values |
| 2 | 90 | | encoder.EncodeUInt8(0); |
| | 91 | |
|
| | 92 | | // Unfortunately we can't use stackalloc. |
| 2 | 93 | | using IMemoryOwner<byte> memoryOwner = MemoryPool<byte>.Shared.Rent(16); |
| 2 | 94 | | Span<byte> buffer = memoryOwner.Memory.Span[0..16]; |
| 2 | 95 | | activity.TraceId.CopyTo(buffer); |
| 2 | 96 | | encoder.WriteByteSpan(buffer); |
| 2 | 97 | | activity.SpanId.CopyTo(buffer[0..8]); |
| 2 | 98 | | encoder.WriteByteSpan(buffer[0..8]); |
| 2 | 99 | | encoder.EncodeUInt8((byte)activity.ActivityTraceFlags); |
| | 100 | |
|
| | 101 | | // TraceState encoded as an string |
| 2 | 102 | | encoder.EncodeString(activity.TraceStateString ?? ""); |
| | 103 | |
|
| | 104 | | // Baggage encoded as a Sequence<BaggageEntry> |
| 2 | 105 | | encoder.EncodeSequence( |
| 2 | 106 | | activity.Baggage, |
| 2 | 107 | | (ref SliceEncoder encoder, KeyValuePair<string, string?> entry) => |
| 2 | 108 | | { |
| 2 | 109 | | encoder.EncodeString(entry.Key); |
| 2 | 110 | | encoder.EncodeString(entry.Value ?? ""); |
| 4 | 111 | | }); |
| 4 | 112 | | } |
| | 113 | | } |