@@ -20,8 +20,8 @@ import { Span } from './span'
2020import { debug } from '../../internal/util'
2121import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222import { StackdriverOptions } from '../../exporters/stackdriver/options'
23- import { TraceContext , OnEndSpanEventListener } from '../types/tracetypes' ;
24- import { TracerConfig , defaultConfig } from '../tracing' ;
23+ import { TraceContext , TraceOptions , OnEndSpanEventListener } from '../types/tracetypes' ;
24+ import { TracerConfig , defaultConfig } from '../tracing' ;
2525
2626export type Func < T > = ( ...args : any [ ] ) => T ;
2727
@@ -66,36 +66,36 @@ export class Tracer implements OnEndSpanEventListener {
6666 return this . _active ;
6767 }
6868
69- public startRootSpan ( context ?: TraceContext ) : RootSpan {
70- let newTrace = new RootSpan ( this , context ) ;
71- this . setCurrentRootSpan ( newTrace ) ;
72- newTrace . start ( ) ;
73- return newTrace ;
69+ public startRootSpan < T > ( options : TraceOptions , fn : ( root : RootSpan ) => T ) : T {
70+ debug ( 'starting root span: %o' , options )
71+ return this . contextManager . runAndReturn ( ( root ) => {
72+ let newRoot = new RootSpan ( this , options ) ;
73+ this . setCurrentRootSpan ( newRoot ) ;
74+ newRoot . start ( ) ;
75+ return fn ( newRoot ) ;
76+ } ) ;
7477 }
7578
7679
7780 public onEndSpan ( root :RootSpan ) : void {
78- if ( ! this . currentRootSpan ) {
81+ if ( ! root ) {
7982 return debug ( 'cannot end trace - no active trace found' )
8083 }
8184 if ( this . currentRootSpan != root ) {
82- return debug ( 'currentRootSpan != root on notifyEnd. Possbile implementation bug .' )
85+ debug ( 'currentRootSpan != root on notifyEnd. Need more investigation .' )
8386 }
84- this . notifyEndSpan ( this . currentRootSpan ) ;
87+ this . notifyEndSpan ( root ) ;
8588 //this.clearCurrentTrace();
8689 }
8790
88- //TODO: review
89- public runInContex < T > ( fn : Func < T > ) : T {
90- return this . contextManager . runAndReturn ( fn )
91- }
9291
9392 public registerEndSpanListener ( listner : OnEndSpanEventListener ) {
9493 this . eventListeners . push ( listner ) ;
9594 }
9695
9796 private notifyEndSpan ( root : RootSpan ) {
9897 if ( this . active ) {
98+ debug ( 'starting to notify listeners the end of rootspans' )
9999 if ( this . eventListeners && this . eventListeners . length > 0 ) {
100100 this . eventListeners . forEach ( ( listener ) => listener . onEndSpan ( root ) )
101101 }
0 commit comments