-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathOpenTracingHelper.cs
54 lines (48 loc) · 1.93 KB
/
OpenTracingHelper.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using OpenTelemetry.AutoInstrumentation.Logging;
using OpenTelemetry.Shims.OpenTracing;
using OpenTelemetry.Trace;
using OpenTracing.Util;
namespace OpenTelemetry.AutoInstrumentation;
internal static class OpenTracingHelper
{
private static readonly IOtelLogger Logger = OtelLogging.GetLogger();
public static TracerProviderBuilder AddOpenTracingShimSource(this TracerProviderBuilder tracerProviderBuilder)
{
return tracerProviderBuilder.AddSource("opentracing-shim");
}
public static void EnableOpenTracing(TracerProvider? tracerProvider)
{
try
{
if (tracerProvider is not null)
{
// Instantiate the OpenTracing shim. The underlying OpenTelemetry tracer will create
// spans using the opentracing-shim source.
var openTracingShim = new TracerShim(tracerProvider);
// This registration must occur prior to any reference to the OpenTracing tracer:
// otherwise the no-op tracer is going to be used by OpenTracing instead.
if (GlobalTracer.RegisterIfAbsent(openTracingShim))
{
Logger.Information("OpenTracingShim registered as the OpenTracing global tracer.");
}
else
{
Logger.Error(
"OpenTracingShim could not be registered as the OpenTracing global tracer." +
"Another tracer was already registered. OpenTracing signals will not be captured.");
}
}
else
{
Logger.Information("OpenTracingShim was not loaded as the provider is not initialized.");
}
}
catch (Exception ex)
{
Logger.Error(ex, "OpenTracingShim exception.");
throw;
}
}
}