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

Commit 4b78a1e

Browse files
committed
Move sampler to options
1 parent 24df9ce commit 4b78a1e

5 files changed

Lines changed: 38 additions & 36 deletions

File tree

packages/opencensus-core/src/trace/model/root-span.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,12 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
7676
}
7777

7878
public startSpan(name: string, type: string) {
79-
if(!this.sampler == null || this.sampler.continue(this._traceId)){
80-
let newSpan = new Span(this);
81-
newSpan.name = name
82-
newSpan.type = type
83-
newSpan.start();
84-
this._spans.push(newSpan);
85-
return newSpan;
86-
}else{
87-
//TODO
88-
debug("ELDREY -> RootSpan return startSpan null")
89-
return
90-
}
79+
let newSpan = new Span(this);
80+
newSpan.name = name
81+
newSpan.type = type
82+
newSpan.start();
83+
this._spans.push(newSpan);
84+
return newSpan;
9185
}
9286

9387
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ export class Span extends SpanBaseModel {
6161
}
6262

6363
public end(): void {
64-
super.end();
65-
this.notifyEnd();
66-
debug('ending span %o',
67-
{
68-
spanId: this.id,
69-
traceId: this.traceId,
70-
name: this.name,
71-
startTime: this.startTime,
72-
endTime: this.endTime,
73-
duration: this.duration
74-
}
75-
)
76-
64+
// if(this.sampler.continue(this.traceId)) {
65+
66+
super.end();
67+
this.notifyEnd();
68+
debug('ending span %o',
69+
{
70+
spanId: this.id,
71+
traceId: this.traceId,
72+
name: this.name,
73+
startTime: this.startTime,
74+
endTime: this.endTime,
75+
duration: this.duration
76+
});
77+
// }
7778
}
7879

7980

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ export class Tracer implements OnEndSpanEventListener {
7777
return this.contextManager.runAndReturn((root) => {
7878
let newRoot = new RootSpan(this, options);
7979
this.setCurrentRootSpan(newRoot);
80-
if(sampler == null){
81-
sampler = new Sampler(newTrace.traceId);
82-
sampler.always();
80+
debug('ELDREY -> START ROOT SPAN OPTIONS: ',options);
81+
if(options.sampler == null){
82+
options.sampler = new Sampler(newRoot.traceId);
83+
options.sampler.always();
84+
}
85+
newRoot.sampler = options.sampler;
86+
if(newRoot.sampler.continue(newRoot.traceId)){
87+
debug('ELDREY -> START ROOT SPAN .START()');
88+
newRoot.start();
8389
}
84-
newRoot.sampler = sampler;
85-
newRoot.start();
8690
return fn(newRoot);
8791
});
8892
}
@@ -124,7 +128,7 @@ export class Tracer implements OnEndSpanEventListener {
124128
let newSpan: Span = null;
125129
if (!this.currentRootSpan) {
126130
debug('no current trace found - cannot start a new span');
127-
} else {
131+
} else{
128132
newSpan = this.currentRootSpan.startSpan(name, type);
129133
}
130134
return newSpan;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { debug, randomSpanId } from '../../internal/util'
1818

1919

2020
const minNumber = 1e-4;
21-
const maxNumber = 1;
21+
const maxNumber = 0xffffffffffffffff;
2222

2323
export class Sampler{
2424
traceId: string;
@@ -69,13 +69,15 @@ export class Sampler{
6969

7070
public continue (traceId:string):boolean{
7171
debug('Samplre continue')
72-
let lower_bytes = traceId.substring(traceId.length - 4)
72+
let lower_bytes = traceId.substring(16)
7373
let j;
7474
let lower_long: number
75-
for(j = 0; j < lower_bytes.length; j++) {
76-
lower_long = lower_bytes.charCodeAt(j);
77-
}
75+
debug('ELDREY -> SAMPLER CONTINUE lower_bytes :',lower_bytes)
76+
77+
lower_long = parseInt(lower_bytes, 16);
7878

79+
debug('ELDREY -> SAMPLER CONTINUE lower_long :',lower_long)
80+
debug('ELDREY -> SAMPLER CONTINUE this.idUpperBound :',this.idUpperBound)
7981
if(lower_long <= this.idUpperBound){
8082
return true
8183
}else{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface TraceContext {
3131
export interface TraceOptions {
3232
name:string;
3333
traceContext?:TraceContext;
34+
sampler?:Sampler;
3435
}
3536

3637
export interface OnEndSpanEventListener {

0 commit comments

Comments
 (0)