|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | + |
| 6 | +namespace Microsoft.AspNetCore.Shared; |
| 7 | + |
| 8 | +internal static class ActivityCreator |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Create an activity with details received from a remote source. |
| 12 | + /// </summary> |
| 13 | + public static Activity? CreateFromRemote( |
| 14 | + ActivitySource activitySource, |
| 15 | + DistributedContextPropagator propagator, |
| 16 | + object distributedContextCarrier, |
| 17 | + DistributedContextPropagator.PropagatorGetterCallback propagatorGetter, |
| 18 | + string activityName, |
| 19 | + IEnumerable<KeyValuePair<string, object?>>? tags, |
| 20 | + IEnumerable<ActivityLink>? links, |
| 21 | + bool diagnosticsOrLoggingEnabled) |
| 22 | + { |
| 23 | + propagator.ExtractTraceIdAndState( |
| 24 | + distributedContextCarrier, |
| 25 | + propagatorGetter, |
| 26 | + out var requestId, |
| 27 | + out var traceState); |
| 28 | + |
| 29 | + Activity? activity = null; |
| 30 | + if (activitySource.HasListeners()) |
| 31 | + { |
| 32 | + if (ActivityContext.TryParse(requestId, traceState, isRemote: true, out ActivityContext context)) |
| 33 | + { |
| 34 | + // The requestId used the W3C ID format. Unfortunately, the ActivitySource.CreateActivity overload that |
| 35 | + // takes a string parentId never sets HasRemoteParent to true. We work around that by calling the |
| 36 | + // ActivityContext overload instead which sets HasRemoteParent to parentContext.IsRemote. |
| 37 | + // https://github.com/dotnet/aspnetcore/pull/41568#discussion_r868733305 |
| 38 | + activity = activitySource.CreateActivity(activityName, ActivityKind.Server, context, tags: tags, links: links); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + // Pass in the ID we got from the headers if there was one. |
| 43 | + activity = activitySource.CreateActivity(activityName, ActivityKind.Server, string.IsNullOrEmpty(requestId) ? null! : requestId, tags: tags, links: links); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (activity is null) |
| 48 | + { |
| 49 | + // CreateActivity didn't create an Activity (this is an optimization for the |
| 50 | + // case when there are no listeners). Let's create it here if needed. |
| 51 | + if (diagnosticsOrLoggingEnabled) |
| 52 | + { |
| 53 | + activity = new Activity(activityName); |
| 54 | + if (!string.IsNullOrEmpty(requestId)) |
| 55 | + { |
| 56 | + activity.SetParentId(requestId); |
| 57 | + } |
| 58 | + if (tags != null) |
| 59 | + { |
| 60 | + foreach (var tag in tags) |
| 61 | + { |
| 62 | + activity.AddTag(tag.Key, tag.Value); |
| 63 | + } |
| 64 | + } |
| 65 | + if (links != null) |
| 66 | + { |
| 67 | + foreach (var link in links) |
| 68 | + { |
| 69 | + activity.AddLink(link); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + return null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // The trace id was successfully extracted, so we can set the trace state |
| 80 | + // https://www.w3.org/TR/trace-context/#tracestate-header |
| 81 | + if (!string.IsNullOrEmpty(requestId)) |
| 82 | + { |
| 83 | + if (!string.IsNullOrEmpty(traceState)) |
| 84 | + { |
| 85 | + activity.TraceStateString = traceState; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Baggage can be used regardless of whether a distributed trace id was present on the inbound request. |
| 90 | + // https://www.w3.org/TR/baggage/#abstract |
| 91 | + var baggage = propagator.ExtractBaggage(distributedContextCarrier, propagatorGetter); |
| 92 | + |
| 93 | + // AddBaggage adds items at the beginning of the list, so we need to add them in reverse to keep the same order as the client |
| 94 | + // By contract, the propagator has already reversed the order of items so we need not reverse it again |
| 95 | + // Order could be important if baggage has two items with the same key (that is allowed by the contract) |
| 96 | + if (baggage is not null) |
| 97 | + { |
| 98 | + foreach (var baggageItem in baggage) |
| 99 | + { |
| 100 | + activity.AddBaggage(baggageItem.Key, baggageItem.Value); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return activity; |
| 105 | + } |
| 106 | +} |
0 commit comments