-
Notifications
You must be signed in to change notification settings - Fork 782
/
Copy pathOtlpExporterBuilder.cs
260 lines (211 loc) · 9.87 KB
/
OtlpExporterBuilder.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#nullable enable
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Exporter;
internal sealed class OtlpExporterBuilder
{
private const int DefaultProcessorPipelineWeight = 10_000;
private readonly string name;
internal OtlpExporterBuilder(
IServiceCollection services,
string? name,
IConfiguration? configuration)
{
Debug.Assert(services != null, "services was null");
if (configuration != null)
{
if (string.IsNullOrEmpty(name))
{
name = "otlp";
}
BindConfigurationToOptions(services!, name!, configuration);
}
name ??= Options.DefaultName;
RegisterOtlpExporterServices(services!, name!);
this.name = name;
this.Services = services!;
}
public IServiceCollection Services { get; }
public OtlpExporterBuilder ConfigureDefaultExporterOptions(
Action<IOtlpExporterOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure<OtlpExporterBuilderOptions>(
this.name,
o => configure(o.DefaultOptions));
return this;
}
public OtlpExporterBuilder ConfigureLoggingExporterOptions(
Action<IOtlpExporterOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure<OtlpExporterBuilderOptions>(
this.name,
o => configure(o.LoggingOptions));
return this;
}
public OtlpExporterBuilder ConfigureLoggingProcessorOptions(
Action<LogRecordExportProcessorOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure(this.name, configure);
return this;
}
public OtlpExporterBuilder ConfigureMetricsExporterOptions(
Action<IOtlpExporterOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure<OtlpExporterBuilderOptions>(
this.name,
o => configure(o.MetricsOptions));
return this;
}
public OtlpExporterBuilder ConfigureMetricsReaderOptions(
Action<MetricReaderOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure(this.name, configure);
return this;
}
public OtlpExporterBuilder ConfigureTracingExporterOptions(
Action<IOtlpExporterOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure<OtlpExporterBuilderOptions>(
this.name,
o => configure(o.TracingOptions));
return this;
}
public OtlpExporterBuilder ConfigureTracingProcessorOptions(
Action<ActivityExportProcessorOptions> configure)
{
Guard.ThrowIfNull(configure);
this.Services.Configure(this.name, configure);
return this;
}
private static void BindConfigurationToOptions(IServiceCollection services, string name, IConfiguration configuration)
{
Debug.Assert(services != null, "services was null");
Debug.Assert(!string.IsNullOrEmpty(name), "name was null or empty");
Debug.Assert(configuration != null, "configuration was null");
/* Config JSON structure is expected to be something like this:
{
"DefaultOptions": {
"Endpoint": "http://default_endpoint/"
},
"LoggingOptions": {
"Endpoint": "http://logs_endpoint/"
"ExportProcessorType": Batch,
"BatchExportProcessorOptions": {
"ScheduledDelayMilliseconds": 5000
}
},
"MetricsOptions": {
"Endpoint": "http://metrics_endpoint/",
"TemporalityPreference": "Delta",
"PeriodicExportingMetricReaderOptions": {
"ExportIntervalMilliseconds": 5000
}
},
"TracingOptions": {
"Endpoint": "http://trcing_endpoint/"
"ExportProcessorType": Batch,
"BatchExportProcessorOptions": {
"ScheduledDelayMilliseconds": 5000
}
}
}
*/
services!.Configure<OtlpExporterBuilderOptions>(name, configuration!);
services!.Configure<LogRecordExportProcessorOptions>(
name, configuration!.GetSection(nameof(OtlpExporterBuilderOptions.LoggingOptions)));
services!.Configure<MetricReaderOptions>(
name, configuration.GetSection(nameof(OtlpExporterBuilderOptions.MetricsOptions)));
services!.Configure<ActivityExportProcessorOptions>(
name, configuration.GetSection(nameof(OtlpExporterBuilderOptions.TracingOptions)));
}
private static void RegisterOtlpExporterServices(IServiceCollection services, string name)
{
Debug.Assert(services != null, "services was null");
Debug.Assert(name != null, "name was null");
services!.AddOtlpExporterLoggingServices();
services!.AddOtlpExporterMetricsServices(name!);
services!.AddOtlpExporterTracingServices();
// Note: UseOtlpExporterRegistration is added to the service collection
// to detect repeated calls to "UseOtlpExporter" and to throw if
// "AddOtlpExporter" extensions are called
services!.AddSingleton<UseOtlpExporterRegistration>();
services!.RegisterOptionsFactory((sp, configuration, name) => new OtlpExporterBuilderOptions(
configuration,
/* Note: We don't use name for SdkLimitOptions. There should only be
one provider for a given service collection so SdkLimitOptions is
treated as a single default instance. */
sp.GetRequiredService<IOptionsMonitor<SdkLimitOptions>>().CurrentValue,
sp.GetRequiredService<IOptionsMonitor<ExperimentalOptions>>().Get(name),
/* Note: We allow LogRecordExportProcessorOptions,
MetricReaderOptions, & ActivityExportProcessorOptions to be null
because those only exist if the corresponding signal is turned on.
Currently this extension turns on all signals so they will always be
there but that may change in the future so it is handled
defensively. */
sp.GetService<IOptionsMonitor<LogRecordExportProcessorOptions>>()?.Get(name),
sp.GetService<IOptionsMonitor<MetricReaderOptions>>()?.Get(name),
sp.GetService<IOptionsMonitor<ActivityExportProcessorOptions>>()?.Get(name)));
services!.ConfigureOpenTelemetryLoggerProvider(
(sp, logging) =>
{
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name!);
var processor = OtlpLogExporterHelperExtensions.BuildOtlpLogExporter(
sp,
builderOptions.LoggingOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance),
builderOptions.LogRecordExportProcessorOptions ?? throw new InvalidOperationException("LogRecordExportProcessorOptions were missing with logging enabled"),
builderOptions.SdkLimitOptions,
builderOptions.ExperimentalOptions,
skipUseOtlpExporterRegistrationCheck: true);
processor.PipelineWeight = DefaultProcessorPipelineWeight;
logging.AddProcessor(processor);
});
services!.ConfigureOpenTelemetryMeterProvider(
(sp, metrics) =>
{
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name!);
metrics.AddReader(
OtlpMetricExporterExtensions.BuildOtlpExporterMetricReader(
sp,
builderOptions.MetricsOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance),
builderOptions.MetricReaderOptions ?? throw new InvalidOperationException("MetricReaderOptions were missing with metrics enabled"),
builderOptions.ExperimentalOptions,
skipUseOtlpExporterRegistrationCheck: true));
});
services!.ConfigureOpenTelemetryTracerProvider(
(sp, tracing) =>
{
var builderOptions = GetBuilderOptionsAndValidateRegistrations(sp, name!);
var processorOptions = builderOptions.ActivityExportProcessorOptions ?? throw new InvalidOperationException("ActivityExportProcessorOptions were missing with tracing enabled");
var processor = OtlpTraceExporterHelperExtensions.BuildOtlpExporterProcessor(
sp,
builderOptions.TracingOptionsInstance.ApplyDefaults(builderOptions.DefaultOptionsInstance),
builderOptions.SdkLimitOptions,
builderOptions.ExperimentalOptions,
processorOptions.ExportProcessorType,
processorOptions.BatchExportProcessorOptions,
skipUseOtlpExporterRegistrationCheck: true);
processor.PipelineWeight = DefaultProcessorPipelineWeight;
tracing.AddProcessor(processor);
});
static OtlpExporterBuilderOptions GetBuilderOptionsAndValidateRegistrations(IServiceProvider sp, string name)
{
sp.EnsureSingleUseOtlpExporterRegistration();
return sp.GetRequiredService<IOptionsMonitor<OtlpExporterBuilderOptions>>().Get(name);
}
}
}