@@ -20,8 +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 { TraceContext , TraceOptions , OnEndSpanEventListener } from '../types/tracetypes' ;
24- import { TracerConfig , defaultConfig } from '../tracing' ;
23+ import { TraceContext , TraceOptions , OnEndSpanEventListener , SpanBaseModel } from '../types/tracetypes' ;
24+ import { TracerConfig , defaultConfig } from '../tracing' ;
2525
2626export type Func < T > = ( ...args : any [ ] ) => T ;
2727
@@ -81,28 +81,24 @@ export class Tracer implements OnEndSpanEventListener {
8181 } ) ;
8282 }
8383
84-
8584 public onEndSpan ( root : RootSpan ) : void {
8685 if ( ! this . currentRootSpan ) {
87- return debug ( 'cannot end trace - no active trace found' )
86+ return debug ( 'cannot end trace - no active trace found' , this . currentRootSpan )
8887 }
8988 if ( this . currentRootSpan != root ) {
90- return debug ( 'currentRootSpan != root on notifyEnd. Possbile implementation bug.' )
89+ debug ( 'currentRootSpan != root on notifyEnd. Possbile implementation bug.' )
90+ debug ( 'ROOT SPAN ' , root )
91+ return debug ( 'CURRENT ROOT SPAN ' , this . currentRootSpan )
92+ //return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
9193 }
9294 this . notifyEndSpan ( this . currentRootSpan ) ;
9395 //this.clearCurrentTrace();
9496 }
9597
96-
9798 public registerEndSpanListener ( listner : OnEndSpanEventListener ) {
98- this . eventListeners . push ( listner ) ;
99+ this . eventListeners . push ( listner ) ;
99100 }
100101
101- /*public registerExporter(exporter: Exporter) {
102- //this.eventListeners.push(listner);
103- this.buffer.registerExporter(exporter)
104- }*/
105-
106102 private notifyEndSpan ( root : RootSpan ) {
107103 if ( this . active ) {
108104 //this.buffer.onEndSpan(root);
@@ -118,16 +114,50 @@ export class Tracer implements OnEndSpanEventListener {
118114 this . setCurrentRootSpan ( null ) ;
119115 }
120116
121- public startSpan ( name ?: string , type ?: string ) : Span {
117+ public startSpan ( name ?: string , type ?: string , parentSpanId ?: string ) : Span {
122118 let newSpan : Span = null ;
123119 if ( ! this . currentRootSpan ) {
124120 debug ( 'no current trace found - must start a new root span first' ) ;
125121 } else {
126- newSpan = this . currentRootSpan . startSpan ( name , type ) ;
122+ newSpan = this . currentRootSpan . startSpan ( name , type , parentSpanId ) ;
127123 }
128124 return newSpan ;
129125 }
130126
127+ /*public startSpan<T>(fn: (span: Span) => T): T {
128+ //debug('starting span: %o', options)
129+ if (!this.currentRootSpan) {
130+ debug('no current trace found - must start a new root span first');
131+ return null;
132+ } else {
133+ return this.contextManager.runAndReturn((span) => {
134+ let newSpan = this.currentRootSpan.startSpan();
135+ return fn(newSpan);
136+ });
137+ }
138+ }*/
139+
140+ /*public startSpan<T>(options: TraceOptions, fn: (span: SpanBaseModel) => T): T {
141+ //debug('starting span: %o', options)
142+ if (this.currentRootSpan) {
143+ // Has an active root span
144+ debug('starting span')
145+ return this.contextManager.runAndReturn((span) => {
146+ let newSpan = this.currentRootSpan.startSpan();
147+ return fn(newSpan);
148+ });
149+ } else {
150+ // Has no active root span
151+ debug('starting root span: %o', options)
152+ return this.contextManager.runAndReturn((root) => {
153+ let newRoot = new RootSpan(this, options);
154+ this.setCurrentRootSpan(newRoot);
155+ newRoot.start();
156+ return fn(newRoot);
157+ });
158+ }
159+ }*/
160+
131161 public wrap < T > ( fn : Func < T > ) : Func < T > {
132162 if ( ! this . active ) {
133163 return fn ;
0 commit comments