< Summary

Class:Azure.Core.Pipeline.RequestActivityPolicy
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Pipeline\Internal\RequestActivityPolicy.cs
Covered lines:50
Uncovered lines:2
Coverable lines:52
Total lines:151
Line coverage:96.1% (50 of 52)
Covered branches:28
Total branches:30
Branch coverage:93.3% (28 of 30)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor(...)-100%100%
ProcessAsync(...)-100%100%
Process(...)-100%100%
ProcessAsync()-93.55%87.5%
ProcessNextAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Pipeline\Internal\RequestActivityPolicy.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Globalization;
 7using System.Threading.Tasks;
 8
 9namespace 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
 220        private static readonly DiagnosticListener s_diagnosticSource = new DiagnosticListener("Azure.Core");
 21
 6222        public RequestActivityPolicy(bool isDistributedTracingEnabled, string? resourceProviderNamespace)
 23        {
 6224            _isDistributedTracingEnabled = isDistributedTracingEnabled;
 6225            _resourceProviderNamespace = resourceProviderNamespace;
 6226        }
 27
 28        public override ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
 29        {
 104830            return ProcessAsync(message, pipeline, true);
 31        }
 32
 33        public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
 34        {
 103035            ProcessAsync(message, pipeline, false).EnsureCompleted();
 102236        }
 37
 38        private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool isAs
 39        {
 207840            if (!_isDistributedTracingEnabled)
 41            {
 442                if (isAsync)
 43                {
 244                    await ProcessNextAsync(message, pipeline, true).ConfigureAwait(false);
 45                }
 46                else
 47                {
 248                    ProcessNextAsync(message, pipeline, false).EnsureCompleted();
 49                }
 50
 451                return;
 52            }
 53
 207454            if (!s_diagnosticSource.IsEnabled())
 55            {
 205856                await ProcessNextAsync(message, pipeline, isAsync).ConfigureAwait(false);
 57
 204658                return;
 59            }
 60
 1661            var activity = new Activity("Azure.Core.Http.Request");
 1662            activity.AddTag("http.method", message.Request.Method.Method);
 1663            activity.AddTag("http.url", message.Request.Uri.ToString());
 1664            activity.AddTag("requestId", message.Request.ClientRequestId);
 1665            activity.AddTag("kind", "client");
 66
 1667            if (_resourceProviderNamespace != null)
 68            {
 1669                activity.AddTag("az.namespace", _resourceProviderNamespace);
 70            }
 71
 1672            if (message.Request.Headers.TryGetValue("User-Agent", out string? userAgent))
 73            {
 874                activity.AddTag("http.user_agent", userAgent);
 75            }
 76
 1677            var diagnosticSourceActivityEnabled = s_diagnosticSource.IsEnabled(activity.OperationName, message);
 78
 1679            if (diagnosticSourceActivityEnabled)
 80            {
 1681                s_diagnosticSource.StartActivity(activity, message);
 82            }
 83            else
 84            {
 085                activity.Start();
 86            }
 87
 88            try
 89            {
 1690                if (isAsync)
 91                {
 892                    await ProcessNextAsync(message, pipeline, true).ConfigureAwait(false);
 93                }
 94                else
 95                {
 896                    ProcessNextAsync(message, pipeline, false).EnsureCompleted();
 97                }
 98
 1299                activity.AddTag("http.status_code", message.Response.Status.ToString(CultureInfo.InvariantCulture));
 12100                activity.AddTag("serviceRequestId", message.Response.Headers.RequestId);
 12101            }
 102            finally
 103            {
 16104                if (diagnosticSourceActivityEnabled)
 105                {
 16106                    s_diagnosticSource.StopActivity(activity, message);
 107                }
 108                else
 109                {
 0110                    activity.Stop();
 111                }
 112            }
 2062113        }
 114
 115        private static async ValueTask ProcessNextAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline
 116        {
 2078117            Activity currentActivity = Activity.Current;
 118
 2078119            if (currentActivity != null)
 120            {
 24121                if (currentActivity.IsW3CFormat())
 122                {
 8123                    if (!message.Request.Headers.Contains(TraceParentHeaderName))
 124                    {
 8125                        message.Request.Headers.Add(TraceParentHeaderName, currentActivity.Id);
 8126                        if (currentActivity.TryGetTraceState(out string? traceStateString) && traceStateString != null)
 127                        {
 4128                            message.Request.Headers.Add(TraceStateHeaderName, traceStateString);
 129                        }
 130                    }
 131                }
 132                else
 133                {
 16134                    if (!message.Request.Headers.Contains(RequestIdHeaderName))
 135                    {
 16136                        message.Request.Headers.Add(RequestIdHeaderName, currentActivity.Id);
 137                    }
 138                }
 139            }
 140
 2078141            if (isAsync)
 142            {
 1048143                await ProcessNextAsync(message, pipeline).ConfigureAwait(false);
 144            }
 145            else
 146            {
 1030147                ProcessNext(message, pipeline);
 148            }
 2062149        }
 150    }
 151}