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

Commit 075bebd

Browse files
Peter Martonmayurkale22
authored andcommitted
fix(zipkin-exporter): fix nested child span support (#574)
1 parent 405758f commit 075bebd

File tree

2 files changed

+93
-24
lines changed

2 files changed

+93
-24
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ export class ZipkinTraceExporter implements Exporter {
144144
/** RootSpan data */
145145
spanList.push(this.translateSpan(span));
146146

147-
// Builds spans data
148-
for (const child of span.spans) {
149-
spanList.push(this.translateSpan(child));
147+
// Traverse child spans recursively
148+
if (span.spans.length > 0) {
149+
Array.prototype.push.apply(spanList, this.mountSpanList(span.spans));
150150
}
151151
}
152152

packages/opencensus-exporter-zipkin/test/test-zipkin.ts

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {CanonicalCode, CoreTracer, MessageEventType, Span, SpanKind, TracerConfi
1818
import * as assert from 'assert';
1919
import * as nock from 'nock';
2020

21-
import {MICROS_PER_MILLI, ZipkinExporterOptions, ZipkinTraceExporter} from '../src/zipkin';
21+
import {MICROS_PER_MILLI, TranslatedSpan, ZipkinExporterOptions, ZipkinTraceExporter} from '../src/zipkin';
2222

2323
/** Zipkin host url */
2424
const zipkinHost = 'http://localhost:9411';
@@ -42,23 +42,6 @@ const defaultConfig: TracerConfig = {
4242
samplingRate: 1
4343
};
4444

45-
/** Run a nock server to replace zipkin service */
46-
const runNockServer = () => {
47-
nock(zipkinHost)
48-
.persist()
49-
.post(postPath)
50-
.reply(202)
51-
.post('/wrong')
52-
.reply(404);
53-
};
54-
55-
/** Checking if tests will use a real network, otherwise run a nock server */
56-
before(() => {
57-
if (!OPENCENSUS_NETWORK_TESTS) {
58-
runNockServer();
59-
}
60-
});
61-
6245
/** Zipkin tests */
6346
describe('Zipkin Exporter', function() {
6447
/** Desabling the timeout for tests */
@@ -89,14 +72,85 @@ describe('Zipkin Exporter', function() {
8972
const tracer = new CoreTracer();
9073
tracer.start(defaultConfig);
9174

75+
let scope: nock.Scope;
76+
let requestBody: [TranslatedSpan];
77+
78+
/**
79+
* Checking if tests will use a real network, otherwise run a nock server
80+
*/
81+
if (!OPENCENSUS_NETWORK_TESTS) {
82+
/** Run a nock server to replace zipkin service */
83+
scope = nock(zipkinHost)
84+
.persist()
85+
.post(
86+
postPath,
87+
(body: [TranslatedSpan]) => {
88+
requestBody = body;
89+
return true;
90+
})
91+
.reply(202);
92+
}
93+
9294
return tracer.startRootSpan(
9395
{name: 'root-test'}, async (rootSpan: Span) => {
94-
const span = rootSpan.startChildSpan(
96+
const span1 = rootSpan.startChildSpan(
9597
{name: 'spanTest', kind: SpanKind.CLIENT});
96-
span.end();
98+
const span2 = tracer.startChildSpan(
99+
{name: 'spanTest', kind: SpanKind.CLIENT, childOf: span1});
100+
span2.end();
101+
span1.end();
97102
rootSpan.end();
98-
return exporter.publish([rootSpan, rootSpan]).then((result) => {
103+
return exporter.publish([rootSpan]).then((result) => {
99104
assert.strictEqual(result.statusCode, 202);
105+
106+
if (!OPENCENSUS_NETWORK_TESTS) {
107+
scope.done();
108+
assert.deepStrictEqual(requestBody, [
109+
{
110+
'annotations': [],
111+
'debug': true,
112+
'duration':
113+
Math.round(rootSpan.duration * MICROS_PER_MILLI),
114+
'id': rootSpan.id,
115+
'kind': 'SERVER',
116+
'localEndpoint': {'serviceName': 'opencensus-tests'},
117+
'name': 'root-test',
118+
'shared': true,
119+
'tags': {'census.status_code': '0'},
120+
'timestamp':
121+
rootSpan.startTime.getTime() * MICROS_PER_MILLI,
122+
'traceId': rootSpan.traceId
123+
},
124+
{
125+
'annotations': [],
126+
'debug': true,
127+
'duration': Math.round(span1.duration * MICROS_PER_MILLI),
128+
'id': span1.id,
129+
'kind': 'CLIENT',
130+
'localEndpoint': {'serviceName': 'opencensus-tests'},
131+
'name': 'spanTest',
132+
'parentId': rootSpan.id,
133+
'shared': false,
134+
'tags': {'census.status_code': '0'},
135+
'timestamp': span1.startTime.getTime() * MICROS_PER_MILLI,
136+
'traceId': span1.traceId
137+
},
138+
{
139+
'annotations': [],
140+
'debug': true,
141+
'duration': Math.round(span2.duration * MICROS_PER_MILLI),
142+
'id': span2.id,
143+
'kind': 'CLIENT',
144+
'localEndpoint': {'serviceName': 'opencensus-tests'},
145+
'name': 'spanTest',
146+
'parentId': span1.id,
147+
'shared': false,
148+
'tags': {'census.status_code': '0'},
149+
'timestamp': span2.startTime.getTime() * MICROS_PER_MILLI,
150+
'traceId': span2.traceId
151+
}
152+
]);
153+
}
100154
});
101155
});
102156
});
@@ -190,6 +244,17 @@ describe('Zipkin Exporter', function() {
190244
serviceName: 'opencensus-tests'
191245
};
192246

247+
let scope: nock.Scope;
248+
249+
/**
250+
* Checking if tests will use a real network, otherwise run a nock
251+
* server
252+
*/
253+
if (!OPENCENSUS_NETWORK_TESTS) {
254+
/** Run a nock server to replace zipkin service */
255+
scope = nock(zipkinHost).persist().post('/wrong').reply(404);
256+
}
257+
193258
const exporter = new ZipkinTraceExporter(options);
194259
const tracer = new CoreTracer();
195260
tracer.start(defaultConfig);
@@ -202,6 +267,10 @@ describe('Zipkin Exporter', function() {
202267
rootSpan.end();
203268
return exporter.publish([rootSpan]).then((result) => {
204269
assert.strictEqual(result.statusCode, 404);
270+
271+
if (!OPENCENSUS_NETWORK_TESTS) {
272+
scope.done();
273+
}
205274
});
206275
});
207276
});

0 commit comments

Comments
 (0)