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

Commit 7484de4

Browse files
Fixed trace tests
1 parent 36421be commit 7484de4

7 files changed

Lines changed: 101 additions & 104 deletions

File tree

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

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

7777
public startSpan(name: string, type: string) {
78+
if (!this.started) {
79+
debug('calling %s.startSpan() on un-started %s %o',
80+
this._className, this._className,
81+
{ id: this.id, name: this.name, type: this.type })
82+
return
83+
}
84+
if (this.ended) {
85+
debug('calling %s.startSpan() on ended %s %o',
86+
this._className, this._className,
87+
{ id: this.id, name: this.name, type: this.type })
88+
return
89+
}
7890
let newSpan = new Span(this);
7991
newSpan.name = name
8092
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
@@ -78,7 +78,7 @@ 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;
8484
if(newRoot.sampler.continue(newRoot.traceId)){

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

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,72 +20,72 @@ 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;
28-
29-
/**
30-
*
31-
* @param traceId
32-
* @param spanId
33-
* @param isRemote
34-
*/
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-
}
51-
52-
public never(): Sampler{
53-
this.idUpperBound = minNumber;
54-
return this;
55-
}
56-
57-
public probability(probability:number): Sampler{
58-
if(probability < minNumber){
23+
export class Sampler {
24+
traceId: string;
25+
spanId: string;
26+
isRemote: boolean;
27+
idUpperBound: number;
28+
29+
/**
30+
*
31+
* @param traceId
32+
* @param spanId
33+
* @param isRemote
34+
*/
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+
}
51+
52+
public never(): Sampler {
53+
this.idUpperBound = minNumber;
54+
return this;
55+
}
56+
57+
public probability(probability: number): Sampler {
58+
if (probability < minNumber) {
5959
return this.never();
6060

61-
} else if (probability > maxNumber){
61+
} else if (probability > maxNumber) {
6262
return this.always();
6363

6464
}
6565

6666
this.idUpperBound = probability * maxNumber;
6767
return this;
68-
}
68+
}
6969

70-
public continue (traceId:string):boolean{
70+
public continue(traceId: string): boolean {
7171
debug('Samplre continue')
7272
let lower_bytes = traceId.substring(16)
7373
let lower_long: number
74-
debug('SAMPLER CONTINUE lower_bytes :',lower_bytes)
74+
debug('SAMPLER CONTINUE lower_bytes :', lower_bytes)
7575

7676
lower_long = parseInt(lower_bytes, 16);
7777

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)
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)
8181

82-
if(lower_long <= this.idUpperBound){
82+
if (lower_long <= this.idUpperBound) {
8383
debug('trace sampler TRUE')
8484
return true
85-
}else{
85+
} else {
8686
debug('trace sampler FALSE')
8787
return false;
8888
}
89-
}
89+
}
9090

91-
}
91+
}

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ export interface MapLabels { [propName: string]: string; }
2323
export interface MapObjects { [propName: string]: any; }
2424

2525
export interface TraceContext {
26-
traceId: string,
27-
spanId: string,
28-
options?: number
26+
traceId: string,
27+
spanId: string,
28+
options?: number
2929
}
3030

3131
export interface TraceOptions {
32-
name:string;
33-
traceContext?:TraceContext;
34-
sampler?:Sampler;
32+
name: string;
33+
traceContext?: TraceContext;
34+
sampler?: Sampler;
3535
}
3636

3737
export interface OnEndSpanEventListener {
38-
onEndSpan(span: SpanBaseModel): void;
38+
onEndSpan(span: SpanBaseModel): void;
3939
}
4040

4141
export interface SpanData {
42-
labels: {[key: string]: string};
42+
labels: { [key: string]: string };
4343
name: string;
4444
spanId: string;
4545
parentSpanId?: string;
46-
}
46+
}
4747

4848

