@@ -21,8 +21,8 @@ import * as uuid from 'uuid';
2121
2222import { cls } from './cls' ;
2323import { Constants , SpanDataType } from './constants' ;
24- import { Func , RootSpanOptions , SpanData , SpanOptions , TraceAgent as TraceAgentInterface } from './plugin-types' ;
25- import { ChildSpanData , RootSpanData , UNCORRELATED_SPAN , UNTRACED_SPAN } from './span-data' ;
24+ import { Func , RootSpanData as RootSpanDataInterface , RootSpanOptions , SpanData , SpanOptions , TraceAgent as TraceAgentInterface } from './plugin-types' ;
25+ import { ChildSpanData , RootSpanData , UNCORRELATED_CHILD_SPAN , UNCORRELATED_ROOT_SPAN , UNTRACED_CHILD_SPAN , UNTRACED_ROOT_SPAN } from './span-data' ;
2626import { SpanKind , Trace } from './trace' ;
2727import { TraceLabels } from './trace-labels' ;
2828import * as TracingPolicy from './tracing-policy' ;
@@ -121,18 +121,20 @@ export class TraceAgent implements TraceAgentInterface {
121121 return ! ! this . config && this . config . enhancedDatabaseReporting ;
122122 }
123123
124- runInRootSpan < T > ( options : RootSpanOptions , fn : ( span : SpanData ) => T ) : T {
124+ runInRootSpan < T > (
125+ options : RootSpanOptions , fn : ( span : RootSpanDataInterface ) => T ) : T {
125126 if ( ! this . isActive ( ) ) {
126- return fn ( UNTRACED_SPAN ) ;
127+ return fn ( UNTRACED_ROOT_SPAN ) ;
127128 }
128129
129- // TODO validate options
130+ options = options || { name : '' } ;
131+
130132 // Don't create a root span if we are already in a root span
131133 const rootSpan = cls . get ( ) . getContext ( ) ;
132134 if ( rootSpan . type === SpanDataType . ROOT && ! rootSpan . span . endTime ) {
133135 this . logger ! . warn ( `TraceApi#runInRootSpan: [${
134136 this . pluginName } ] Cannot create nested root spans.`) ;
135- return fn ( UNCORRELATED_SPAN ) ;
137+ return fn ( UNCORRELATED_ROOT_SPAN ) ;
136138 }
137139
138140 return cls . get ( ) . runWithNewContext ( ( ) => {
@@ -155,8 +157,8 @@ export class TraceAgent implements TraceAgentInterface {
155157 ! ! ( incomingTraceContext . options &
156158 Constants . TRACE_OPTIONS_TRACE_ENABLED ) ;
157159 if ( ! locallyAllowed || ! remotelyAllowed ) {
158- cls . get ( ) . setContext ( UNTRACED_SPAN ) ;
159- return fn ( UNTRACED_SPAN ) ;
160+ cls . get ( ) . setContext ( UNTRACED_ROOT_SPAN ) ;
161+ return fn ( UNTRACED_ROOT_SPAN ) ;
160162 }
161163
162164 // Create a new root span, and invoke fn with it.
@@ -193,11 +195,12 @@ export class TraceAgent implements TraceAgentInterface {
193195 }
194196 }
195197
196- createChildSpan ( options : SpanOptions ) : SpanData {
198+ createChildSpan ( options ? : SpanOptions ) : SpanData {
197199 if ( ! this . isActive ( ) ) {
198- return UNTRACED_SPAN ;
200+ return UNTRACED_CHILD_SPAN ;
199201 }
200202
203+ options = options || { name : '' } ;
201204 const rootSpan = cls . get ( ) . getContext ( ) ;
202205 if ( rootSpan . type === SpanDataType . ROOT ) {
203206 if ( ! ! rootSpan . span . endTime ) {
@@ -212,29 +215,26 @@ export class TraceAgent implements TraceAgentInterface {
212215 this . pluginName } ] Creating phantom child span [${
213216 options . name } ] because root span [${
214217 rootSpan . span . name } ] was already closed.`) ;
215- return UNCORRELATED_SPAN ;
218+ return UNCORRELATED_CHILD_SPAN ;
216219 }
217220 // Create a new child span and return it.
218- options = options || { name : '' } ;
219- const skipFrames = options . skipFrames ? options . skipFrames + 1 : 1 ;
220- const childContext = new ChildSpanData (
221- rootSpan . trace , /* Trace object */
222- options . name , /* Span name */
223- rootSpan . span . spanId , /* Parent's span ID */
224- skipFrames ) ; /* # of frames to skip in stack trace */
221+ const childContext = rootSpan . createChildSpan ( {
222+ name : options . name ,
223+ skipFrames : options . skipFrames ? options . skipFrames + 1 : 1
224+ } ) ;
225225 this . logger ! . info ( `TraceApi#createChildSpan: [${
226226 this . pluginName } ] Created child span [${ options . name } ]`) ;
227227 return childContext ;
228228 } else if ( rootSpan . type === SpanDataType . UNTRACED ) {
229229 // Context wasn't lost, but there's no root span, indicating that this
230230 // request should not be traced.
231- return UNTRACED_SPAN ;
231+ return UNTRACED_CHILD_SPAN ;
232232 } else {
233233 // Context was lost.
234234 this . logger ! . warn ( `TraceApi#createChildSpan: [${
235235 this . pluginName } ] Creating phantom child span [${
236236 options . name } ] because there is no root span.`) ;
237- return UNCORRELATED_SPAN ;
237+ return UNCORRELATED_CHILD_SPAN ;
238238 }
239239 }
240240
0 commit comments