@@ -22,6 +22,8 @@ import * as util from 'util';
2222import { TraceCLSConfig , TraceCLSMechanism } from '../src/cls' ;
2323
2424import * as testTraceModule from './trace' ;
25+ import { NormalizedConfig } from '../src/tracing' ;
26+ import { StackdriverTracer } from '../src/trace-api' ;
2527
2628describe ( 'Behavior set by config for context propagation mechanism' , ( ) => {
2729 const useAH = semver . satisfies ( process . version , '>=8' ) ;
@@ -90,3 +92,64 @@ describe('Behavior set by config for context propagation mechanism', () => {
9092 } ) ;
9193 }
9294} ) ;
95+
96+ describe ( 'Behavior set by config for overriding root span name' , ( ) => {
97+ let capturedConfig : NormalizedConfig | null ;
98+
99+ class CaptureConfigTestTracing extends testTraceModule . TestTracing {
100+ constructor ( config : NormalizedConfig , traceAgent : StackdriverTracer ) {
101+ super ( config , traceAgent ) ;
102+ // Capture the config object passed into this constructor.
103+ capturedConfig = config ;
104+ }
105+ }
106+
107+ beforeEach ( ( ) => {
108+ capturedConfig = null ;
109+ } ) ;
110+
111+ before ( ( ) => {
112+ testTraceModule . setTracingForTest ( CaptureConfigTestTracing ) ;
113+ } ) ;
114+
115+ after ( ( ) => {
116+ testTraceModule . setTracingForTest ( testTraceModule . TestTracing ) ;
117+ } ) ;
118+
119+ it ( 'should convert a string to a function' , ( ) => {
120+ testTraceModule . start ( {
121+ rootSpanNameOverride : 'hello'
122+ } ) ;
123+ assert . ok ( capturedConfig ! ) ;
124+ // Avoid using the ! operator multiple times.
125+ const config = capturedConfig ! ;
126+ // If !config.enabled, then TSC does not permit access to other fields on
127+ // config. So use this structure instead of assert.ok(config.enabled).
128+ if ( config . enabled ) {
129+ assert . strictEqual ( typeof config . rootSpanNameOverride , 'function' ) ;
130+ assert . strictEqual ( config . rootSpanNameOverride ( '' ) , 'hello' ) ;
131+ } else {
132+ assert . fail ( 'Configuration was not enabled.' ) ;
133+ }
134+ } ) ;
135+
136+ it ( 'should convert a non-string, non-function to the identity fn' , ( ) => {
137+ testTraceModule . start ( {
138+ // We should make sure passing in unsupported values at least doesn't
139+ // result in a crash.
140+ // tslint:disable-next-line:no-any
141+ rootSpanNameOverride : 2 as any
142+ } ) ;
143+ assert . ok ( capturedConfig ! ) ;
144+ // Avoid using the ! operator multiple times.
145+ const config = capturedConfig ! ;
146+ // If !config.enabled, then TSC does not permit access to other fields on
147+ // config. So use this structure instead of assert.ok(config.enabled).
148+ if ( config . enabled ) {
149+ assert . strictEqual ( typeof config . rootSpanNameOverride , 'function' ) ;
150+ assert . strictEqual ( config . rootSpanNameOverride ( 'a' ) , 'a' ) ;
151+ } else {
152+ assert . fail ( 'Configuration was not enabled.' ) ;
153+ }
154+ } ) ;
155+ } ) ;
0 commit comments