@@ -5,18 +5,12 @@ const assert = require('node:assert/strict')
55const { describe, it, beforeEach } = require ( 'mocha' )
66const { context, propagation, trace, ROOT_CONTEXT } = require ( '@opentelemetry/api' )
77const api = require ( '@opentelemetry/api' )
8+ const { getAllBaggageItems, setBaggageItem, removeBaggageItem } = require ( '../../src/baggage' )
89
910require ( '../setup/core' )
1011const ContextManager = require ( '../../src/opentelemetry/context_manager' )
1112const TracerProvider = require ( '../../src/opentelemetry/tracer_provider' )
12- const tracer = require ( '../../' ) . init ( )
13-
14- function makeSpan ( ...args ) {
15- const tracerProvider = new TracerProvider ( )
16- tracerProvider . register ( )
17- const tracer = tracerProvider . getTracer ( )
18- return tracer . startSpan ( ...args )
19- }
13+ require ( '../../' ) . init ( )
2014
2115function getTracer ( ) {
2216 const tracerProvider = new TracerProvider ( )
@@ -138,63 +132,46 @@ describe('OTel Context Manager', () => {
138132 }
139133 const baggage = propagation . createBaggage ( entries )
140134 const contextWithBaggage = propagation . setBaggage ( context . active ( ) , baggage )
141- const span = makeSpan ( 'otel-to-dd' )
142- const contextWithSpan = trace . setSpan ( contextWithBaggage , span )
143- api . context . with ( contextWithSpan , ( ) => {
144- assert . strictEqual ( tracer . scope ( ) . active ( ) . getBaggageItem ( 'foo' ) , 'bar' )
135+ api . context . with ( contextWithBaggage , ( ) => {
136+ assert . deepStrictEqual ( getAllBaggageItems ( ) , { foo : 'bar' } )
145137 } )
146138 } )
147139
148140 it ( 'should propagate baggage from a datadog span to an otel span' , ( ) => {
149- const baggageKey = 'raccoon'
150- const baggageVal = 'chunky'
151- const ddSpan = tracer . startSpan ( 'dd-to-otel' )
152- ddSpan . setBaggageItem ( baggageKey , baggageVal )
153- tracer . scope ( ) . activate ( ddSpan , ( ) => {
154- const baggages = propagation . getActiveBaggage ( ) . getAllEntries ( )
155- assert . strictEqual ( baggages . length , 1 )
156- const baggage = baggages [ 0 ]
157- assert . strictEqual ( baggage [ 0 ] , baggageKey )
158- assert . strictEqual ( baggage [ 1 ] . value , baggageVal )
159- } )
141+ setBaggageItem ( 'raccoon' , 'chunky' )
142+ assert . deepStrictEqual ( propagation . getActiveBaggage ( ) . getAllEntries ( ) ,
143+ [ [ 'raccoon' , { value : 'chunky' } ] ]
144+ )
160145 } )
161146
162147 it ( 'should handle dd-otel baggage conflict' , ( ) => {
163- const ddSpan = tracer . startSpan ( 'dd' )
164- ddSpan . setBaggageItem ( 'key1' , 'dd1' )
165- let contextWithUpdatedBaggages
166- tracer . scope ( ) . activate ( ddSpan , ( ) => {
167- let baggages = propagation . getBaggage ( api . context . active ( ) )
168- baggages = baggages . setEntry ( 'key1' , { value : 'otel1' } )
169- baggages = baggages . setEntry ( 'key2' , { value : 'otel2' } )
170- contextWithUpdatedBaggages = propagation . setBaggage ( api . context . active ( ) , baggages )
171- } )
172- assert . deepStrictEqual ( JSON . parse ( ddSpan . getAllBaggageItems ( ) ) , { key1 : 'dd1' } )
148+ setBaggageItem ( 'key1' , 'dd1' )
149+ let baggages = propagation . getActiveBaggage ( )
150+ baggages = baggages . setEntry ( 'key1' , { value : 'otel1' } )
151+ baggages = baggages . setEntry ( 'key2' , { value : 'otel2' } )
152+ const contextWithUpdatedBaggages = propagation . setBaggage ( context . active ( ) , baggages )
153+ assert . deepStrictEqual ( getAllBaggageItems ( ) , { key1 : 'dd1' } )
173154 api . context . with ( contextWithUpdatedBaggages , ( ) => {
174- assert . deepStrictEqual ( JSON . parse ( ddSpan . getAllBaggageItems ( ) ) , { key1 : 'otel1' , key2 : 'otel2' } )
175- ddSpan . setBaggageItem ( 'key2' , 'dd2' )
176- assert . deepStrictEqual ( propagation . getActiveBaggage ( ) . getAllEntries ( ) ,
177- [ [ 'key1' , { value : 'otel1' } ] , [ 'key2' , { value : 'dd2' } ] ]
178- )
155+ assert . deepStrictEqual ( getAllBaggageItems ( ) , { key1 : 'otel1' , key2 : 'otel2' } )
179156 } )
157+ setBaggageItem ( 'key2' , 'dd2' )
158+ assert . deepStrictEqual ( propagation . getActiveBaggage ( ) . getAllEntries ( ) ,
159+ [ [ 'key1' , { value : 'otel1' } ] , [ 'key2' , { value : 'dd2' } ] ]
160+ )
180161 } )
181162
182163 it ( 'should handle dd-otel baggage removal' , ( ) => {
183- const ddSpan = tracer . startSpan ( 'dd' )
184- ddSpan . setBaggageItem ( 'key1' , 'dd1' )
185- ddSpan . setBaggageItem ( 'key2' , 'dd2' )
186- let contextWithUpdatedBaggages
187- tracer . scope ( ) . activate ( ddSpan , ( ) => {
188- let baggages = propagation . getBaggage ( api . context . active ( ) )
189- baggages = baggages . removeEntry ( 'key1' )
190- contextWithUpdatedBaggages = propagation . setBaggage ( api . context . active ( ) , baggages )
191- } )
192- assert . deepStrictEqual ( JSON . parse ( ddSpan . getAllBaggageItems ( ) ) , { key1 : 'dd1' , key2 : 'dd2' } )
164+ setBaggageItem ( 'key1' , 'dd1' )
165+ setBaggageItem ( 'key2' , 'dd2' )
166+ let baggages = propagation . getActiveBaggage ( )
167+ baggages = baggages . removeEntry ( 'key1' )
168+ const contextWithUpdatedBaggages = propagation . setBaggage ( context . active ( ) , baggages )
169+ assert . deepStrictEqual ( getAllBaggageItems ( ) , { key1 : 'dd1' , key2 : 'dd2' } )
193170 api . context . with ( contextWithUpdatedBaggages , ( ) => {
194- assert . deepStrictEqual ( JSON . parse ( ddSpan . getAllBaggageItems ( ) ) , { key2 : 'dd2' } )
195- ddSpan . removeBaggageItem ( 'key2' )
196- assert . deepStrictEqual ( propagation . getActiveBaggage ( ) . getAllEntries ( ) , [ ] )
171+ assert . deepStrictEqual ( getAllBaggageItems ( ) , { key2 : 'dd2' } )
197172 } )
173+ removeBaggageItem ( 'key2' )
174+ assert . deepStrictEqual ( propagation . getActiveBaggage ( ) , undefined )
198175 } )
199176
200177 it ( 'should return active span' , ( ) => {
0 commit comments