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

Commit fc92980

Browse files
djonathascardososilva-fabio
authored andcommitted
test: created new tests for trace
1 parent 677eef4 commit fc92980

3 files changed

Lines changed: 321 additions & 172 deletions

File tree

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

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -16,138 +16,138 @@
1616

1717
import * as cls from '../../internal/cls';
1818
import {debug} from '../../internal/util';
19-
import {RootSpan, Span} from './types';
20-
import {TraceOptions, Tracer} from './types';
21-
import {OnEndSpanEventListener, Func } from './types';
19+
import {SamplerImpl} from '../config/sampler';
2220
import {Sampler, TracerConfig} from '../config/types';
2321
import {Config} from '../config/types';
24-
import {SpanImpl} from './span';
25-
import {SamplerImpl} from '../config/sampler';
26-
import {RootSpanImpl} from './rootspan';
2722

23+
import {RootSpanImpl} from './rootspan';
24+
import {SpanImpl} from './span';
25+
import {RootSpan, Span} from './types';
26+
import {TraceOptions, Tracer} from './types';
27+
import {Func, OnEndSpanEventListener} from './types';
2828

2929
export class TracerImpl implements Tracer {
30-
31-
//public buffer: Buffer;
32-
private activeLocal: boolean;
33-
private contextManager: cls.Namespace;
34-
private config: TracerConfig;
35-
sampler: Sampler;
36-
37-
//TODO: simple solution - to be rewied in future
38-
private eventListenersLocal: OnEndSpanEventListener[] = [];
39-
//TODO: temp solution
40-
private endedTraces: RootSpan[] = [];
41-
42-
constructor() {
43-
this.activeLocal = false;
44-
this.contextManager = cls.createNamespace();
45-
}
46-
47-
get currentRootSpan(): RootSpan {
48-
return this.contextManager.get('rootspan');
49-
}
50-
51-
set currentRootSpan(root: RootSpan) {
52-
this.contextManager.set('rootspan', root);
53-
}
54-
55-
start(config: TracerConfig): Tracer {
56-
this.activeLocal = true;
57-
this.config = config;
58-
this.sampler = new SamplerImpl().probability(config.samplingRate);
59-
return this;
60-
}
61-
62-
get eventListeners(): OnEndSpanEventListener[] {
63-
return this.eventListenersLocal;
64-
}
65-
66-
stop() {
67-
this.activeLocal = false;
68-
}
69-
70-
get active(): boolean {
71-
return this.activeLocal;
72-
}
73-
74-
startRootSpan<T>(options: TraceOptions, fn: (root: RootSpan) => T): T {
75-
return this.contextManager.runAndReturn((root) => {
76-
let newRoot = null;
77-
if(this.active) {
78-
newRoot = new RootSpanImpl (this, options);
79-
if (this.sampler.shouldSample(newRoot.traceId)) {
80-
newRoot.start();
81-
this.currentRootSpan = newRoot;
82-
return fn(newRoot);
83-
}
84-
} else {
85-
debug("Tracer is inactive, can't start new RootSpan");
86-
}
87-
return fn(newRoot);
88-
});
89-
}
90-
91-
onEndSpan(root: RootSpan): void {
92-
if (!root) {
93-
return debug('cannot end trace - no active trace found');
94-
}
95-
if (this.currentRootSpan !== root) {
96-
debug('currentRootSpan != root on notifyEnd. Need more investigation.');
30+
// public buffer: Buffer;
31+
private activeLocal: boolean;
32+
private contextManager: cls.Namespace;
33+
private config: TracerConfig;
34+
sampler: Sampler;
35+
36+
// TODO: simple solution - to be rewied in future
37+
private eventListenersLocal: OnEndSpanEventListener[] = [];
38+
// TODO: temp solution
39+
private endedTraces: RootSpan[] = [];
40+
41+
constructor() {
42+
this.activeLocal = false;
43+
this.contextManager = cls.createNamespace();
44+
this.clearCurrentTrace();
45+
}
46+
47+
get currentRootSpan(): RootSpan {
48+
return this.contextManager.get('rootspan');
49+
}
50+
51+
set currentRootSpan(root: RootSpan) {
52+
this.contextManager.set('rootspan', root);
53+
}
54+
55+
start(config: TracerConfig): Tracer {
56+
this.activeLocal = true;
57+
this.config = config;
58+
this.sampler = new SamplerImpl().probability(config.samplingRate);
59+
return this;
60+
}
61+
62+
get eventListeners(): OnEndSpanEventListener[] {
63+
return this.eventListenersLocal;
64+
}
65+
66+
stop() {
67+
this.activeLocal = false;
68+
}
69+
70+
get active(): boolean {
71+
return this.activeLocal;
72+
}
73+
74+
startRootSpan<T>(options: TraceOptions, fn: (root: RootSpan) => T): T {
75+
return this.contextManager.runAndReturn((root) => {
76+
let newRoot = null;
77+
if (this.active) {
78+
newRoot = new RootSpanImpl(this, options);
79+
if (this.sampler.shouldSample(newRoot.traceId)) {
80+
newRoot.start();
81+
this.currentRootSpan = newRoot;
82+
return fn(newRoot);
9783
}
98-
this.notifyEndSpan(root);
99-
//this.clearCurrentTrace();
84+
} else {
85+
debug('Tracer is inactive, can\'t start new RootSpan');
86+
}
87+
return fn(newRoot);
88+
});
89+
}
90+
91+
onEndSpan(root: RootSpan): void {
92+
if (!root) {
93+
debug('cannot end trace - no active trace found');
94+
return;
10095
}
101-
102-
registerEndSpanListener(listner: OnEndSpanEventListener) {
103-
this.eventListenersLocal.push(listner);
96+
if (this.currentRootSpan !== root) {
97+
debug('currentRootSpan != root on notifyEnd. Need more investigation.');
98+
return;
10499
}
105-
106-
private notifyEndSpan(root: RootSpan) {
107-
if (this.active) {
108-
debug('starting to notify listeners the end of rootspans');
109-
if (this.eventListenersLocal && this.eventListenersLocal.length > 0) {
110-
this.eventListenersLocal.forEach((listener) => listener.onEndSpan(root));
111-
}
112-
} else {
113-
debug('this tracer is inactivate cant notify endspan');
100+
this.notifyEndSpan(root);
101+
// this.clearCurrentTrace();
102+
}
103+
104+
registerEndSpanListener(listner: OnEndSpanEventListener) {
105+
this.eventListenersLocal.push(listner);
106+
}
107+
108+
private notifyEndSpan(root: RootSpan) {
109+
if (this.active) {
110+
debug('starting to notify listeners the end of rootspans');
111+
if (this.eventListenersLocal && this.eventListenersLocal.length > 0) {
112+
for (const listener of this.eventListenersLocal) {
113+
listener.onEndSpan(root);
114114
}
115+
}
116+
} else {
117+
debug('this tracer is inactivate cant notify endspan');
115118
}
119+
}
116120

117-
clearCurrentTrace() {
118-
this.currentRootSpan = null;
119-
}
121+
clearCurrentTrace() {
122+
this.currentRootSpan = null;
123+
}
120124

121-
startSpan(name?: string, type?: string, parentSpanId?: string): Span {
122-
let newSpan: Span = null;
123-
if (!this.currentRootSpan) {
124-
debug('no current trace found - must start a new root span first');
125-
} else {
126-
newSpan = this.currentRootSpan.startSpan(name, type, parentSpanId);
127-
}
128-
return newSpan;
125+
startSpan(name?: string, type?: string, parentSpanId?: string): Span {
126+
if (!this.currentRootSpan) {
127+
debug('no current trace found - must start a new root span first');
128+
return null;
129129
}
130130

131-
wrap<T>(fn: Func<T>): Func<T> {
132-
if (!this.active) {
133-
return fn;
134-
}
131+
return this.currentRootSpan.startSpan(name, type, parentSpanId);
132+
}
135133

136-
// This is safe because isActive checks the value of this.namespace.
137-
const namespace = this.contextManager as cls.Namespace;
138-
return namespace.bind<T>(fn);
134+
wrap<T>(fn: Func<T>): Func<T> {
135+
if (!this.active) {
136+
return fn;
139137
}
140138

141-
wrapEmitter(emitter: NodeJS.EventEmitter): void {
142-
if (!this.active) {
143-
return;
144-
}
139+
// This is safe because isActive checks the value of this.namespace.
140+
const namespace = this.contextManager as cls.Namespace;
141+
return namespace.bind<T>(fn);
142+
}
145143

146-
// This is safe because isActive checks the value of this.namespace.
147-
const namespace = this.contextManager as cls.Namespace;
148-
namespace.bindEmitter(emitter);
144+
wrapEmitter(emitter: NodeJS.EventEmitter): void {
145+
if (!this.active) {
146+
return;
149147
}
150-
}
151-
152-
153148

149+
// This is safe because isActive checks the value of this.namespace.
150+
const namespace = this.contextManager as cls.Namespace;
151+
namespace.bindEmitter(emitter);
152+
}
153+
}

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ export class SamplerImpl {
3030

3131
/**
3232
* @param traceId Used for probability calculation
33-
* @param spanId todo: integration with propagation class
34-
* @param isRemote todo: integration with propagation class
35-
*/
36-
constructor(traceId?: string, spanId?: string, isRemote?: boolean) {
33+
*/
34+
constructor(traceId?: string) {
3735
if (traceId) {
3836
this.traceId = traceId;
3937
}
@@ -65,7 +63,7 @@ export class SamplerImpl {
6563
* @returns a Sampler object
6664
*/
6765
probability(probability?: number): Sampler {
68-
if(!probability || probability > MAX_NUMBER) {
66+
if (probability == null || probability > MAX_NUMBER) {
6967
return this.always();
7068
}
7169
else if (probability < MIN_NUMBER) {
@@ -94,17 +92,4 @@ export class SamplerImpl {
9492
}
9593
}
9694

97-
// setRate(samplerRate?:number){
98-
// if(samplerRate){
99-
// if(samplerRate <= MIN_NUMBER){
100-
// this.never();
101-
// }else if(samplerRate >= MAX_NUMBER){
102-
// this.always();
103-
// }else{
104-
// this.probability(samplerRate);
105-
// }
106-
// }else{
107-
// this.always();
108-
// }
109-
// }
11095
}

0 commit comments

Comments
 (0)