| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.Metrics; |
| | | 7 | | |
| | | 8 | | namespace 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 | | { |
| | 0 | 23 | | 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 | | |
| | 0 | 30 | | 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> |
| | 0 | 38 | | 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> |
| | 0 | 46 | | 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> |
| | 0 | 54 | | 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> |
| | 0 | 64 | | 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) |
| | 0 | 72 | | { |
| | 0 | 73 | | ArgumentNullException.ThrowIfNull(request); |
| | 0 | 74 | | ArgumentNullException.ThrowIfNull(callback); |
| | | 75 | | |
| | 0 | 76 | | HttpRequestOptions options = request.Options; |
| | | 77 | | |
| | 0 | 78 | | if (options.TryGetValue(s_optionsKeyForCallbacks, out List<Action<HttpMetricsEnrichmentContext>>? callbacks) |
| | 0 | 79 | | { |
| | 0 | 80 | | callbacks.Add(callback); |
| | 0 | 81 | | } |
| | | 82 | | else |
| | 0 | 83 | | { |
| | 0 | 84 | | options.Set(s_optionsKeyForCallbacks, [callback]); |
| | 0 | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal static List<Action<HttpMetricsEnrichmentContext>>? GetEnrichmentCallbacksForRequest(HttpRequestMessage |
| | 0 | 89 | | { |
| | 0 | 90 | | if (request._options is HttpRequestOptions options && |
| | 0 | 91 | | options.Remove(s_optionsKeyForCallbacks.Key, out object? callbacks)) |
| | 0 | 92 | | { |
| | 0 | 93 | | return (List<Action<HttpMetricsEnrichmentContext>>)callbacks!; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return null; |
| | 0 | 97 | | } |
| | | 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) |
| | 0 | 107 | | { |
| | 0 | 108 | | var context = new HttpMetricsEnrichmentContext |
| | 0 | 109 | | { |
| | 0 | 110 | | _request = request, |
| | 0 | 111 | | _response = response, |
| | 0 | 112 | | _exception = exception, |
| | 0 | 113 | | _tags = commonTags, |
| | 0 | 114 | | }; |
| | | 115 | | |
| | 0 | 116 | | foreach (Action<HttpMetricsEnrichmentContext> callback in callbacks) |
| | 0 | 117 | | { |
| | 0 | 118 | | callback(context); |
| | 0 | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | requestDuration.Record(durationTime.TotalSeconds, context._tags); |
| | 0 | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |