From 1c084bed8d7a22ff7f22e414da4d2ee0e472fbee Mon Sep 17 00:00:00 2001 From: Dmytro Bohdanov Date: Thu, 2 Oct 2025 14:26:38 +0200 Subject: [PATCH 1/5] new metadata resolver api --- ...r.cs => HttpDependencyMetadataResolver.cs} | 62 +++++++++++++------ ...pDiagnosticsServiceCollectionExtensions.cs | 4 +- .../IHttpDependencyMetadata.cs | 27 ++++++++ 3 files changed, 73 insertions(+), 20 deletions(-) rename src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/{DownstreamDependencyMetadataManager.cs => HttpDependencyMetadataResolver.cs} (84%) create mode 100644 src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DownstreamDependencyMetadataManager.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs similarity index 84% rename from src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DownstreamDependencyMetadataManager.cs rename to src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs index 93c3e534e8e..16b93c52b22 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DownstreamDependencyMetadataManager.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs @@ -8,11 +8,18 @@ using System.Net; using System.Net.Http; using System.Text.RegularExpressions; -using Microsoft.Extensions.Telemetry.Internal; namespace Microsoft.Extensions.Http.Diagnostics; -internal sealed class DownstreamDependencyMetadataManager : IDownstreamDependencyMetadataManager +/// +/// Resolves metadata for HTTP requests based on hostname, path, and method patterns. +/// +/// +/// This class provides a high-performance way to identify HTTP requests by mapping them to previously +/// configured metadata using specialized trie-based data structures. This enables efficient lookup +/// of service information, operation names, and other metadata for telemetry and policy application. +/// +public abstract class HttpDependencyMetadataResolver { internal readonly struct ProcessedMetadata { @@ -27,22 +34,37 @@ internal readonly struct ProcessedMetadata private readonly HostSuffixTrieNode _hostSuffixTrieRoot = new(); private readonly FrozenDictionary _frozenProcessedMetadataMap; - public DownstreamDependencyMetadataManager(IEnumerable downstreamDependencyMetadata) + /// + /// Initializes a new instance of the class. + /// + /// A collection of HTTP dependency metadata used for request resolution. + /// is . + protected HttpDependencyMetadataResolver(IEnumerable dependencyMetadata) { + if (dependencyMetadata == null) + { + throw new ArgumentNullException(nameof(dependencyMetadata)); + } + Dictionary dependencyTrieMap = []; - foreach (var dependency in downstreamDependencyMetadata) + foreach (var dependency in dependencyMetadata) { AddDependency(dependency, dependencyTrieMap); } - _frozenProcessedMetadataMap = ProcessDownstreamDependencyMetadata(dependencyTrieMap).ToFrozenDictionary(StringComparer.Ordinal); + _frozenProcessedMetadataMap = ProcessDependencyMetadata(dependencyTrieMap).ToFrozenDictionary(StringComparer.Ordinal); } - public RequestMetadata? GetRequestMetadata(HttpRequestMessage requestMessage) + /// + /// Gets request metadata for the specified HTTP request message. + /// + /// The HTTP request message. + /// The resolved if found; otherwise, . + public virtual RequestMetadata? GetRequestMetadata(HttpRequestMessage requestMessage) { try { - if (requestMessage.RequestUri == null) + if (requestMessage?.RequestUri == null) { return null; } @@ -57,7 +79,12 @@ public DownstreamDependencyMetadataManager(IEnumerable + /// Gets request metadata for the specified HTTP web request. + /// + /// The HTTP web request. + /// The resolved if found; otherwise, . + public virtual RequestMetadata? GetRequestMetadata(HttpWebRequest requestMessage) { try { @@ -75,7 +102,6 @@ private static char[] MakeToUpperArray() { // Initialize the _toUpper array for quick conversion of any ascii char to upper // without incurring cost of checking whether the character requires conversion. - var a = new char[Constants.ASCIICharCount]; for (int i = 0; i < Constants.ASCIICharCount; i++) { @@ -173,12 +199,12 @@ private static void AddRouteToTrie(RequestMetadata routeMetadata, Dictionary ProcessDownstreamDependencyMetadata(Dictionary dependencyTrieMap) + private static Dictionary ProcessDependencyMetadata(Dictionary dependencyTrieMap) { Dictionary finalArrayDict = []; foreach (var dep in dependencyTrieMap) { - var finalArray = ProcessDownstreamDependencyMetadataInternal(dep.Value); + var finalArray = ProcessDependencyMetadataInternal(dep.Value); finalArrayDict.Add(dep.Key, finalArray); } @@ -190,7 +216,7 @@ private static Dictionary ProcessDownstreamDependency // remove the ExlcudeCodeCoverage attribute and ensure it's covered fully using local runs and enable it back before // pushing the change to PR. [ExcludeFromCodeCoverage] - private static ProcessedMetadata ProcessDownstreamDependencyMetadataInternal(RequestMetadataTrieNode requestMetadataTrieRoot) + private static ProcessedMetadata ProcessDependencyMetadataInternal(RequestMetadataTrieNode requestMetadataTrieRoot) { Queue queue = new(); queue.Enqueue(requestMetadataTrieRoot); @@ -278,17 +304,17 @@ private static ProcessedMetadata ProcessDownstreamDependencyMetadataInternal(Req return null; } - private void AddDependency(IDownstreamDependencyMetadata downstreamDependencyMetadata, Dictionary dependencyTrieMap) + private void AddDependency(IHttpDependencyMetadata dependencyMetadata, Dictionary dependencyTrieMap) { - foreach (var hostNameSuffix in downstreamDependencyMetadata.UniqueHostNameSuffixes) + foreach (var hostNameSuffix in dependencyMetadata.UniqueHostNameSuffixes) { // Add hostname to hostname suffix trie - AddHostnameToTrie(hostNameSuffix, downstreamDependencyMetadata.DependencyName); + AddHostnameToTrie(hostNameSuffix, dependencyMetadata.DependencyName); } - foreach (var routeMetadata in downstreamDependencyMetadata.RequestMetadata) + foreach (var routeMetadata in dependencyMetadata.RequestMetadata) { - routeMetadata.DependencyName = downstreamDependencyMetadata.DependencyName; + routeMetadata.DependencyName = dependencyMetadata.DependencyName; // Add route metadata to the route per dependency trie AddRouteToTrie(routeMetadata, dependencyTrieMap); @@ -445,4 +471,4 @@ private void AddHostnameToTrie(string hostNameSuffix, string dependencyName) hostMetadata.RequestMetadata : // Return the default request metadata for this host if no matching route is found. routeMetadataTrieRoot.RequestMetadatas[trieCurrent.RequestMetadataEntryIndex]; } -} +} \ No newline at end of file diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs index 6cdcd74b263..9cad693613e 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs @@ -23,7 +23,7 @@ public static class HttpDiagnosticsServiceCollectionExtensions public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCollection services, IDownstreamDependencyMetadata downstreamDependencyMetadata) { _ = Throw.IfNull(services); - services.TryAddSingleton(); + services.TryAddSingleton(); _ = services.AddSingleton(downstreamDependencyMetadata); return services; @@ -39,7 +39,7 @@ public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCo where T : class, IDownstreamDependencyMetadata { _ = Throw.IfNull(services); - services.TryAddSingleton(); + services.TryAddSingleton(); _ = services.AddSingleton(); return services; diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs new file mode 100644 index 00000000000..489eb3d0bcd --- /dev/null +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; + +namespace Microsoft.Extensions.Http.Diagnostics; + +/// +/// Interface for passing dependency metadata. +/// +public interface IHttpDependencyMetadata +{ + /// + /// Gets the name of the dependent service. + /// + string DependencyName { get; } + + /// + /// Gets the list of host name suffixes that can uniquely identify a host as this dependency. + /// + ISet UniqueHostNameSuffixes { get; } + + /// + /// Gets the list of all metadata for all routes to the dependency service. + /// + ISet RequestMetadata { get; } +} From 11ccdb3b50bdd74829ac303b9009bc2f84069650 Mon Sep 17 00:00:00 2001 From: Dmytro Bohdanov Date: Thu, 2 Oct 2025 15:48:20 +0200 Subject: [PATCH 2/5] extension methods for the metadata resolvers --- .../DefaultHttpDependencyMetadataResolver.cs | 22 +++++++++++++ ...pDiagnosticsServiceCollectionExtensions.cs | 32 ++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs new file mode 100644 index 00000000000..b2c837c6688 --- /dev/null +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; + +namespace Microsoft.Extensions.Http.Diagnostics; + +/// +/// Default implementation of that uses the base +/// trie-based lookup algorithm. +/// +public sealed class DefaultHttpDependencyMetadataResolver : HttpDependencyMetadataResolver +{ + /// + /// Initializes a new instance of the class. + /// + /// A collection of HTTP dependency metadata used for request resolution. + public DefaultHttpDependencyMetadataResolver(IEnumerable dependencyMetadata) + : base(dependencyMetadata) + { + } +} diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs index 9cad693613e..9caeb0ea441 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs @@ -4,7 +4,6 @@ using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Http.Diagnostics; -using Microsoft.Extensions.Telemetry.Internal; using Microsoft.Shared.Diagnostics; namespace Microsoft.Extensions.DependencyInjection; @@ -44,4 +43,35 @@ public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCo return services; } + + /// + /// Adds services required for HTTP dependency metadata resolution. + /// + /// The to add the services to. + /// The so that additional calls can be chained. + public static IServiceCollection AddHttpDependencyMetadataResolver(this IServiceCollection services) + { + services.TryAddSingleton(); + return services; + } + + /// + /// Adds services required for HTTP dependency metadata resolution with the specified metadata providers. + /// + /// The to add the services to. + /// The HTTP dependency metadata providers to register. + /// The so that additional calls can be chained. + public static IServiceCollection AddHttpDependencyMetadataResolver( + this IServiceCollection services, + params IHttpDependencyMetadata[] metadataProviders) + { + foreach (var provider in metadataProviders) + { + services.TryAddEnumerable(ServiceDescriptor.Singleton(provider)); + } + + services.TryAddSingleton(); + + return services; + } } From 798cd50b235742257e5e14996e20f671bc1b2589 Mon Sep 17 00:00:00 2001 From: Dmytro Bohdanov Date: Mon, 6 Oct 2025 11:34:02 +0200 Subject: [PATCH 3/5] update di resolution --- .../Http/HttpDiagnosticsServiceCollectionExtensions.cs | 4 ++-- .../Internal/IDownstreamDependencyMetadataManager.cs | 4 +++- .../DownstreamDependencyMetadataManagerTests.cs | 10 +++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs index 9caeb0ea441..0460be4d3db 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs @@ -22,7 +22,7 @@ public static class HttpDiagnosticsServiceCollectionExtensions public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCollection services, IDownstreamDependencyMetadata downstreamDependencyMetadata) { _ = Throw.IfNull(services); - services.TryAddSingleton(); + services.TryAddSingleton(); _ = services.AddSingleton(downstreamDependencyMetadata); return services; @@ -38,7 +38,7 @@ public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCo where T : class, IDownstreamDependencyMetadata { _ = Throw.IfNull(services); - services.TryAddSingleton(); + services.TryAddSingleton(); _ = services.AddSingleton(); return services; diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/IDownstreamDependencyMetadataManager.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/IDownstreamDependencyMetadataManager.cs index 5c49827a3b0..2da95ca2b95 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/IDownstreamDependencyMetadataManager.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/IDownstreamDependencyMetadataManager.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Net; using System.Net.Http; using Microsoft.Extensions.Http.Diagnostics; @@ -8,8 +9,9 @@ namespace Microsoft.Extensions.Telemetry.Internal; /// -/// Interface to manage dependency metadata. +/// (Obsolete) Use . /// +[Obsolete("Use HttpDependencyMetadataResolver instead. This internal interface will be removed.")] internal interface IDownstreamDependencyMetadataManager { /// diff --git a/test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Telemetry/DownstreamDependencyMetadataManagerTests.cs b/test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Telemetry/DownstreamDependencyMetadataManagerTests.cs index e7cdb4d3f32..c0763cc7ed8 100644 --- a/test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Telemetry/DownstreamDependencyMetadataManagerTests.cs +++ b/test/Libraries/Microsoft.Extensions.Http.Diagnostics.Tests/Telemetry/DownstreamDependencyMetadataManagerTests.cs @@ -4,14 +4,14 @@ using System; using System.Net.Http; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Telemetry.Internal; +using Microsoft.Extensions.Http.Diagnostics; using Xunit; namespace Microsoft.Extensions.Telemetry.Telemetry; public class DownstreamDependencyMetadataManagerTests : IDisposable { - private readonly IDownstreamDependencyMetadataManager _depMetadataManager; + private readonly HttpDependencyMetadataResolver _metadataResolver; private readonly ServiceProvider _sp; public DownstreamDependencyMetadataManagerTests() @@ -20,7 +20,7 @@ public DownstreamDependencyMetadataManagerTests() .AddDownstreamDependencyMetadata(new BackslashDownstreamDependencyMetadata()) .AddDownstreamDependencyMetadata(new EmptyRouteDependencyMetadata()) .BuildServiceProvider(); - _depMetadataManager = _sp.GetRequiredService(); + _metadataResolver = _sp.GetRequiredService(); } [Theory] @@ -51,7 +51,7 @@ public void GetRequestMetadata_RoutesRegisteredWithBackslashes_ShouldReturnHostM RequestUri = new Uri(uriString: urlString) }; - var requestMetadata = _depMetadataManager.GetRequestMetadata(requestMessage); + var requestMetadata = _metadataResolver.GetRequestMetadata(requestMessage); Assert.NotNull(requestMetadata); Assert.Equal(new BackslashDownstreamDependencyMetadata().DependencyName, requestMetadata.DependencyName); Assert.Equal(expectedRequestName, requestMetadata.RequestName); @@ -71,7 +71,7 @@ public void GetRequestMetadata_EmptyRouteRegisteredForDependency_ShouldReturnMet RequestUri = new Uri(uriString: urlString) }; - var requestMetadata = _depMetadataManager.GetRequestMetadata(requestMessage); + var requestMetadata = _metadataResolver.GetRequestMetadata(requestMessage); Assert.NotNull(requestMetadata); Assert.Equal("EmptyRouteService", requestMetadata.DependencyName); From 040e51baae3f658ccfb76a5fbb3555b1e834118b Mon Sep 17 00:00:00 2001 From: Dmytro Bohdanov Date: Mon, 6 Oct 2025 14:25:26 +0200 Subject: [PATCH 4/5] update metadata resolver usage, fix tests --- .../DefaultHttpDependencyMetadataResolver.cs | 2 +- .../Http/HttpDependencyMetadataResolver.cs | 4 +-- ...pDiagnosticsServiceCollectionExtensions.cs | 19 ++++++------- .../IHttpDependencyMetadata.cs | 27 ------------------- .../Logging/Internal/HttpRequestReader.cs | 13 +++++---- 5 files changed, 19 insertions(+), 46 deletions(-) delete mode 100644 src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs index b2c837c6688..fa15cea6283 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/DefaultHttpDependencyMetadataResolver.cs @@ -15,7 +15,7 @@ public sealed class DefaultHttpDependencyMetadataResolver : HttpDependencyMetada /// Initializes a new instance of the class. /// /// A collection of HTTP dependency metadata used for request resolution. - public DefaultHttpDependencyMetadataResolver(IEnumerable dependencyMetadata) + public DefaultHttpDependencyMetadataResolver(IEnumerable dependencyMetadata) : base(dependencyMetadata) { } diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs index 16b93c52b22..a1fca06c841 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs @@ -39,7 +39,7 @@ internal readonly struct ProcessedMetadata /// /// A collection of HTTP dependency metadata used for request resolution. /// is . - protected HttpDependencyMetadataResolver(IEnumerable dependencyMetadata) + protected HttpDependencyMetadataResolver(IEnumerable dependencyMetadata) { if (dependencyMetadata == null) { @@ -304,7 +304,7 @@ private static ProcessedMetadata ProcessDependencyMetadataInternal(RequestMetada return null; } - private void AddDependency(IHttpDependencyMetadata dependencyMetadata, Dictionary dependencyTrieMap) + private void AddDependency(IDownstreamDependencyMetadata dependencyMetadata, Dictionary dependencyTrieMap) { foreach (var hostNameSuffix in dependencyMetadata.UniqueHostNameSuffixes) { diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs index 0460be4d3db..8a70a5d6860 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDiagnosticsServiceCollectionExtensions.cs @@ -22,9 +22,10 @@ public static class HttpDiagnosticsServiceCollectionExtensions public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCollection services, IDownstreamDependencyMetadata downstreamDependencyMetadata) { _ = Throw.IfNull(services); - services.TryAddSingleton(); - _ = services.AddSingleton(downstreamDependencyMetadata); + _ = Throw.IfNull(downstreamDependencyMetadata); + services.TryAddEnumerable(ServiceDescriptor.Singleton(downstreamDependencyMetadata)); + services.TryAddSingleton(); return services; } @@ -38,9 +39,8 @@ public static IServiceCollection AddDownstreamDependencyMetadata(this IServiceCo where T : class, IDownstreamDependencyMetadata { _ = Throw.IfNull(services); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); services.TryAddSingleton(); - _ = services.AddSingleton(); - return services; } @@ -59,19 +59,20 @@ public static IServiceCollection AddHttpDependencyMetadataResolver(this IService /// Adds services required for HTTP dependency metadata resolution with the specified metadata providers. /// /// The to add the services to. - /// The HTTP dependency metadata providers to register. + /// The HTTP dependency metadata providers to register. /// The so that additional calls can be chained. public static IServiceCollection AddHttpDependencyMetadataResolver( this IServiceCollection services, - params IHttpDependencyMetadata[] metadataProviders) + params IDownstreamDependencyMetadata[] providers) { - foreach (var provider in metadataProviders) + _ = Throw.IfNull(services); + + foreach (var provider in providers) { - services.TryAddEnumerable(ServiceDescriptor.Singleton(provider)); + services.TryAddEnumerable(ServiceDescriptor.Singleton(provider)); } services.TryAddSingleton(); - return services; } } diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs deleted file mode 100644 index 489eb3d0bcd..00000000000 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/IHttpDependencyMetadata.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; - -namespace Microsoft.Extensions.Http.Diagnostics; - -/// -/// Interface for passing dependency metadata. -/// -public interface IHttpDependencyMetadata -{ - /// - /// Gets the name of the dependent service. - /// - string DependencyName { get; } - - /// - /// Gets the list of host name suffixes that can uniquely identify a host as this dependency. - /// - ISet UniqueHostNameSuffixes { get; } - - /// - /// Gets the list of all metadata for all routes to the dependency service. - /// - ISet RequestMetadata { get; } -} diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpRequestReader.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpRequestReader.cs index 78cb73bbddc..c9d7883a22c 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpRequestReader.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Logging/Internal/HttpRequestReader.cs @@ -12,7 +12,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Http.Diagnostics; using Microsoft.Extensions.Options; -using Microsoft.Extensions.Telemetry.Internal; using Microsoft.Shared.Diagnostics; using Microsoft.Shared.Pools; @@ -43,7 +42,7 @@ internal sealed class HttpRequestReader : IHttpRequestReader private readonly OutgoingPathLoggingMode _outgoingPathLogMode; private readonly IOutgoingRequestContext _requestMetadataContext; - private readonly IDownstreamDependencyMetadataManager? _downstreamDependencyMetadataManager; + private readonly HttpDependencyMetadataResolver? _dependencyMetadataResolver; public HttpRequestReader( IServiceProvider serviceProvider, @@ -51,7 +50,7 @@ public HttpRequestReader( IHttpRouteFormatter routeFormatter, IHttpRouteParser httpRouteParser, IOutgoingRequestContext requestMetadataContext, - IDownstreamDependencyMetadataManager? downstreamDependencyMetadataManager = null, + HttpDependencyMetadataResolver? dependencyMetadataResolver = null, [ServiceKey] string? serviceKey = null) : this( optionsMonitor.GetKeyedOrCurrent(serviceKey), @@ -59,7 +58,7 @@ public HttpRequestReader( httpRouteParser, serviceProvider.GetRequiredOrKeyedRequiredService(serviceKey), requestMetadataContext, - downstreamDependencyMetadataManager) + dependencyMetadataResolver) { } @@ -69,7 +68,7 @@ internal HttpRequestReader( IHttpRouteParser httpRouteParser, IHttpHeadersReader httpHeadersReader, IOutgoingRequestContext requestMetadataContext, - IDownstreamDependencyMetadataManager? downstreamDependencyMetadataManager = null) + HttpDependencyMetadataResolver? dependencyMetadataResolver = null) { _outgoingPathLogMode = Throw.IfOutOfRange(options.RequestPathLoggingMode); _httpHeadersReader = httpHeadersReader; @@ -77,7 +76,7 @@ internal HttpRequestReader( _routeFormatter = routeFormatter; _httpRouteParser = httpRouteParser; _requestMetadataContext = requestMetadataContext; - _downstreamDependencyMetadataManager = downstreamDependencyMetadataManager; + _dependencyMetadataResolver = dependencyMetadataResolver; _defaultSensitiveParameters = options.RouteParameterDataClasses.ToFrozenDictionary(StringComparer.Ordinal); _queryParameterDataClasses = options.RequestQueryParametersDataClasses.ToFrozenDictionary(StringComparer.Ordinal); @@ -234,7 +233,7 @@ private void GetRedactedPathAndParameters(HttpRequestMessage request, LogRecord var requestMetadata = request.GetRequestMetadata() ?? _requestMetadataContext.RequestMetadata ?? - _downstreamDependencyMetadataManager?.GetRequestMetadata(request); + _dependencyMetadataResolver?.GetRequestMetadata(request); if (requestMetadata == null) { From 51e6cce9f543de9ab740b164c81bd0890c017e5d Mon Sep 17 00:00:00 2001 From: Dmytro Bohdanov Date: Wed, 8 Oct 2025 15:43:58 +0200 Subject: [PATCH 5/5] update api according to the proposal --- .../Http/HttpDependencyMetadataResolver.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs index a1fca06c841..e9e1845dd90 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Http/HttpDependencyMetadataResolver.cs @@ -8,6 +8,7 @@ using System.Net; using System.Net.Http; using System.Text.RegularExpressions; +using Microsoft.Shared.Diagnostics; namespace Microsoft.Extensions.Http.Diagnostics; @@ -41,10 +42,7 @@ internal readonly struct ProcessedMetadata /// is . protected HttpDependencyMetadataResolver(IEnumerable dependencyMetadata) { - if (dependencyMetadata == null) - { - throw new ArgumentNullException(nameof(dependencyMetadata)); - } + _ = Throw.IfNull(dependencyMetadata); Dictionary dependencyTrieMap = []; foreach (var dependency in dependencyMetadata) @@ -55,6 +53,7 @@ protected HttpDependencyMetadataResolver(IEnumerable /// Gets request metadata for the specified HTTP request message. /// @@ -78,7 +77,7 @@ protected HttpDependencyMetadataResolver(IEnumerable /// Gets request metadata for the specified HTTP web request. /// @@ -97,6 +96,7 @@ protected HttpDependencyMetadataResolver(IEnumerable