@@ -20,31 +20,22 @@ import { Span } from './span'
2020import { debug } from '../../internal/util'
2121import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222import { StackdriverOptions } from '../../exporters/stackdriver/options'
23- import { Exporter , NoopExporter } from '../../exporters/exporter'
24- import { TraceContext } from '../types/tracetypes ' ;
23+ import { TraceContext , OnEndSpanEventListener } from '../types/tracetypes' ;
24+ import { TracerConfig , defaultConfig } from '../tracing ' ;
2525
2626export type Func < T > = ( ...args : any [ ] ) => T ;
2727
28- export interface TracerConfig {
29- exporter ?: Exporter ,
30- sampleRate ?: number ;
31- ignoreUrls ?: Array < string | RegExp > ;
32- }
33-
34- export const defaultConfig : TracerConfig = {
35- exporter : new NoopExporter ( ) ,
36- sampleRate : 1.0
37- }
3828
39- export class Tracer {
29+ export class Tracer implements OnEndSpanEventListener {
4030
4131 readonly PLUGINS = [ 'http' , 'https' , 'mongodb-core' , 'express' ] ;
4232
4333 private _active : boolean ;
4434 private contextManager : cls . Namespace ;
45- private exporter : Exporter ;
4635 private config : TracerConfig ;
4736
37+ //TODO: simple solution - to be rewied in future
38+ private eventListeners : OnEndSpanEventListener [ ] = [ ] ;
4839 //TODO: temp solution
4940 private endedTraces : RootSpan [ ] = [ ] ;
5041
@@ -53,18 +44,17 @@ export class Tracer {
5344 this . contextManager = cls . createNamespace ( ) ;
5445 }
5546
56- public get currentTrace ( ) : RootSpan {
57- return this . contextManager . get ( 'trace ' ) ;
47+ public get currentRootSpan ( ) : RootSpan {
48+ return this . contextManager . get ( 'rootspan ' ) ;
5849 }
5950
60- private setCurrentTrace ( trace : RootSpan ) {
61- this . contextManager . set ( 'trace ' , trace ) ;
51+ private setCurrentRootSpan ( root : RootSpan ) {
52+ this . contextManager . set ( 'rootspan ' , root ) ;
6253 }
6354
6455 public start ( config ?: TracerConfig ) : Tracer {
6556 this . _active = true ;
6657 this . config = config || defaultConfig ;
67- this . exporter = this . config . exporter ;
6858 return this ;
6959 }
7060
@@ -77,54 +67,57 @@ export class Tracer {
7767 }
7868
7969 public startRootSpan ( context ?: TraceContext ) : RootSpan {
80- let newTrace = new RootSpan ( context ) ;
81- this . setCurrentTrace ( newTrace ) ;
70+ let newTrace = new RootSpan ( this , context ) ;
71+ this . setCurrentRootSpan ( newTrace ) ;
8272 newTrace . start ( ) ;
8373 return newTrace ;
8474 }
8575
76+
77+ public onEndSpan ( root :RootSpan ) : void {
78+ if ( ! this . currentRootSpan ) {
79+ return debug ( 'cannot end trace - no active trace found' )
80+ }
81+ if ( this . currentRootSpan != root ) {
82+ return debug ( 'currentRootSpan != root on notifyEnd. Possbile implementation bug.' )
83+ }
84+ this . notifyEndSpan ( this . currentRootSpan ) ;
85+ //this.clearCurrentTrace();
86+ }
87+
8688 //TODO: review
8789 public runInContex < T > ( fn : Func < T > ) : T {
8890 return this . contextManager . runAndReturn ( fn )
8991 }
92+
93+ public registerEndSpanListener ( listner : OnEndSpanEventListener ) {
94+ this . eventListeners . push ( listner ) ;
95+ }
9096
91- public endRootSpan ( ) : void {
92- if ( ! this . currentTrace ) {
93- return debug ( 'cannot end trace - no active trace found' )
97+ private notifyEndSpan ( root : RootSpan ) {
98+ if ( this . active ) {
99+ if ( this . eventListeners && this . eventListeners . length > 0 ) {
100+ this . eventListeners . forEach ( ( listener ) => listener . onEndSpan ( root ) )
101+ }
102+ } else {
103+ debug ( 'this tracer is inactivate cant notify endspan' )
94104 }
95- this . currentTrace . end ( ) ;
96- this . addEndedTrace ( this . currentTrace ) ;
97- //this.clearCurrentTrace();
98105 }
99106
100107 public clearCurrentTrace ( ) {
101- this . setCurrentTrace ( null ) ;
108+ this . setCurrentRootSpan ( null ) ;
102109 }
103110
104111 public startSpan ( name : string , type : string ) : Span {
105112 let newSpan : Span = null ;
106- if ( ! this . currentTrace ) {
113+ if ( ! this . currentRootSpan ) {
107114 debug ( 'no current trace found - cannot start a new span' ) ;
108115 } else {
109- newSpan = this . currentTrace . startSpan ( name , type ) ;
116+ newSpan = this . currentRootSpan . startSpan ( name , type ) ;
110117 }
111118 return newSpan ;
112119 }
113120
114- /*public endSpan(span: Span) {
115- debug('END SPAN', span);
116- span.end();
117- this.exporter.emit(this.currentTrace);
118- }*/
119-
120- private addEndedTrace ( trace : RootSpan ) {
121- if ( this . active ) {
122- //TODO: temp solution
123- //this.endedTraces.push(trace);
124- this . exporter . writeTrace ( this . currentTrace ) ;
125- }
126- }
127-
128121 public wrap < T > ( fn : Func < T > ) : Func < T > {
129122 if ( ! this . active ) {
130123 return fn ;
@@ -145,10 +138,6 @@ export class Tracer {
145138 namespace . bindEmitter ( emitter ) ;
146139 }
147140
148- public registerExporter ( exporter : Exporter ) {
149- this . exporter = exporter ;
150- }
151-
152141}
153142
154143
0 commit comments