-
Notifications
You must be signed in to change notification settings - Fork 785
/
Copy pathSdk.cs
133 lines (118 loc) · 5.33 KB
/
Sdk.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Reflection;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace OpenTelemetry;
/// <summary>
/// OpenTelemetry helper.
/// </summary>
public static class Sdk
{
static Sdk()
{
Propagators.DefaultTextMapPropagator = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
SelfDiagnostics.EnsureInitialized();
var assemblyInformationalVersion = typeof(Sdk).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
InformationalVersion = ParseAssemblyInformationalVersion(assemblyInformationalVersion);
}
/// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </summary>
public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed;
internal static string InformationalVersion { get; }
/// <summary>
/// Sets the Default TextMapPropagator.
/// </summary>
/// <param name="textMapPropagator">TextMapPropagator to be set as default.</param>
public static void SetDefaultTextMapPropagator(TextMapPropagator textMapPropagator)
{
Guard.ThrowIfNull(textMapPropagator);
Propagators.DefaultTextMapPropagator = textMapPropagator;
}
/// <summary>
/// Creates a <see cref="MeterProviderBuilder"/> which is used to build
/// a <see cref="MeterProvider"/>. In a typical application, a single
/// <see cref="MeterProvider"/> is created at application startup and disposed
/// at application shutdown. It is important to ensure that the provider is not
/// disposed too early.
/// </summary>
/// <returns><see cref="MeterProviderBuilder"/> instance, which is used to build a <see cref="MeterProvider"/>.</returns>
public static MeterProviderBuilder CreateMeterProviderBuilder()
{
return new MeterProviderBuilderBase();
}
/// <summary>
/// Creates a <see cref="TracerProviderBuilder"/> which is used to build
/// a <see cref="TracerProvider"/>. In a typical application, a single
/// <see cref="TracerProvider"/> is created at application startup and disposed
/// at application shutdown. It is important to ensure that the provider is not
/// disposed too early.
/// </summary>
/// <returns><see cref="TracerProviderBuilder"/> instance, which is used to build a <see cref="TracerProvider"/>.</returns>
public static TracerProviderBuilder CreateTracerProviderBuilder()
{
return new TracerProviderBuilderBase();
}
#if EXPOSE_EXPERIMENTAL_FEATURES
/// <summary>
/// Creates a <see cref="LoggerProviderBuilder"/> which is used to build
/// a <see cref="LoggerProvider"/>. In a typical application, a single
/// <see cref="LoggerProvider"/> is created at application startup and
/// disposed at application shutdown. It is important to ensure that the
/// provider is not disposed too early.
/// </summary>
/// <remarks><b>WARNING</b>: This is an experimental API which might change or be removed in the future. Use at your own risk.</remarks>
/// <returns><see cref="LoggerProviderBuilder"/> instance, which is used
/// to build a <see cref="LoggerProvider"/>.</returns>
#if NET8_0_OR_GREATER
[Experimental(DiagnosticDefinitions.LoggerProviderExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
#endif
public
#else
/// <summary>
/// Creates a <see cref="LoggerProviderBuilder"/> which is used to build
/// a <see cref="LoggerProvider"/>. In a typical application, a single
/// <see cref="LoggerProvider"/> is created at application startup and
/// disposed at application shutdown. It is important to ensure that the
/// provider is not disposed too early.
/// </summary>
/// <returns><see cref="LoggerProviderBuilder"/> instance, which is used
/// to build a <see cref="LoggerProvider"/>.</returns>
internal
#endif
static LoggerProviderBuilder CreateLoggerProviderBuilder()
{
return new LoggerProviderBuilderBase();
}
internal static string ParseAssemblyInformationalVersion(string? informationalVersion)
{
if (string.IsNullOrWhiteSpace(informationalVersion))
{
informationalVersion = "1.0.0";
}
/*
* InformationalVersion will be in the following format:
* {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit}
* Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4
* The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit
*/
var indexOfPlusSign = informationalVersion!.IndexOf('+');
return indexOfPlusSign > 0
? informationalVersion.Substring(0, indexOfPlusSign)
: informationalVersion;
}
}