@@ -74,30 +74,33 @@ describe('with a testing otel span collector', () => {
7474 const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
7575 expect ( spanEvent . name ) . toEqual ( 'feature_flag' ) ;
7676 expect ( spanEvent . attributes ! [ 'feature_flag.key' ] ) . toEqual ( 'test-bool' ) ;
77- expect ( spanEvent . attributes ! [ 'feature_flag.provider_name ' ] ) . toEqual ( 'LaunchDarkly' ) ;
78- expect ( spanEvent . attributes ! [ 'feature_flag.context.key ' ] ) . toEqual ( 'user-key' ) ;
79- expect ( spanEvent . attributes ! [ 'feature_flag.variant ' ] ) . toBeUndefined ( ) ;
77+ expect ( spanEvent . attributes ! [ 'feature_flag.provider.name ' ] ) . toEqual ( 'LaunchDarkly' ) ;
78+ expect ( spanEvent . attributes ! [ 'feature_flag.context.id ' ] ) . toEqual ( 'user-key' ) ;
79+ expect ( spanEvent . attributes ! [ 'feature_flag.result.value ' ] ) . toBeUndefined ( ) ;
8080 expect ( spanEvent . attributes ! [ 'feature_flag.set.id' ] ) . toBeUndefined ( ) ;
8181 } ) ;
8282
83- it ( 'can include variant in span events' , async ( ) => {
84- const td = new integrations . TestData ( ) ;
85- const client = init ( 'bad-key' , {
86- sendEvents : false ,
87- updateProcessor : td . getFactory ( ) ,
88- hooks : [ new TracingHook ( { includeVariant : true } ) ] ,
89- } ) ;
83+ it . each ( [ 'includeVariant' , 'includeValue' ] ) (
84+ 'can include value in span events' ,
85+ async ( optKey ) => {
86+ const td = new integrations . TestData ( ) ;
87+ const client = init ( 'bad-key' , {
88+ sendEvents : false ,
89+ updateProcessor : td . getFactory ( ) ,
90+ hooks : [ new TracingHook ( { [ optKey ] : true } ) ] ,
91+ } ) ;
9092
91- const tracer = trace . getTracer ( 'trace-hook-test-tracer' ) ;
92- await tracer . startActiveSpan ( 'test-span' , { root : true } , async ( span ) => {
93- await client . boolVariation ( 'test-bool' , { kind : 'user' , key : 'user-key' } , false ) ;
94- span . end ( ) ;
95- } ) ;
93+ const tracer = trace . getTracer ( 'trace-hook-test-tracer' ) ;
94+ await tracer . startActiveSpan ( 'test-span' , { root : true } , async ( span ) => {
95+ await client . boolVariation ( 'test-bool' , { kind : 'user' , key : 'user-key' } , false ) ;
96+ span . end ( ) ;
97+ } ) ;
9698
97- const spans = spanExporter . getFinishedSpans ( ) ;
98- const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
99- expect ( spanEvent . attributes ! [ 'feature_flag.variant' ] ) . toEqual ( 'false' ) ;
100- } ) ;
99+ const spans = spanExporter . getFinishedSpans ( ) ;
100+ const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
101+ expect ( spanEvent . attributes ! [ 'feature_flag.result.value' ] ) . toEqual ( 'false' ) ;
102+ } ,
103+ ) ;
101104
102105 it ( 'can include variation spans' , async ( ) => {
103106 const td = new integrations . TestData ( ) ;
@@ -116,7 +119,7 @@ describe('with a testing otel span collector', () => {
116119 const spans = spanExporter . getFinishedSpans ( ) ;
117120 const variationSpan = spans [ 0 ] ;
118121 expect ( variationSpan . name ) . toEqual ( 'LDClient.boolVariation' ) ;
119- expect ( variationSpan . attributes [ 'feature_flag.context.key ' ] ) . toEqual ( 'user-key' ) ;
122+ expect ( variationSpan . attributes [ 'feature_flag.context.id ' ] ) . toEqual ( 'user-key' ) ;
120123 } ) ;
121124
122125 it ( 'can handle multi-context key requirements' , async ( ) => {
@@ -139,7 +142,7 @@ describe('with a testing otel span collector', () => {
139142
140143 const spans = spanExporter . getFinishedSpans ( ) ;
141144 const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
142- expect ( spanEvent . attributes ! [ 'feature_flag.context.key ' ] ) . toEqual ( 'org:org-key:user:bob' ) ;
145+ expect ( spanEvent . attributes ! [ 'feature_flag.context.id ' ] ) . toEqual ( 'org:org-key:user:bob' ) ;
143146 } ) ;
144147
145148 it ( 'can include environmentId from options' , async ( ) => {
@@ -218,4 +221,102 @@ describe('with a testing otel span collector', () => {
218221 const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
219222 expect ( spanEvent . attributes ! [ 'feature_flag.set.id' ] ) . toEqual ( 'id-from-options' ) ;
220223 } ) ;
224+
225+ it ( 'includes inExperiment attribute in span events' , async ( ) => {
226+ const td = new integrations . TestData ( ) ;
227+ td . usePreconfiguredFlag ( {
228+ key : 'test-bool' ,
229+ version : 1 ,
230+ on : true ,
231+ targets : [ ] ,
232+ rules : [ ] ,
233+ fallthrough : {
234+ rollout : {
235+ kind : 'experiment' ,
236+ variations : [
237+ {
238+ weight : 100000 ,
239+ variation : 0 ,
240+ } ,
241+ ] ,
242+ } ,
243+ } ,
244+ variations : [ true , false ] ,
245+ } ) ;
246+ const client = init ( 'bad-key' , {
247+ sendEvents : false ,
248+ updateProcessor : td . getFactory ( ) ,
249+ hooks : [ new TracingHook ( ) ] ,
250+ } ) ;
251+
252+ const tracer = trace . getTracer ( 'trace-hook-test-tracer' ) ;
253+ await tracer . startActiveSpan ( 'test-span' , { root : true } , async ( span ) => {
254+ await client . boolVariation ( 'test-bool' , { kind : 'user' , key : 'user-key' } , false ) ;
255+ span . end ( ) ;
256+ } ) ;
257+
258+ const spans = spanExporter . getFinishedSpans ( ) ;
259+ const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
260+ expect ( spanEvent . attributes ! [ 'feature_flag.result.reason.inExperiment' ] ) . toEqual ( true ) ;
261+ } ) ;
262+
263+ it ( 'includes variationIndex attribute in span events' , async ( ) => {
264+ const td = new integrations . TestData ( ) ;
265+ td . usePreconfiguredFlag ( {
266+ key : 'test-bool' ,
267+ version : 1 ,
268+ on : true ,
269+ targets : [ ] ,
270+ rules : [ ] ,
271+ fallthrough : {
272+ variation : 1 ,
273+ } ,
274+ variations : [ true , false ] ,
275+ } ) ;
276+ const client = init ( 'bad-key' , {
277+ sendEvents : false ,
278+ updateProcessor : td . getFactory ( ) ,
279+ hooks : [ new TracingHook ( ) ] ,
280+ } ) ;
281+
282+ const tracer = trace . getTracer ( 'trace-hook-test-tracer' ) ;
283+ await tracer . startActiveSpan ( 'test-span' , { root : true } , async ( span ) => {
284+ await client . boolVariation ( 'test-bool' , { kind : 'user' , key : 'user-key' } , false ) ;
285+ span . end ( ) ;
286+ } ) ;
287+
288+ const spans = spanExporter . getFinishedSpans ( ) ;
289+ const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
290+ expect ( spanEvent . attributes ! [ 'feature_flag.result.variationIndex' ] ) . toEqual ( 1 ) ;
291+ } ) ;
292+
293+ it ( 'does not include inExperiment attribute when not in experiment' , async ( ) => {
294+ const td = new integrations . TestData ( ) ;
295+ td . usePreconfiguredFlag ( {
296+ key : 'test-bool' ,
297+ version : 1 ,
298+ on : true ,
299+ targets : [ ] ,
300+ rules : [ ] ,
301+ fallthrough : {
302+ variation : 0 ,
303+ } ,
304+ variations : [ true , false ] ,
305+ } ) ;
306+ const client = init ( 'bad-key' , {
307+ sendEvents : false ,
308+ updateProcessor : td . getFactory ( ) ,
309+ hooks : [ new TracingHook ( ) ] ,
310+ } ) ;
311+
312+ const tracer = trace . getTracer ( 'trace-hook-test-tracer' ) ;
313+ await tracer . startActiveSpan ( 'test-span' , { root : true } , async ( span ) => {
314+ await client . boolVariation ( 'test-bool' , { kind : 'user' , key : 'user-key' } , false ) ;
315+ span . end ( ) ;
316+ } ) ;
317+
318+ const spans = spanExporter . getFinishedSpans ( ) ;
319+ const spanEvent = spans [ 0 ] ! . events [ 0 ] ! ;
320+ expect ( spanEvent . attributes ! [ 'feature_flag.result.reason.inExperiment' ] ) . toBeUndefined ( ) ;
321+ } ) ;
221322} ) ;
0 commit comments