-
-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathStartupExtension.cs
More file actions
92 lines (79 loc) · 3.15 KB
/
StartupExtension.cs
File metadata and controls
92 lines (79 loc) · 3.15 KB
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
using System;
using System.Net.Http;
using AgileConfig.Server.Apisite.Metrics;
using AgileConfig.Server.Common;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace AgileConfig.Server.Apisite;
public static class StartupExtension
{
public static void AddDefaultHttpClient(this IServiceCollection services, bool isTrustSsl)
{
services.AddHttpClient(Global.DefaultHttpClientName)
.ConfigurePrimaryHttpMessageHandler(() => { return NewMessageHandler(isTrustSsl); })
;
}
public static IOpenTelemetryBuilder AddOtlpTraces(this IOpenTelemetryBuilder builder)
{
if (string.IsNullOrEmpty(Appsettings.OtlpTracesEndpoint)) return builder;
builder.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddNpgsql()
.AddOtlpExporter(op =>
{
op.Protocol = Appsettings.OtlpTracesProtocol == "http"
? OtlpExportProtocol.HttpProtobuf
: OtlpExportProtocol.Grpc;
op.Endpoint = new Uri(Appsettings.OtlpTracesEndpoint);
if (!string.IsNullOrEmpty(Appsettings.OtlpTracesHeaders)) op.Headers = Appsettings.OtlpTracesHeaders;
})
);
return builder;
}
public static IOpenTelemetryBuilder AddOtlpMetrics(this IOpenTelemetryBuilder builder)
{
if (string.IsNullOrEmpty(Appsettings.OtlpMetricsEndpoint)) return builder;
builder.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddMeter(MeterService.MeterName)
.AddOtlpExporter((op, reader) =>
{
op.Protocol = Appsettings.OtlpMetricsProtocol == "http"
? OtlpExportProtocol.HttpProtobuf
: OtlpExportProtocol.Grpc;
op.Endpoint = new Uri(Appsettings.OtlpMetricsEndpoint);
reader.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
if (!string.IsNullOrEmpty(Appsettings.OtlpMetricsHeaders)) op.Headers = Appsettings.OtlpMetricsHeaders;
})
);
return builder;
}
public static IServiceCollection AddMeterService(this IServiceCollection services)
{
// Note: AddResourceMonitoring() requires cgroup support, skip on systems without it
// if (!string.IsNullOrEmpty(Appsettings.OtlpMetricsEndpoint))
// {
// try
// {
// services.AddResourceMonitoring();
// }
// catch
// {
// // Ignore - may fail in environments without cgroup support
// }
// }
services.AddSingleton<IMeterService, MeterService>();
return services;
}
private static HttpMessageHandler NewMessageHandler(bool alwaysTrustSsl)
{
var handler = new HttpClientHandler();
if (alwaysTrustSsl) handler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
return handler;
}
}