@@ -20,20 +20,21 @@ import { debug, randomSpanId } from '../../internal/util'
2020const minNumber = 1e-4 ;
2121const maxNumber = 0xffffffffffffffff ;
2222
23+ /**
24+ * Class Sampler
25+ */
2326export class Sampler {
24- traceId : string ;
25- spanId : string ;
26- isRemote : boolean ;
27- idUpperBound : number ;
27+ traceId : string ;
28+ spanId : string ;
29+ isRemote : boolean ;
30+ private idUpperBound : number ;
2831
2932 /**
30- *
31- * @param traceId
32- * @param spanId
33- * @param isRemote
33+ * @param traceId Used for probability calculation
34+ * @param spanId todo: integration with propagation class
35+ * @param isRemote todo: integration with propagation class
3436 */
3537 constructor ( traceId ?:string , spanId ?:string , isRemote ?:boolean ) {
36- debug ( 'Samplre constructor' )
3738 if ( traceId ) {
3839 this . traceId = traceId ;
3940 }
@@ -43,17 +44,29 @@ export class Sampler{
4344 this . isRemote = isRemote || false ;
4445
4546 }
46-
47+ /**
48+ * @description Set idUpperBound with maxNumber
49+ * @returns a Sampler object
50+ */
4751 public always ( ) : Sampler {
4852 this . idUpperBound = maxNumber ;
4953 return this ;
5054 }
51-
55+ /**
56+ * @description Set idUpperBound with minNumber
57+ * @returns a Sampler object
58+ */
5259 public never ( ) : Sampler {
5360 this . idUpperBound = minNumber ;
5461 return this ;
5562 }
56-
63+ /**
64+ * @description Set idUpperBound with the probability. If probability
65+ * parameter is bigger then 1 set always. If probability parameter less
66+ * than 0, set never.
67+ * @param probability probability between 0 and 1
68+ * @returns a Sampler object
69+ */
5770 public probability ( probability :number ) : Sampler {
5871 if ( probability < minNumber ) {
5972 return this . never ( ) ;
@@ -66,24 +79,18 @@ export class Sampler{
6679 this . idUpperBound = probability * maxNumber ;
6780 return this ;
6881 }
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 )
82+ /**
83+ * @description
84+ * @param traceId
85+ * @returns a boolean
86+ */
87+ public shouldSample ( traceId :string ) :boolean {
88+ const lower_bytes = traceId . substring ( 16 )
89+ const lower_long = parseInt ( lower_bytes , 16 ) ;
8190
8291 if ( lower_long <= this . idUpperBound ) {
83- debug ( 'trace sampler TRUE' )
8492 return true
8593 } else {
86- debug ( 'trace sampler FALSE' )
8794 return false ;
8895 }
8996 }
0 commit comments