| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Azure.Core.Pipeline |
| | 10 | | { |
| | 11 | | internal class RequestActivityPolicy : HttpPipelinePolicy |
| | 12 | | { |
| | 13 | | private readonly bool _isDistributedTracingEnabled; |
| | 14 | | private readonly string? _resourceProviderNamespace; |
| | 15 | |
|
| | 16 | | private const string TraceParentHeaderName = "traceparent"; |
| | 17 | | private const string TraceStateHeaderName = "tracestate"; |
| | 18 | | private const string RequestIdHeaderName = "Request-Id"; |
| | 19 | |
|
| 2 | 20 | | private static readonly DiagnosticListener s_diagnosticSource = new DiagnosticListener("Azure.Core"); |
| | 21 | |
|
| 62 | 22 | | public RequestActivityPolicy(bool isDistributedTracingEnabled, string? resourceProviderNamespace) |
| | 23 | | { |
| 62 | 24 | | _isDistributedTracingEnabled = isDistributedTracingEnabled; |
| 62 | 25 | | _resourceProviderNamespace = resourceProviderNamespace; |
| 62 | 26 | | } |
| | 27 | |
|
| | 28 | | public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) |
| | 29 | | { |
| 1048 | 30 | | return ProcessAsync(message, pipeline, true); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) |
| | 34 | | { |
| 1030 | 35 | | ProcessAsync(message, pipeline, false).EnsureCompleted(); |
| 1022 | 36 | | } |
| | 37 | |
|
| | 38 | | private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool isAs |
| | 39 | | { |
| 2078 | 40 | | if (!_isDistributedTracingEnabled) |
| | 41 | | { |
| 4 | 42 | | if (isAsync) |
| | 43 | | { |
| 2 | 44 | | await ProcessNextAsync(message, pipeline, true).ConfigureAwait(false); |
| | 45 | | } |
| | 46 | | else |
| | 47 | | { |
| 2 | 48 | | ProcessNextAsync(message, pipeline, false).EnsureCompleted(); |
| | 49 | | } |
| | 50 | |
|
| 4 | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| 2074 | 54 | | if (!s_diagnosticSource.IsEnabled()) |
| | 55 | | { |
| 2058 | 56 | | await ProcessNextAsync(message, pipeline, isAsync).ConfigureAwait(false); |
| | 57 | |
|
| 2046 | 58 | | return; |
| | 59 | | } |
| | 60 | |
|
| 16 | 61 | | var activity = new Activity("Azure.Core.Http.Request"); |
| 16 | 62 | | activity.AddTag("http.method", message.Request.Method.Method); |
| 16 | 63 | | activity.AddTag("http.url", message.Request.Uri.ToString()); |
| 16 | 64 | | activity.AddTag("requestId", message.Request.ClientRequestId); |
| 16 | 65 | | activity.AddTag("kind", "client"); |
| | 66 | |
|
| 16 | 67 | | if (_resourceProviderNamespace != null) |
| | 68 | | { |
| 16 | 69 | | activity.AddTag("az.namespace", _resourceProviderNamespace); |
| | 70 | | } |
| | 71 | |
|
| 16 | 72 | | if (message.Request.Headers.TryGetValue("User-Agent", out string? userAgent)) |
| | 73 | | { |
| 8 | 74 | | activity.AddTag("http.user_agent", userAgent); |
| | 75 | | } |
| | 76 | |
|
| 16 | 77 | | var diagnosticSourceActivityEnabled = s_diagnosticSource.IsEnabled(activity.OperationName, message); |
| | 78 | |
|
| 16 | 79 | | if (diagnosticSourceActivityEnabled) |
| | 80 | | { |
| 16 | 81 | | s_diagnosticSource.StartActivity(activity, message); |
| | 82 | | } |
| | 83 | | else |
| | 84 | | { |
| 0 | 85 | | activity.Start(); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | try |
| | 89 | | { |
| 16 | 90 | | if (isAsync) |
| | 91 | | { |
| 8 | 92 | | await ProcessNextAsync(message, pipeline, true).ConfigureAwait(false); |
| | 93 | | } |
| | 94 | | else |
| | 95 | | { |
| 8 | 96 | | ProcessNextAsync(message, pipeline, false).EnsureCompleted(); |
| | 97 | | } |
| | 98 | |
|
| 12 | 99 | | activity.AddTag("http.status_code", message.Response.Status.ToString(CultureInfo.InvariantCulture)); |
| 12 | 100 | | activity.AddTag("serviceRequestId", message.Response.Headers.RequestId); |
| 12 | 101 | | } |
| | 102 | | finally |
| | 103 | | { |
| 16 | 104 | | if (diagnosticSourceActivityEnabled) |
| | 105 | | { |
| 16 | 106 | | s_diagnosticSource.StopActivity(activity, message); |
| | 107 | | } |
| | 108 | | else |
| | 109 | | { |
| 0 | 110 | | activity.Stop(); |
| | 111 | | } |
| | 112 | | } |
| 2062 | 113 | | } |
| | 114 | |
|
| | 115 | | private static async ValueTask ProcessNextAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline |
| | 116 | | { |
| 2078 | 117 | | Activity currentActivity = Activity.Current; |
| | 118 | |
|
| 2078 | 119 | | if (currentActivity != null) |
| | 120 | | { |
| 24 | 121 | | if (currentActivity.IsW3CFormat()) |
| | 122 | | { |
| 8 | 123 | | if (!message.Request.Headers.Contains(TraceParentHeaderName)) |
| | 124 | | { |
| 8 | 125 | | message.Request.Headers.Add(TraceParentHeaderName, currentActivity.Id); |
| 8 | 126 | | if (currentActivity.TryGetTraceState(out string? traceStateString) && traceStateString != null) |
| | 127 | | { |
| 4 | 128 | | message.Request.Headers.Add(TraceStateHeaderName, traceStateString); |
| | 129 | | } |
| | 130 | | } |
| | 131 | | } |
| | 132 | | else |
| | 133 | | { |
| 16 | 134 | | if (!message.Request.Headers.Contains(RequestIdHeaderName)) |
| | 135 | | { |
| 16 | 136 | | message.Request.Headers.Add(RequestIdHeaderName, currentActivity.Id); |
| | 137 | | } |
| | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| 2078 | 141 | | if (isAsync) |
| | 142 | | { |
| 1048 | 143 | | await ProcessNextAsync(message, pipeline).ConfigureAwait(false); |
| | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| 1030 | 147 | | ProcessNext(message, pipeline); |
| | 148 | | } |
| 2062 | 149 | | } |
| | 150 | | } |
| | 151 | | } |