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

Commit d4c3b19

Browse files
Peter Martonmayurkale22
authored andcommitted
fix(tracer): report every span to exporters (#493)
* fix(tracer): report every span to exporters * test(exporter-zipkin): separate root and child span translation tests * refactor(exporters): address PR comments * test(exporter-mongodb): fix * test(redis): fix exporter tests
1 parent 96a6ab7 commit d4c3b19

File tree

22 files changed

+309
-301
lines changed

22 files changed

+309
-301
lines changed

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export class ConsoleExporter implements Exporter {
4848
this.logger = config.logger;
4949
}
5050

51-
onStartSpan(root: modelTypes.Span) {}
51+
onStartSpan(span: modelTypes.Span) {}
5252

5353
/**
5454
* Event called when a span is ended.
55-
* @param root Ended span.
55+
* @param span Ended span.
5656
*/
57-
onEndSpan(root: modelTypes.Span) {
58-
this.buffer.addToBuffer(root);
57+
onEndSpan(span: modelTypes.Span) {
58+
this.buffer.addToBuffer(span);
5959
}
6060

6161
/**

packages/opencensus-core/src/exporters/exporter-buffer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ export class ExporterBuffer {
7575
}
7676

7777
/**
78-
* Add a rootSpan in the buffer.
79-
* @param root RootSpan to be added in the buffer.
78+
* Add a span in the buffer.
79+
* @param span Span to be added in the buffer.
8080
*/
81-
addToBuffer(root: modelTypes.Span) {
82-
this.queue.push(root);
83-
this.logger.debug('ExporterBuffer: added new rootspan');
81+
addToBuffer(span: modelTypes.Span) {
82+
this.queue.push(span);
83+
this.logger.debug('ExporterBuffer: added new span');
8484

8585
if (this.queue.length > this.bufferSize) {
8686
this.flush();

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import * as types from './types';
2020

2121
/** Defines a root span */
2222
export class RootSpan extends Span {
23-
/** A tracer object */
24-
private tracer: types.TracerBase;
2523
/** Its trace ID. */
2624
private traceIdLocal: string;
2725
/** Its trace state. */
@@ -45,8 +43,7 @@ export class RootSpan extends Span {
4543
constructor(
4644
tracer: types.TracerBase, name: string, kind: types.SpanKind,
4745
traceId: string, parentSpanId: string, traceState?: types.TraceState) {
48-
super();
49-
this.tracer = tracer;
46+
super(tracer);
5047
this.traceIdLocal = traceId;
5148
this.name = name;
5249
this.kind = kind;
@@ -71,17 +68,4 @@ export class RootSpan extends Span {
7168
get parentSpanId(): string {
7269
return this.parentSpanIdLocal;
7370
}
74-
75-
/** Starts a rootspan instance. */
76-
start() {
77-
super.start();
78-
79-
this.tracer.onStartSpan(this);
80-
}
81-
82-
/** Ends a rootspan instance. */
83-
end() {
84-
super.end();
85-
this.tracer.onEndSpan(this);
86-
}
8771
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const STATUS_OK = {
2929
/** Defines a base model for spans. */
3030
export class Span implements types.Span {
3131
protected className: string;
32+
/** A tracer object */
33+
private tracer: types.TracerBase;
3234
/** The clock used to mesure the beginning and ending of a span */
3335
private clock!: Clock;
3436
/** Indicates if this span was started */
@@ -77,7 +79,8 @@ export class Span implements types.Span {
7779
droppedMessageEventsCount = 0;
7880

7981
/** Constructs a new Span instance. */
80-
constructor(parent?: Span) {
82+
constructor(tracer: types.TracerBase, parent?: Span) {
83+
this.tracer = tracer;
8184
this.className = this.constructor.name;
8285
this.id = randomSpanId();
8386
this.spansLocal = [];
@@ -310,6 +313,8 @@ export class Span implements types.Span {
310313
parentSpanId: this.parentSpanId,
311314
traceState: this.traceState
312315
});
316+
317+
this.tracer.onStartSpan(this);
313318
}
314319

315320
/** Ends the span and all of its children, recursively. */
@@ -335,6 +340,8 @@ export class Span implements types.Span {
335340
span.truncate();
336341
}
337342
}
343+
344+
this.tracer.onEndSpan(this);
338345
}
339346

340347
/** Forces the span to end. */
@@ -366,7 +373,7 @@ export class Span implements types.Span {
366373
return new NoRecordSpan();
367374
}
368375

369-
const child = new Span(this);
376+
const child = new Span(this.tracer, this);
370377
const spanName =
371378
typeof nameOrOptions === 'object' ? nameOrOptions.name : nameOrOptions;
372379
const spanKind =

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,20 @@ export class CoreTracerBase implements types.TracerBase {
180180
}
181181
}
182182

183-
private notifyStartSpan(root: types.Span) {
184-
this.logger.debug('starting to notify listeners the start of rootspans');
183+
private notifyStartSpan(span: types.Span) {
184+
this.logger.debug('starting to notify listeners the start of spans');
185185
if (this.eventListenersLocal && this.eventListenersLocal.length > 0) {
186186
for (const listener of this.eventListenersLocal) {
187-
listener.onStartSpan(root);
187+
listener.onStartSpan(span);
188188
}
189189
}
190190
}
191191

192-
private notifyEndSpan(root: types.Span) {
193-
this.logger.debug('starting to notify listeners the end of rootspans');
192+
private notifyEndSpan(span: types.Span) {
193+
this.logger.debug('starting to notify listeners the end of spans');
194194
if (this.eventListenersLocal && this.eventListenersLocal.length > 0) {
195195
for (const listener of this.eventListenersLocal) {
196-
listener.onEndSpan(root);
196+
listener.onEndSpan(span);
197197
}
198198
}
199199
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe('RootSpan', () => {
293293
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
294294
rootSpan.start();
295295

296-
const span = new Span(rootSpan);
296+
const span = new Span(tracer, rootSpan);
297297
span.start();
298298

299299
rootSpan.addLink(

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('Span', () => {
4444
describe('new Span()', () => {
4545
it('should create a Span instance', () => {
4646
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
47-
const span = new Span(rootSpan);
47+
const span = new Span(tracer, rootSpan);
4848
assert.ok(span instanceof Span);
4949
});
5050
});
@@ -56,7 +56,7 @@ describe('Span', () => {
5656
it('should return the trace id', () => {
5757
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
5858
rootSpan.start();
59-
const span = new Span(rootSpan);
59+
const span = new Span(tracer, rootSpan);
6060
assert.equal(span.traceId, rootSpan.traceId);
6161
});
6262
});
@@ -69,7 +69,7 @@ describe('Span', () => {
6969
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
7070
rootSpan.start();
7171

72-
const span = new Span(rootSpan);
72+
const span = new Span(tracer, rootSpan);
7373
const context = span.spanContext;
7474

7575
assert.equal(context.traceId, rootSpan.traceId);
@@ -87,7 +87,7 @@ describe('Span', () => {
8787
before(() => {
8888
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
8989
rootSpan.start();
90-
span = new Span(rootSpan);
90+
span = new Span(tracer, rootSpan);
9191
});
9292
it('should get startTime()', () => {
9393
assert.ok(span.startTime);
@@ -108,7 +108,7 @@ describe('Span', () => {
108108
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
109109
rootSpan.start();
110110

111-
const span = new Span(rootSpan);
111+
const span = new Span(tracer, rootSpan);
112112
span.start();
113113

114114
assert.ok(span.started);
@@ -122,7 +122,7 @@ describe('Span', () => {
122122
it('should not change the initial startTime', () => {
123123
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
124124
rootSpan.start();
125-
const span = new Span(rootSpan);
125+
const span = new Span(tracer, rootSpan);
126126
span.start();
127127
const initialStartTime = span.startTime;
128128
span.start();
@@ -139,7 +139,7 @@ describe('Span', () => {
139139
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
140140
rootSpan.start();
141141

142-
const span = new Span(rootSpan);
142+
const span = new Span(tracer, rootSpan);
143143
span.start();
144144
span.end();
145145

@@ -155,7 +155,7 @@ describe('Span', () => {
155155
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
156156
rootSpan.start();
157157

158-
const span = new Span(rootSpan);
158+
const span = new Span(tracer, rootSpan);
159159
span.end();
160160

161161
assert.ok(!span.ended);
@@ -169,7 +169,7 @@ describe('Span', () => {
169169
it('should not change the endTime', () => {
170170
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
171171
rootSpan.start();
172-
const span = new Span(rootSpan);
172+
const span = new Span(tracer, rootSpan);
173173
span.start();
174174
span.end();
175175
const initialEndTime = span.endTime;
@@ -187,7 +187,7 @@ describe('Span', () => {
187187
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
188188
rootSpan.start();
189189

190-
const span = new Span(rootSpan);
190+
const span = new Span(tracer, rootSpan);
191191
span.start();
192192

193193
['String', 'Number', 'Boolean'].map(attType => {
@@ -205,7 +205,7 @@ describe('Span', () => {
205205
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
206206
rootSpan.start();
207207

208-
const span = new Span(rootSpan);
208+
const span = new Span(tracer, rootSpan);
209209
span.start();
210210
for (let i = 0; i < 40; i++) {
211211
span.addAttribute('attr' + i, 100);
@@ -230,7 +230,7 @@ describe('Span', () => {
230230
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
231231
rootSpan.start();
232232

233-
const span = new Span(rootSpan);
233+
const span = new Span(tracer, rootSpan);
234234
span.start();
235235

236236
span.addAnnotation('description test', {} as Attributes, Date.now());
@@ -244,7 +244,7 @@ describe('Span', () => {
244244
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
245245
rootSpan.start();
246246

247-
const span = new Span(rootSpan);
247+
const span = new Span(tracer, rootSpan);
248248
span.start();
249249
for (let i = 0; i < 40; i++) {
250250
span.addAnnotation('description test', {} as Attributes, Date.now());
@@ -268,7 +268,7 @@ describe('Span', () => {
268268
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
269269
rootSpan.start();
270270

271-
const span = new Span(rootSpan);
271+
const span = new Span(tracer, rootSpan);
272272
span.start();
273273

274274
span.addLink(
@@ -282,7 +282,7 @@ describe('Span', () => {
282282
it('should drop extra links', () => {
283283
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
284284
rootSpan.start();
285-
const span = new Span(rootSpan);
285+
const span = new Span(tracer, rootSpan);
286286
span.start();
287287

288288
for (let i = 0; i < 35; i++) {
@@ -308,7 +308,7 @@ describe('Span', () => {
308308
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
309309
rootSpan.start();
310310

311-
const span = new Span(rootSpan);
311+
const span = new Span(tracer, rootSpan);
312312
span.start();
313313

314314
span.addMessageEvent(
@@ -332,7 +332,7 @@ describe('Span', () => {
332332
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
333333
rootSpan.start();
334334

335-
const span = new Span(rootSpan);
335+
const span = new Span(tracer, rootSpan);
336336
span.start();
337337
for (let i = 0; i < 35; i++) {
338338
span.addMessageEvent(types.MessageEventType.UNSPECIFIED, 1);
@@ -348,7 +348,7 @@ describe('Span', () => {
348348
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
349349
rootSpan.start();
350350

351-
const span = new Span(rootSpan);
351+
const span = new Span(tracer, rootSpan);
352352
span.start();
353353

354354
assert.equal(rootSpan.status.code, 0);
@@ -360,7 +360,7 @@ describe('Span', () => {
360360
it('should set an error status', () => {
361361
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
362362
rootSpan.start();
363-
const span = new Span(rootSpan);
363+
const span = new Span(tracer, rootSpan);
364364
span.start();
365365
span.setStatus(types.CanonicalCode.PERMISSION_DENIED, 'This is an error');
366366

packages/opencensus-exporter-instana/src/instana.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export class InstanaTraceExporter implements Exporter {
6060
this.exporterBuffer = new ExporterBuffer(this, options);
6161
}
6262

63-
onStartSpan(root: Span) {}
63+
onStartSpan(span: Span) {}
6464

65-
onEndSpan(root: Span) {
66-
this.exporterBuffer.addToBuffer(root);
65+
onEndSpan(span: Span) {
66+
this.exporterBuffer.addToBuffer(span);
6767
}
6868

6969
/**

packages/opencensus-exporter-jaeger/src/jaeger.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,24 @@ export class JaegerTraceExporter implements Exporter {
9898

9999
/**
100100
* Is called whenever a span is ended.
101-
* @param root the ended span
101+
* @param span the ended span
102102
*/
103-
onEndSpan(root: Span) {
104-
this.logger.debug('onEndSpan: adding rootSpan: %s', root.name);
103+
onEndSpan(span: Span) {
104+
// Add spans of a trace together when root is ended, skip non root spans.
105+
if (span.constructor.name !== 'RootSpan') {
106+
return;
107+
}
108+
109+
this.logger.debug('onEndSpan: adding rootSpan: %s', span.name);
105110

106111
// UDPSender buffer is limited by maxPacketSize
107-
this.addSpanToSenderBuffer(root)
112+
this.addSpanToSenderBuffer(span)
108113
.then(result => {
109-
this.addToBuffer(root, result as number);
110-
for (const span of root.spans) {
111-
this.addSpanToSenderBuffer(span)
114+
this.addToBuffer(span, result as number);
115+
for (const localSpan of span.spans) {
116+
this.addSpanToSenderBuffer(localSpan)
112117
.then(result => {
113-
this.addToBuffer(span, result as number);
118+
this.addToBuffer(localSpan, result as number);
114119
})
115120
.catch(err => {
116121
return;

packages/opencensus-exporter-ocagent/src/ocagent.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,15 @@ export class OCAgentExporter implements Exporter {
204204
}
205205
}
206206

207-
onStartSpan(root: Span) {}
207+
onStartSpan(span: Span) {}
208208

209-
onEndSpan(root: Span) {
210-
this.buffer.addToBuffer(root);
209+
onEndSpan(span: Span) {
210+
// Add spans of a trace together when root is ended, skip non root spans.
211+
// adaptRootSpan() will extract child spans from root.
212+
if (span.constructor.name !== 'RootSpan') {
213+
return;
214+
}
215+
this.buffer.addToBuffer(span);
211216
}
212217

213218
publish(rootSpans: Span[]): Promise<number|string|void> {

0 commit comments

Comments
 (0)