-
Notifications
You must be signed in to change notification settings - Fork 819
/
Copy pathOtlpExporterOptionsExtensions.cs
195 lines (167 loc) · 8.32 KB
/
OtlpExporterOptionsExtensions.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NETFRAMEWORK
using System.Net.Http;
#endif
using System.Diagnostics;
using System.Reflection;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission;
namespace OpenTelemetry.Exporter;
internal static class OtlpExporterOptionsExtensions
{
private const string TraceGrpcServicePath = "opentelemetry.proto.collector.trace.v1.TraceService/Export";
private const string MetricsGrpcServicePath = "opentelemetry.proto.collector.metrics.v1.MetricsService/Export";
private const string LogsGrpcServicePath = "opentelemetry.proto.collector.logs.v1.LogsService/Export";
private const string TraceHttpServicePath = "v1/traces";
private const string MetricsHttpServicePath = "v1/metrics";
private const string LogsHttpServicePath = "v1/logs";
public static THeaders GetHeaders<THeaders>(this OtlpExporterOptions options, Action<THeaders, string, string> addHeader)
where THeaders : new()
{
var optionHeaders = options.Headers;
var headers = new THeaders();
if (!string.IsNullOrEmpty(optionHeaders))
{
// According to the specification, URL-encoded headers must be supported.
optionHeaders = Uri.UnescapeDataString(optionHeaders);
ReadOnlySpan<char> headersSpan = optionHeaders.AsSpan();
while (!headersSpan.IsEmpty)
{
int commaIndex = headersSpan.IndexOf(',');
ReadOnlySpan<char> pair;
if (commaIndex == -1)
{
pair = headersSpan;
headersSpan = ReadOnlySpan<char>.Empty;
}
else
{
pair = headersSpan.Slice(0, commaIndex);
headersSpan = headersSpan.Slice(commaIndex + 1);
}
int equalIndex = pair.IndexOf('=');
if (equalIndex == -1)
{
throw new ArgumentException("Headers provided in an invalid format.");
}
var key = pair.Slice(0, equalIndex).Trim().ToString();
var value = pair.Slice(equalIndex + 1).Trim().ToString();
addHeader(headers, key, value);
}
}
foreach (var header in OtlpExporterOptions.StandardHeaders)
{
addHeader(headers, header.Key, header.Value);
}
return headers;
}
public static OtlpExporterTransmissionHandler GetExportTransmissionHandler(this OtlpExporterOptions options, ExperimentalOptions experimentalOptions, OtlpSignalType otlpSignalType)
{
var exportClient = GetExportClient(options, otlpSignalType);
// `HttpClient.Timeout.TotalMilliseconds` would be populated with the correct timeout value for both the exporter configuration cases:
// 1. User provides their own HttpClient. This case is straightforward as the user wants to use their `HttpClient` and thereby the same client's timeout value.
// 2. If the user configures timeout via the exporter options, then the timeout set for the `HttpClient` initialized by the exporter will be set to user provided value.
double timeoutMilliseconds = exportClient is OtlpHttpExportClient httpTraceExportClient
? httpTraceExportClient.HttpClient.Timeout.TotalMilliseconds
: options.TimeoutMilliseconds;
if (experimentalOptions.EnableInMemoryRetry)
{
return new OtlpExporterRetryTransmissionHandler(exportClient, timeoutMilliseconds);
}
else if (experimentalOptions.EnableDiskRetry)
{
Debug.Assert(!string.IsNullOrEmpty(experimentalOptions.DiskRetryDirectoryPath), $"{nameof(experimentalOptions.DiskRetryDirectoryPath)} is null or empty");
return new OtlpExporterPersistentStorageTransmissionHandler(
exportClient,
timeoutMilliseconds,
Path.Combine(experimentalOptions.DiskRetryDirectoryPath, "traces"));
}
else
{
return new OtlpExporterTransmissionHandler(exportClient, timeoutMilliseconds);
}
}
public static IExportClient GetExportClient(this OtlpExporterOptions options, OtlpSignalType otlpSignalType)
{
var httpClient = options.HttpClientFactory?.Invoke() ?? throw new InvalidOperationException("OtlpExporterOptions was missing HttpClientFactory or it returned null.");
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
if (options.Protocol != OtlpExportProtocol.Grpc && options.Protocol != OtlpExportProtocol.HttpProtobuf)
{
throw new NotSupportedException($"Protocol {options.Protocol} is not supported.");
}
return otlpSignalType switch
{
OtlpSignalType.Traces => options.Protocol == OtlpExportProtocol.Grpc
? new OtlpGrpcExportClient(options, httpClient, TraceGrpcServicePath)
: new OtlpHttpExportClient(options, httpClient, TraceHttpServicePath),
OtlpSignalType.Metrics => options.Protocol == OtlpExportProtocol.Grpc
? new OtlpGrpcExportClient(options, httpClient, MetricsGrpcServicePath)
: new OtlpHttpExportClient(options, httpClient, MetricsHttpServicePath),
OtlpSignalType.Logs => options.Protocol == OtlpExportProtocol.Grpc
? new OtlpGrpcExportClient(options, httpClient, LogsGrpcServicePath)
: new OtlpHttpExportClient(options, httpClient, LogsHttpServicePath),
_ => throw new NotSupportedException($"OtlpSignalType {otlpSignalType} is not supported."),
};
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
}
public static void TryEnableIHttpClientFactoryIntegration(this OtlpExporterOptions options, IServiceProvider serviceProvider, string httpClientName)
{
if (serviceProvider != null
&& options.Protocol == OtlpExportProtocol.HttpProtobuf
&& options.HttpClientFactory == options.DefaultHttpClientFactory)
{
options.HttpClientFactory = () =>
{
Type? httpClientFactoryType = Type.GetType("System.Net.Http.IHttpClientFactory, Microsoft.Extensions.Http", throwOnError: false);
if (httpClientFactoryType != null)
{
object? httpClientFactory = serviceProvider.GetService(httpClientFactoryType);
if (httpClientFactory != null)
{
MethodInfo? createClientMethod = httpClientFactoryType.GetMethod(
"CreateClient",
BindingFlags.Public | BindingFlags.Instance,
binder: null,
[typeof(string)],
modifiers: null);
if (createClientMethod != null)
{
HttpClient? client = (HttpClient?)createClientMethod.Invoke(httpClientFactory, [httpClientName]);
if (client != null)
{
client.Timeout = TimeSpan.FromMilliseconds(options.TimeoutMilliseconds);
return client;
}
}
}
}
return options.DefaultHttpClientFactory();
};
}
}
internal static Uri AppendPathIfNotPresent(this Uri uri, string path)
{
var absoluteUri = uri.AbsoluteUri;
var separator = string.Empty;
if (absoluteUri.EndsWith("/"))
{
// Endpoint already ends with 'path/'
if (absoluteUri.EndsWith(string.Concat(path, "/"), StringComparison.OrdinalIgnoreCase))
{
return uri;
}
}
else
{
// Endpoint already ends with 'path'
if (absoluteUri.EndsWith(path, StringComparison.OrdinalIgnoreCase))
{
return uri;
}
separator = "/";
}
return new Uri(string.Concat(uri.AbsoluteUri, separator, path));
}
}