@@ -25,7 +25,7 @@ import { NormalizedConfig } from '../src/tracing';
2525import { StackdriverTracer } from '../src/trace-api' ;
2626import { Logger } from '../src/logger' ;
2727
28- describe ( 'Behavior set by config for context propagation mechanism ' , ( ) => {
28+ describe ( 'Behavior set by config for CLS ' , ( ) => {
2929 const useAH = semver . satisfies ( process . version , '>=8' ) ;
3030 const autoMechanism =
3131 useAH ? TraceCLSMechanism . ASYNC_HOOKS : TraceCLSMechanism . ASYNC_LISTENER ;
@@ -93,9 +93,25 @@ describe('Behavior set by config for context propagation mechanism', () => {
9393 }
9494} ) ;
9595
96- describe ( 'Behavior set by config for overriding root span name ' , ( ) => {
96+ describe ( 'Behavior set by config for Tracer ' , ( ) => {
9797 let capturedConfig : NormalizedConfig | null ;
9898
99+ // Convenience function to assert properties of capturedConfig that we want
100+ // to be true on every test, and return an object with a conveniently
101+ // sanitized type.
102+ const getCapturedConfig = ( ) => {
103+ assert . ok ( capturedConfig ) ;
104+ const config = capturedConfig ! ;
105+ // If !config.enabled, then TSC does not permit access to other fields on
106+ // config. So use this structure instead of assert.ok(config.enabled).
107+ if ( config . enabled ) {
108+ return config ;
109+ } else {
110+ assert . fail ( 'Configuration was not enabled.' ) ;
111+ throw new Error ( ) ; // unreachable.
112+ }
113+ } ;
114+
99115 class CaptureConfigTestTracing extends testTraceModule . TestTracing {
100116 constructor ( config : NormalizedConfig , traceAgent : StackdriverTracer ) {
101117 super ( config , traceAgent ) ;
@@ -116,40 +132,67 @@ describe('Behavior set by config for overriding root span name', () => {
116132 testTraceModule . setTracingForTest ( testTraceModule . TestTracing ) ;
117133 } ) ;
118134
119- it ( 'should convert a string to a function' , ( ) => {
120- testTraceModule . start ( {
121- rootSpanNameOverride : 'hello'
135+
136+ describe ( 'Context header behavior' , ( ) => {
137+ it ( 'should copy over an explicitly-set value' , ( ) => {
138+ testTraceModule . start ( {
139+ contextHeaderBehavior : 'require'
140+ } ) ;
141+ const config = getCapturedConfig ( ) ;
142+ assert . strictEqual ( config . contextHeaderBehavior , 'require' ) ;
143+ } ) ;
144+
145+ it ( 'should respect the value of ignoreContextHeader if not set' , ( ) => {
146+ testTraceModule . start ( {
147+ ignoreContextHeader : false
148+ } ) ;
149+ let config = getCapturedConfig ( ) ;
150+ assert . strictEqual ( config . contextHeaderBehavior , 'default' ) ;
151+ capturedConfig = null ;
152+ testTraceModule . start ( {
153+ ignoreContextHeader : true
154+ } ) ;
155+ config = getCapturedConfig ( ) ;
156+ assert . strictEqual ( config . contextHeaderBehavior , 'ignore' ) ;
157+ } ) ;
158+
159+ it ( 'should override the value of ignoreContextHeader if both set' , ( ) => {
160+ testTraceModule . start ( {
161+ ignoreContextHeader : false ,
162+ contextHeaderBehavior : 'require'
163+ } ) ;
164+ let config = getCapturedConfig ( ) ;
165+ assert . strictEqual ( config . contextHeaderBehavior , 'require' ) ;
166+ capturedConfig = null ;
167+ testTraceModule . start ( {
168+ ignoreContextHeader : true ,
169+ contextHeaderBehavior : 'require'
170+ } ) ;
171+ config = getCapturedConfig ( ) ;
172+ assert . strictEqual ( config . contextHeaderBehavior , 'require' ) ;
122173 } ) ;
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- }
134174 } ) ;
135175
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
176+ describe ( 'Overriding root span name' , ( ) => {
177+ it ( 'should convert a string to a function' , ( ) => {
178+ testTraceModule . start ( {
179+ rootSpanNameOverride : 'hello'
180+ } ) ;
181+ const config = getCapturedConfig ( ) ;
182+ assert . strictEqual ( typeof config . rootSpanNameOverride , 'function' ) ;
183+ assert . strictEqual ( config . rootSpanNameOverride ( '' ) , 'hello' ) ;
142184 } ) ;
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 ) {
185+
186+ it ( 'should convert a non-string, non-function to the identity fn' , ( ) => {
187+ testTraceModule . start ( {
188+ // We should make sure passing in unsupported values at least doesn't
189+ // result in a crash.
190+ // tslint:disable-next-line:no-any
191+ rootSpanNameOverride : 2 as any
192+ } ) ;
193+ const config = getCapturedConfig ( ) ;
149194 assert . strictEqual ( typeof config . rootSpanNameOverride , 'function' ) ;
150195 assert . strictEqual ( config . rootSpanNameOverride ( 'a' ) , 'a' ) ;
151- } else {
152- assert . fail ( 'Configuration was not enabled.' ) ;
153- }
196+ } ) ;
154197 } ) ;
155198} ) ;
0 commit comments