@@ -20,42 +20,51 @@ import { debug, randomSpanId } from '../../internal/util'
2020const minNumber = 1e-4 ;
2121const maxNumber = 0xffffffffffffffff ;
2222
23- export class Sampler {
24- traceId : string ;
25- spanId : string ;
26- isRemote : boolean ;
27- idUpperBound : number ;
23+ /**
24+ * Class Sampler
25+ */
26+ export class Sampler {
27+ traceId : string ;
28+ private idUpperBound : number ;
29+
30+ /**
31+ * @param traceId Used for probability calculation
32+ * @param spanId todo: integration with propagation class
33+ * @param isRemote todo: integration with propagation class
34+ */
35+ constructor ( traceId ?:string , spanId ?:string , isRemote ?:boolean ) {
36+ if ( traceId ) {
37+ this . traceId = traceId ;
38+ }
39+ }
2840
2941 /**
30- *
31- * @param traceId
32- * @param spanId
33- * @param isRemote
42+ * @description Set idUpperBound with maxNumber
43+ * @returns a Sampler object
3444 */
35- constructor ( traceId ?: string , spanId ?: string , isRemote ?: boolean ) {
36- debug ( 'Samplre constructor' )
37- if ( traceId ) {
38- this . traceId = traceId ;
39- }
40- if ( spanId ) {
41- this . spanId = spanId ;
42- }
43- this . isRemote = isRemote || false ;
44-
45- }
46-
47- public always ( ) : Sampler {
48- this . idUpperBound = maxNumber ;
49- return this ;
50- }
45+ public always ( ) : Sampler {
46+ this . idUpperBound = maxNumber ;
47+ return this ;
48+ }
5149
52- public never ( ) : Sampler {
53- this . idUpperBound = minNumber ;
54- return this ;
55- }
50+ /**
51+ * @description Set idUpperBound with minNumber
52+ * @returns a Sampler object
53+ */
54+ public never ( ) : Sampler {
55+ this . idUpperBound = minNumber ;
56+ return this ;
57+ }
5658
57- public probability ( probability : number ) : Sampler {
58- if ( probability < minNumber ) {
59+ /**
60+ * @description Set idUpperBound with the probability. If probability
61+ * parameter is bigger then 1 set always. If probability parameter less
62+ * than 0, set never.
63+ * @param probability probability between 0 and 1
64+ * @returns a Sampler object
65+ */
66+ public probability ( probability :number ) : Sampler {
67+ if ( probability < minNumber ) {
5968 return this . never ( ) ;
6069
6170 } else if ( probability > maxNumber ) {
@@ -65,25 +74,20 @@ export class Sampler {
6574
6675 this . idUpperBound = probability * maxNumber ;
6776 return this ;
68- }
69-
70- public continue ( traceId : string ) : boolean {
71- debug ( 'Samplre continue' )
72- let lower_bytes = traceId . substring ( 16 )
73- let lower_long : number
74- debug ( 'SAMPLER CONTINUE lower_bytes :' , lower_bytes )
75-
76- lower_long = parseInt ( lower_bytes , 16 ) ;
77-
78- debug ( 'SAMPLER CONTINUE lower_long :' , lower_long )
79- debug ( 'SAMPLER CONTINUE this.idUpperBound :' , this . idUpperBound )
80- debug ( 'SAMPLER CONTINUE diff :' , lower_long - this . idUpperBound )
77+ }
78+
79+ /**
80+ * @description
81+ * @param traceId
82+ * @returns a boolean
83+ */
84+ public shouldSample ( traceId :string ) :boolean {
85+ const lower_bytes = traceId . substring ( 16 )
86+ const lower_long = parseInt ( lower_bytes , 16 ) ;
8187
82- if ( lower_long <= this . idUpperBound ) {
83- debug ( 'trace sampler TRUE' )
88+ if ( lower_long <= this . idUpperBound ) {
8489 return true
85- } else {
86- debug ( 'trace sampler FALSE' )
90+ } else {
8791 return false ;
8892 }
8993 }
0 commit comments