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

Commit 2b12413

Browse files
committed
Refactor the Sampler class
1 parent 36421be commit 2b12413

3 files changed

Lines changed: 35 additions & 31 deletions

File tree

packages/opencensus-core/src/trace/model/tracer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export class Tracer implements OnEndSpanEventListener {
7878
this.setCurrentRootSpan(newRoot);
7979
if(options.sampler == null){
8080
options.sampler = new Sampler(newRoot.traceId);
81-
options.sampler.probability(0.6);
81+
options.sampler.always();
8282
}
8383
newRoot.sampler = options.sampler;
84-
if(newRoot.sampler.continue(newRoot.traceId)){
84+
if(newRoot.sampler.shouldSample(newRoot.traceId)){
8585
newRoot.start();
8686
return fn(newRoot);
8787
}

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ import { debug, randomSpanId } from '../../internal/util'
2020
const minNumber = 1e-4;
2121
const maxNumber = 0xffffffffffffffff;
2222

23+
/**
24+
* Class Sampler
25+
*/
2326
export 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
}

packages/opencensus-core/src/trace/types/tracetypes.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ export abstract class SpanBaseModel {
191191
debug('calling %s.end() on un-started %s %o',
192192
this._className, this._className,
193193
{ id: this.id, name: this.name, type: this.type })
194-
this._started = false;
195-
this._ended = true;
196-
// this.clock.end();
197194
return
198195
} else if (this.ended) {
199196
debug('calling %s.end() on already ended %s %o',

0 commit comments

Comments
 (0)