@@ -52,6 +52,9 @@ export class TracerBase implements webTypes.TracerBase {
5252 */
5353 readonly activeTraceParams = { } ;
5454
55+ /** A configuration for starting the tracer */
56+ private config : webTypes . TracerConfig = { } ;
57+
5558 /**
5659 * Starts the tracer. This makes the tracer active and sets `logger` and
5760 * `propagation` based on the given config. The `samplingRate` property of
@@ -60,6 +63,7 @@ export class TracerBase implements webTypes.TracerBase {
6063 start ( config : webTypes . TracerConfig ) : this {
6164 this . logger = config . logger || console ;
6265 this . propagation = config . propagation || NO_HEADERS_PROPAGATION ;
66+ this . config = config ;
6367 return this ;
6468 }
6569
@@ -76,6 +80,13 @@ export class TracerBase implements webTypes.TracerBase {
7680 */
7781 startRootSpan < T > ( options : webTypes . TraceOptions , fn : ( root : Span ) => T ) : T {
7882 const rootSpan = new RootSpan ( this , options ) ;
83+ // Add default attributes
84+ const defaultAttributes = this . config && this . config . defaultAttributes ;
85+ if ( defaultAttributes ) {
86+ for ( const key of Object . keys ( defaultAttributes ) ) {
87+ rootSpan . addAttribute ( key , defaultAttributes [ key ] ) ;
88+ }
89+ }
7990 rootSpan . start ( ) ;
8091 return fn ( rootSpan ) ;
8192 }
@@ -108,10 +119,18 @@ export class TracerBase implements webTypes.TracerBase {
108119 * @returns The new Span instance started
109120 */
110121 startChildSpan ( options ?: webTypes . SpanOptions ) : Span {
111- let span = new Span ( ) ;
122+ let rootSpan = new Span ( ) ;
112123 if ( options && options . childOf ) {
113- span = options . childOf as Span ;
124+ rootSpan = options . childOf as Span ;
125+ }
126+ const span = rootSpan . startChildSpan ( options ) ;
127+ // Add default attributes
128+ const defaultAttributes = this . config && this . config . defaultAttributes ;
129+ if ( defaultAttributes ) {
130+ for ( const key of Object . keys ( defaultAttributes ) ) {
131+ rootSpan . addAttribute ( key , defaultAttributes [ key ] ) ;
132+ }
114133 }
115- return span . startChildSpan ( options ) ;
134+ return span ;
116135 }
117136}
0 commit comments