< Summary

Information
Class: System.Net.Http.Metrics.HttpMetricsEnrichmentContext
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Metrics\HttpMetricsEnrichmentContext.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 124
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
.ctor()100%110%
AddCustomTag(...)100%110%
AddCallback(...)0%220%
GetEnrichmentCallbacksForRequest(...)0%440%
RecordDurationWithEnrichment(...)0%220%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Metrics\HttpMetricsEnrichmentContext.cs

#LineLine coverage
 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
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.Metrics;
 7
 8namespace System.Net.Http.Metrics
 9{
 10    /// <summary>
 11    /// Provides functionality for enriching request metrics `http-client-request-duration` and `http-client-failed-requ
 12    /// </summary>
 13    /// <remarks>
 14    /// Enrichment is done on per-request basis by callbacks registered with <see cref="AddCallback(HttpRequestMessage, 
 15    /// The callbacks are responsible for adding custom tags via <see cref="AddCustomTag(string, object?)"/> for which t
 16    /// information exposed on the <see cref="HttpMetricsEnrichmentContext"/> instance.
 17    ///
 18    /// > [!IMPORTANT]
 19    /// > The <see cref="HttpMetricsEnrichmentContext"/> instance must not be used from outside of the enrichment callba
 20    /// </remarks>
 21    public sealed class HttpMetricsEnrichmentContext
 22    {
 023        private static readonly HttpRequestOptionsKey<List<Action<HttpMetricsEnrichmentContext>>> s_optionsKeyForCallbac
 24
 25        private HttpRequestMessage? _request;
 26        private HttpResponseMessage? _response;
 27        private Exception? _exception;
 28        private TagList _tags;
 29
 030        private HttpMetricsEnrichmentContext() { } // Hide the default parameterless constructor.
 31
 32        /// <summary>
 33        /// Gets the <see cref="HttpRequestMessage"/> that has been sent.
 34        /// </summary>
 35        /// <remarks>
 36        /// This property must not be used from outside of the enrichment callbacks.
 37        /// </remarks>
 038        public HttpRequestMessage Request => _request!;
 39
 40        /// <summary>
 41        /// Gets the <see cref="HttpRequestMessage"/> received from the server or <see langword="null"/> if the request 
 42        /// </summary>
 43        /// <remarks>
 44        /// This property must not be used from outside of the enrichment callbacks.
 45        /// </remarks>
 046        public HttpResponseMessage? Response => _response;
 47
 48        /// <summary>
 49        /// Gets the exception that occurred or <see langword="null"/> if there was no error.
 50        /// </summary>
 51        /// <remarks>
 52        /// This property must not be used from outside of the enrichment callbacks.
 53        /// </remarks>
 054        public Exception? Exception => _exception;
 55
 56        /// <summary>
 57        /// Appends a custom tag to the list of tags to be recorded with the request metrics `http-client-request-durati
 58        /// </summary>
 59        /// <param name="name">The name of the tag.</param>
 60        /// <param name="value">The value of the tag.</param>
 61        /// <remarks>
 62        /// This method must not be used from outside of the enrichment callbacks.
 63        /// </remarks>
 064        public void AddCustomTag(string name, object? value) => _tags.Add(name, value);
 65
 66        /// <summary>
 67        /// Adds a callback to register custom tags for request metrics `http-client-request-duration` and `http-client-
 68        /// </summary>
 69        /// <param name="request">The <see cref="HttpRequestMessage"/> to apply enrichment to.</param>
 70        /// <param name="callback">The callback responsible to add custom tags via <see cref="AddCustomTag(string, objec
 71        public static void AddCallback(HttpRequestMessage request, Action<HttpMetricsEnrichmentContext> callback)
 072        {
 073            ArgumentNullException.ThrowIfNull(request);
 074            ArgumentNullException.ThrowIfNull(callback);
 75
 076            HttpRequestOptions options = request.Options;
 77
 078            if (options.TryGetValue(s_optionsKeyForCallbacks, out List<Action<HttpMetricsEnrichmentContext>>? callbacks)
 079            {
 080                callbacks.Add(callback);
 081            }
 82            else
 083            {
 084                options.Set(s_optionsKeyForCallbacks, [callback]);
 085            }
 086        }
 87
 88        internal static List<Action<HttpMetricsEnrichmentContext>>? GetEnrichmentCallbacksForRequest(HttpRequestMessage 
 089        {
 090            if (request._options is HttpRequestOptions options &&
 091                options.Remove(s_optionsKeyForCallbacks.Key, out object? callbacks))
 092            {
 093                return (List<Action<HttpMetricsEnrichmentContext>>)callbacks!;
 94            }
 95
 096            return null;
 097        }
 98
 99        internal static void RecordDurationWithEnrichment(
 100            List<Action<HttpMetricsEnrichmentContext>> callbacks,
 101            HttpRequestMessage request,
 102            HttpResponseMessage? response,
 103            Exception? exception,
 104            TimeSpan durationTime,
 105            in TagList commonTags,
 106            Histogram<double> requestDuration)
 0107        {
 0108            var context = new HttpMetricsEnrichmentContext
 0109            {
 0110                _request = request,
 0111                _response = response,
 0112                _exception = exception,
 0113                _tags = commonTags,
 0114            };
 115
 0116            foreach (Action<HttpMetricsEnrichmentContext> callback in callbacks)
 0117            {
 0118                callback(context);
 0119            }
 120
 0121            requestDuration.Record(durationTime.TotalSeconds, context._tags);
 0122        }
 123    }
 124}