Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit aa307a2

Browse files
committed
refactor: apply code guidelines rules to sampler.ts file
1 parent 528be79 commit aa307a2

1 file changed

Lines changed: 60 additions & 61 deletions

File tree

  • packages/opencensus-core/src/trace/sampler

packages/opencensus-core/src/trace/sampler/sampler.ts

Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,81 @@
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
/**
2424
* Class Sampler
2525
*/
2626
export class Sampler {
27-
traceId: string;
28-
private idUpperBound: number;
27+
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
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
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+
*
80+
* @param traceId
81+
* @returns a boolean
82+
*/
83+
shouldSample(traceId: string): boolean {
84+
const LOWER_BYTES = traceId.substring(16);
85+
// tslint:disable-next-line:ban Needed to parse hexadecimal.
86+
const LOWER_LONG = parseInt(LOWER_BYTES, 16);
9487

88+
if (LOWER_LONG <= this.idUpperBound) {
89+
return true;
90+
} else {
91+
return false;
92+
}
93+
}
9594
}

0 commit comments

Comments
 (0)