1515 */
1616
1717import * as cls from '../../internal/cls'
18- import { Trace } from './trace '
18+ import { RootSpan } from './rootspan '
1919import { 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'
23+ import { TraceContext , OnEndSpanEventListener } from '../types/tracetypes' ;
24+ import { TracerConfig , defaultConfig } from '../tracing' ;
2425
2526export type Func < T > = ( ...args : any [ ] ) => T ;
2627
27- export class Tracer {
28+
29+ export class Tracer implements OnEndSpanEventListener {
2830
2931 readonly PLUGINS = [ 'http' , 'https' , 'mongodb-core' , 'express' ] ;
3032
3133 private _active : boolean ;
3234 private contextManager : cls . Namespace ;
33- private exporter : Exporter ;
35+ private config : TracerConfig ;
3436
37+ //TODO: simple solution - to be rewied in future
38+ private eventListeners : OnEndSpanEventListener [ ] = [ ] ;
3539 //TODO: temp solution
36- private endedTraces : Trace [ ] = [ ] ;
40+ private endedTraces : RootSpan [ ] = [ ] ;
3741
3842 constructor ( ) {
3943 this . _active = false ;
4044 this . contextManager = cls . createNamespace ( ) ;
4145 }
4246
43- public get currentTrace ( ) : Trace {
44- return this . contextManager . get ( 'trace ' ) ;
47+ public get currentRootSpan ( ) : RootSpan {
48+ return this . contextManager . get ( 'rootspan ' ) ;
4549 }
4650
47- private setCurrentTrace ( trace : Trace ) {
48- this . contextManager . set ( 'trace ' , trace ) ;
51+ private setCurrentRootSpan ( root : RootSpan ) {
52+ this . contextManager . set ( 'rootspan ' , root ) ;
4953 }
5054
51- public start ( config ?: Object ) : Tracer {
55+ public start ( config ?: TracerConfig ) : Tracer {
5256 this . _active = true ;
57+ this . config = config || defaultConfig ;
5358 return this ;
5459 }
5560
61+ public stop ( ) {
62+ this . _active = false ;
63+ }
64+
5665 public get active ( ) : boolean {
5766 return this . _active ;
5867 }
5968
60- public startTrace ( ) : Trace {
61- let newTrace = new Trace ( ) ;
62- this . setCurrentTrace ( newTrace ) ;
69+ public startRootSpan ( context ?: TraceContext ) : RootSpan {
70+ let newTrace = new RootSpan ( this , context ) ;
71+ this . setCurrentRootSpan ( newTrace ) ;
6372 newTrace . start ( ) ;
6473 return newTrace ;
6574 }
6675
67- public endTrace ( ) : void {
68- if ( ! this . currentTrace ) {
76+
77+ public onEndSpan ( root :RootSpan ) : void {
78+ if ( ! this . currentRootSpan ) {
6979 return debug ( 'cannot end trace - no active trace found' )
7080 }
71- this . currentTrace . end ( ) ;
72- this . addEndedTrace ( this . currentTrace ) ;
81+ if ( this . currentRootSpan != root ) {
82+ return debug ( 'currentRootSpan != root on notifyEnd. Possbile implementation bug.' )
83+ }
84+ this . notifyEndSpan ( this . currentRootSpan ) ;
7385 //this.clearCurrentTrace();
7486 }
7587
88+ //TODO: review
89+ public runInContex < T > ( fn : Func < T > ) : T {
90+ return this . contextManager . runAndReturn ( fn )
91+ }
92+
93+ public registerEndSpanListener ( listner : OnEndSpanEventListener ) {
94+ this . eventListeners . push ( listner ) ;
95+ }
96+
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' )
104+ }
105+ }
106+
76107 public clearCurrentTrace ( ) {
77- this . setCurrentTrace ( null ) ;
108+ this . setCurrentRootSpan ( null ) ;
78109 }
79110
80111 public startSpan ( name : string , type : string ) : Span {
81112 let newSpan : Span = null ;
82- if ( ! this . currentTrace ) {
113+ if ( ! this . currentRootSpan ) {
83114 debug ( 'no current trace found - cannot start a new span' ) ;
84115 } else {
85- newSpan = this . currentTrace . startSpan ( name , type ) ;
116+ newSpan = this . currentRootSpan . startSpan ( name , type ) ;
86117 }
87118 return newSpan ;
88119 }
89120
90- /*public endSpan(span: Span) {
91- debug('END SPAN', span);
92- span.end();
93- this.exporter.emit(this.currentTrace);
94- }*/
95-
96- private addEndedTrace ( trace : Trace ) {
97- if ( this . active ) {
98- //TODO: temp solution
99- //this.endedTraces.push(trace);
100- this . exporter . writeTrace ( this . currentTrace ) ;
101- }
102- }
103-
104121 public wrap < T > ( fn : Func < T > ) : Func < T > {
105122 if ( ! this . active ) {
106123 return fn ;
@@ -121,10 +138,6 @@ export class Tracer {
121138 namespace . bindEmitter ( emitter ) ;
122139 }
123140
124- public registerExporter ( exporter : Exporter ) {
125- this . exporter = exporter ;
126- }
127-
128141}
129142
130143
0 commit comments