-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstrumentation.cs
490 lines (433 loc) · 20.4 KB
/
Instrumentation.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using System.Diagnostics;
#endif
using System.Reflection;
using OpenTelemetry.AutoInstrumentation.Configurations;
#if NET
using OpenTelemetry.AutoInstrumentation.ContinuousProfiler;
#endif
using OpenTelemetry.AutoInstrumentation.Diagnostics;
using OpenTelemetry.AutoInstrumentation.Loading;
using OpenTelemetry.AutoInstrumentation.Logging;
using OpenTelemetry.AutoInstrumentation.Plugins;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace OpenTelemetry.AutoInstrumentation;
/// <summary>
/// Instrumentation
/// </summary>
internal static class Instrumentation
{
private static readonly IOtelLogger Logger = OtelLogging.GetLogger();
private static readonly LazyInstrumentationLoader LazyInstrumentationLoader = new();
private static readonly Lazy<LoggerProvider?> LoggerProviderFactory = new(InitializeLoggerProvider, true);
private static int _initialized;
private static int _isExiting;
private static SdkSelfDiagnosticsEventListener? _sdkEventListener;
private static TracerProvider? _tracerProvider;
private static MeterProvider? _meterProvider;
private static PluginManager? _pluginManager;
#if NET
private static ContinuousProfilerProcessor? _profilerProcessor;
#endif
internal static LoggerProvider? LoggerProvider
{
get => LoggerProviderFactory.Value;
}
internal static PluginManager? PluginManager => _pluginManager;
internal static ILifespanManager LifespanManager => LazyInstrumentationLoader.LifespanManager;
internal static Lazy<FailFastSettings> FailFastSettings { get; } = new(() => Settings.FromDefaultSources<FailFastSettings>(false));
internal static Lazy<GeneralSettings> GeneralSettings { get; } = new(() => Settings.FromDefaultSources<GeneralSettings>(FailFastSettings.Value.FailFast));
internal static Lazy<TracerSettings> TracerSettings { get; } = new(() => Settings.FromDefaultSources<TracerSettings>(FailFastSettings.Value.FailFast));
internal static Lazy<MetricSettings> MetricSettings { get; } = new(() => Settings.FromDefaultSources<MetricSettings>(FailFastSettings.Value.FailFast));
internal static Lazy<LogSettings> LogSettings { get; } = new(() => Settings.FromDefaultSources<LogSettings>(FailFastSettings.Value.FailFast));
internal static Lazy<SdkSettings> SdkSettings { get; } = new(() => Settings.FromDefaultSources<SdkSettings>(FailFastSettings.Value.FailFast));
/// <summary>
/// Initialize the OpenTelemetry SDK with a pre-defined set of exporters, shims, and
/// instrumentations.
/// </summary>
public static void Initialize()
{
if (Interlocked.Exchange(ref _initialized, value: 1) != 0)
{
// Initialize() was already called before
return;
}
#if NETFRAMEWORK
try
{
// On .NET Framework only, initialize env vars from app.config/web.config
// this does not override settings which where already set via env vars.
// We are doing so as the OTel .NET SDK only supports the env vars and we want to
// be able to set them via app.config/web.config.
EnvironmentInitializer.Initialize(System.Configuration.ConfigurationManager.AppSettings);
}
catch (Exception ex)
{
Logger.Error(ex, "Failed to initialize from AppSettings.");
throw;
}
#endif
try
{
// Initialize SdkSelfDiagnosticsEventListener to create an EventListener for the OpenTelemetry SDK
_sdkEventListener = new(Logger);
_pluginManager = new PluginManager(GeneralSettings.Value);
_pluginManager.Initializing();
#if NET
var profilerEnabled = GeneralSettings.Value.ProfilerEnabled;
if (profilerEnabled)
{
var (threadSamplingEnabled, threadSamplingInterval, allocationSamplingEnabled, maxMemorySamplesPerMinute, exportInterval, exportTimeout, continuousProfilerExporter) = _pluginManager.GetFirstContinuousConfiguration();
Logger.Debug($"Continuous profiling configuration: Thread sampling enabled: {threadSamplingEnabled}, thread sampling interval: {threadSamplingInterval}, allocation sampling enabled: {allocationSamplingEnabled}, max memory samples per minute: {maxMemorySamplesPerMinute}, export interval: {exportInterval}, export timeout: {exportTimeout}, continuous profiler exporter: {continuousProfilerExporter.GetType()}");
if (threadSamplingEnabled || allocationSamplingEnabled)
{
InitializeContinuousProfiling(continuousProfilerExporter, threadSamplingEnabled, allocationSamplingEnabled, threadSamplingInterval, maxMemorySamplesPerMinute, exportInterval, exportTimeout);
}
}
else
{
Logger.Information("CLR Profiler is not enabled. Continuous Profiler will be not started even if configured correctly.");
}
#endif
if (TracerSettings.Value.TracesEnabled || MetricSettings.Value.MetricsEnabled)
{
// Register to shutdown events
AppDomain.CurrentDomain.ProcessExit += OnExit;
AppDomain.CurrentDomain.DomainUnload += OnExit;
if (GeneralSettings.Value.FlushOnUnhandledException)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
}
EnvironmentConfigurationSdkHelper.UseEnvironmentVariables(SdkSettings.Value);
}
if (TracerSettings.Value.TracesEnabled)
{
if (GeneralSettings.Value.SetupSdk)
{
var builder = Sdk
.CreateTracerProviderBuilder()
.InvokePluginsBefore(_pluginManager)
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder(GeneralSettings.Value.EnabledResourceDetectors))
.UseEnvironmentVariables(LazyInstrumentationLoader, TracerSettings.Value, _pluginManager)
.InvokePluginsAfter(_pluginManager);
_tracerProvider = builder.Build();
_tracerProvider.TryCallInitialized(_pluginManager);
Logger.Information("OpenTelemetry tracer initialized.");
}
else
{
AddLazilyLoadedTraceInstrumentations(LazyInstrumentationLoader, _pluginManager, TracerSettings.Value);
Logger.Information("Initialized lazily-loaded trace instrumentations without initializing sdk.");
}
}
if (MetricSettings.Value.MetricsEnabled)
{
if (GeneralSettings.Value.SetupSdk)
{
var builder = Sdk
.CreateMeterProviderBuilder()
.InvokePluginsBefore(_pluginManager)
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder(GeneralSettings.Value.EnabledResourceDetectors))
.UseEnvironmentVariables(LazyInstrumentationLoader, MetricSettings.Value, _pluginManager)
.InvokePluginsAfter(_pluginManager);
_meterProvider = builder.Build();
_meterProvider.TryCallInitialized(_pluginManager);
Logger.Information("OpenTelemetry meter initialized.");
}
else
{
AddLazilyLoadedMetricInstrumentations(LazyInstrumentationLoader, _pluginManager, MetricSettings.Value.EnabledInstrumentations);
Logger.Information("Initialized lazily-loaded metric instrumentations without initializing sdk.");
}
}
}
catch (Exception ex)
{
Logger.Error(ex, "OpenTelemetry SDK load exception.");
throw;
}
if (GeneralSettings.Value.ProfilerEnabled)
{
RegisterBytecodeInstrumentations(InstrumentationDefinitions.GetAllDefinitions());
try
{
foreach (var payload in _pluginManager.GetAllDefinitionsPayloads())
{
RegisterBytecodeInstrumentations(payload);
}
}
catch (Exception ex)
{
Logger.Error(ex, "Exception occurred while registering instrumentations from plugins.");
}
try
{
Logger.Debug("Sending CallTarget derived integration definitions to native library.");
var payload = InstrumentationDefinitions.GetDerivedDefinitions();
NativeMethods.AddDerivedInstrumentations(payload.DefinitionsId, payload.Definitions);
foreach (var def in payload.Definitions)
{
def.Dispose();
}
Logger.Information("The profiler has been initialized with {0} derived definitions.", payload.Definitions.Length);
}
catch (Exception ex)
{
Logger.Error(ex, ex.Message);
}
}
else
{
Logger.Debug("Skipping CLR Profiler initialization. {0} environment variable was not set to '1'.", ConfigurationKeys.ProfilingEnabled);
}
if (TracerSettings.Value.OpenTracingEnabled)
{
OpenTracingHelper.EnableOpenTracing(_tracerProvider);
}
}
private static LoggerProvider? InitializeLoggerProvider()
{
// ILogger bridge is initialized using ILogger-specific extension methods in LoggerInitializer class.
// That extension methods sets up its own LogProvider.
if (LogSettings.Value.EnableLog4NetBridge && LogSettings.Value.LogsEnabled && LogSettings.Value.EnabledInstrumentations.Contains(LogInstrumentation.Log4Net))
{
// TODO: Replace reflection usage when Logs Api is made public in non-rc builds.
// Sdk.CreateLoggerProviderBuilder()
var createLoggerProviderBuilderMethod = typeof(Sdk).GetMethod("CreateLoggerProviderBuilder", BindingFlags.Static | BindingFlags.NonPublic)!;
var loggerProviderBuilder = createLoggerProviderBuilderMethod.Invoke(null, null) as LoggerProviderBuilder;
// TODO: plugins support
var loggerProvider = loggerProviderBuilder!
.SetResourceBuilder(ResourceConfigurator.CreateResourceBuilder(GeneralSettings.Value.EnabledResourceDetectors))
.UseEnvironmentVariables(LazyInstrumentationLoader, LogSettings.Value, _pluginManager!)
.Build();
Logger.Information("OpenTelemetry logger provider initialized.");
return loggerProvider;
}
return null;
}
#if NET
private static void InitializeContinuousProfiling(
object continuousProfilerExporter,
bool threadSamplingEnabled,
bool allocationSamplingEnabled,
uint threadSamplingInterval,
uint maxMemorySamplesPerMinute,
TimeSpan exportInterval,
TimeSpan exportTimeout)
{
var continuousProfilerExporterType = continuousProfilerExporter.GetType();
var exportThreadSamplesMethod = continuousProfilerExporterType.GetMethod("ExportThreadSamples");
if (exportThreadSamplesMethod == null)
{
Logger.Warning("Exporter does not have ExportThreadSamples method. Continuous Profiler initialization failed.");
return;
}
var exportAllocationSamplesMethod = continuousProfilerExporterType.GetMethod("ExportAllocationSamples");
if (exportAllocationSamplesMethod == null)
{
Logger.Warning("Exporter does not have ExportAllocationSamples method. Continuous Profiler initialization failed.");
return;
}
NativeMethods.ConfigureNativeContinuousProfiler(threadSamplingEnabled, threadSamplingInterval, allocationSamplingEnabled, maxMemorySamplesPerMinute);
var threadSamplesMethod = exportThreadSamplesMethod.CreateDelegate<Action<byte[], int, CancellationToken>>(continuousProfilerExporter);
var allocationSamplesMethod = exportAllocationSamplesMethod.CreateDelegate<Action<byte[], int, CancellationToken>>(continuousProfilerExporter);
var bufferProcessor = new BufferProcessor(threadSamplingEnabled, allocationSamplingEnabled, threadSamplesMethod, allocationSamplesMethod, exportTimeout);
_profilerProcessor = new ContinuousProfilerProcessor(bufferProcessor, exportInterval, exportTimeout);
Activity.CurrentChanged += _profilerProcessor.Activity_CurrentChanged;
}
#endif
private static void RegisterBytecodeInstrumentations(InstrumentationDefinitions.Payload payload)
{
try
{
Logger.Debug("Sending CallTarget integration definitions to native library for {0}.", payload.DefinitionsId);
NativeMethods.AddInstrumentations(payload.DefinitionsId, payload.Definitions);
foreach (var def in payload.Definitions)
{
def.Dispose();
}
Logger.Information("The profiler has been initialized with {0} definitions for {1}.", payload.Definitions.Length, payload.DefinitionsId);
}
catch (Exception ex)
{
Logger.Error(ex, ex.Message);
}
}
private static void AddLazilyLoadedMetricInstrumentations(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager, IReadOnlyList<MetricInstrumentation> enabledInstrumentations)
{
foreach (var instrumentation in enabledInstrumentations)
{
switch (instrumentation)
{
#if NETFRAMEWORK
case MetricInstrumentation.AspNet:
DelayedInitialization.Metrics.AddAspNet(lazyInstrumentationLoader, pluginManager);
break;
#endif
#if NET
case MetricInstrumentation.AspNetCore:
break;
#endif
case MetricInstrumentation.HttpClient:
DelayedInitialization.Metrics.AddHttpClient(lazyInstrumentationLoader);
break;
case MetricInstrumentation.NetRuntime:
break;
case MetricInstrumentation.Process:
break;
case MetricInstrumentation.NServiceBus:
break;
case MetricInstrumentation.SqlClient:
DelayedInitialization.Metrics.AddSqlClient(lazyInstrumentationLoader, pluginManager);
break;
default:
Logger.Warning($"Configured metric instrumentation type is not supported: {instrumentation}");
if (FailFastSettings.Value.FailFast)
{
throw new NotSupportedException($"Configured metric instrumentation type is not supported: {instrumentation}");
}
break;
}
}
}
private static void AddLazilyLoadedTraceInstrumentations(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager, TracerSettings tracerSettings)
{
foreach (var instrumentation in tracerSettings.EnabledInstrumentations)
{
switch (instrumentation)
{
#if NETFRAMEWORK
case TracerInstrumentation.AspNet:
DelayedInitialization.Traces.AddAspNet(lazyInstrumentationLoader, pluginManager, tracerSettings);
break;
case TracerInstrumentation.WcfService:
break;
#endif
case TracerInstrumentation.HttpClient:
DelayedInitialization.Traces.AddHttpClient(lazyInstrumentationLoader, pluginManager, tracerSettings);
break;
case TracerInstrumentation.GrpcNetClient:
DelayedInitialization.Traces.AddGrpcClient(lazyInstrumentationLoader, pluginManager, tracerSettings);
break;
case TracerInstrumentation.SqlClient:
DelayedInitialization.Traces.AddSqlClient(lazyInstrumentationLoader, pluginManager, tracerSettings);
break;
case TracerInstrumentation.Quartz:
DelayedInitialization.Traces.AddQuartz(lazyInstrumentationLoader, pluginManager);
break;
case TracerInstrumentation.WcfClient:
break;
#if NET
case TracerInstrumentation.AspNetCore:
DelayedInitialization.Traces.AddAspNetCore(lazyInstrumentationLoader, pluginManager, tracerSettings);
break;
case TracerInstrumentation.MySqlData:
break;
case TracerInstrumentation.EntityFrameworkCore:
DelayedInitialization.Traces.AddEntityFrameworkCore(LazyInstrumentationLoader, pluginManager, tracerSettings);
break;
case TracerInstrumentation.StackExchangeRedis:
break;
case TracerInstrumentation.MassTransit:
break;
case TracerInstrumentation.GraphQL:
DelayedInitialization.Traces.AddGraphQL(LazyInstrumentationLoader, pluginManager, tracerSettings);
break;
#endif
case TracerInstrumentation.Azure:
break;
case TracerInstrumentation.MongoDB:
break;
case TracerInstrumentation.Npgsql:
break;
case TracerInstrumentation.NServiceBus:
break;
case TracerInstrumentation.Elasticsearch:
break;
case TracerInstrumentation.ElasticTransport:
break;
case TracerInstrumentation.MySqlConnector:
break;
case TracerInstrumentation.Kafka:
break;
case TracerInstrumentation.OracleMda:
break;
case TracerInstrumentation.RabbitMq:
break;
default:
Logger.Warning($"Configured trace instrumentation type is not supported: {instrumentation}");
if (FailFastSettings.Value.FailFast)
{
throw new NotSupportedException($"Configured trace instrumentation type is not supported: {instrumentation}");
}
break;
}
}
}
private static void OnExit(object? sender, EventArgs e)
{
if (Interlocked.Exchange(ref _isExiting, value: 1) != 0)
{
// OnExit() was already called before
return;
}
try
{
#if NET
LazyInstrumentationLoader?.Dispose();
if (_profilerProcessor != null)
{
Activity.CurrentChanged -= _profilerProcessor.Activity_CurrentChanged;
_profilerProcessor.Dispose();
}
#endif
_tracerProvider?.Dispose();
_meterProvider?.Dispose();
if (LoggerProviderFactory.IsValueCreated)
{
LoggerProvider?.Dispose();
}
_sdkEventListener?.Dispose();
Logger.Information("OpenTelemetry Automatic Instrumentation exit.");
}
catch (Exception ex)
{
try
{
Logger.Error(ex, "An error occurred while attempting to exit.");
}
catch
{
// If we encounter an error while logging there is nothing else we can do
// with the exception.
}
}
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
try
{
if (args.IsTerminating)
{
Logger.Error("UnhandledException event raised with a terminating exception.");
OnExit(sender, args);
}
}
catch (Exception ex)
{
try
{
Logger.Error(ex, "An exception occurred while processing an unhandled exception.");
}
catch
{
// If we encounter an error while logging there is nothing else we can do
// with the exception.
}
}
}
}