1414 * limitations under the License.
1515 */
1616
17- import { debug , randomSpanId } from '../../internal/util'
17+ import { debug , randomSpanId } from '../../internal/util' ;
1818
1919
20- const minNumber = 1e-4 ;
21- const maxNumber = 0xffffffffffffffff ;
20+ const MIN_NUMBER = 1e-4 ;
21+ const MAX_NUMBER = 0xffffffffffffffff ;
2222
2323/**
24- * Class Sampler
24+ * This class represent the probability of a tracer.
2525 */
2626export class Sampler {
27- traceId : string ;
28- private idUpperBound : number ;
27+ private traceId : string ;
28+ private idUpperBound : number ;
2929
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- }
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 ;
3938 }
39+ }
4040
41- /**
42- * @description Set idUpperBound with maxNumber
43- * @returns a Sampler object
44- */
45- public always ( ) : Sampler {
46- this . idUpperBound = maxNumber ;
47- return this ;
48- }
49-
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- }
41+ /**
42+ * Set idUpperBound with MAX_NUMBER that is equivalent the probability be 1
43+ * @returns a Sampler object
44+ */
45+ always ( ) : Sampler {
46+ this . idUpperBound = MAX_NUMBER ;
47+ return this ;
48+ }
5849
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 ) {
68- return this . never ( ) ;
50+ /**
51+ * Set idUpperBound with MIN_NUMBER that is equivalent the probability be 0
52+ * @returns a Sampler object
53+ */
54+ never ( ) : Sampler {
55+ this . idUpperBound = MIN_NUMBER ;
56+ return this ;
57+ }
6958
70- } else if ( probability > maxNumber ) {
71- return this . always ( ) ;
59+ /**
60+ * 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+ probability ( probability : number ) : Sampler {
67+ if ( probability < MIN_NUMBER ) {
68+ return this . never ( ) ;
7269
73- }
74-
75- this . idUpperBound = probability * maxNumber ;
76- return this ;
70+ } else if ( probability > MAX_NUMBER ) {
71+ return this . always ( ) ;
7772 }
7873
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 ) ;
74+ this . idUpperBound = probability * MAX_NUMBER ;
75+ return this ;
76+ }
8777
88- if ( lower_long <= this . idUpperBound ) {
89- return true
90- } else {
91- return false ;
92- }
93- }
78+ /**
79+ * Checks if trace belong the sample.
80+ * @param traceId Used to check the probability
81+ * @returns a boolean. True if the traceId is in probability
82+ * False if the traceId is not in probability.
83+ */
84+ shouldSample ( traceId : string ) : boolean {
85+ const LOWER_BYTES = traceId . substring ( 16 ) ;
86+ // tslint:disable-next-line:ban Needed to parse hexadecimal.
87+ const LOWER_LONG = parseInt ( LOWER_BYTES , 16 ) ;
9488
89+ if ( LOWER_LONG <= this . idUpperBound ) {
90+ return true ;
91+ } else {
92+ return false ;
93+ }
94+ }
9595}
0 commit comments