-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathTracerProviderBuilderSdk.cs
196 lines (145 loc) · 5.93 KB
/
TracerProviderBuilderSdk.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;
namespace OpenTelemetry.Trace;
/// <summary>
/// Stores state used to build a <see cref="TracerProvider"/>.
/// </summary>
internal sealed class TracerProviderBuilderSdk : TracerProviderBuilder, ITracerProviderBuilder
{
private const string DefaultInstrumentationVersion = "1.0.0.0";
private readonly IServiceProvider serviceProvider;
private TracerProviderSdk? tracerProvider;
public TracerProviderBuilderSdk(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public List<InstrumentationRegistration> Instrumentation { get; } = new();
public ResourceBuilder? ResourceBuilder { get; private set; }
public TracerProvider? Provider => this.tracerProvider;
public List<BaseProcessor<Activity>> Processors { get; } = new();
public List<string> Sources { get; } = new();
public HashSet<string> LegacyActivityOperationNames { get; } = new(StringComparer.OrdinalIgnoreCase);
public Sampler? Sampler { get; private set; }
public bool ExceptionProcessorEnabled { get; private set; }
public void RegisterProvider(TracerProviderSdk tracerProvider)
{
Debug.Assert(tracerProvider != null, "tracerProvider was null");
if (this.tracerProvider != null)
{
throw new NotSupportedException("TracerProvider cannot be accessed while build is executing.");
}
this.tracerProvider = tracerProvider;
}
public override TracerProviderBuilder AddInstrumentation<TInstrumentation>(Func<TInstrumentation> instrumentationFactory)
{
Debug.Assert(instrumentationFactory != null, "instrumentationFactory was null");
return this.AddInstrumentation(
typeof(TInstrumentation).Name,
typeof(TInstrumentation).Assembly.GetName().Version?.ToString() ?? DefaultInstrumentationVersion,
instrumentationFactory!());
}
public TracerProviderBuilder AddInstrumentation(
string instrumentationName,
string instrumentationVersion,
object? instrumentation)
{
Debug.Assert(!string.IsNullOrWhiteSpace(instrumentationName), "instrumentationName was null or whitespace");
Debug.Assert(!string.IsNullOrWhiteSpace(instrumentationVersion), "instrumentationVersion was null or whitespace");
this.Instrumentation.Add(
new InstrumentationRegistration(
instrumentationName,
instrumentationVersion,
instrumentation));
return this;
}
public TracerProviderBuilder ConfigureResource(Action<ResourceBuilder> configure)
{
Debug.Assert(configure != null, "configure was null");
var resourceBuilder = this.ResourceBuilder ??= ResourceBuilder.CreateDefault();
configure!(resourceBuilder);
return this;
}
public TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder)
{
Debug.Assert(resourceBuilder != null, "resourceBuilder was null");
this.ResourceBuilder = resourceBuilder;
return this;
}
public override TracerProviderBuilder AddLegacySource(string operationName)
{
Debug.Assert(!string.IsNullOrWhiteSpace(operationName), "operationName was null or whitespace");
this.LegacyActivityOperationNames.Add(operationName);
return this;
}
public override TracerProviderBuilder AddSource(params string[] names)
{
Debug.Assert(names != null, "names was null");
foreach (var name in names!)
{
Guard.ThrowIfNullOrWhitespace(name);
// TODO: We need to fix the listening model.
// Today it ignores version.
this.Sources.Add(name);
}
return this;
}
public TracerProviderBuilder AddProcessor(BaseProcessor<Activity> processor)
{
Debug.Assert(processor != null, "processor was null");
this.Processors.Add(processor!);
return this;
}
public TracerProviderBuilder SetSampler(Sampler sampler)
{
Debug.Assert(sampler != null, "sampler was null");
this.Sampler = sampler;
return this;
}
public TracerProviderBuilder SetErrorStatusOnException(bool enabled)
{
this.ExceptionProcessorEnabled = enabled;
return this;
}
public TracerProviderBuilder ConfigureBuilder(Action<IServiceProvider, TracerProviderBuilder> configure)
{
Debug.Assert(configure != null, "configure was null");
configure!(this.serviceProvider, this);
return this;
}
public TracerProviderBuilder ConfigureServices(Action<IServiceCollection> configure)
{
throw new NotSupportedException("Services cannot be configured after ServiceProvider has been created.");
}
public void AddExceptionProcessorIfEnabled()
{
if (this.ExceptionProcessorEnabled)
{
try
{
this.Processors.Insert(0, new ExceptionProcessor());
}
catch (Exception ex)
{
throw new NotSupportedException($"'{nameof(TracerProviderBuilderExtensions.SetErrorStatusOnException)}' is not supported on this platform", ex);
}
}
}
TracerProviderBuilder IDeferredTracerProviderBuilder.Configure(Action<IServiceProvider, TracerProviderBuilder> configure)
=> this.ConfigureBuilder(configure);
internal readonly struct InstrumentationRegistration
{
public readonly string Name;
public readonly string Version;
public readonly object? Instance;
internal InstrumentationRegistration(string name, string version, object? instance)
{
this.Name = name;
this.Version = version;
this.Instance = instance;
}
}
}