4949
export abstract class SpanBaseModel {
@@ -129,20 +129,20 @@ export abstract class SpanBaseModel {
129129
}
130130

131131
public get startTime(): Date {
132-
if(this.clock){
132+
if (this.clock) {
133133
return this.clock.startTime;
134134
}
135-
135+
136136
}
137137

138138
public get endTime(): Date {
139-
if(this.clock){
139+
if (this.clock) {
140140
return this.clock.endTime;
141141
}
142142
}
143143

144144
public get duration(): number {
145-
if(this.clock){
145+
if (this.clock) {
146146
return this.clock.duration;
147147
}
148148
}
@@ -165,12 +165,12 @@ export abstract class SpanBaseModel {
165165
this.annotations[key] = value;
166166
}
167167

168-
public get sampler(){
168+
public get sampler() {
169169
debug('tracetypes get sampler()')
170170
return this._sampler;
171171
}
172172

173-
public set sampler(sampler:Sampler){
173+
public set sampler(sampler: Sampler) {
174174
debug('tracetypes set sempler(sampler)')
175175
this._sampler = sampler;
176176
}
@@ -189,16 +189,14 @@ 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 })
194-
this._started = false;
195-
this._ended = true;
196-
// this.clock.end();
192+
this._className, this._className,
193+
{ id: this.id, name: this.name, type: this.type })
197194
return
198-
} else if (this.ended) {
195+
}
196+
if (this.ended) {
199197
debug('calling %s.end() on already ended %s %o',
200-
this._className, this._className,
201-
{ id: this.id, name: this.name, type: this.type })
198+
this._className, this._className,
199+
{ id: this.id, name: this.name, type: this.type })
202200
return
203201
}
204202
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: 18 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.
@@ -18,15 +18,13 @@ import { Tracer } from '../src/trace/model/tracer';
1818
import { RootSpan } from '../src/trace/model/rootspan';
1919
import { Span } from '../src/trace/model/span';
2020
import { Exporter } from '../src/exporters/exporter';
21+
import { Sampler } from '../src/trace/config/sampler';
2122

2223
let assert = require('assert');
2324

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

3129
describe('new Tracer()', function () {
3230
it('should create a Tracer instance', function () {
@@ -52,35 +50,27 @@ describe('Tracer', function () {
5250
describe('startRootSpan()', function () {
5351

5452
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();
53+
let tracer = new Tracer();
6454
tracer.start();
65-
const root = tracer.startRootSpan(options, callback);
55+
let rootSpan = tracer.startRootSpan(options, callback);
6656

67-
assert.equal(tracer.currentRootSpan.id, root.id);
57+
assert.ok(rootSpan.started);
6858
});
6959
});
7060

7161
describe('end()', function () {
7262
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);
63+
let tracer = new Tracer();
64+
let rootSpan = tracer.startRootSpan(options, callback);
65+
rootSpan.end();
66+
assert.ok(rootSpan.ended);
7767
});
7868
});
7969

8070
describe('clearCurrentRootSpan()', function () {
8171
it('should set the current root span to null', function () {
82-
const tracer = new Tracer();
83-
const trace = tracer.startRootSpan(options, callback);
72+
let tracer = new Tracer();
73+
let rootSpan = tracer.startRootSpan(options, callback);
8474
tracer.clearCurrentTrace();
8575

8676
assert.ok(tracer.currentRootSpan == null);
@@ -89,19 +79,16 @@ describe('Tracer', function () {
8979

9080
describe('startSpan()', function () {
9181
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);
82+
let tracer = new Tracer();
83+
let rootSpan = tracer.startRootSpan(options, callback);
84+
let span = tracer.startSpan("spanName", "spanType");
9785
assert.ok(span instanceof Span);
9886
});
9987

10088
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");
89+
let tracer = new Tracer();
90+
let rootSpan = tracer.startRootSpan(options, callback);
91+
let span = tracer.startSpan("spanName", "spanType");
10592
assert.ok(span.started);
10693
});
10794
});

0 commit comments

Comments
 (0)