@@ -32,11 +32,14 @@ import { Resource } from '@opentelemetry/resources';
32
32
import { SpanProcessor , Tracer } from '.' ;
33
33
import { DEFAULT_CONFIG } from './config' ;
34
34
import { MultiSpanProcessor } from './MultiSpanProcessor' ;
35
- import { NoopSpanProcessor } from './NoopSpanProcessor' ;
35
+ import { NoopSpanProcessor } from './export/ NoopSpanProcessor' ;
36
36
import { SDKRegistrationConfig , TracerConfig } from './types' ;
37
37
const merge = require ( 'lodash.merge' ) ;
38
+ import { SpanExporter } from './export/SpanExporter' ;
39
+ import { BatchSpanProcessor } from './export/BatchSpanProcessor' ;
38
40
39
41
export type PROPAGATOR_FACTORY = ( ) => TextMapPropagator ;
42
+ export type EXPORTER_FACTORY = ( ) => SpanExporter ;
40
43
41
44
/**
42
45
* This class represents a basic tracer provider which platform libraries can extend
@@ -50,11 +53,16 @@ export class BasicTracerProvider implements TracerProvider {
50
53
[ 'baggage' , ( ) => new HttpBaggagePropagator ( ) ] ,
51
54
] ) ;
52
55
56
+ protected static readonly _registeredExporters = new Map <
57
+ string ,
58
+ EXPORTER_FACTORY
59
+ > ( ) ;
60
+
53
61
private readonly _config : TracerConfig ;
54
62
private readonly _registeredSpanProcessors : SpanProcessor [ ] = [ ] ;
55
63
private readonly _tracers : Map < string , Tracer > = new Map ( ) ;
56
64
57
- activeSpanProcessor : SpanProcessor = new NoopSpanProcessor ( ) ;
65
+ activeSpanProcessor : SpanProcessor ;
58
66
readonly resource : Resource ;
59
67
60
68
constructor ( config : TracerConfig = { } ) {
@@ -64,6 +72,14 @@ export class BasicTracerProvider implements TracerProvider {
64
72
this . _config = Object . assign ( { } , mergedConfig , {
65
73
resource : this . resource ,
66
74
} ) ;
75
+
76
+ const defaultExporter = this . _buildExporterFromEnv ( ) ;
77
+ if ( defaultExporter !== undefined ) {
78
+ const batchProcessor = new BatchSpanProcessor ( defaultExporter ) ;
79
+ this . activeSpanProcessor = batchProcessor ;
80
+ } else {
81
+ this . activeSpanProcessor = new NoopSpanProcessor ( ) ;
82
+ }
67
83
}
68
84
69
85
getTracer ( name : string , version ?: string ) : Tracer {
@@ -80,6 +96,18 @@ export class BasicTracerProvider implements TracerProvider {
80
96
* @param spanProcessor the new SpanProcessor to be added.
81
97
*/
82
98
addSpanProcessor ( spanProcessor : SpanProcessor ) : void {
99
+ if ( this . _registeredSpanProcessors . length === 0 ) {
100
+ // since we might have enabled by default a batchProcessor, we disable it
101
+ // before adding the new one
102
+ this . activeSpanProcessor
103
+ . shutdown ( )
104
+ . catch ( err =>
105
+ diag . error (
106
+ 'Error while trying to shutdown current span processor' ,
107
+ err
108
+ )
109
+ ) ;
110
+ }
83
111
this . _registeredSpanProcessors . push ( spanProcessor ) ;
84
112
this . activeSpanProcessor = new MultiSpanProcessor (
85
113
this . _registeredSpanProcessors
@@ -120,6 +148,10 @@ export class BasicTracerProvider implements TracerProvider {
120
148
return BasicTracerProvider . _registeredPropagators . get ( name ) ?.( ) ;
121
149
}
122
150
151
+ protected _getSpanExporter ( name : string ) : SpanExporter | undefined {
152
+ return BasicTracerProvider . _registeredExporters . get ( name ) ?.( ) ;
153
+ }
154
+
123
155
protected _buildPropagatorFromEnv ( ) : TextMapPropagator | undefined {
124
156
// per spec, propagators from env must be deduplicated
125
157
const uniquePropagatorNames = Array . from (
@@ -156,4 +188,16 @@ export class BasicTracerProvider implements TracerProvider {
156
188
} ) ;
157
189
}
158
190
}
191
+
192
+ protected _buildExporterFromEnv ( ) : SpanExporter | undefined {
193
+ const exporterName = getEnv ( ) . OTEL_TRACES_EXPORTER ;
194
+ if ( exporterName === 'none' ) return ;
195
+ const exporter = this . _getSpanExporter ( exporterName ) ;
196
+ if ( ! exporter ) {
197
+ diag . error (
198
+ `Exporter "${ exporterName } " requested through environment variable is unavailable.`
199
+ ) ;
200
+ }
201
+ return exporter ;
202
+ }
159
203
}
0 commit comments