1
1
import { Span as SpanInterface } from '@sentry/types' ;
2
2
import { uuid4 } from '@sentry/utils' ;
3
3
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 ] * $ / ;
5
5
6
6
/**
7
7
* Span containg all data about a span
@@ -10,19 +10,41 @@ export class Span implements SpanInterface {
10
10
public constructor (
11
11
private readonly _traceId : string = uuid4 ( ) ,
12
12
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 ,
15
15
) { }
16
16
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
+
17
33
/**
18
34
* Continues a trace
19
35
* @param traceparent Traceparent string
20
36
*/
21
37
public static fromTraceparent ( traceparent : string ) : Span | undefined {
22
38
const matches = traceparent . match ( TRACEPARENT_REGEXP ) ;
23
39
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 ) ;
26
48
}
27
49
return undefined ;
28
50
}
@@ -31,7 +53,14 @@ export class Span implements SpanInterface {
31
53
* @inheritDoc
32
54
*/
33
55
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 } ` ;
35
64
}
36
65
37
66
/**
@@ -40,6 +69,7 @@ export class Span implements SpanInterface {
40
69
public toJSON ( ) : object {
41
70
return {
42
71
parent : ( this . _parent && this . _parent . toJSON ( ) ) || undefined ,
72
+ sampled : this . _sampled ,
43
73
span_id : this . _spanId ,
44
74
trace_id : this . _traceId ,
45
75
} ;
0 commit comments