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

Commit e409d69

Browse files
committed
chord: merge commit
2 parents 71f70c6 + 90445f3 commit e409d69

7 files changed

Lines changed: 99 additions & 98 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,19 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
8787
debug('%s notified ending by %o', { id: span.id, name: span.name })
8888
}
8989

90-
public startSpan(name?: string, type?: string, parentSpanId?: string) {
90+
public startSpan(name: string, type: string, parentSpanId?: string) {
91+
if (!this.started) {
92+
debug('calling %s.startSpan() on un-started %s %o',
93+
this._className, this._className,
94+
{ id: this.id, name: this.name, type: this.type })
95+
return
96+
}
97+
if (this.ended) {
98+
debug('calling %s.startSpan() on ended %s %o',
99+
this._className, this._className,
100+
{ id: this.id, name: this.name, type: this.type })
101+
return
102+
}
91103
let newSpan = new Span(this);
92104
if (name) { newSpan.name = name }
93105
if (type) { newSpan.type = type }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class Tracer implements OnEndSpanEventListener {
8585
options.sampler.always();
8686
}
8787
newRoot.sampler = options.sampler;
88-
if (newRoot.sampler.continue(newRoot.traceId)) {
88+
if (newRoot.sampler.shouldSample(newRoot.traceId)) {
8989
newRoot.start();
9090
return fn(newRoot);
9191
}

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

Lines changed: 53 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,26 +74,22 @@ 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
}
94+
9095
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ export interface TraceOptions {
3737
}
3838

3939
export interface OnEndSpanEventListener {
40-
onEndSpan(span: SpanBaseModel): void;
40+
onEndSpan(span: SpanBaseModel): void;
4141
}
4242

4343
export interface SpanData {
44-
labels: {[key: string]: string};
44+
labels: { [key: string]: string };
4545
name: string;
4646
spanId: string;
4747
parentSpanId?: string;
48-
}
48+
}
4949

5050

5151
export abstract class SpanBaseModel {
@@ -141,20 +141,20 @@ export abstract class SpanBaseModel {
141141
}
142142

143143
public get startTime(): Date {
144-
if(this.clock){
144+
if (this.clock) {
145145
return this.clock.startTime;
146146
}
147-
147+
148148
}
149149

150150
public get endTime(): Date {
151-
if(this.clock){
151+
if (this.clock) {
152152
return this.clock.endTime;
153153
}
154154
}
155155

156156
public get duration(): number {
157-
if(this.clock){
157+
if (this.clock) {
158158
return this.clock.duration;
159159
}
160160
}
@@ -177,12 +177,12 @@ export abstract class SpanBaseModel {
177177
this.annotations[key] = value;
178178
}
179179

180-
public get sampler(){
180+
public get sampler() {
181181
debug('tracetypes get sampler()')
182182
return this._sampler;
183183
}
184184

185-
public set sampler(sampler:Sampler){
185+
public set sampler(sampler: Sampler) {
186186
debug('tracetypes set sempler(sampler)')
187187
this._sampler = sampler;
188188
}
@@ -203,14 +203,12 @@ export abstract class SpanBaseModel {
203203
debug('calling %s.end() on un-started %s %o',
204204
this._className, this._className,
205205
{ id: this.id, name: this.name, type: this.type })
206-
this._started = false;
207-
this._ended = true;
208-
// this.clock.end();
209206
return
210-
} else if (this.ended) {
207+
}
208+
if (this.ended) {
211209
debug('calling %s.end() on already ended %s %o',
212-
this._className, this._className,
213-
{ id: this.id, name: this.name, type: this.type })
210+
this._className, this._className,
211+
{ id: this.id, name: this.name, type: this.type })
214212
return
215213
}
216214
this._started = false;

packages/opencensus-core/test/test-root-span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 Google Inc. All Rights Reserved.
2+
* Copyright 2018 Google Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the 'License');
55
* you may not use this file except in compliance with the License.

packages/opencensus-core/test/test-span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 Google Inc. All Rights Reserved.
2+
* Copyright 2018 Google Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

packages/opencensus-core/test/test-tracer.ts

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 Google Inc. All Rights Reserved.
2+
* Copyright 2018 Google Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,10 +23,7 @@ let assert = require('assert');
2323

2424
describe('Tracer', function () {
2525
const options = { name: "test" };
26-
const callback = (root) => {
27-
28-
return root;
29-
}
26+
const callback = (root) => { return root; }
3027

3128
describe('new Tracer()', function () {
3229
it('should create a Tracer instance', function () {
@@ -52,35 +49,27 @@ describe('Tracer', function () {
5249
describe('startRootSpan()', function () {
5350

5451
it('should start the rootSpan', function () {
55-
const tracer = new Tracer();
56-
tracer.start();
57-
const root = tracer.startRootSpan(options, callback);
58-
59-
assert.ok(root.started);
60-
});
61-
62-
it('should set the new span root as currentRootSpan', function () {
63-
const tracer = new Tracer();
52+
let tracer = new Tracer();
6453
tracer.start();
65-
const root = tracer.startRootSpan(options, callback);
54+
let rootSpan = tracer.startRootSpan(options, callback);
6655

67-
assert.equal(tracer.currentRootSpan.id, root.id);
56+
assert.ok(rootSpan.started);
6857
});
6958
});
7059

7160
describe('end()', function () {
7261
it('should end current trace', function () {
73-
const tracer = new Tracer();
74-
const trace = tracer.startRootSpan(options, callback);
75-
trace.end();
76-
assert.ok(trace.ended);
62+
let tracer = new Tracer();
63+
let rootSpan = tracer.startRootSpan(options, callback);
64+
rootSpan.end();
65+
assert.ok(rootSpan.ended);
7766
});
7867
});
7968

8069
describe('clearCurrentRootSpan()', function () {
8170
it('should set the current root span to null', function () {
82-
const tracer = new Tracer();
83-
const trace = tracer.startRootSpan(options, callback);
71+
let tracer = new Tracer();
72+
let rootSpan = tracer.startRootSpan(options, callback);
8473
tracer.clearCurrentTrace();
8574

8675
assert.ok(tracer.currentRootSpan == null);
@@ -89,19 +78,16 @@ describe('Tracer', function () {
8978

9079
describe('startSpan()', function () {
9180
it('should return a Span instance', function () {
92-
const tracer = new Tracer();
93-
const trace = tracer.startRootSpan(options, callback);
94-
trace.start();
95-
const span = tracer.startSpan("spanName", "spanType");
96-
console.log(span);
81+
let tracer = new Tracer();
82+
let rootSpan = tracer.startRootSpan(options, callback);
83+
let span = tracer.startSpan("spanName", "spanType");
9784
assert.ok(span instanceof Span);
9885
});
9986

10087
it('should start a span', function () {
101-
const tracer = new Tracer();
102-
const trace = tracer.startRootSpan(options, callback);
103-
trace.start();
104-
const span = tracer.startSpan("spanName", "spanType");
88+
let tracer = new Tracer();
89+
let rootSpan = tracer.startRootSpan(options, callback);
90+
let span = tracer.startSpan("spanName", "spanType");
10591
assert.ok(span.started);
10692
});
10793
});

0 commit comments

Comments
 (0)