From f6018be854c9b9159fc8552eaf609ba09dd5b596 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sun, 11 Apr 2021 10:20:47 +0200 Subject: [PATCH] feat(zipkin): allow to configure url via environment #1675 --- .../src/utils/environment.ts | 2 ++ .../src/zipkin.ts | 5 ++-- .../test/browser/zipkin.test.ts | 28 +++++++++++++++++++ .../test/node/zipkin.test.ts | 14 ++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-core/src/utils/environment.ts b/packages/opentelemetry-core/src/utils/environment.ts index e50318db14..ac85665699 100644 --- a/packages/opentelemetry-core/src/utils/environment.ts +++ b/packages/opentelemetry-core/src/utils/environment.ts @@ -67,6 +67,7 @@ export type ENVIRONMENT = { OTEL_EXPORTER_JAEGER_ENDPOINT?: string; OTEL_EXPORTER_JAEGER_PASSWORD?: string; OTEL_EXPORTER_JAEGER_USER?: string; + OTEL_EXPORTER_ZIPKIN_ENDPOINT?: string; OTEL_LOG_LEVEL?: DiagLogLevel; OTEL_RESOURCE_ATTRIBUTES?: string; } & ENVIRONMENT_NUMBERS & @@ -94,6 +95,7 @@ export const DEFAULT_ENVIRONMENT: Required = { OTEL_EXPORTER_JAEGER_ENDPOINT: '', OTEL_EXPORTER_JAEGER_PASSWORD: '', OTEL_EXPORTER_JAEGER_USER: '', + OTEL_EXPORTER_ZIPKIN_ENDPOINT: 'http://localhost:9411/api/v2/spans', OTEL_LOG_LEVEL: DiagLogLevel.INFO, OTEL_NO_PATCH_MODULES: [], OTEL_PROPAGATORS: ['tracecontext', 'baggage'], diff --git a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts index ea53db39a4..6fbaeb25c6 100644 --- a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts +++ b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts @@ -15,7 +15,7 @@ */ import { diag } from '@opentelemetry/api'; -import { ExportResult, ExportResultCode } from '@opentelemetry/core'; +import { ExportResult, ExportResultCode, getEnv } from '@opentelemetry/core'; import { SpanExporter, ReadableSpan } from '@opentelemetry/tracing'; import { prepareSend } from './platform/index'; import * as zipkinTypes from './types'; @@ -31,7 +31,6 @@ import { prepareGetHeaders } from './utils'; * Zipkin Exporter */ export class ZipkinExporter implements SpanExporter { - static readonly DEFAULT_URL = 'http://localhost:9411/api/v2/spans'; private readonly DEFAULT_SERVICE_NAME = 'OpenTelemetry Service'; private readonly _statusCodeTagName: string; private readonly _statusDescriptionTagName: string; @@ -43,7 +42,7 @@ export class ZipkinExporter implements SpanExporter { private _sendingPromises: Promise[] = []; constructor(config: zipkinTypes.ExporterConfig = {}) { - this._urlStr = config.url || ZipkinExporter.DEFAULT_URL; + this._urlStr = config.url || getEnv().OTEL_EXPORTER_ZIPKIN_ENDPOINT; this._send = prepareSend(this._urlStr, config.headers); this._serviceName = config.serviceName; this._statusCodeTagName = config.statusCodeTagName || statusCodeTagName; diff --git a/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts index 2f954d7bd2..8b1a99666c 100644 --- a/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts @@ -96,6 +96,34 @@ describe('Zipkin Exporter - web', () => { }); }); }); + + describe('should use url defined in environment', () => { + let server: any; + const endpointUrl = 'http://localhost:9412'; + beforeEach(() => { + (window.navigator as any).sendBeacon = false; + (window as any).OTEL_EXPORTER_ZIPKIN_ENDPOINT = endpointUrl; + zipkinExporter = new ZipkinExporter(zipkinConfig); + server = sinon.fakeServer.create(); + }); + afterEach(() => { + server.restore(); + }); + + it('should successfully send the spans using XMLHttpRequest', done => { + zipkinExporter.export(spans, () => {}); + + setTimeout(() => { + const request = server.requests[0]; + assert(request.url, endpointUrl); + const body = request.requestBody; + const json = JSON.parse(body) as any; + ensureSpanIsCorrect(json[0]); + + done(); + }); + }); + }); }); describe('when getExportRequestHeaders is defined', () => { let server: any; diff --git a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts index 9ddeade89e..9c63092ec4 100644 --- a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts @@ -650,6 +650,20 @@ describe('Zipkin Exporter - node', () => { ); }); }); + + it('should support setting url via env', () => { + process.env.OTEL_EXPORTER_ZIPKIN_ENDPOINT = 'http://localhost:9412'; + const scope = nock('http://localhost:9412').post('/').reply(200); + + const exporter = new ZipkinExporter({ + serviceName: 'my-service', + }); + + exporter.export([getReadableSpan()], (result: ExportResult) => { + scope.done(); + assert.strictEqual(result.code, ExportResultCode.SUCCESS); + }); + }); }); describe('shutdown', () => {