1717import * as cls from '../../internal/cls' ;
1818import { debug } from '../../internal/util' ;
1919import { RootSpan , Span } from './types' ;
20- import { TraceOptions , TracerConfig , defaultConfig , Tracer } from './types' ;
20+ import { TraceOptions , Tracer } from './types' ;
2121import { OnEndSpanEventListener , Func } from './types' ;
22- import { Sampler } from '../config/types' ;
22+ import { Sampler , TracerConfig } from '../config/types' ;
23+ import { Config } from '../config/types' ;
2324import { SpanImpl } from './span' ;
2425import { SamplerImpl } from '../config/sampler'
2526import { RootSpanImpl } from './rootspan' ;
@@ -31,12 +32,14 @@ export class TracerImpl implements Tracer {
3132 private activeLocal : boolean ;
3233 private contextManager : cls . Namespace ;
3334 private config : TracerConfig ;
35+ sampler : Sampler ;
3436
3537 //TODO: simple solution - to be rewied in future
3638 private eventListenersLocal : OnEndSpanEventListener [ ] = [ ] ;
3739 //TODO: temp solution
3840 private endedTraces : RootSpan [ ] = [ ] ;
39-
41+
42+
4043 samplingRate : number ;
4144
4245 constructor ( ) {
@@ -52,9 +55,10 @@ export class TracerImpl implements Tracer {
5255 this . contextManager . set ( 'rootspan' , root ) ;
5356 }
5457
55- start ( config ? : TracerConfig ) : Tracer {
58+ start ( config : TracerConfig ) : Tracer {
5659 this . activeLocal = true ;
57- this . config = config || defaultConfig ;
60+ this . config = config ;
61+ this . sampler = new SamplerImpl ( ) . probability ( config . samplingRate ) ;
5862 return this ;
5963 }
6064
@@ -72,22 +76,19 @@ export class TracerImpl implements Tracer {
7276
7377 startRootSpan < T > ( options : TraceOptions , fn : ( root : RootSpan ) => T ) : T {
7478 return this . contextManager . runAndReturn ( ( root ) => {
75- const newRoot = new RootSpanImpl ( this , options ) ;
76- this . currentRootSpan = newRoot ;
77- if ( ! options ) {
78- options = { } as TraceOptions ;
79- }
80- // if (!options.sampler) {
81- // options.sampler = new SamplerImpl(newRoot.traceId);
82- // options.sampler.always();
83- // }
84- // newRoot.sampler = options.sampler;
85- newRoot . sampler = new SamplerImpl ( ) . probability ( this . samplingRate ) ;
86- if ( newRoot . sampler . shouldSample ( newRoot . traceId ) ) {
87- newRoot . start ( ) ;
88- return fn ( newRoot ) ;
79+ let newRoot = null ;
80+ if ( this . active ) {
81+ newRoot = new RootSpanImpl ( this , options ) ;
82+ this . currentRootSpan = newRoot ;
83+
84+ if ( this . sampler . shouldSample ( newRoot . traceId ) ) {
85+ newRoot . start ( ) ;
86+ return fn ( newRoot ) ;
87+ }
88+ } else {
89+ debug ( "Tracer is inactive, can't start new RootSpan" )
8990 }
90- return fn ( null ) ;
91+ return fn ( newRoot ) ;
9192 } ) ;
9293 }
9394
0 commit comments