/
HttpClientLatencyTelemetryExtensions.cs
93 lines (82 loc) · 4.33 KB
/
HttpClientLatencyTelemetryExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.Diagnostics.CodeAnalysis;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Http.Latency;
using Microsoft.Extensions.Http.Latency.Internal;
using Microsoft.Extensions.Http.Logging;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Extensions to add http client latency telemetry.
/// </summary>
public static class HttpClientLatencyTelemetryExtensions
{
/// <summary>
/// Adds a <see cref="DelegatingHandler" /> to collect latency information and enrich outgoing request log for all http clients.
/// </summary>
/// <remarks>
/// This extension configures latency information collection globally for all http clients.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
public static IServiceCollection AddHttpClientLatencyTelemetry(this IServiceCollection services)
{
_ = Throw.IfNull(services);
_ = services.RegisterCheckpointNames(HttpCheckpoints.Checkpoints);
_ = services.AddOptions<HttpClientLatencyTelemetryOptions>();
_ = services.AddActivatedSingleton<HttpRequestLatencyListener>();
_ = services.AddActivatedSingleton<HttpClientLatencyContext>();
_ = services.AddTransient<HttpLatencyTelemetryHandler>();
_ = services.AddHttpClientLogEnricher<HttpClientLatencyLogEnricher>();
return services.ConfigureAll<HttpClientFactoryOptions>(
httpClientOptions =>
{
httpClientOptions
.HttpMessageHandlerBuilderActions.Add(httpMessageHandlerBuilder =>
{
var handler = httpMessageHandlerBuilder.Services.GetRequiredService<HttpLatencyTelemetryHandler>();
httpMessageHandlerBuilder.AdditionalHandlers.Add(handler);
});
});
}
/// <summary>
/// Adds a <see cref="DelegatingHandler" /> to collect latency information and enrich outgoing request log for all http clients.
/// </summary>
/// <remarks>
/// This extension configures outgoing request logs auto collection globally for all http clients.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="HttpClientLatencyTelemetryOptions"/>.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof(LoggingOptions))]
[UnconditionalSuppressMessage("Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "Addressed with [DynamicDependency]")]
public static IServiceCollection AddHttpClientLatencyTelemetry(this IServiceCollection services, IConfigurationSection section)
{
_ = Throw.IfNull(section);
_ = services
.Configure<HttpClientLatencyTelemetryOptions>(section);
return services.AddHttpClientLatencyTelemetry();
}
/// <summary>
/// Adds a <see cref="DelegatingHandler" /> to collect latency information and enrich outgoing request log for all http clients.
/// </summary>
/// <remarks>
/// This extension configures outgoing request logs auto collection globally for all http clients.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="configure">The delegate to configure <see cref="HttpClientLatencyTelemetryOptions"/> with.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
public static IServiceCollection AddHttpClientLatencyTelemetry(this IServiceCollection services, Action<HttpClientLatencyTelemetryOptions> configure)
{
_ = Throw.IfNull(configure);
_ = services
.Configure(configure);
return services.AddHttpClientLatencyTelemetry();
}
}