@@ -12,8 +12,10 @@ import {
1212 EventHubConsumerClient ,
1313 EventHubProducerClient ,
1414 EventPosition ,
15+ OperationOptions ,
1516 ReceivedEventData ,
16- SendBatchOptions
17+ SendBatchOptions ,
18+ TryAddOptions
1719} from "../../src" ;
1820import {
1921 EnvVarKeys ,
@@ -355,123 +357,134 @@ describe("EventHub Sender", function(): void {
355357 resetTracer ( ) ;
356358 } ) ;
357359
358- it ( "will not instrument already instrumented events" , async function ( ) : Promise < void > {
359- const { tracer, resetTracer } = setTracerForTest ( ) ;
360+ function legacyOptionsUsingSpanContext ( rootSpan : TestSpan ) : Pick < TryAddOptions , "parentSpan" > {
361+ return {
362+ parentSpan : rootSpan . context ( )
363+ } ;
364+ }
360365
361- const rootSpan = tracer . startSpan ( "test" ) ;
366+ function legacyOptionsUsingSpan ( rootSpan : TestSpan ) : Pick < TryAddOptions , "parentSpan" > {
367+ return {
368+ parentSpan : rootSpan
369+ } ;
370+ }
362371
363- const list = [
364- { name : "Albert" } ,
365- {
366- name : "Marie" ,
367- properties : {
368- [ TRACEPARENT_PROPERTY ] : "foo"
372+ function modernOptions ( rootSpan : TestSpan ) : OperationOptions {
373+ return {
374+ tracingOptions : {
375+ spanOptions : {
376+ parent : rootSpan . context ( )
369377 }
370378 }
371- ] ;
379+ } ;
380+ }
372381
373- const eventDataBatch = await producerClient . createBatch ( {
374- partitionId : "0"
375- } ) ;
382+ [ legacyOptionsUsingSpan , legacyOptionsUsingSpanContext , modernOptions ] . forEach ( ( optionsFn ) => {
383+ describe ( `tracing (${ optionsFn . name } )` , ( ) => {
384+ it ( "will not instrument already instrumented events" , async function ( ) : Promise < void > {
385+ const { tracer, resetTracer } = setTracerForTest ( ) ;
376386
377- for ( let i = 0 ; i < 2 ; i ++ ) {
378- eventDataBatch . tryAdd (
379- { body : `${ list [ i ] . name } ` , properties : list [ i ] . properties } ,
380- {
381- tracingOptions : {
382- spanOptions : {
383- parent : rootSpan . context ( )
387+ const rootSpan = tracer . startSpan ( "test" ) ;
388+
389+ const list = [
390+ { name : "Albert" } ,
391+ {
392+ name : "Marie" ,
393+ properties : {
394+ [ TRACEPARENT_PROPERTY ] : "foo"
384395 }
385396 }
397+ ] ;
398+
399+ const eventDataBatch = await producerClient . createBatch ( {
400+ partitionId : "0"
401+ } ) ;
402+
403+ for ( let i = 0 ; i < 2 ; i ++ ) {
404+ eventDataBatch . tryAdd (
405+ { body : `${ list [ i ] . name } ` , properties : list [ i ] . properties } ,
406+ optionsFn ( rootSpan )
407+ ) ;
386408 }
387- ) ;
388- }
389- await producerClient . sendBatch ( eventDataBatch ) ;
390- rootSpan . end ( ) ;
409+ await producerClient . sendBatch ( eventDataBatch ) ;
410+ rootSpan . end ( ) ;
391411
392- const rootSpans = tracer . getRootSpans ( ) ;
393- rootSpans . length . should . equal ( 2 , "Should only have two root spans." ) ;
394- rootSpans [ 0 ] . should . equal ( rootSpan , "The root span should match what was passed in." ) ;
412+ const rootSpans = tracer . getRootSpans ( ) ;
413+ rootSpans . length . should . equal ( 2 , "Should only have two root spans." ) ;
414+ rootSpans [ 0 ] . should . equal ( rootSpan , "The root span should match what was passed in." ) ;
395415
396- const expectedGraph : SpanGraph = {
397- roots : [
398- {
399- name : rootSpan . name ,
400- children : [
416+ const expectedGraph : SpanGraph = {
417+ roots : [
401418 {
402- name : "Azure.EventHubs.message" ,
403- children : [ ]
419+ name : rootSpan . name ,
420+ children : [
421+ {
422+ name : "Azure.EventHubs.message" ,
423+ children : [ ]
424+ }
425+ ]
404426 }
405427 ]
406- }
407- ]
408- } ;
428+ } ;
409429
410- tracer . getSpanGraph ( rootSpan . context ( ) . traceId ) . should . eql ( expectedGraph ) ;
411- tracer . getActiveSpans ( ) . length . should . equal ( 0 , "All spans should have had end called." ) ;
412- resetTracer ( ) ;
413- } ) ;
430+ tracer . getSpanGraph ( rootSpan . context ( ) . traceId ) . should . eql ( expectedGraph ) ;
431+ tracer . getActiveSpans ( ) . length . should . equal ( 0 , "All spans should have had end called." ) ;
432+ resetTracer ( ) ;
433+ } ) ;
414434
415- it ( "will support tracing batch and send" , async function ( ) : Promise < void > {
416- const { tracer, resetTracer } = setTracerForTest ( ) ;
435+ it ( "will support tracing batch and send" , async function ( ) : Promise < void > {
436+ const { tracer, resetTracer } = setTracerForTest ( ) ;
417437
418- const rootSpan = tracer . startSpan ( "root" ) ;
438+ const rootSpan = tracer . startSpan ( "root" ) ;
419439
420- const list = [ { name : "Albert" } , { name : "Marie" } ] ;
440+ const list = [ { name : "Albert" } , { name : "Marie" } ] ;
421441
422- const eventDataBatch = await producerClient . createBatch ( {
423- partitionId : "0"
424- } ) ;
425- for ( let i = 0 ; i < 2 ; i ++ ) {
426- eventDataBatch . tryAdd (
427- { body : ` ${ list [ i ] . name } ` } ,
428- {
442+ const eventDataBatch = await producerClient . createBatch ( {
443+ partitionId : "0"
444+ } ) ;
445+ for ( let i = 0 ; i < 2 ; i ++ ) {
446+ eventDataBatch . tryAdd ( { body : ` ${ list [ i ] . name } ` } , optionsFn ( rootSpan ) ) ;
447+ }
448+ await producerClient . sendBatch ( eventDataBatch , {
429449 tracingOptions : {
430450 spanOptions : {
431451 parent : rootSpan . context ( )
432452 }
433453 }
434- }
435- ) ;
436- }
437- await producerClient . sendBatch ( eventDataBatch , {
438- tracingOptions : {
439- spanOptions : {
440- parent : rootSpan . context ( )
441- }
442- }
443- } ) ;
444- rootSpan . end ( ) ;
454+ } ) ;
455+ rootSpan . end ( ) ;
445456
446- const rootSpans = tracer . getRootSpans ( ) ;
447- rootSpans . length . should . equal ( 1 , "Should only have one root span." ) ;
448- rootSpans [ 0 ] . should . equal ( rootSpan , "The root span should match what was passed in." ) ;
457+ const rootSpans = tracer . getRootSpans ( ) ;
458+ rootSpans . length . should . equal ( 1 , "Should only have one root span." ) ;
459+ rootSpans [ 0 ] . should . equal ( rootSpan , "The root span should match what was passed in." ) ;
449460
450- const expectedGraph : SpanGraph = {
451- roots : [
452- {
453- name : rootSpan . name ,
454- children : [
455- {
456- name : "Azure.EventHubs.message" ,
457- children : [ ]
458- } ,
459- {
460- name : "Azure.EventHubs.message" ,
461- children : [ ]
462- } ,
461+ const expectedGraph : SpanGraph = {
462+ roots : [
463463 {
464- name : "Azure.EventHubs.send" ,
465- children : [ ]
464+ name : rootSpan . name ,
465+ children : [
466+ {
467+ name : "Azure.EventHubs.message" ,
468+ children : [ ]
469+ } ,
470+ {
471+ name : "Azure.EventHubs.message" ,
472+ children : [ ]
473+ } ,
474+ {
475+ name : "Azure.EventHubs.send" ,
476+ children : [ ]
477+ }
478+ ]
466479 }
467480 ]
468- }
469- ]
470- } ;
481+ } ;
471482
472- tracer . getSpanGraph ( rootSpan . context ( ) . traceId ) . should . eql ( expectedGraph ) ;
473- tracer . getActiveSpans ( ) . length . should . equal ( 0 , "All spans should have had end called." ) ;
474- resetTracer ( ) ;
483+ tracer . getSpanGraph ( rootSpan . context ( ) . traceId ) . should . eql ( expectedGraph ) ;
484+ tracer . getActiveSpans ( ) . length . should . equal ( 0 , "All spans should have had end called." ) ;
485+ resetTracer ( ) ;
486+ } ) ;
487+ } ) ;
475488 } ) ;
476489
477490 it ( "with partition key should be sent successfully." , async function ( ) : Promise < void > {
0 commit comments