11import { Span as SpanInterface } from '@sentry/types' ;
22import { uuid4 } from '@sentry/utils' ;
33
4- export const TRACEPARENT_REGEXP = / ( [ 0 - 9 a - f ] { 2 } ) - ( [ 0 - 9 a - f ] { 32 } ) - ( [ 0 - 9 a - f ] { 16 } ) - ( [ 0 - 9 a - f ] { 2 } ) / ;
4+ export const TRACEPARENT_REGEXP = / ^ [ \t ] * ( [ 0 - 9 a - f ] { 32 } ) ? - ? ( [ 0 - 9 a - f ] { 16 } ) ? - ? ( [ 0 1 ] ) ? [ \t ] * $ / ;
55
66/**
77 * Span containg all data about a span
@@ -10,19 +10,41 @@ export class Span implements SpanInterface {
1010 public constructor (
1111 private readonly _traceId : string = uuid4 ( ) ,
1212 private readonly _spanId : string = uuid4 ( ) . substring ( 16 ) ,
13- private readonly _recorded : boolean = false ,
14- private readonly _parent ?: Span ,
13+ private _sampled ? : boolean ,
14+ private _parent ?: Span ,
1515 ) { }
1616
17+ /**
18+ * Setter for parent
19+ */
20+ public setParent ( parent : Span | undefined ) : this {
21+ this . _parent = parent ;
22+ return this ;
23+ }
24+
25+ /**
26+ * Setter for sampled
27+ */
28+ public setSampled ( sampled : boolean | undefined ) : this {
29+ this . _sampled = sampled ;
30+ return this ;
31+ }
32+
1733 /**
1834 * Continues a trace
1935 * @param traceparent Traceparent string
2036 */
2137 public static fromTraceparent ( traceparent : string ) : Span | undefined {
2238 const matches = traceparent . match ( TRACEPARENT_REGEXP ) ;
2339 if ( matches ) {
24- const parent = new Span ( matches [ 2 ] , matches [ 3 ] , matches [ 4 ] === '01' ? true : false ) ;
25- return new Span ( matches [ 2 ] , undefined , undefined , parent ) ;
40+ let sampled ;
41+ if ( matches [ 3 ] === '1' ) {
42+ sampled = true ;
43+ } else if ( matches [ 3 ] === '0' ) {
44+ sampled = false ;
45+ }
46+ const parent = new Span ( matches [ 1 ] , matches [ 2 ] , sampled ) ;
47+ return new Span ( matches [ 1 ] , undefined , sampled , parent ) ;
2648 }
2749 return undefined ;
2850 }
@@ -31,7 +53,14 @@ export class Span implements SpanInterface {
3153 * @inheritDoc
3254 */
3355 public toTraceparent ( ) : string {
34- return `00-${ this . _traceId } -${ this . _spanId } -${ this . _recorded ? '01' : '00' } ` ;
56+ let sampled = '' ;
57+ if ( this . _sampled === true ) {
58+ sampled = '-1' ;
59+ } else if ( this . _sampled === false ) {
60+ sampled = '-0' ;
61+ }
62+
63+ return `${ this . _traceId } -${ this . _spanId } ${ sampled } ` ;
3564 }
3665
3766 /**
@@ -40,6 +69,7 @@ export class Span implements SpanInterface {
4069 public toJSON ( ) : object {
4170 return {
4271 parent : ( this . _parent && this . _parent . toJSON ( ) ) || undefined ,
72+ sampled : this . _sampled ,
4373 span_id : this . _spanId ,
4474 trace_id : this . _traceId ,
4575 } ;
0 commit comments