-
Notifications
You must be signed in to change notification settings - Fork 819
/
Copy pathOtlpExporterOptions.cs
290 lines (250 loc) · 10.2 KB
/
OtlpExporterOptions.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
#if NETFRAMEWORK
using System.Net.Http;
#endif
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Exporter;
/// <summary>
/// OpenTelemetry Protocol (OTLP) exporter options.
/// </summary>
/// <remarks>
/// Note: OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS,
/// OTEL_EXPORTER_OTLP_TIMEOUT, and OTEL_EXPORTER_OTLP_PROTOCOL environment
/// variables are parsed during object construction.
/// </remarks>
public class OtlpExporterOptions : IOtlpExporterOptions
{
internal const string DefaultGrpcEndpoint = "http://localhost:4317";
internal const string DefaultHttpEndpoint = "http://localhost:4318";
#if NET462_OR_GREATER || NETSTANDARD2_0
internal const OtlpExportProtocol DefaultOtlpExportProtocol = OtlpExportProtocol.HttpProtobuf;
#else
internal const OtlpExportProtocol DefaultOtlpExportProtocol = OtlpExportProtocol.Grpc;
#endif
internal static readonly KeyValuePair<string, string>[] StandardHeaders = new KeyValuePair<string, string>[]
{
new("User-Agent", GetUserAgentString()),
};
internal readonly Func<HttpClient> DefaultHttpClientFactory;
private OtlpExportProtocol? protocol;
private Uri? endpoint;
private int? timeoutMilliseconds;
private Func<HttpClient>? httpClientFactory;
/// <summary>
/// Initializes a new instance of the <see cref="OtlpExporterOptions"/> class.
/// </summary>
public OtlpExporterOptions()
: this(OtlpExporterOptionsConfigurationType.Default)
{
}
internal OtlpExporterOptions(
OtlpExporterOptionsConfigurationType configurationType)
: this(
configuration: new ConfigurationBuilder().AddEnvironmentVariables().Build(),
configurationType,
defaultBatchOptions: new())
{
}
internal OtlpExporterOptions(
IConfiguration configuration,
OtlpExporterOptionsConfigurationType configurationType,
BatchExportActivityProcessorOptions defaultBatchOptions)
{
Debug.Assert(defaultBatchOptions != null, "defaultBatchOptions was null");
this.ApplyConfiguration(configuration, configurationType);
this.DefaultHttpClientFactory = () =>
{
return new HttpClient
{
Timeout = TimeSpan.FromMilliseconds(this.TimeoutMilliseconds),
};
};
this.BatchExportProcessorOptions = defaultBatchOptions!;
}
/// <inheritdoc/>
public Uri Endpoint
{
get
{
if (this.endpoint == null)
{
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
return this.Protocol == OtlpExportProtocol.Grpc
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
? new Uri(DefaultGrpcEndpoint)
: new Uri(DefaultHttpEndpoint);
}
return this.endpoint;
}
set
{
Guard.ThrowIfNull(value);
this.endpoint = value;
this.AppendSignalPathToEndpoint = false;
}
}
/// <inheritdoc/>
public string? Headers { get; set; }
/// <inheritdoc/>
public int TimeoutMilliseconds
{
get => this.timeoutMilliseconds ?? 10000;
set => this.timeoutMilliseconds = value;
}
/// <inheritdoc/>
public OtlpExportProtocol Protocol
{
get => this.protocol ?? DefaultOtlpExportProtocol;
set => this.protocol = value;
}
/// <summary>
/// Gets or sets the export processor type to be used with the OpenTelemetry Protocol Exporter. The default value is <see cref="ExportProcessorType.Batch"/>.
/// </summary>
/// <remarks>Note: This only applies when exporting traces.</remarks>
public ExportProcessorType ExportProcessorType { get; set; } = ExportProcessorType.Batch;
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is Batch.
/// </summary>
/// <remarks>Note: This only applies when exporting traces.</remarks>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; }
/// <inheritdoc/>
public Func<HttpClient> HttpClientFactory
{
get => this.httpClientFactory ?? this.DefaultHttpClientFactory;
set
{
Guard.ThrowIfNull(value);
this.httpClientFactory = value;
}
}
/// <summary>
/// Gets a value indicating whether or not the signal-specific path should
/// be appended to <see cref="Endpoint"/>.
/// </summary>
/// <remarks>
/// Note: Only applicable when <see cref="OtlpExportProtocol.HttpProtobuf"/>
/// is used.
/// </remarks>
internal bool AppendSignalPathToEndpoint { get; private set; } = true;
internal bool HasData
=> this.protocol.HasValue
|| this.endpoint != null
|| this.timeoutMilliseconds.HasValue
|| this.httpClientFactory != null;
internal static OtlpExporterOptions CreateOtlpExporterOptions(
IServiceProvider serviceProvider,
IConfiguration configuration,
string name)
=> new(
configuration,
OtlpExporterOptionsConfigurationType.Default,
serviceProvider.GetRequiredService<IOptionsMonitor<BatchExportActivityProcessorOptions>>().Get(name));
internal void ApplyConfigurationUsingSpecificationEnvVars(
IConfiguration configuration,
string endpointEnvVarKey,
bool appendSignalPathToEndpoint,
string protocolEnvVarKey,
string headersEnvVarKey,
string timeoutEnvVarKey)
{
if (configuration.TryGetUriValue(OpenTelemetryProtocolExporterEventSource.Log, endpointEnvVarKey, out var endpoint))
{
this.endpoint = endpoint;
this.AppendSignalPathToEndpoint = appendSignalPathToEndpoint;
}
if (configuration.TryGetValue<OtlpExportProtocol>(
OpenTelemetryProtocolExporterEventSource.Log,
protocolEnvVarKey,
OtlpExportProtocolParser.TryParse,
out var protocol))
{
this.Protocol = protocol;
}
if (configuration.TryGetStringValue(headersEnvVarKey, out var headers))
{
this.Headers = headers;
}
if (configuration.TryGetIntValue(OpenTelemetryProtocolExporterEventSource.Log, timeoutEnvVarKey, out var timeout))
{
this.TimeoutMilliseconds = timeout;
}
}
internal OtlpExporterOptions ApplyDefaults(OtlpExporterOptions defaultExporterOptions)
{
this.protocol ??= defaultExporterOptions.protocol;
this.endpoint ??= defaultExporterOptions.endpoint;
// Note: We leave AppendSignalPathToEndpoint set to true here because we
// want to append the signal if the endpoint came from the default
// endpoint.
this.Headers ??= defaultExporterOptions.Headers;
this.timeoutMilliseconds ??= defaultExporterOptions.timeoutMilliseconds;
this.httpClientFactory ??= defaultExporterOptions.httpClientFactory;
return this;
}
private static string GetUserAgentString()
{
var assembly = typeof(OtlpExporterOptions).Assembly;
return $"OTel-OTLP-Exporter-Dotnet/{assembly.GetPackageVersion()}";
}
private void ApplyConfiguration(
IConfiguration configuration,
OtlpExporterOptionsConfigurationType configurationType)
{
Debug.Assert(configuration != null, "configuration was null");
// Note: When using the "AddOtlpExporter" extensions configurationType
// never has a value other than "Default" because OtlpExporterOptions is
// shared by all signals and there is no way to differentiate which
// signal is being constructed.
if (configurationType == OtlpExporterOptionsConfigurationType.Default)
{
this.ApplyConfigurationUsingSpecificationEnvVars(
configuration!,
OtlpSpecConfigDefinitions.DefaultEndpointEnvVarName,
appendSignalPathToEndpoint: true,
OtlpSpecConfigDefinitions.DefaultProtocolEnvVarName,
OtlpSpecConfigDefinitions.DefaultHeadersEnvVarName,
OtlpSpecConfigDefinitions.DefaultTimeoutEnvVarName);
}
else if (configurationType == OtlpExporterOptionsConfigurationType.Logs)
{
this.ApplyConfigurationUsingSpecificationEnvVars(
configuration!,
OtlpSpecConfigDefinitions.LogsEndpointEnvVarName,
appendSignalPathToEndpoint: false,
OtlpSpecConfigDefinitions.LogsProtocolEnvVarName,
OtlpSpecConfigDefinitions.LogsHeadersEnvVarName,
OtlpSpecConfigDefinitions.LogsTimeoutEnvVarName);
}
else if (configurationType == OtlpExporterOptionsConfigurationType.Metrics)
{
this.ApplyConfigurationUsingSpecificationEnvVars(
configuration!,
OtlpSpecConfigDefinitions.MetricsEndpointEnvVarName,
appendSignalPathToEndpoint: false,
OtlpSpecConfigDefinitions.MetricsProtocolEnvVarName,
OtlpSpecConfigDefinitions.MetricsHeadersEnvVarName,
OtlpSpecConfigDefinitions.MetricsTimeoutEnvVarName);
}
else if (configurationType == OtlpExporterOptionsConfigurationType.Traces)
{
this.ApplyConfigurationUsingSpecificationEnvVars(
configuration!,
OtlpSpecConfigDefinitions.TracesEndpointEnvVarName,
appendSignalPathToEndpoint: false,
OtlpSpecConfigDefinitions.TracesProtocolEnvVarName,
OtlpSpecConfigDefinitions.TracesHeadersEnvVarName,
OtlpSpecConfigDefinitions.TracesTimeoutEnvVarName);
}
else
{
throw new NotSupportedException($"OtlpExporterOptionsConfigurationType '{configurationType}' is not supported.");
}
}
}