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

Commit fe5ece9

Browse files
committed
More refactorings, add endspan event listener, correct bug in zipkin exporter and others
1 parent de75000 commit fe5ece9

6 files changed

Lines changed: 81 additions & 67 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ import { Span } from './span'
1818
import { Clock } from '../../internal/clock'
1919
import * as uuid from 'uuid';
2020
import { debug } from '../../internal/util'
21-
import { SpanBaseModel, TraceContext } from '../types/tracetypes'
21+
import { SpanBaseModel, TraceContext, OnEndSpanEventListener } from '../types/tracetypes'
22+
import { Tracer } from './tracer';
2223

23-
export class RootSpan extends SpanBaseModel {
24+
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
2425

26+
private tracer: Tracer;
2527
private _spans: Span[];
2628
private _traceId: string;
2729

28-
constructor(context?: TraceContext ) {
30+
constructor(tracer: Tracer, context?: TraceContext ) {
2931
super()
32+
this.tracer = tracer;
3033
this._traceId = context&&context.traceId?context.traceId:(uuid.v4().split('-').join(''));
3134
this._spans = [];
3235
}
@@ -61,6 +64,12 @@ export class RootSpan extends SpanBaseModel {
6164
startTime: this.startTime,
6265
endTime: this.endTime,
6366
duration: this.duration })
67+
68+
this.tracer.onEndSpan(this)
69+
}
70+
71+
public onEndSpan(span: Span) {
72+
debug('%s notified ending by %o',{id: span.id, name: span.name})
6473
}
6574

6675
public startSpan(name: string, type: string) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import { SpanBaseModel, TraceContext } from '../types/tracetypes'
2222

2323
export class Span extends SpanBaseModel {
2424

25-
private root: SpanBaseModel;
25+
private root: RootSpan;
2626
// private _parentSpanId: string;
2727

28-
constructor(root: SpanBaseModel) {
28+
constructor(root: RootSpan) {
2929
super()
3030
this.root = root;
3131
}
@@ -56,8 +56,13 @@ export class Span extends SpanBaseModel {
5656
})
5757
}
5858

