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

Commit 778fc7c

Browse files
committed
chord: merge commit
2 parents c833eed + 9d08c47 commit 778fc7c

3 files changed

Lines changed: 55 additions & 51 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class Tracer implements OnEndSpanEventListener {
8181
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: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,51 @@ import { debug, randomSpanId } from '../../internal/util'
2020
const minNumber = 1e-4;
2121
const 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ export abstract class SpanBaseModel {
189189
public end(): void {
190190
if (!this.started) {
191191
debug('calling %s.end() on un-started %s %o',
192-
this._className, this._className,
193-
{ id: this.id, name: this.name, type: this.type })
192+
this._className, this._className,
193+
{ id: this.id, name: this.name, type: this.type })
194194
return
195195
}
196196
if (this.ended) {

0 commit comments

Comments
 (0)