15
15
// </copyright>
16
16
using System ;
17
17
using System . Collections . Generic ;
18
- using System . Diagnostics ;
19
18
using OpenTelemetry . Resources ;
20
19
using OpenTelemetry . Trace . Samplers ;
21
20
@@ -26,17 +25,17 @@ namespace OpenTelemetry.Trace
26
25
/// </summary>
27
26
public class TracerProviderBuilder
28
27
{
29
- internal TracerProviderBuilder ( )
30
- {
31
- }
28
+ private readonly List < ActivityProcessor > processors = new List < ActivityProcessor > ( ) ;
32
29
33
- internal Sampler Sampler { get ; private set ; }
30
+ private readonly List < string > sources = new List < string > ( ) ;
34
31
35
- internal Resource Resource { get ; private set ; } = Resource . Empty ;
32
+ private Sampler sampler = new ParentOrElseSampler ( new AlwaysOnSampler ( ) ) ;
36
33
37
- internal ActivityProcessor ActivityProcessor { get ; private set ; }
34
+ private Resource resource = Resource . Empty ;
38
35
39
- internal Dictionary < string , bool > ActivitySourceNames { get ; private set ; }
36
+ internal TracerProviderBuilder ( )
37
+ {
38
+ }
40
39
41
40
internal List < InstrumentationFactory > InstrumentationFactories { get ; private set ; }
42
41
@@ -47,7 +46,7 @@ internal TracerProviderBuilder()
47
46
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
48
47
public TracerProviderBuilder SetSampler ( Sampler sampler )
49
48
{
50
- this . Sampler = sampler ?? throw new ArgumentNullException ( nameof ( sampler ) ) ;
49
+ this . sampler = sampler ;
51
50
return this ;
52
51
}
53
52
@@ -58,45 +57,44 @@ public TracerProviderBuilder SetSampler(Sampler sampler)
58
57
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
59
58
public TracerProviderBuilder SetResource ( Resource resource )
60
59
{
61
- this . Resource = resource ?? Resource . Empty ;
60
+ this . resource = resource ?? Resource . Empty ;
62
61
return this ;
63
62
}
64
63
65
64
/// <summary>
66
65
/// Adds given activitysource name to the list of subscribed sources.
67
66
/// </summary>
68
- /// <param name="activitySourceName ">Activity source name.</param>
67
+ /// <param name="name ">Activity source name.</param>
69
68
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
70
- public TracerProviderBuilder AddActivitySource ( string activitySourceName )
71
- {
69
+ public TracerProviderBuilder AddActivitySource ( string name )
70
+ {
71
+ if ( string . IsNullOrWhiteSpace ( name ) )
72
+ {
73
+ throw new ArgumentException ( $ "{ nameof ( name ) } is null or whitespace.") ;
74
+ }
75
+
72
76
// TODO: We need to fix the listening model.
73
77
// Today it ignores version.
74
- if ( this . ActivitySourceNames == null )
75
- {
76
- this . ActivitySourceNames = new Dictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
77
- }
78
+ this . sources . Add ( name ) ;
78
79
79
- this . ActivitySourceNames [ activitySourceName ] = true ;
80
80
return this ;
81
81
}
82
82
83
83
/// <summary>
84
84
/// Adds given activitysource names to the list of subscribed sources.
85
85
/// </summary>
86
- /// <param name="activitySourceNames ">Activity source names.</param>
86
+ /// <param name="names ">Activity source names.</param>
87
87
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
88
- public TracerProviderBuilder AddActivitySources ( IEnumerable < string > activitySourceNames )
89
- {
90
- // TODO: We need to fix the listening model.
91
- // Today it ignores version.
92
- if ( this . ActivitySourceNames == null )
93
- {
94
- this . ActivitySourceNames = new Dictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
88
+ public TracerProviderBuilder AddActivitySource ( IEnumerable < string > names )
89
+ {
90
+ if ( names == null )
91
+ {
92
+ throw new ArgumentNullException ( nameof ( names ) ) ;
95
93
}
96
94
97
- foreach ( var activitySourceName in activitySourceNames )
95
+ foreach ( var name in names )
98
96
{
99
- this . ActivitySourceNames [ activitySourceName ] = true ;
97
+ this . AddActivitySource ( name ) ;
100
98
}
101
99
102
100
return this ;
@@ -108,24 +106,14 @@ public TracerProviderBuilder AddActivitySources(IEnumerable<string> activitySour
108
106
/// <param name="activityProcessor">Activity processor to add.</param>
109
107
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
110
108
public TracerProviderBuilder AddProcessor ( ActivityProcessor activityProcessor )
111
- {
112
- if ( this . ActivityProcessor == null )
113
- {
114
- this . ActivityProcessor = activityProcessor ;
115
- }
116
- else if ( this . ActivityProcessor is CompositeActivityProcessor compositeProcessor )
117
- {
118
- compositeProcessor . AddProcessor ( activityProcessor ) ;
119
- }
120
- else
121
- {
122
- this . ActivityProcessor = new CompositeActivityProcessor ( new [ ]
123
- {
124
- this . ActivityProcessor ,
125
- activityProcessor ,
126
- } ) ;
109
+ {
110
+ if ( activityProcessor == null )
111
+ {
112
+ throw new ArgumentNullException ( nameof ( activityProcessor ) ) ;
127
113
}
128
114
115
+ this . processors . Add ( activityProcessor ) ;
116
+
129
117
return this ;
130
118
}
131
119
@@ -160,104 +148,7 @@ public TracerProviderBuilder AddInstrumentation<TInstrumentation>(
160
148
161
149
public TracerProvider Build ( )
162
150
{
163
- this . Sampler = this . Sampler ?? new ParentOrElseSampler ( new AlwaysOnSampler ( ) ) ;
164
-
165
- var provider = new TracerProviderSdk
166
- {
167
- Resource = this . Resource ,
168
- Sampler = this . Sampler ,
169
- ActivityProcessor = this . ActivityProcessor ,
170
- } ;
171
-
172
- var activitySource = new ActivitySourceAdapter ( provider . Sampler , provider . ActivityProcessor , provider . Resource ) ;
173
-
174
- if ( this . InstrumentationFactories != null )
175
- {
176
- foreach ( var instrumentation in this . InstrumentationFactories )
177
- {
178
- provider . Instrumentations . Add ( instrumentation . Factory ( activitySource ) ) ;
179
- }
180
- }
181
-
182
- provider . ActivityListener = new ActivityListener
183
- {
184
- // Callback when Activity is started.
185
- ActivityStarted = ( activity ) =>
186
- {
187
- if ( activity . IsAllDataRequested )
188
- {
189
- activity . SetResource ( this . Resource ) ;
190
- }
191
-
192
- provider . ActivityProcessor ? . OnStart ( activity ) ;
193
- } ,
194
-
195
- // Callback when Activity is stopped.
196
- ActivityStopped = ( activity ) =>
197
- {
198
- provider . ActivityProcessor ? . OnEnd ( activity ) ;
199
- } ,
200
-
201
- // Function which takes ActivitySource and returns true/false to indicate if it should be subscribed to
202
- // or not.
203
- ShouldListenTo = ( activitySource ) =>
204
- {
205
- if ( this . ActivitySourceNames == null )
206
- {
207
- return false ;
208
- }
209
-
210
- return this . ActivitySourceNames . ContainsKey ( activitySource . Name ) ;
211
- } ,
212
-
213
- // Setting this to true means TraceId will be always
214
- // available in sampling callbacks and will be the actual
215
- // traceid used, if activity ends up getting created.
216
- AutoGenerateRootContextTraceId = true ,
217
-
218
- // This delegate informs ActivitySource about sampling decision when the parent context is an ActivityContext.
219
- GetRequestedDataUsingContext = ( ref ActivityCreationOptions < ActivityContext > options ) => ComputeActivityDataRequest ( options , this . Sampler ) ,
220
- } ;
221
-
222
- ActivitySource . AddActivityListener ( provider . ActivityListener ) ;
223
-
224
- return provider ;
225
- }
226
-
227
- internal static ActivityDataRequest ComputeActivityDataRequest (
228
- in ActivityCreationOptions < ActivityContext > options ,
229
- Sampler sampler )
230
- {
231
- var isRootSpan = /*TODO: Put back once AutoGenerateRootContextTraceId is removed.
232
- options.Parent.TraceId == default ||*/ options . Parent . SpanId == default ;
233
-
234
- if ( sampler != null )
235
- {
236
- // As we set ActivityListener.AutoGenerateRootContextTraceId = true,
237
- // Parent.TraceId will always be the TraceId of the to-be-created Activity,
238
- // if it get created.
239
- ActivityTraceId traceId = options . Parent . TraceId ;
240
-
241
- var samplingParameters = new SamplingParameters (
242
- options . Parent ,
243
- traceId ,
244
- options . Name ,
245
- options . Kind ,
246
- options . Tags ,
247
- options . Links ) ;
248
-
249
- var shouldSample = sampler . ShouldSample ( samplingParameters ) ;
250
- if ( shouldSample . IsSampled )
251
- {
252
- return ActivityDataRequest . AllDataAndRecorded ;
253
- }
254
- }
255
-
256
- // If it is the root span select PropagationData so the trace ID is preserved
257
- // even if no activity of the trace is recorded (sampled per OpenTelemetry parlance).
258
- return isRootSpan
259
- ? ActivityDataRequest . PropagationData
260
- : ActivityDataRequest . None ;
151
+ return new TracerProviderSdk ( this . sources , this . processors , this . InstrumentationFactories , this . sampler , this . resource ) ;
261
152
}
262
153
263
154
internal readonly struct InstrumentationFactory
0 commit comments