59+
private notifyEnd () {
60+
this.root.onEndSpan(this);
61+
}
62+
5963
public end(): void {
6064
super.end();
65+
this.notifyEnd();
6166
debug('ending span %o',
6267
{
6368
spanId: this.id,

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

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,22 @@ import { Span } from './span'
2020
import { debug } from '../../internal/util'
2121
import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222
import { StackdriverOptions } from '../../exporters/stackdriver/options'
23-
import { Exporter, NoopExporter } from '../../exporters/exporter'
24-
import { TraceContext } from '../types/tracetypes';
23+
import { TraceContext, OnEndSpanEventListener } from '../types/tracetypes';
24+
import { TracerConfig, defaultConfig } from '../tracing';
2525

2626
export type Func<T> = (...args: any[]) => T;
2727

28-
export interface TracerConfig {
29-
exporter?: Exporter,
30-
sampleRate?: number;
31-
ignoreUrls?: Array<string|RegExp>;
32-
}
33-
34-
export const defaultConfig: TracerConfig = {
35-
exporter: new NoopExporter(),
36-
sampleRate: 1.0
37-
}
3828

39-
export class Tracer {
29+
export class Tracer implements OnEndSpanEventListener {
4030

4131
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
4232

4333
private _active: boolean;
4434
private contextManager: cls.Namespace;
45-
private exporter: Exporter;
4635
private config: TracerConfig;
4736

37+
//TODO: simple solution - to be rewied in future
38+
private eventListeners: OnEndSpanEventListener[] = [];
4839
//TODO: temp solution
4940
private endedTraces: RootSpan[] = [];
5041

@@ -53,18 +44,17 @@ export class Tracer {
5344
this.contextManager = cls.createNamespace();
5445
}
5546

56-
public get currentTrace(): RootSpan {
57-
return this.contextManager.get('trace');
47+
public get currentRootSpan(): RootSpan {
48+
return this.contextManager.get('rootspan');
5849
}
5950

60-
private setCurrentTrace(trace: RootSpan) {
61-
this.contextManager.set('trace', trace);
51+
private setCurrentRootSpan(root: RootSpan) {
52+
this.contextManager.set('rootspan', root);
6253
}
6354

6455
public start(config?: TracerConfig): Tracer {
6556
this._active = true;
6657
this.config = config || defaultConfig;
67-
this.exporter = this.config.exporter;
6858
return this;
6959
}
7060

@@ -77,54 +67,57 @@ export class Tracer {
7767
}
7868

7969
public startRootSpan(context?: TraceContext): RootSpan {
80-
let newTrace = new RootSpan(context);
81-
this.setCurrentTrace(newTrace);
70+
let newTrace = new RootSpan(this, context);
71+
this.setCurrentRootSpan(newTrace);
8272
newTrace.start();
8373
return newTrace;
8474
}
8575

76+
77+
public onEndSpan(root:RootSpan): void {
78+
if (!this.currentRootSpan) {
79+
return debug('cannot end trace - no active trace found')
80+
}
81+
if(this.currentRootSpan != root) {
82+
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
83+
}
84+
this.notifyEndSpan(this.currentRootSpan);
85+
//this.clearCurrentTrace();
86+
}
87+
8688
//TODO: review
8789
public runInContex<T>(fn: Func<T>): T {
8890
return this.contextManager.runAndReturn (fn)
8991
}
92+
93+
public registerEndSpanListener(listner: OnEndSpanEventListener) {
94+
this.eventListeners.push(listner);
95+
}
9096

91-
public endRootSpan(): void {
92-
if (!this.currentTrace) {
93-
return debug('cannot end trace - no active trace found')
97+
private notifyEndSpan(root: RootSpan) {
98+
if (this.active) {
99+
if(this.eventListeners&&this.eventListeners.length >0) {
100+
this.eventListeners.forEach((listener) => listener.onEndSpan(root))
101+
}
102+
} else {
103+
debug ('this tracer is inactivate cant notify endspan')
94104
}
95-
this.currentTrace.end();
96-
this.addEndedTrace(this.currentTrace);
97-
//this.clearCurrentTrace();
98105
}
99106

100107
public clearCurrentTrace() {
101-
this.setCurrentTrace(null);
108+
this.setCurrentRootSpan(null);
102109
}
103110

104111
public startSpan(name: string, type: string): Span {
105112
let newSpan: Span = null;
106-
if (!this.currentTrace) {
113+
if (!this.currentRootSpan) {
107114
debug('no current trace found - cannot start a new span');
108115
} else {
109-
newSpan = this.currentTrace.startSpan(name, type);
116+
newSpan = this.currentRootSpan.startSpan(name, type);
110117
}
111118
return newSpan;
112119
}
113120

114-
/*public endSpan(span: Span) {
115-
debug('END SPAN', span);
116-
span.end();
117-
this.exporter.emit(this.currentTrace);
118-
}*/
119-
120-
private addEndedTrace(trace: RootSpan) {
121-
if (this.active) {
122-
//TODO: temp solution
123-
//this.endedTraces.push(trace);
124-
this.exporter.writeTrace(this.currentTrace);
125-
}
126-
}
127-
128121
public wrap<T>(fn: Func<T>): Func<T> {
129122
if (!this.active) {
130123
return fn;
@@ -145,10 +138,6 @@ export class Tracer {
145138
namespace.bindEmitter(emitter);
146139
}
147140

148-
public registerExporter(exporter: Exporter) {
149-
this.exporter = exporter;
150-
}
151-
152141
}
153142

154143

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ export interface TraceContext {
2727
options?: number
2828
}
2929

30+
export interface OnEndSpanEventListener {
31+
onEndSpan(span: SpanBaseModel): void;
32+
}
33+
3034
export abstract class SpanBaseModel {
3135

3236
protected _className: string;
3337
private _id: string;
3438
private clock: Clock;
35-
//------
39+
//--Tra----
3640
private _remoteParent: string;
3741
private _name: string;
3842
private _started: boolean;
@@ -163,7 +167,6 @@ export abstract class SpanBaseModel {
163167
this._started = false;
164168
this._ended = true;
165169
this.clock.end();
166-
167170
}
168171

169172

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@
1616

1717
import { Span } from '../src/trace/model/span';
1818
import { RootSpan } from '../src/trace/model/rootspan';
19+
import { SpanBaseModel } from '../src/trace/types/tracetypes';
20+
import { Tracer } from '../src/trace/model/tracer';
21+
1922

2023
var assert = require('assert');
2124

25+
let tracer = new Tracer()
26+
2227
describe('Span creation', function () {
2328

2429
let root;
2530
let span;
2631

2732
before(function () {
28-
root = new RootSpan();
33+
root = new RootSpan(tracer);
2934
span = root.startSpan('spanName', 'typeSpan');
3035
});
3136

3237
it('should create an span on the trace', function () {
33-
assert.ok(root instanceof RootSpan);
38+
assert.ok(root instanceof SpanBaseModel);
3439
span = root.startSpan('spanName', 'typeSpan');
3540
assert.ok(span instanceof Span);
3641
assert.ok(span.id);
@@ -53,7 +58,7 @@ describe('Span checking creation', function () {
5358
let span;
5459

5560
before(function () {
56-
root = new RootSpan();
61+
root = new RootSpan(tracer);
5762
span = root.startSpan('spanName', 'typeSpan');
5863
});
5964

@@ -68,7 +73,7 @@ describe('Span data', function () {
6873
let root;
6974

7075
before(function () {
71-
root = new RootSpan();
76+
root = new RootSpan(tracer);
7277
});
7378

7479
it('generates unique numeric span ID strings', function () {

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,30 @@
1616

1717
import { RootSpan } from '../src/trace/model/rootspan';
1818
import { Span } from '../src/trace/model/span';
19+
import { Tracer } from '../src/trace/model/tracer';
20+
import { SpanBaseModel } from '../src/trace/types/tracetypes';
1921

2022
let assert = require('assert');
23+
let tracer = new Tracer()
2124

2225
describe('Trace', function () {
2326
describe('new Trace()', function () {
2427
it('should be a Trace instance', function () {
25-
let root = new RootSpan();
26-
assert.ok(root instanceof RootSpan);
28+
let root = new RootSpan(tracer);
29+
assert.ok(root instanceof SpanBaseModel);
2730
});
2831
});
2932

3033
describe('start()', function () {
3134
it('the trace was started', function () {
32-
let root = new RootSpan();
35+
let root = new RootSpan(tracer);
3336
root.start();
3437
assert.ok(root.started);
3538
});
3639
});
3740

3841
describe('startSpan()', function () {
39-
let root = new RootSpan();
42+
let root = new RootSpan(tracer);
4043
root.start()
4144
let span = root.startSpan("spanName", "spanType");
4245
it('should return a Span instance', function () {
@@ -50,7 +53,7 @@ describe('Trace', function () {
5053

5154
describe('end()', function () {
5255
it('the trace was ended', function () {
53-
let root = new RootSpan();
56+
let root = new RootSpan(tracer);
5457
root.start()
5558
root.end();
5659
assert.ok(root.ended);
@@ -59,23 +62,23 @@ describe('Trace', function () {
5962

6063
describe('end() before trace started', function () {
6164
it('the trace was not ended', function () {
62-
let root = new RootSpan();
65+
let root = new RootSpan(tracer);
6366
root.end();
6467
assert.ok(!root.ended);
6568
});
6669
});
6770

6871
describe('startSpan() before trace started', function () {
6972
it('should return null', function () {
70-
let root = new RootSpan();
73+
let root = new RootSpan(tracer);
7174
let span = root.startSpan("spanName", "spanType");
7275
assert.ok(span == null);
7376
});
7477
});
7578

7679
describe('startSpan() after trace ended', function () {
7780
it('should return null', function () {
78-
let root = new RootSpan();
81+
let root = new RootSpan(tracer);
7982
root.start()
8083
root.end();
8184
let span = root.startSpan("spanName", "spanType");

0 commit comments

Comments
 (0)