-
Notifications
You must be signed in to change notification settings - Fork 819
/
Copy pathOtlpLogExporterHelperExtensions.cs
385 lines (336 loc) · 18.6 KB
/
OtlpLogExporterHelperExtensions.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Logs;
/// <summary>
/// Extension methods to simplify registering of the OpenTelemetry Protocol (OTLP) exporter.
/// </summary>
public static class OtlpLogExporterHelperExtensions
{
/// <summary>
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
/// </summary>
/// <remarks><inheritdoc cref="AddOtlpExporter(OpenTelemetryLoggerOptions, Action{OtlpExporterOptions})" path="/remarks"/></remarks>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
// TODO: [Obsolete("Call LoggerProviderBuilder.AddOtlpExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddOtlpExporter(this OpenTelemetryLoggerOptions loggerOptions)
=> AddOtlpExporter(loggerOptions, name: null, configure: null);
/// <summary>
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="configure">Callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
// TODO: [Obsolete("Call LoggerProviderBuilder.AddOtlpExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddOtlpExporter(
this OpenTelemetryLoggerOptions loggerOptions,
Action<OtlpExporterOptions> configure)
=> AddOtlpExporter(loggerOptions, name: null, configure);
/// <summary>
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configure">Optional callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
// TODO: [Obsolete("Call LoggerProviderBuilder.AddOtlpExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddOtlpExporter(
this OpenTelemetryLoggerOptions loggerOptions,
string? name,
Action<OtlpExporterOptions>? configure)
{
Guard.ThrowIfNull(loggerOptions);
var finalOptionsName = name ?? Options.DefaultName;
return loggerOptions.AddProcessor(sp =>
{
var exporterOptions = GetOptions(sp, name, finalOptionsName, OtlpExporterOptions.CreateOtlpExporterOptions);
var processorOptions = sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().Get(finalOptionsName);
configure?.Invoke(exporterOptions);
return BuildOtlpLogExporter(
sp,
exporterOptions,
processorOptions,
GetOptions(sp, Options.DefaultName, Options.DefaultName, (sp, c, n) => new SdkLimitOptions(c)),
GetOptions(sp, name, finalOptionsName, (sp, c, n) => new ExperimentalOptions(c)));
});
}
/// <summary>
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="configureExporterAndProcessor">Callback action for configuring <see cref="OtlpExporterOptions"/> and <see cref="LogRecordExportProcessorOptions"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
// TODO: [Obsolete("Call LoggerProviderBuilder.AddOtlpExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddOtlpExporter(
this OpenTelemetryLoggerOptions loggerOptions,
Action<OtlpExporterOptions, LogRecordExportProcessorOptions> configureExporterAndProcessor)
=> AddOtlpExporter(loggerOptions, name: null, configureExporterAndProcessor);
/// <summary>
/// Adds an OTLP Exporter to the OpenTelemetry <see cref="ILoggerProvider"/>.
/// </summary>
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporterAndProcessor">Optional callback action for configuring <see cref="OtlpExporterOptions"/> and <see cref="LogRecordExportProcessorOptions"/>.</param>
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
// TODO: [Obsolete("Call LoggerProviderBuilder.AddOtlpExporter instead this method will be removed in a future version.")]
public static OpenTelemetryLoggerOptions AddOtlpExporter(
this OpenTelemetryLoggerOptions loggerOptions,
string? name,
Action<OtlpExporterOptions, LogRecordExportProcessorOptions>? configureExporterAndProcessor)
{
Guard.ThrowIfNull(loggerOptions);
var finalOptionsName = name ?? Options.DefaultName;
return loggerOptions.AddProcessor(sp =>
{
var exporterOptions = GetOptions(sp, name, finalOptionsName, OtlpExporterOptions.CreateOtlpExporterOptions);
var processorOptions = sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().Get(finalOptionsName);
configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions);
return BuildOtlpLogExporter(
sp,
exporterOptions,
processorOptions,
GetOptions(sp, Options.DefaultName, Options.DefaultName, (sp, c, n) => new SdkLimitOptions(c)),
GetOptions(sp, name, finalOptionsName, (sp, c, n) => new ExperimentalOptions(c)));
});
}
/// <summary>
/// Adds an OTLP exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder)
=> AddOtlpExporter(builder, name: null, configureExporter: null);
/// <summary>
/// Adds an OTLP exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="configureExporter">Callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, Action<OtlpExporterOptions> configureExporter)
=> AddOtlpExporter(builder, name: null, configureExporter);
/// <summary>
/// Adds an OTLP exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="configureExporterAndProcessor">Callback action for
/// configuring <see cref="OtlpExporterOptions"/> and <see
/// cref="LogRecordExportProcessorOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddOtlpExporter(this LoggerProviderBuilder builder, Action<OtlpExporterOptions, LogRecordExportProcessorOptions> configureExporterAndProcessor)
=> AddOtlpExporter(builder, name: null, configureExporterAndProcessor);
/// <summary>
/// Adds OpenTelemetry Protocol (OTLP) exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporter">Optional callback action for configuring <see cref="OtlpExporterOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddOtlpExporter(
this LoggerProviderBuilder builder,
string? name,
Action<OtlpExporterOptions>? configureExporter)
{
var finalOptionsName = name ?? Options.DefaultName;
builder.ConfigureServices(services =>
{
if (name != null && configureExporter != null)
{
// If we are using named options we register the
// configuration delegate into options pipeline.
services.Configure(finalOptionsName, configureExporter);
}
services.AddOtlpExporterLoggingServices();
});
return builder.AddProcessor(sp =>
{
OtlpExporterOptions exporterOptions;
if (name == null)
{
// If we are NOT using named options we create a new
// instance always. The reason for this is
// OtlpExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together. See:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
exporterOptions = sp.GetRequiredService<IOptionsFactory<OtlpExporterOptions>>().Create(finalOptionsName);
// Configuration delegate is executed inline on the fresh instance.
configureExporter?.Invoke(exporterOptions);
}
else
{
// When using named options we can properly utilize Options
// API to create or reuse an instance.
exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
}
// Note: Not using finalOptionsName here for SdkLimitOptions.
// There should only be one provider for a given service
// collection so SdkLimitOptions is treated as a single default
// instance.
var sdkLimitOptions = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;
return BuildOtlpLogExporter(
sp,
exporterOptions,
sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().Get(finalOptionsName),
sdkLimitOptions,
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().Get(finalOptionsName));
});
}
/// <summary>
/// Adds an OTLP exporter to the LoggerProvider.
/// </summary>
/// <param name="builder"><see cref="LoggerProviderBuilder"/> builder to use.</param>
/// <param name="name">Optional name which is used when retrieving options.</param>
/// <param name="configureExporterAndProcessor">Optional callback action for
/// configuring <see cref="OtlpExporterOptions"/> and <see
/// cref="LogRecordExportProcessorOptions"/>.</param>
/// <returns>The instance of <see cref="LoggerProviderBuilder"/> to chain the calls.</returns>
public static LoggerProviderBuilder AddOtlpExporter(
this LoggerProviderBuilder builder,
string? name,
Action<OtlpExporterOptions, LogRecordExportProcessorOptions>? configureExporterAndProcessor)
{
var finalOptionsName = name ?? Options.DefaultName;
builder.ConfigureServices(services => services.AddOtlpExporterLoggingServices());
return builder.AddProcessor(sp =>
{
OtlpExporterOptions exporterOptions;
if (name == null)
{
// If we are NOT using named options we create a new
// instance always. The reason for this is
// OtlpExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together. See:
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
exporterOptions = sp.GetRequiredService<IOptionsFactory<OtlpExporterOptions>>().Create(finalOptionsName);
}
else
{
// When using named options we can properly utilize Options
// API to create or reuse an instance.
exporterOptions = sp.GetRequiredService<IOptionsMonitor<OtlpExporterOptions>>().Get(finalOptionsName);
}
var processorOptions = sp.GetRequiredService<IOptionsMonitor<LogRecordExportProcessorOptions>>().Get(finalOptionsName);
// Configuration delegate is executed inline.
configureExporterAndProcessor?.Invoke(exporterOptions, processorOptions);
// Note: Not using finalOptionsName here for SdkLimitOptions.
// There should only be one provider for a given service
// collection so SdkLimitOptions is treated as a single default
// instance.
var sdkLimitOptions = sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue;
return BuildOtlpLogExporter(
sp,
exporterOptions,
processorOptions,
sdkLimitOptions,
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().Get(finalOptionsName));
});
}
internal static BaseProcessor<LogRecord> BuildOtlpLogExporter(
IServiceProvider serviceProvider,
OtlpExporterOptions exporterOptions,
LogRecordExportProcessorOptions processorOptions,
SdkLimitOptions sdkLimitOptions,
ExperimentalOptions experimentalOptions,
bool skipUseOtlpExporterRegistrationCheck = false,
Func<BaseExporter<LogRecord>, BaseExporter<LogRecord>>? configureExporterInstance = null)
{
Debug.Assert(serviceProvider != null, "serviceProvider was null");
Debug.Assert(exporterOptions != null, "exporterOptions was null");
Debug.Assert(processorOptions != null, "processorOptions was null");
Debug.Assert(sdkLimitOptions != null, "sdkLimitOptions was null");
Debug.Assert(experimentalOptions != null, "experimentalOptions was null");
#if NET462_OR_GREATER || NETSTANDARD2_0
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
if (exporterOptions!.Protocol == OtlpExportProtocol.Grpc &&
ReferenceEquals(exporterOptions.HttpClientFactory, exporterOptions.DefaultHttpClientFactory))
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
{
throw new NotSupportedException("OtlpExportProtocol.Grpc with the default HTTP client factory is not supported on .NET Framework or .NET Standard 2.0." +
"Please switch to OtlpExportProtocol.HttpProtobuf or provide a custom HttpClientFactory.");
}
#endif
if (!skipUseOtlpExporterRegistrationCheck)
{
serviceProvider!.EnsureNoUseOtlpExporterRegistrations();
}
/*
* Note:
*
* We don't currently enable IHttpClientFactory for OtlpLogExporter.
*
* The DefaultHttpClientFactory requires the ILoggerFactory in its ctor:
* https://github.com/dotnet/runtime/blob/fa40ecf7d36bf4e31d7ae968807c1c529bac66d6/src/libraries/Microsoft.Extensions.Http/src/DefaultHttpClientFactory.cs#L64
*
* This creates a circular reference: ILoggerFactory ->
* OpenTelemetryLoggerProvider -> OtlpLogExporter -> IHttpClientFactory
* -> ILoggerFactory
*
* exporterOptions.TryEnableIHttpClientFactoryIntegration(sp,
* "OtlpLogExporter");
*/
BaseExporter<LogRecord> otlpExporter = new OtlpLogExporter(
exporterOptions!,
sdkLimitOptions!,
experimentalOptions!);
if (configureExporterInstance != null)
{
otlpExporter = configureExporterInstance(otlpExporter);
}
if (processorOptions!.ExportProcessorType == ExportProcessorType.Simple)
{
return new SimpleLogRecordExportProcessor(otlpExporter);
}
else
{
var batchOptions = processorOptions.BatchExportProcessorOptions;
return new BatchLogRecordExportProcessor(
otlpExporter,
batchOptions.MaxQueueSize,
batchOptions.ScheduledDelayMilliseconds,
batchOptions.ExporterTimeoutMilliseconds,
batchOptions.MaxExportBatchSize);
}
}
private static T GetOptions<T>(
IServiceProvider sp,
string? name,
string finalName,
Func<IServiceProvider, IConfiguration, string, T> createOptionsFunc)
where T : class, new()
{
// Note: If OtlpExporter has been registered for tracing and/or metrics
// then IOptionsFactory will be set by a call to
// OtlpExporterOptions.RegisterOtlpExporterOptionsFactory. However, if we
// are only using logging, we don't have an opportunity to do that
// registration so we manually create a factory.
var optionsFactory = sp.GetRequiredService<IOptionsFactory<T>>();
if (optionsFactory is not DelegatingOptionsFactory<T>)
{
optionsFactory = new DelegatingOptionsFactory<T>(
(c, n) => createOptionsFunc(sp, c, n),
sp.GetRequiredService<IConfiguration>(),
sp.GetServices<IConfigureOptions<T>>(),
sp.GetServices<IPostConfigureOptions<T>>(),
sp.GetServices<IValidateOptions<T>>());
return optionsFactory.Create(finalName);
}
if (name == null)
{
// If we are NOT using named options we create a new
// instance always. The reason for this is
// OtlpExporterOptions is shared by all signals. Without a
// name, delegates for all signals will mix together.
return optionsFactory.Create(finalName);
}
// If we have a valid factory AND we are using named options, we can
// safely use the Options API fully.
return sp.GetRequiredService<IOptionsMonitor<T>>().Get(finalName);
}
}