forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracerProviderBuilderSdk.cs
More file actions
213 lines (189 loc) · 8.29 KB
/
Copy pathTracerProviderBuilderSdk.cs
File metadata and controls
213 lines (189 loc) · 8.29 KB
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
// <copyright file="TracerProviderBuilderSdk.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Resources;
namespace OpenTelemetry.Trace
{
/// <summary>
/// Build TracerProvider with Resource, Sampler, Processors and Instrumentation.
/// </summary>
public class TracerProviderBuilderSdk : TracerProviderBuilder
{
private readonly List<InstrumentationFactory> instrumentationFactories = new List<InstrumentationFactory>();
private readonly List<BaseProcessor<Activity>> processors = new List<BaseProcessor<Activity>>();
private readonly List<string> sources = new List<string>();
private readonly Dictionary<string, bool> legacyActivityOperationNames = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
private ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault();
private Sampler sampler = new ParentBasedSampler(new AlwaysOnSampler());
/// <summary>
/// Adds an instrumentation to the provider.
/// </summary>
/// <typeparam name="TInstrumentation">Type of instrumentation class.</typeparam>
/// <param name="instrumentationFactory">Function that builds instrumentation.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
public override TracerProviderBuilder AddInstrumentation<TInstrumentation>(
Func<TInstrumentation> instrumentationFactory)
where TInstrumentation : class
{
if (instrumentationFactory == null)
{
throw new ArgumentNullException(nameof(instrumentationFactory));
}
this.instrumentationFactories.Add(
new InstrumentationFactory(
typeof(TInstrumentation).Name,
"semver:" + typeof(TInstrumentation).Assembly.GetName().Version,
instrumentationFactory));
return this;
}
/// <summary>
/// Adds given activitysource names to the list of subscribed sources.
/// </summary>
/// <param name="names">Activity source names.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
public override TracerProviderBuilder AddSource(params string[] names)
{
if (names == null)
{
throw new ArgumentNullException(nameof(names));
}
foreach (var name in names)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException($"{nameof(names)} contains null or whitespace string.");
}
// TODO: We need to fix the listening model.
// Today it ignores version.
this.sources.Add(name);
}
return this;
}
/// <summary>
/// Sets whether the status of <see cref="System.Diagnostics.Activity"/>
/// should be set to <c>Status.Error</c> when it ended abnormally due to an unhandled exception.
/// </summary>
/// <param name="enabled">Enabled or not.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder SetErrorStatusOnException(bool enabled)
{
ExceptionProcessor existingExceptionProcessor = null;
if (this.processors.Count > 0)
{
existingExceptionProcessor = this.processors[0] as ExceptionProcessor;
}
if (enabled)
{
if (existingExceptionProcessor == null)
{
try
{
this.processors.Insert(0, new ExceptionProcessor());
}
catch (Exception ex)
{
throw new NotSupportedException("SetErrorStatusOnException is not supported on this platform.", ex);
}
}
}
else
{
if (existingExceptionProcessor != null)
{
this.processors.RemoveAt(0);
existingExceptionProcessor.Dispose();
}
}
return this;
}
/// <summary>
/// Sets sampler.
/// </summary>
/// <param name="sampler">Sampler instance.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder SetSampler(Sampler sampler)
{
this.sampler = sampler ?? throw new ArgumentNullException(nameof(sampler));
return this;
}
/// <summary>
/// Sets the <see cref="ResourceBuilder"/> from which the Resource associated with
/// this provider is built from. Overwrites currently set ResourceBuilder.
/// </summary>
/// <param name="resourceBuilder"><see cref="ResourceBuilder"/> from which Resource will be built.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder)
{
this.resourceBuilder = resourceBuilder ?? throw new ArgumentNullException(nameof(resourceBuilder));
return this;
}
/// <summary>
/// Adds processor to the provider.
/// </summary>
/// <param name="processor">Activity processor to add.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder AddProcessor(BaseProcessor<Activity> processor)
{
if (processor == null)
{
throw new ArgumentNullException(nameof(processor));
}
this.processors.Add(processor);
return this;
}
/// <summary>
/// Adds a listener for <see cref="Activity"/> objects created with the given operation name to the <see cref="TracerProviderBuilder"/>.
/// </summary>
/// <remarks>
/// This is provided to capture legacy <see cref="Activity"/> objects created without using the <see cref="ActivitySource"/> API.
/// </remarks>
/// <param name="operationName">Operation name of the <see cref="Activity"/> objects to capture.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder AddLegacySource(string operationName)
{
if (string.IsNullOrWhiteSpace(operationName))
{
throw new ArgumentException($"{nameof(operationName)} contains null or whitespace string.");
}
this.legacyActivityOperationNames[operationName] = true;
return this;
}
internal TracerProvider Build()
{
return new TracerProviderSdk(
this.resourceBuilder.Build(),
this.sources,
this.instrumentationFactories,
this.sampler,
this.processors,
this.legacyActivityOperationNames);
}
internal readonly struct InstrumentationFactory
{
public readonly string Name;
public readonly string Version;
public readonly Func<object> Factory;
internal InstrumentationFactory(string name, string version, Func<object> factory)
{
this.Name = name;
this.Version = version;
this.Factory = factory;
}
}
}
}