From 3e05188bfdf1cbb1cbf85d59e6a8b823f0420703 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Mon, 4 Jan 2021 17:27:14 -0800 Subject: [PATCH 1/3] fix(exporter-zipkin): support per-span service name either from its own attributes or its resource's attributes --- packages/opentelemetry-exporter-zipkin/src/zipkin.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts index 93a8fe3006f..ea8be143b64 100644 --- a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts +++ b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts @@ -59,10 +59,7 @@ export class ZipkinExporter implements SpanExporter { resultCallback: (result: ExportResult) => void ) { if (typeof this._serviceName !== 'string') { - this._serviceName = String( - spans[0].resource.attributes[SERVICE_RESOURCE.NAME] || - this.DEFAULT_SERVICE_NAME - ); + this._serviceName = this.DEFAULT_SERVICE_NAME; } this._logger.debug('Zipkin exporter export'); if (this._isShutdown) { @@ -109,7 +106,11 @@ export class ZipkinExporter implements SpanExporter { const zipkinSpans = spans.map(span => toZipkinSpan( span, - serviceName, + String( + span.attributes[SERVICE_RESOURCE.NAME] || + span.resource.attributes[SERVICE_RESOURCE.NAME] || + serviceName + ), this._statusCodeTagName, this._statusDescriptionTagName ) From d241930ff2a8e34214119b52e139a788dbaff068 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Mon, 11 Jan 2021 17:08:13 -0800 Subject: [PATCH 2/3] fix: add tests, fix failing tests for persistent service name --- .../src/zipkin.ts | 5 +- .../test/node/zipkin.test.ts | 177 ++++++++++++++++++ 2 files changed, 181 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts index ea8be143b64..ee44926c8a0 100644 --- a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts +++ b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts @@ -59,7 +59,10 @@ export class ZipkinExporter implements SpanExporter { resultCallback: (result: ExportResult) => void ) { if (typeof this._serviceName !== 'string') { - this._serviceName = this.DEFAULT_SERVICE_NAME; + this._serviceName = String( + spans[0].resource.attributes[SERVICE_RESOURCE.NAME] || + this.DEFAULT_SERVICE_NAME + ); } this._logger.debug('Zipkin exporter export'); if (this._isShutdown) { diff --git a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts index aa2e357b715..422ba8a8797 100644 --- a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts @@ -482,6 +482,183 @@ describe('Zipkin Exporter - node', () => { }); }); }); + + it('should set serviceName per-span if resource has one', () => { + const resource_service_name = 'resource_service_name'; + const resource_service_name_prime = 'resource_service_name_prime'; + + let requestBody: any; + const scope = nock('http://localhost:9411') + .post('/api/v2/spans', body => { + requestBody = body; + return true; + }) + .replyWithError(new Error('My Socket Error')); + + const parentSpanId = '5c1c63257de34c67'; + const startTime = 1566156729709; + const duration = 2000; + + const span1: ReadableSpan = { + name: 'my-span', + kind: api.SpanKind.INTERNAL, + parentSpanId, + spanContext: { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.NONE, + }, + startTime: [startTime, 0], + endTime: [startTime + duration, 0], + ended: true, + duration: [duration, 0], + status: { + code: api.StatusCode.OK, + }, + attributes: { + key1: 'value1', + key2: 'value2', + }, + links: [], + events: [ + { + name: 'my-event', + time: [startTime + 10, 0], + attributes: { key3: 'value3' }, + }, + ], + resource: new Resource({ + [SERVICE_RESOURCE.NAME]: resource_service_name, + }), + instrumentationLibrary: { name: 'default', version: '0.0.1' }, + }; + const span2: ReadableSpan = { + name: 'my-span', + kind: api.SpanKind.SERVER, + spanContext: { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.NONE, + }, + startTime: [startTime, 0], + endTime: [startTime + duration, 0], + ended: true, + duration: [duration, 0], + status: { + code: api.StatusCode.OK, + }, + attributes: {}, + links: [], + events: [], + resource: new Resource({ + [SERVICE_RESOURCE.NAME]: resource_service_name_prime, + }), + instrumentationLibrary: { name: 'default', version: '0.0.1' }, + }; + + const exporter = new ZipkinExporter({}); + + exporter.export([span1, span2], (result: ExportResult) => { + requestBody; + scope.done(); + assert.equal( + requestBody[0].localEndpoint.serviceName, + resource_service_name + ); + assert.equal( + requestBody[1].localEndpoint.serviceName, + resource_service_name_prime + ); + }); + }); + + it('should set serviceName per-span if span has attribute', () => { + const span_service_name = 'span_service_name'; + const span_service_name_prime = 'span_service_name_prime'; + + let requestBody: any; + const scope = nock('http://localhost:9411') + .post('/api/v2/spans', body => { + requestBody = body; + return true; + }) + .replyWithError(new Error('My Socket Error')); + + const parentSpanId = '5c1c63257de34c67'; + const startTime = 1566156729709; + const duration = 2000; + + const span1: ReadableSpan = { + name: 'my-span', + kind: api.SpanKind.INTERNAL, + parentSpanId, + spanContext: { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.NONE, + }, + startTime: [startTime, 0], + endTime: [startTime + duration, 0], + ended: true, + duration: [duration, 0], + status: { + code: api.StatusCode.OK, + }, + attributes: { + key1: 'value1', + key2: 'value2', + [SERVICE_RESOURCE.NAME]: span_service_name, + }, + links: [], + events: [ + { + name: 'my-event', + time: [startTime + 10, 0], + attributes: { key3: 'value3' }, + }, + ], + resource: Resource.empty(), + instrumentationLibrary: { name: 'default', version: '0.0.1' }, + }; + const span2: ReadableSpan = { + name: 'my-span', + kind: api.SpanKind.SERVER, + spanContext: { + traceId: 'd4cda95b652f4a1592b449d5929fda1b', + spanId: '6e0c63257de34c92', + traceFlags: TraceFlags.NONE, + }, + startTime: [startTime, 0], + endTime: [startTime + duration, 0], + ended: true, + duration: [duration, 0], + status: { + code: api.StatusCode.OK, + }, + attributes: { + [SERVICE_RESOURCE.NAME]: span_service_name_prime, + }, + links: [], + events: [], + resource: Resource.empty(), + instrumentationLibrary: { name: 'default', version: '0.0.1' }, + }; + + const exporter = new ZipkinExporter({}); + + exporter.export([span1, span2], (result: ExportResult) => { + requestBody; + scope.done(); + assert.equal( + requestBody[0].localEndpoint.serviceName, + span_service_name + ); + assert.equal( + requestBody[1].localEndpoint.serviceName, + span_service_name_prime + ); + }); + }); }); describe('shutdown', () => { From 9079b08337fe2a5439bcfa930fcfc3bd47382469 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Mon, 11 Jan 2021 17:13:05 -0800 Subject: [PATCH 3/3] fix: remove any type --- packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts index 422ba8a8797..efff9b139c7 100644 --- a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts @@ -487,7 +487,7 @@ describe('Zipkin Exporter - node', () => { const resource_service_name = 'resource_service_name'; const resource_service_name_prime = 'resource_service_name_prime'; - let requestBody: any; + let requestBody: zipkinTypes.Span[]; const scope = nock('http://localhost:9411') .post('/api/v2/spans', body => { requestBody = body;