@@ -20,10 +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 { Exporter } from '../../exporters/exporter'
24- import { TraceContext , OnEndSpanEventListener } from '../types/tracetypes' ;
25- import { TracerConfig , defaultConfig } from '../tracing'
26- import { Buffer } from '../../exporters/buffer'
23+ import { TraceContext , TraceOptions , OnEndSpanEventListener } from '../types/tracetypes' ;
24+ import { TracerConfig , defaultConfig } from '../tracing' ;
2725import { Sampler } from './sampler'
2826
2927export type Func < T > = ( ...args : any [ ] ) => T ;
@@ -74,18 +72,19 @@ export class Tracer implements OnEndSpanEventListener {
7472 return this . _active ;
7573 }
7674
77- public startRootSpan ( context ?: TraceContext , sampler ?: Sampler ) : RootSpan {
78- let newTrace = new RootSpan ( this , context ) ;
79- debug ( "tracer startRootSpan " )
80- this . setCurrentRootSpan ( newTrace ) ;
81- if ( sampler == null ) {
82- sampler = new Sampler ( newTrace . traceId ) ;
83- sampler . always ( ) ;
84- }
85- debug ( "tracer startRootSpan " )
86- newTrace . sampler = sampler ;
87- newTrace . start ( ) ;
88- return newTrace ;
75+ public startRootSpan < T > ( options : TraceOptions , fn : ( root : RootSpan ) => T ) : T {
76+ debug ( 'starting root span: %o' , options )
77+ return this . contextManager . runAndReturn ( ( root ) => {
78+ let newRoot = new RootSpan ( this , options ) ;
79+ this . setCurrentRootSpan ( newRoot ) ;
80+ if ( sampler == null ) {
81+ sampler = new Sampler ( newTrace . traceId ) ;
82+ sampler . always ( ) ;
83+ }
84+ newRoot . sampler = sampler ;
85+ newRoot . start ( ) ;
86+ return fn ( newRoot ) ;
87+ } ) ;
8988 }
9089
9190
@@ -100,10 +99,6 @@ export class Tracer implements OnEndSpanEventListener {
10099 //this.clearCurrentTrace();
101100 }
102101
103- //TODO: review
104- public runInContex < T > ( fn : Func < T > ) : T {
105- return this . contextManager . runAndReturn ( fn )
106- }
107102
108103 public registerEndSpanListener ( listener : OnEndSpanEventListener ) {
109104 this . eventListeners . push ( listener ) ;
0 commit comments