@@ -20,24 +20,24 @@ import { Span } from './span'
2020import { debug } from '../../internal/util'
2121import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222import { StackdriverOptions } from '../../exporters/stackdriver/options'
23- import { TraceContext , TraceOptions , OnEndSpanEventListener } from '../types/tracetypes' ;
24- import { TracerConfig , defaultConfig } from '../tracing' ;
2523import { Sampler } from '../config/sampler'
24+ import { TraceContext , TraceOptions , OnEndSpanEventListener , SpanBaseModel } from '../types/tracetypes' ;
25+ import { TracerConfig , defaultConfig } from '../tracing' ;
2626
2727export type Func < T > = ( ...args : any [ ] ) => T ;
2828
2929
3030export class Tracer implements OnEndSpanEventListener {
3131
32- readonly PLUGINS = [ 'http' , 'https' , 'mongodb-core' , 'express' ] ;
32+ readonly PLUGINS = [ 'http' , 'https' , 'mongodb-core' ] ;
3333
3434 //public buffer: Buffer;
3535 private _active : boolean ;
3636 private contextManager : cls . Namespace ;
3737 private config : TracerConfig ;
3838
3939 //TODO: simple solution - to be rewied in future
40- private eventListeners : OnEndSpanEventListener [ ] = [ ] ;
40+ private eventListeners : OnEndSpanEventListener [ ] = [ ] ;
4141 //TODO: temp solution
4242 private endedTraces : RootSpan [ ] = [ ] ;
4343
@@ -76,12 +76,16 @@ export class Tracer implements OnEndSpanEventListener {
7676 return this . contextManager . runAndReturn ( ( root ) => {
7777 let newRoot = new RootSpan ( this , options ) ;
7878 this . setCurrentRootSpan ( newRoot ) ;
79- if ( options . sampler == null ) {
79+ if ( ! options ) {
80+ options = < TraceOptions > { }
81+ }
82+ if ( ! options . sampler ) {
8083 options . sampler = new Sampler ( newRoot . traceId ) ;
84+ //options.sampler.probability(0.5);
8185 options . sampler . always ( ) ;
8286 }
8387 newRoot . sampler = options . sampler ;
84- if ( newRoot . sampler . shouldSample ( newRoot . traceId ) ) {
88+ if ( newRoot . sampler . shouldSample ( newRoot . traceId ) ) {
8589 newRoot . start ( ) ;
8690 return fn ( newRoot ) ;
8791 }
@@ -90,27 +94,25 @@ export class Tracer implements OnEndSpanEventListener {
9094 }
9195
9296
93- public onEndSpan ( root :RootSpan ) : void {
97+ public onEndSpan ( root : RootSpan ) : void {
9498 if ( ! root ) {
9599 return debug ( 'cannot end trace - no active trace found' )
96100 }
97- if ( this . currentRootSpan != root ) {
98- debug ( 'currentRootSpan != root on notifyEnd. Need more investigation.' )
101+ if ( this . currentRootSpan != root ) {
102+ debug ( 'currentRootSpan != root on notifyEnd. Need more investigation.' )
99103 }
100104 this . notifyEndSpan ( root ) ;
101105 //this.clearCurrentTrace();
102106 }
103107
104-
105- public registerEndSpanListener ( listener : OnEndSpanEventListener ) {
106- this . eventListeners . push ( listener ) ;
107- //this.buffer.registerExporter(exporter)
108+ public registerEndSpanListener ( listner : OnEndSpanEventListener ) {
109+ this . eventListeners . push ( listner ) ;
108110 }
109111
110112 private notifyEndSpan ( root : RootSpan ) {
111113 if ( this . active ) {
112- debug ( 'starting to notify listeners the end of rootspans' )
113- if ( this . eventListeners && this . eventListeners . length > 0 ) {
114+ debug ( 'starting to notify listeners the end of rootspans' )
115+ if ( this . eventListeners && this . eventListeners . length > 0 ) {
114116 this . eventListeners . forEach ( ( listener ) => listener . onEndSpan ( root ) )
115117 }
116118 } else {
@@ -122,12 +124,12 @@ export class Tracer implements OnEndSpanEventListener {
122124 this . setCurrentRootSpan ( null ) ;
123125 }
124126
125- public startSpan ( name : string , type : string ) : Span {
127+ public startSpan ( name ? : string , type ?: string , parentSpanId ? : string ) : Span {
126128 let newSpan : Span = null ;
127129 if ( ! this . currentRootSpan ) {
128- debug ( 'no current trace found - cannot start a new span' ) ;
129- } else {
130- newSpan = this . currentRootSpan . startSpan ( name , type ) ;
130+ debug ( 'no current trace found - must start a new root span first ' ) ;
131+ } else {
132+ newSpan = this . currentRootSpan . startSpan ( name , type , parentSpanId ) ;
131133 }
132134 return newSpan ;
133135 }
0 commit comments