From c9c1b2a6f3ebaa374ce9335d347273037f8021d5 Mon Sep 17 00:00:00 2001 From: David Luna Date: Fri, 8 Nov 2024 17:00:47 +0100 Subject: [PATCH 1/6] feat: remove addSpanProcessor API from BasicTraceProvider --- examples/esm-http-ts/index.ts | 7 +-- .../test/OTLPTraceExporter.test.ts | 12 +++-- .../test/fetch.test.ts | 9 ++-- .../test/grpc-protobuf-ts.test.ts | 5 +- .../test/helper.ts | 25 +++++---- .../test/functionals/http-enable.test.ts | 5 +- .../test/functionals/http-package.test.ts | 5 +- .../test/functionals/https-enable.test.ts | 5 +- .../test/functionals/https-package.test.ts | 5 +- .../test/integrations/esm.test.mjs | 5 +- .../test/integrations/http-enable.test.ts | 5 +- .../test/integrations/https-enable.test.ts | 5 +- .../test/unmocked.test.ts | 7 +-- .../test/xhr.test.ts | 20 ++++---- .../src/BasicTracerProvider.ts | 40 +++++---------- .../opentelemetry-sdk-trace-base/src/types.ts | 6 +++ .../test/common/BasicTracerProvider.test.ts | 13 +++-- .../test/common/MultiSpanProcessor.test.ts | 51 ++++++++----------- .../test/common/Span.test.ts | 24 ++++----- .../test/common/Tracer.test.ts | 10 ++-- .../common/export/ConsoleSpanExporter.test.ts | 7 +-- .../export/InMemorySpanExporter.test.ts | 5 +- .../common/export/TestTracingSpanExporter.ts | 6 +-- 23 files changed, 139 insertions(+), 143 deletions(-) diff --git a/examples/esm-http-ts/index.ts b/examples/esm-http-ts/index.ts index 38ea5680471..d26738a026f 100644 --- a/examples/esm-http-ts/index.ts +++ b/examples/esm-http-ts/index.ts @@ -11,14 +11,15 @@ import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; import http from 'http'; diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); +const exporter = new ConsoleSpanExporter(); +const processor = new SimpleSpanProcessor(exporter); + const tracerProvider = new NodeTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'esm-http-ts-example', }), + spanProcessors: [processor], }); -const exporter = new ConsoleSpanExporter(); -const processor = new SimpleSpanProcessor(exporter); -tracerProvider.addSpanProcessor(processor); tracerProvider.register(); registerInstrumentations({ diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index 801f85a36b9..b4286546e6c 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -138,8 +138,10 @@ const testCollectorExporter = (params: TestParams) => { metadata: metadata, }); - const provider = new BasicTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(collectorExporter)); + // @ts-expect-error -- we need to create a provider to test the exporter + const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(collectorExporter)] + }); done(); }); @@ -271,8 +273,10 @@ const testCollectorExporter = (params: TestParams) => { compression: CompressionAlgorithm.GZIP, }); - const provider = new BasicTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(collectorExporter)); + // @ts-expect-error -- we need to create a provider to test the exporter + const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(collectorExporter)] + }); }); it('should successfully send the spans', done => { const responseSpy = sinon.spy(); diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 7e14cc35883..2105e906771 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -253,18 +253,17 @@ describe('fetch', () => { } fetchInstrumentation = new FetchInstrumentation(config); - webTracerProviderWithZone = new WebTracerProvider(); + dummySpanExporter = new DummySpanExporter(); + webTracerProviderWithZone = new WebTracerProvider({ + spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)] + }); registerInstrumentations({ tracerProvider: webTracerProviderWithZone, instrumentations: [fetchInstrumentation], }); webTracerWithZone = webTracerProviderWithZone.getTracer('fetch-test'); - dummySpanExporter = new DummySpanExporter(); exportSpy = sinon.stub(dummySpanExporter, 'export'); clearResourceTimingsSpy = sinon.stub(performance, 'clearResourceTimings'); - webTracerProviderWithZone.addSpanProcessor( - new tracing.SimpleSpanProcessor(dummySpanExporter) - ); // endSpan is called after the whole response body is read // this process is scheduled at the same time the fetch promise is resolved diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-protobuf-ts.test.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-protobuf-ts.test.ts index 8e238a6acc1..df920ae059f 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-protobuf-ts.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/grpc-protobuf-ts.test.ts @@ -154,9 +154,10 @@ function shouldNotCreateSpans( describe('#grpc-protobuf', () => { let client: GrpcTesterClient; let server: grpc.Server; - const provider = new NodeTracerProvider(); let contextManager: ContextManager; - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], + }); before(() => { propagation.setGlobalPropagator(new W3CTraceContextPropagator()); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts index 131dde04b4b..15f3824355c 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts @@ -755,8 +755,9 @@ export const runTests = ( }; describe('enable()', () => { - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + }); beforeEach(() => { memoryExporter.reset(); }); @@ -799,8 +800,9 @@ export const runTests = ( }); describe('disable()', () => { - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + }); beforeEach(() => { memoryExporter.reset(); }); @@ -830,8 +832,9 @@ export const runTests = ( }); describe('Test filtering requests using metadata', () => { - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + }); beforeEach(() => { memoryExporter.reset(); }); @@ -859,7 +862,9 @@ export const runTests = ( }); describe('Test filtering requests using options', () => { - const provider = new NodeTracerProvider(); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], + }); const checkSpans: { [key: string]: boolean } = { unaryMethod: false, UnaryMethod: false, @@ -868,7 +873,6 @@ export const runTests = ( ServerStreamMethod: true, BidiStreamMethod: false, }; - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); beforeEach(() => { memoryExporter.reset(); }); @@ -936,8 +940,9 @@ export const runTests = ( }); describe('Test capturing metadata', () => { - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + }); const clientMetadata = new Metadata(); clientMetadata.add('client_metadata_key', 'client_metadata_value'); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index b6470e1f3e6..8de692be739 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -98,8 +98,9 @@ const hostname = 'localhost'; const pathname = '/test'; const serverName = 'my.server.name'; const memoryExporter = new InMemorySpanExporter(); -const provider = new NodeTracerProvider(); -provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); +const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], +}); instrumentation.setTracerProvider(provider); function doNock( diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts index 62ad4ccebc0..63418999773 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts @@ -55,8 +55,9 @@ describe('Packages', () => { context.disable(); }); describe('get', () => { - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], + }); instrumentation.setTracerProvider(provider); beforeEach(() => { memoryExporter.reset(); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts index 0f48a30ae72..ddf064834e5 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts @@ -65,10 +65,11 @@ const hostname = 'localhost'; const serverName = 'my.server.name'; const pathname = '/test'; const memoryExporter = new InMemorySpanExporter(); -const provider = new BasicTracerProvider(); +const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], +}); instrumentation.setTracerProvider(provider); const tracer = provider.getTracer('test-https'); -provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); function doNock( hostname: string, diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts index 80a003f535d..85e12123ed1 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts @@ -55,8 +55,9 @@ describe('Packages', () => { context.disable(); }); describe('get', () => { - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], + }); instrumentation.setTracerProvider(provider); beforeEach(() => { memoryExporter.reset(); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/esm.test.mjs b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/esm.test.mjs index db35b4ee455..1286d166737 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/esm.test.mjs +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/esm.test.mjs @@ -31,9 +31,10 @@ import { import { assertSpan } from '../../build/test/utils/assertSpan.js'; import { HttpInstrumentation } from '../../build/src/index.js'; -const provider = new NodeTracerProvider(); const memoryExporter = new InMemorySpanExporter(); -provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); +const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], +}); const instrumentation = new HttpInstrumentation(); instrumentation.setTracerProvider(provider); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts index 1f7829c4d22..bba57d1c5fb 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts @@ -130,8 +130,9 @@ describe('HttpInstrumentation Integration tests', () => { }); }); - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], + }); instrumentation.setTracerProvider(provider); beforeEach(() => { memoryExporter.reset(); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts index aa19a8629f9..7a1e57236e6 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts @@ -131,8 +131,9 @@ describe('HttpsInstrumentation Integration tests', () => { done(); }); }); - const provider = new NodeTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + }); instrumentation.setTracerProvider(provider); beforeEach(() => { memoryExporter.reset(); diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts index 0886650e114..4f780e27ff8 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts @@ -41,13 +41,14 @@ describe('unmocked xhr', () => { let testSpans: TestSpanProcessor; let provider: WebTracerProvider; beforeEach(() => { - provider = new WebTracerProvider(); + testSpans = new TestSpanProcessor(); + provider = new WebTracerProvider({ + spanProcessors: [testSpans] + }); registerInstrumentations({ instrumentations: [new XMLHttpRequestInstrumentation()], tracerProvider: provider, }); - testSpans = new TestSpanProcessor(); - provider.addSpanProcessor(testSpans); }); afterEach(() => { // nop diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index f3685e06a13..9f64b4674e8 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -268,21 +268,21 @@ describe('xhr', () => { xmlHttpRequestInstrumentation = new XMLHttpRequestInstrumentation( config ); - webTracerProviderWithZone = new WebTracerProvider(); + dummySpanExporter = new DummySpanExporter(); + webTracerProviderWithZone = new WebTracerProvider({ + spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)] + }); registerInstrumentations({ instrumentations: [xmlHttpRequestInstrumentation], tracerProvider: webTracerProviderWithZone, }); webTracerWithZone = webTracerProviderWithZone.getTracer('xhr-test'); - dummySpanExporter = new DummySpanExporter(); + exportSpy = sinon.stub(dummySpanExporter, 'export'); clearResourceTimingsSpy = sinon.stub( performance as unknown as Performance, 'clearResourceTimings' ); - webTracerProviderWithZone.addSpanProcessor( - new tracing.SimpleSpanProcessor(dummySpanExporter) - ); rootSpan = webTracerWithZone.startSpan('root'); api.context.with( @@ -845,18 +845,18 @@ describe('xhr', () => { ); spyEntries.withArgs('resource').returns(resources); - webTracerWithZoneProvider = new WebTracerProvider(); + dummySpanExporter = new DummySpanExporter(); + webTracerWithZoneProvider = new WebTracerProvider({ + spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)] + }); registerInstrumentations({ instrumentations: [new XMLHttpRequestInstrumentation(config)], tracerProvider: webTracerWithZoneProvider, }); - dummySpanExporter = new DummySpanExporter(); + exportSpy = sinon.stub(dummySpanExporter, 'export'); - webTracerWithZoneProvider.addSpanProcessor( - new tracing.SimpleSpanProcessor(dummySpanExporter) - ); webTracerWithZone = webTracerWithZoneProvider.getTracer('xhr-test'); rootSpan = webTracerWithZone.startSpan('root'); diff --git a/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts b/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts index 51866a4538e..3c5656cfb15 100644 --- a/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts +++ b/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts @@ -85,12 +85,19 @@ export class BasicTracerProvider implements TracerProvider { resource: this.resource, }); - const defaultExporter = this._buildExporterFromEnv(); - if (defaultExporter !== undefined) { - const batchProcessor = new BatchSpanProcessor(defaultExporter); - this.activeSpanProcessor = batchProcessor; + if (config.spanProcessors?.length) { + this._registeredSpanProcessors = [...config.spanProcessors]; + this.activeSpanProcessor = new MultiSpanProcessor( + this._registeredSpanProcessors + ); } else { - this.activeSpanProcessor = new NoopSpanProcessor(); + const defaultExporter = this._buildExporterFromEnv(); + if (defaultExporter !== undefined) { + const batchProcessor = new BatchSpanProcessor(defaultExporter); + this.activeSpanProcessor = batchProcessor; + } else { + this.activeSpanProcessor = new NoopSpanProcessor(); + } } } @@ -115,29 +122,6 @@ export class BasicTracerProvider implements TracerProvider { return this._tracers.get(key)!; } - /** - * Adds a new {@link SpanProcessor} to this tracer. - * @param spanProcessor the new SpanProcessor to be added. - */ - addSpanProcessor(spanProcessor: SpanProcessor): void { - if (this._registeredSpanProcessors.length === 0) { - // since we might have enabled by default a batchProcessor, we disable it - // before adding the new one - this.activeSpanProcessor - .shutdown() - .catch(err => - diag.error( - 'Error while trying to shutdown current span processor', - err - ) - ); - } - this._registeredSpanProcessors.push(spanProcessor); - this.activeSpanProcessor = new MultiSpanProcessor( - this._registeredSpanProcessors - ); - } - getActiveSpanProcessor(): SpanProcessor { return this.activeSpanProcessor; } diff --git a/packages/opentelemetry-sdk-trace-base/src/types.ts b/packages/opentelemetry-sdk-trace-base/src/types.ts index 99eeedbe817..9c3bc222a8c 100644 --- a/packages/opentelemetry-sdk-trace-base/src/types.ts +++ b/packages/opentelemetry-sdk-trace-base/src/types.ts @@ -18,6 +18,7 @@ import { ContextManager, TextMapPropagator } from '@opentelemetry/api'; import { IResource } from '@opentelemetry/resources'; import { IdGenerator } from './IdGenerator'; import { Sampler } from './Sampler'; +import { SpanProcessor } from './SpanProcessor'; /** * TracerConfig provides an interface for configuring a Basic Tracer. @@ -48,6 +49,11 @@ export interface TracerConfig { * The default value is 30000ms */ forceFlushTimeoutMillis?: number; + + /** + * List of SpanProcessor for the tracer + */ + spanProcessors?: SpanProcessor[]; } /** diff --git a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts index 92fe52605b6..ddbcb537396 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts @@ -767,12 +767,11 @@ describe('BasicTracerProvider', () => { ); forceFlushStub.resolves(); - const tracerProvider = new BasicTracerProvider(); const spanProcessorOne = new NoopSpanProcessor(); const spanProcessorTwo = new NoopSpanProcessor(); - - tracerProvider.addSpanProcessor(spanProcessorOne); - tracerProvider.addSpanProcessor(spanProcessorTwo); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [spanProcessorOne, spanProcessorTwo] + }); tracerProvider .forceFlush() @@ -796,11 +795,11 @@ describe('BasicTracerProvider', () => { ); forceFlushStub.returns(Promise.reject('Error')); - const tracerProvider = new BasicTracerProvider(); const spanProcessorOne = new NoopSpanProcessor(); const spanProcessorTwo = new NoopSpanProcessor(); - tracerProvider.addSpanProcessor(spanProcessorOne); - tracerProvider.addSpanProcessor(spanProcessorTwo); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [spanProcessorOne, spanProcessorTwo] + }); tracerProvider .forceFlush() diff --git a/packages/opentelemetry-sdk-trace-base/test/common/MultiSpanProcessor.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/MultiSpanProcessor.test.ts index 7412a121ca3..b28bd69ba5d 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/MultiSpanProcessor.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/MultiSpanProcessor.test.ts @@ -53,38 +53,25 @@ describe('MultiSpanProcessor', () => { } }); - it('should handle empty span processor', () => { - const multiSpanProcessor = new MultiSpanProcessor([]); - - const tracerProvider = new BasicTracerProvider(); - tracerProvider.addSpanProcessor(multiSpanProcessor); - const tracer = tracerProvider.getTracer('default'); - const span = tracer.startSpan('one'); - span.end(); - multiSpanProcessor.shutdown(); - }); - it('should handle one span processor', () => { const processor1 = new TestProcessor(); - const multiSpanProcessor = new MultiSpanProcessor([processor1]); - - const tracerProvider = new BasicTracerProvider(); - tracerProvider.addSpanProcessor(multiSpanProcessor); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [processor1], + }); const tracer = tracerProvider.getTracer('default'); const span = tracer.startSpan('one'); assert.strictEqual(processor1.spans.length, 0); span.end(); assert.strictEqual(processor1.spans.length, 1); - multiSpanProcessor.shutdown(); + tracerProvider.getActiveSpanProcessor().shutdown(); }); it('should handle two span processor', async () => { const processor1 = new TestProcessor(); const processor2 = new TestProcessor(); - const multiSpanProcessor = new MultiSpanProcessor([processor1, processor2]); - - const tracerProvider = new BasicTracerProvider(); - tracerProvider.addSpanProcessor(multiSpanProcessor); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [processor1, processor2], + }); const tracer = tracerProvider.getTracer('default'); const span = tracer.startSpan('one'); assert.strictEqual(processor1.spans.length, 0); @@ -94,18 +81,21 @@ describe('MultiSpanProcessor', () => { assert.strictEqual(processor1.spans.length, 1); assert.strictEqual(processor1.spans.length, processor2.spans.length); - await multiSpanProcessor.shutdown(); - assert.strictEqual(processor1.spans.length, 0); - assert.strictEqual(processor1.spans.length, processor2.spans.length); + // await tracerProvider.getActiveSpanProcessor().shutdown(); + // assert.strictEqual(processor1.spans.length, 0); + // assert.strictEqual(processor1.spans.length, processor2.spans.length); + tracerProvider.shutdown().then(() => { + assert.strictEqual(processor1.spans.length, 0); + assert.strictEqual(processor1.spans.length, processor2.spans.length); + }); }); it('should export spans on manual shutdown from two span processor', () => { const processor1 = new TestProcessor(); const processor2 = new TestProcessor(); - const multiSpanProcessor = new MultiSpanProcessor([processor1, processor2]); - - const tracerProvider = new BasicTracerProvider(); - tracerProvider.addSpanProcessor(multiSpanProcessor); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [processor1, processor2], + }); const tracer = tracerProvider.getTracer('default'); const span = tracer.startSpan('one'); assert.strictEqual(processor1.spans.length, 0); @@ -124,10 +114,9 @@ describe('MultiSpanProcessor', () => { it('should export spans on manual shutdown from two span processor', () => { const processor1 = new TestProcessor(); const processor2 = new TestProcessor(); - const multiSpanProcessor = new MultiSpanProcessor([processor1, processor2]); - - const tracerProvider = new BasicTracerProvider(); - tracerProvider.addSpanProcessor(multiSpanProcessor); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [processor1, processor2], + }); const tracer = tracerProvider.getTracer('default'); const span = tracer.startSpan('one'); assert.strictEqual(processor1.spans.length, 0); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index a08bd0d306f..c59a3aa7657 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -1132,9 +1132,9 @@ describe('Span', () => { shutdown: () => Promise.resolve(), }; - const provider = new BasicTracerProvider(); - - provider.addSpanProcessor(processor); + const provider = new BasicTracerProvider({ + spanProcessors: [processor] + }); provider.getTracer('default').startSpan('test'); assert.ok(started); @@ -1151,9 +1151,9 @@ describe('Span', () => { shutdown: () => Promise.resolve(), }; - const provider = new BasicTracerProvider(); - - provider.addSpanProcessor(processor); + const provider = new BasicTracerProvider({ + spanProcessors: [processor] + }); provider .getTracer('default') @@ -1172,9 +1172,9 @@ describe('Span', () => { shutdown: () => Promise.resolve(), }; - const provider = new BasicTracerProvider(); - - provider.addSpanProcessor(processor); + const provider = new BasicTracerProvider({ + spanProcessors: [processor] + }); provider.getTracer('default').startSpan('test').end(); assert.ok(ended); @@ -1190,9 +1190,9 @@ describe('Span', () => { shutdown: () => Promise.resolve(), }; - const provider = new BasicTracerProvider(); - - provider.addSpanProcessor(processor); + const provider = new BasicTracerProvider({ + spanProcessors: [processor] + }); const s = provider.getTracer('default').startSpan('test') as Span; assert.ok(s.attributes.attr); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts index 3d1dc91c463..991fb511b3d 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts @@ -259,8 +259,9 @@ describe('Tracer', () => { const sp: SpanProcessor = new DummySpanProcessor(); const onStartSpy = sinon.spy(sp, 'onStart'); - const tp = new BasicTracerProvider(); - tp.addSpanProcessor(sp); + const tp = new BasicTracerProvider({ + spanProcessors: [sp] + }); const sampler: Sampler = new AlwaysOnSampler(); const shouldSampleSpy = sinon.spy(sampler, 'shouldSample'); @@ -289,8 +290,9 @@ describe('Tracer', () => { const sp: SpanProcessor = new DummySpanProcessor(); const onStartSpy = sinon.spy(sp, 'onStart'); - const tp = new BasicTracerProvider(); - tp.addSpanProcessor(sp); + const tp = new BasicTracerProvider({ + spanProcessors: [sp] + }); const sampler: Sampler = new AlwaysOnSampler(); const shouldSampleSpy = sinon.spy(sampler, 'shouldSample'); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts index c73f318fffb..f24f0531e31 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts @@ -43,18 +43,15 @@ describe('ConsoleSpanExporter', () => { describe('.export()', () => { it('should export information about span', () => { assert.doesNotThrow(() => { + consoleExporter = new ConsoleSpanExporter(); const basicTracerProvider = new BasicTracerProvider({ sampler: new AlwaysOnSampler(), + spanProcessors: [new SimpleSpanProcessor(consoleExporter)] }); - consoleExporter = new ConsoleSpanExporter(); const spyConsole = sinon.spy(console, 'dir'); const spyExport = sinon.spy(consoleExporter, 'export'); - basicTracerProvider.addSpanProcessor( - new SimpleSpanProcessor(consoleExporter) - ); - const instrumentationScopeName = '@opentelemetry/sdk-trace-base/test'; const instrumentationScopeVersion = '1.2.3'; const tracer = basicTracerProvider.getTracer( diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/InMemorySpanExporter.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/InMemorySpanExporter.test.ts index 5bf4ddb8ff8..712f4205889 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/InMemorySpanExporter.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/InMemorySpanExporter.test.ts @@ -29,8 +29,9 @@ describe('InMemorySpanExporter', () => { beforeEach(() => { memoryExporter = new InMemorySpanExporter(); - provider = new BasicTracerProvider(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); + provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], + }); }); it('should get finished spans', () => { diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts index 89eae8d8b57..e06e736f473 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts @@ -35,8 +35,6 @@ export class TestTracingSpanExporter extends InMemorySpanExporter { constructor() { super(); - const tracerProvider = new BasicTracerProvider(); - const spanProcessor: SpanProcessor = { forceFlush: () => { return Promise.resolve(); @@ -50,7 +48,9 @@ export class TestTracingSpanExporter extends InMemorySpanExporter { }, }; - tracerProvider.addSpanProcessor(spanProcessor); + const tracerProvider = new BasicTracerProvider({ + spanProcessors: [spanProcessor] + }); this._tracer = new Tracer( { name: 'default', version: '0.0.1' }, From ad536fdfa0aa89603656a9a530c713dad53b4cb7 Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 11 Nov 2024 12:09:39 +0100 Subject: [PATCH 2/6] chore(sdk-node): remove TraceProviderWithEnvExporter class --- .../src/TracerProviderWithEnvExporter.ts | 179 ---------- .../opentelemetry-sdk-node/src/sdk.ts | 47 ++- .../opentelemetry-sdk-node/src/utils.ts | 102 ++++++ .../TracerProviderWithEnvExporter.test.ts | 313 ------------------ .../opentelemetry-sdk-node/test/sdk.test.ts | 29 +- .../packages/shim-opencensus/test/util.ts | 6 +- 6 files changed, 136 insertions(+), 540 deletions(-) delete mode 100644 experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts delete mode 100644 experimental/packages/opentelemetry-sdk-node/test/TracerProviderWithEnvExporter.test.ts diff --git a/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts b/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts deleted file mode 100644 index 4db49699391..00000000000 --- a/experimental/packages/opentelemetry-sdk-node/src/TracerProviderWithEnvExporter.ts +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { diag } from '@opentelemetry/api'; -import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; -import { - ConsoleSpanExporter, - SpanExporter, - BatchSpanProcessor, - SimpleSpanProcessor, - SDKRegistrationConfig, - SpanProcessor, -} from '@opentelemetry/sdk-trace-base'; -import { - NodeTracerConfig, - NodeTracerProvider, -} from '@opentelemetry/sdk-trace-node'; -import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; -import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; -import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'; -import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; -import { filterBlanksAndNulls } from './utils'; - -export class TracerProviderWithEnvExporters extends NodeTracerProvider { - private _configuredExporters: SpanExporter[] = []; - private _spanProcessors: SpanProcessor[] | undefined; - private _hasSpanProcessors: boolean = false; - - static configureOtlp(): SpanExporter { - const protocol = this.getOtlpProtocol(); - - switch (protocol) { - case 'grpc': - return new OTLPGrpcTraceExporter(); - case 'http/json': - return new OTLPHttpTraceExporter(); - case 'http/protobuf': - return new OTLPProtoTraceExporter(); - default: - diag.warn( - `Unsupported OTLP traces protocol: ${protocol}. Using http/protobuf.` - ); - return new OTLPProtoTraceExporter(); - } - } - - static getOtlpProtocol(): string { - const parsedEnvValues = getEnvWithoutDefaults(); - - return ( - parsedEnvValues.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL ?? - parsedEnvValues.OTEL_EXPORTER_OTLP_PROTOCOL ?? - getEnv().OTEL_EXPORTER_OTLP_TRACES_PROTOCOL ?? - getEnv().OTEL_EXPORTER_OTLP_PROTOCOL - ); - } - - private static configureJaeger() { - // The JaegerExporter does not support being required in bundled - // environments. By delaying the require statement to here, we only crash when - // the exporter is actually used in such an environment. - try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); - return new JaegerExporter(); - } catch (e) { - throw new Error( - `Could not instantiate JaegerExporter. This could be due to the JaegerExporter's lack of support for bundling. If possible, use @opentelemetry/exporter-trace-otlp-proto instead. Original Error: ${e}` - ); - } - } - - protected static override _registeredExporters = new Map< - string, - () => SpanExporter - >([ - ['otlp', () => this.configureOtlp()], - ['zipkin', () => new ZipkinExporter()], - ['jaeger', () => this.configureJaeger()], - ['console', () => new ConsoleSpanExporter()], - ]); - - public constructor(config: NodeTracerConfig = {}) { - super(config); - let traceExportersList = filterBlanksAndNulls( - Array.from(new Set(getEnv().OTEL_TRACES_EXPORTER.split(','))) - ); - - if (traceExportersList[0] === 'none') { - diag.warn( - 'OTEL_TRACES_EXPORTER contains "none". SDK will not be initialized.' - ); - } else if (traceExportersList.length === 0) { - diag.warn('OTEL_TRACES_EXPORTER is empty. Using default otlp exporter.'); - - traceExportersList = ['otlp']; - this.createExportersFromList(traceExportersList); - - this._spanProcessors = this.configureSpanProcessors( - this._configuredExporters - ); - this._spanProcessors.forEach(processor => { - this.addSpanProcessor(processor); - }); - } else { - if ( - traceExportersList.length > 1 && - traceExportersList.includes('none') - ) { - diag.warn( - 'OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.' - ); - traceExportersList = ['otlp']; - } - - this.createExportersFromList(traceExportersList); - - if (this._configuredExporters.length > 0) { - this._spanProcessors = this.configureSpanProcessors( - this._configuredExporters - ); - this._spanProcessors.forEach(processor => { - this.addSpanProcessor(processor); - }); - } else { - diag.warn( - 'Unable to set up trace exporter(s) due to invalid exporter and/or protocol values.' - ); - } - } - } - - override addSpanProcessor(spanProcessor: SpanProcessor) { - super.addSpanProcessor(spanProcessor); - this._hasSpanProcessors = true; - } - - override register(config?: SDKRegistrationConfig) { - if (this._hasSpanProcessors) { - super.register(config); - } - } - - private createExportersFromList(exporterList: string[]) { - exporterList.forEach(exporterName => { - const exporter = this._getSpanExporter(exporterName); - if (exporter) { - this._configuredExporters.push(exporter); - } else { - diag.warn(`Unrecognized OTEL_TRACES_EXPORTER value: ${exporterName}.`); - } - }); - } - - private configureSpanProcessors( - exporters: SpanExporter[] - ): (BatchSpanProcessor | SimpleSpanProcessor)[] { - return exporters.map(exporter => { - if (exporter instanceof ConsoleSpanExporter) { - return new SimpleSpanProcessor(exporter); - } else { - return new BatchSpanProcessor(exporter); - } - }); - } -} diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index 74bdfb2165b..35a1f277f16 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -61,11 +61,10 @@ import { NodeTracerConfig, NodeTracerProvider, } from '@opentelemetry/sdk-trace-node'; -import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; +import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; import { NodeSDKConfiguration } from './types'; -import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter'; import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; -import { getResourceDetectorsFromEnv, filterBlanksAndNulls } from './utils'; +import { getResourceDetectorsFromEnv, getSpanProcessorsFromEnv, filterBlanksAndNulls } from './utils'; /** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */ @@ -103,7 +102,7 @@ export class NodeSDK { private _autoDetectResources: boolean; - private _tracerProvider?: NodeTracerProvider | TracerProviderWithEnvExporters; + private _tracerProvider?: NodeTracerProvider; private _loggerProvider?: LoggerProvider; private _meterProvider?: MeterProvider; private _serviceName?: string; @@ -245,37 +244,33 @@ export class NodeSDK { ? this._resource : this._resource.merge( new Resource({ - [SEMRESATTRS_SERVICE_NAME]: this._serviceName, + [ATTR_SERVICE_NAME]: this._serviceName, }) ); - // if there is a tracerProviderConfig (traceExporter/spanProcessor was set manually) or the traceExporter is set manually, use NodeTracerProvider - const Provider = this._tracerProviderConfig - ? NodeTracerProvider - : TracerProviderWithEnvExporters; - - // If the Provider is configured with Env Exporters, we need to check if the SDK had any manual configurations and set them here - const tracerProvider = new Provider({ + const spanProcessors = this._tracerProviderConfig ? + this._tracerProviderConfig.spanProcessors : + getSpanProcessorsFromEnv(); + + this._tracerProvider = new NodeTracerProvider({ ...this._configuration, resource: this._resource, + spanProcessors, }); - this._tracerProvider = tracerProvider; - - if (this._tracerProviderConfig) { - for (const spanProcessor of this._tracerProviderConfig.spanProcessors) { - tracerProvider.addSpanProcessor(spanProcessor); - } + // TODO: the former class `TracerProviderWithEnvExporters` was doing registration only if + // spanProcessors were configured. Should we have this logic here? + // if we register anyway the context manager changes and break some tests + if (spanProcessors.length > 0) { + this._tracerProvider.register({ + contextManager: + this._tracerProviderConfig?.contextManager ?? + // _tracerProviderConfig may be undefined if trace-specific settings are not provided - fall back to raw config + this._configuration?.contextManager, + propagator: this._tracerProviderConfig?.textMapPropagator, + }); } - tracerProvider.register({ - contextManager: - this._tracerProviderConfig?.contextManager ?? - // _tracerProviderConfig may be undefined if trace-specific settings are not provided - fall back to raw config - this._configuration?.contextManager, - propagator: this._tracerProviderConfig?.textMapPropagator, - }); - if (this._loggerProviderConfig) { const loggerProvider = new LoggerProvider({ resource: this._resource, diff --git a/experimental/packages/opentelemetry-sdk-node/src/utils.ts b/experimental/packages/opentelemetry-sdk-node/src/utils.ts index 293a3e34858..7f728d35505 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/utils.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/utils.ts @@ -15,6 +15,11 @@ */ import { diag } from '@opentelemetry/api'; +import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; +import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; +import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; +import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'; +import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; import { DetectorSync, envDetectorSync, @@ -23,6 +28,7 @@ import { processDetectorSync, serviceInstanceIdDetectorSync, } from '@opentelemetry/resources'; +import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor, SpanExporter, SpanProcessor } from '@opentelemetry/sdk-trace-base'; const RESOURCE_DETECTOR_ENVIRONMENT = 'env'; const RESOURCE_DETECTOR_HOST = 'host'; @@ -65,3 +71,99 @@ export function getResourceDetectorsFromEnv(): Array { export function filterBlanksAndNulls(list: string[]): string[] { return list.map(item => item.trim()).filter(s => s !== 'null' && s !== ''); } + +export function getOtlpProtocolFomEnv(): string { + const parsedEnvValues = getEnvWithoutDefaults(); + + return ( + parsedEnvValues.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL ?? + parsedEnvValues.OTEL_EXPORTER_OTLP_PROTOCOL ?? + getEnv().OTEL_EXPORTER_OTLP_TRACES_PROTOCOL ?? + getEnv().OTEL_EXPORTER_OTLP_PROTOCOL + ); +} + +function getOtlpExporterFomEnv(): SpanExporter { + const protocol = getOtlpProtocolFomEnv(); + + switch (protocol) { + case 'grpc': + return new OTLPGrpcTraceExporter(); + case 'http/json': + return new OTLPHttpTraceExporter(); + case 'http/protobuf': + return new OTLPProtoTraceExporter(); + default: + diag.warn( + `Unsupported OTLP traces protocol: ${protocol}. Using http/protobuf.` + ); + return new OTLPProtoTraceExporter(); + } +} + +function getJaegerExporter() { + // The JaegerExporter does not support being required in bundled + // environments. By delaying the require statement to here, we only crash when + // the exporter is actually used in such an environment. + try { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); + return new JaegerExporter(); + } catch (e) { + throw new Error( + `Could not instantiate JaegerExporter. This could be due to the JaegerExporter's lack of support for bundling. If possible, use @opentelemetry/exporter-trace-otlp-proto instead. Original Error: ${e}` + ); + } +} + +export function getSpanProcessorsFromEnv(): SpanProcessor[] { + const exportersMap = new Map SpanExporter>([ + ['otlp', () => getOtlpExporterFomEnv()], + ['zipkin', () => new ZipkinExporter()], + ['console', () => new ConsoleSpanExporter()], + ['jaeger', () => getJaegerExporter()], + ]); + const exporters: SpanExporter[] = []; + const processors: SpanProcessor[] = []; + let traceExportersList = filterBlanksAndNulls( + Array.from(new Set(getEnv().OTEL_TRACES_EXPORTER.split(','))) + ); + + if (traceExportersList[0] === 'none') { + diag.warn('OTEL_TRACES_EXPORTER contains "none". SDK will not be initialized.'); + return []; + } + + if (traceExportersList.length === 0) { + diag.warn('OTEL_TRACES_EXPORTER is empty. Using default otlp exporter.'); + traceExportersList = ['otlp']; + } else if (traceExportersList.length > 1 && traceExportersList.includes('none')) { + diag.warn('OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.'); + traceExportersList = ['otlp']; + } + + for (const name of traceExportersList) { + const exporter = exportersMap.get(name)?.(); + if (exporter) { + exporters.push(exporter); + } else { + diag.warn(`Unrecognized OTEL_TRACES_EXPORTER value: ${name}.`); + } + } + + for (const exp of exporters) { + if (exp instanceof ConsoleSpanExporter) { + processors.push(new SimpleSpanProcessor(exp)) + } else { + processors.push(new BatchSpanProcessor(exp)) + } + } + + if (exporters.length === 0) { + diag.warn( + 'Unable to set up trace exporter(s) due to invalid exporter and/or protocol values.' + ); + } + + return processors; +} \ No newline at end of file diff --git a/experimental/packages/opentelemetry-sdk-node/test/TracerProviderWithEnvExporter.test.ts b/experimental/packages/opentelemetry-sdk-node/test/TracerProviderWithEnvExporter.test.ts deleted file mode 100644 index 21a1fa93d79..00000000000 --- a/experimental/packages/opentelemetry-sdk-node/test/TracerProviderWithEnvExporter.test.ts +++ /dev/null @@ -1,313 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { diag } from '@opentelemetry/api'; -import { - ConsoleSpanExporter, - SimpleSpanProcessor, - BatchSpanProcessor, -} from '@opentelemetry/sdk-trace-base'; -import * as assert from 'assert'; -import * as Sinon from 'sinon'; -import { env } from 'process'; -import { - OTLPTraceExporter as OTLPProtoTraceExporter, - OTLPTraceExporter, -} from '@opentelemetry/exporter-trace-otlp-proto'; -import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; -import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'; -import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; -import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; -import { TracerProviderWithEnvExporters } from '../src/TracerProviderWithEnvExporter'; - -describe('set up trace exporter with env exporters', () => { - let spyGetOtlpProtocol: Sinon.SinonSpy; - let stubLoggerError: Sinon.SinonStub; - - beforeEach(() => { - spyGetOtlpProtocol = Sinon.spy( - TracerProviderWithEnvExporters, - 'getOtlpProtocol' - ); - stubLoggerError = Sinon.stub(diag, 'warn'); - }); - afterEach(() => { - spyGetOtlpProtocol.restore(); - stubLoggerError.restore(); - }); - describe('setup otlp exporter from env', () => { - it('set up default exporter when user does not define otel trace exporter', async () => { - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.returned('http/protobuf')); - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof OTLPProtoTraceExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - }); - it('use otlp exporter and grpc exporter protocol env value', async () => { - env.OTEL_TRACES_EXPORTER = 'otlp'; - env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; - - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.returned('grpc')); - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof OTLPGrpcTraceExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - - delete env.OTEL_TRACES_EXPORTER; - delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; - }); - it('sdk will ignore protocol defined with no-signal env and use signal specific protocol instead', async () => { - env.OTEL_TRACES_EXPORTER = 'otlp'; - env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'http/protobuf'; - env.OTEL_EXPORTER_OTLP_PROTOCOL = 'grpc'; - - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.returned('http/protobuf')); - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof OTLPTraceExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - - delete env.OTEL_TRACES_EXPORTER; - delete env.OTEL_EXPORTER_OTLP_PROTOCOL; - delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; - }); - it('use default otlp exporter when user does not set exporter via env or config', async () => { - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(listOfExporters[0] instanceof OTLPProtoTraceExporter); - assert(listOfExporters.length === 1); - - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - }); - it('use default otlp exporter when empty value is provided for exporter via env', async () => { - env.OTEL_TRACES_EXPORTER = ''; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(listOfExporters[0] instanceof OTLPProtoTraceExporter); - assert(listOfExporters.length === 1); - - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - - env.OTEL_TRACES_EXPORTER = ''; - }); - it('do not use any exporters when none value is only provided', async () => { - env.OTEL_TRACES_EXPORTER = 'none'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.notCalled); - assert(listOfExporters.length === 0); - assert(listOfProcessors === undefined); - delete env.OTEL_TRACES_EXPORTER; - }); - it('log warning that sdk will not be initialized when exporter is set to none', async () => { - env.OTEL_TRACES_EXPORTER = 'none'; - new TracerProviderWithEnvExporters(); - - assert.strictEqual( - stubLoggerError.args[0][0], - 'OTEL_TRACES_EXPORTER contains "none". SDK will not be initialized.' - ); - delete env.OTEL_TRACES_EXPORTER; - }); - it('use default exporter when none value is provided with other exports', async () => { - env.OTEL_TRACES_EXPORTER = 'otlp,zipkin,none'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(listOfExporters[0] instanceof OTLPProtoTraceExporter); - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof OTLPHttpTraceExporter === false); - assert(listOfExporters[0] instanceof ZipkinExporter === false); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - delete env.OTEL_TRACES_EXPORTER; - }); - it('log warning that default exporter will be used since exporter list contains none with other exports ', async () => { - env.OTEL_TRACES_EXPORTER = 'otlp,zipkin,none'; - new TracerProviderWithEnvExporters(); - - assert.strictEqual( - stubLoggerError.args[0][0], - 'OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.' - ); - delete env.OTEL_TRACES_EXPORTER; - }); - it('should warn that exporter is unrecognized and not able to be set up', async () => { - env.OTEL_TRACES_EXPORTER = 'invalid'; - new TracerProviderWithEnvExporters(); - - assert.strictEqual( - stubLoggerError.args[0][0], - 'Unrecognized OTEL_TRACES_EXPORTER value: invalid.' - ); - - assert.strictEqual( - stubLoggerError.args[1][0], - 'Unable to set up trace exporter(s) due to invalid exporter and/or protocol values.' - ); - - delete env.OTEL_TRACES_EXPORTER; - }); - it('should log warning when provided protocol name is not valid', async () => { - env.OTEL_EXPORTER_OTLP_PROTOCOL = 'invalid'; - new TracerProviderWithEnvExporters(); - - assert.strictEqual( - stubLoggerError.args[0][0], - 'OTEL_TRACES_EXPORTER is empty. Using default otlp exporter.' - ); - - assert.strictEqual( - stubLoggerError.args[1][0], - 'Unsupported OTLP traces protocol: invalid. Using http/protobuf.' - ); - delete env.OTEL_EXPORTER_OTLP_PROTOCOL; - }); - }); - describe('setup zipkin exporter from env', () => { - it('use the zipkin exporter', async () => { - env.OTEL_TRACES_EXPORTER = 'zipkin'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof ZipkinExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - delete env.OTEL_TRACES_EXPORTER; - }); - it('setup zipkin exporter and otlp exporter', async () => { - env.OTEL_TRACES_EXPORTER = 'zipkin, otlp'; - env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; - - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.returned('grpc')); - assert(listOfExporters.length === 2); - assert(listOfExporters[0] instanceof ZipkinExporter); - assert(listOfExporters[1] instanceof OTLPGrpcTraceExporter); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - assert(listOfProcessors[1] instanceof BatchSpanProcessor); - - delete env.OTEL_TRACES_EXPORTER; - delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; - }); - }); - describe('setup jaeger exporter from env', () => { - it('use the jaeger exporter', async () => { - env.OTEL_TRACES_EXPORTER = 'jaeger'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof JaegerExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - delete env.OTEL_TRACES_EXPORTER; - }); - it('setup jaeger exporter and otlp exporter', async () => { - env.OTEL_TRACES_EXPORTER = 'jaeger, otlp'; - env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'http/json'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.returned('http/json')); - assert(listOfExporters.length === 2); - assert(listOfExporters[0] instanceof JaegerExporter); - assert(listOfExporters[1] instanceof OTLPHttpTraceExporter); - assert(listOfProcessors.length === 2); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); - assert(listOfProcessors[1] instanceof BatchSpanProcessor); - - delete env.OTEL_TRACES_EXPORTER; - delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; - }); - }); - describe('setup console exporter from env', () => { - it('use the console exporter', async () => { - env.OTEL_TRACES_EXPORTER = 'console'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof ConsoleSpanExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof SimpleSpanProcessor); - delete env.OTEL_TRACES_EXPORTER; - }); - it('ignores the protocol', async () => { - env.OTEL_TRACES_EXPORTER = 'console'; - env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.notCalled); - assert(listOfExporters.length === 1); - assert(listOfExporters[0] instanceof ConsoleSpanExporter); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof SimpleSpanProcessor); - - delete env.OTEL_TRACES_EXPORTER; - delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; - }); - it('setup console exporter and otlp exporter', async () => { - env.OTEL_TRACES_EXPORTER = 'console, otlp'; - env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; - const sdk = new TracerProviderWithEnvExporters(); - const listOfProcessors = sdk['_spanProcessors']!; - const listOfExporters = sdk['_configuredExporters']; - - assert(spyGetOtlpProtocol.returned('grpc')); - assert(listOfExporters.length === 2); - assert(listOfExporters[0] instanceof ConsoleSpanExporter); - assert(listOfExporters[1] instanceof OTLPGrpcTraceExporter); - assert(listOfProcessors.length === 2); - assert(listOfProcessors[0] instanceof SimpleSpanProcessor); - assert(listOfProcessors[1] instanceof BatchSpanProcessor); - - delete env.OTEL_TRACES_EXPORTER; - delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; - }); - }); -}); diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 7b6e80f1888..0bd5aef7455 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -54,8 +54,8 @@ import * as assert from 'assert'; import * as semver from 'semver'; import * as Sinon from 'sinon'; import { NodeSDK } from '../src'; +import * as sdkUtils from '../src/utils'; import { env } from 'process'; -import { TracerProviderWithEnvExporters } from '../src/TracerProviderWithEnvExporter'; import { envDetector, envDetectorSync, @@ -1099,8 +1099,8 @@ describe('setup exporter from env', () => { beforeEach(() => { spyGetOtlpProtocol = Sinon.spy( - TracerProviderWithEnvExporters, - 'getOtlpProtocol' + sdkUtils, + 'getOtlpProtocolFomEnv' ); stubLoggerError = Sinon.stub(diag, 'warn'); }); @@ -1114,7 +1114,6 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); await sdk.shutdown(); @@ -1128,9 +1127,6 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert( - sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters === false - ); assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof SimpleSpanProcessor === false); assert(listOfProcessors[0] instanceof BatchSpanProcessor); @@ -1146,9 +1142,6 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert( - sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters === false - ); assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof SimpleSpanProcessor); assert(listOfProcessors[0] instanceof BatchSpanProcessor === false); @@ -1164,9 +1157,6 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert( - sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters === false - ); assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof SimpleSpanProcessor === false); assert(listOfProcessors[0] instanceof BatchSpanProcessor); @@ -1181,7 +1171,7 @@ describe('setup exporter from env', () => { sdk.start(); const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert.ok(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); + assert.ok( sdk['_tracerProvider']!['_config']?.sampler instanceof AlwaysOffSampler ); @@ -1197,7 +1187,7 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); + assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); delete env.OTEL_TRACES_EXPORTER; @@ -1236,7 +1226,7 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); + assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); await sdk.shutdown(); @@ -1248,7 +1238,7 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); + assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); env.OTEL_TRACES_EXPORTER = ''; @@ -1262,7 +1252,7 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); + assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); @@ -1307,7 +1297,7 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters); + assert(listOfProcessors.length === 3); assert(listOfProcessors[0] instanceof BatchSpanProcessor); assert(listOfProcessors[1] instanceof BatchSpanProcessor); @@ -1324,6 +1314,7 @@ describe('setup exporter from env', () => { const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; + assert(listOfProcessors.length === 2); assert(listOfProcessors[0] instanceof SimpleSpanProcessor); assert(listOfProcessors[1] instanceof BatchSpanProcessor); diff --git a/experimental/packages/shim-opencensus/test/util.ts b/experimental/packages/shim-opencensus/test/util.ts index 0d34bd3cca1..9bd55e83e99 100644 --- a/experimental/packages/shim-opencensus/test/util.ts +++ b/experimental/packages/shim-opencensus/test/util.ts @@ -39,12 +39,12 @@ export async function withTestTracer( export async function withTestTracerProvider( func: (otelTracerProvider: TracerProvider) => void | Promise ): Promise { + const inMemExporter = new InMemorySpanExporter(); const tracerProvider = new BasicTracerProvider({ sampler: new AlwaysOnSampler(), + spanProcessors: [new SimpleSpanProcessor(inMemExporter)] }); - const inMemExporter = new InMemorySpanExporter(); - tracerProvider.addSpanProcessor(new SimpleSpanProcessor(inMemExporter)); - + await func(tracerProvider); await tracerProvider.forceFlush(); From e28686a2aac64446a98d64d52f633e59a48819cd Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 11 Nov 2024 12:11:00 +0100 Subject: [PATCH 3/6] chore: fix lint issues --- .../test/OTLPTraceExporter.test.ts | 4 +-- .../test/fetch.test.ts | 2 +- .../test/helper.ts | 8 ++--- .../test/integrations/https-enable.test.ts | 2 +- .../test/unmocked.test.ts | 2 +- .../test/xhr.test.ts | 11 ++++--- .../opentelemetry-sdk-node/src/sdk.ts | 14 +++++---- .../opentelemetry-sdk-node/src/utils.ts | 29 ++++++++++++++----- .../opentelemetry-sdk-node/test/sdk.test.ts | 5 +--- .../packages/shim-opencensus/test/util.ts | 4 +-- .../test/common/BasicTracerProvider.test.ts | 4 +-- .../test/common/Span.test.ts | 8 ++--- .../test/common/Tracer.test.ts | 4 +-- .../common/export/ConsoleSpanExporter.test.ts | 2 +- .../common/export/TestTracingSpanExporter.ts | 2 +- 15 files changed, 59 insertions(+), 42 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index b4286546e6c..65a289ec3c1 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -140,7 +140,7 @@ const testCollectorExporter = (params: TestParams) => { // @ts-expect-error -- we need to create a provider to test the exporter const provider = new BasicTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(collectorExporter)] + spanProcessors: [new SimpleSpanProcessor(collectorExporter)], }); done(); }); @@ -275,7 +275,7 @@ const testCollectorExporter = (params: TestParams) => { // @ts-expect-error -- we need to create a provider to test the exporter const provider = new BasicTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(collectorExporter)] + spanProcessors: [new SimpleSpanProcessor(collectorExporter)], }); }); it('should successfully send the spans', done => { diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 2105e906771..8ba1a4c3dab 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -255,7 +255,7 @@ describe('fetch', () => { fetchInstrumentation = new FetchInstrumentation(config); dummySpanExporter = new DummySpanExporter(); webTracerProviderWithZone = new WebTracerProvider({ - spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)] + spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)], }); registerInstrumentations({ tracerProvider: webTracerProviderWithZone, diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts index 15f3824355c..bd5104bdbfe 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts +++ b/experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts @@ -756,7 +756,7 @@ export const runTests = ( describe('enable()', () => { const provider = new NodeTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], }); beforeEach(() => { memoryExporter.reset(); @@ -801,7 +801,7 @@ export const runTests = ( describe('disable()', () => { const provider = new NodeTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], }); beforeEach(() => { memoryExporter.reset(); @@ -833,7 +833,7 @@ export const runTests = ( describe('Test filtering requests using metadata', () => { const provider = new NodeTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], }); beforeEach(() => { memoryExporter.reset(); @@ -941,7 +941,7 @@ export const runTests = ( describe('Test capturing metadata', () => { const provider = new NodeTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], }); const clientMetadata = new Metadata(); diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts index 7a1e57236e6..047dc8f1166 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts @@ -132,7 +132,7 @@ describe('HttpsInstrumentation Integration tests', () => { }); }); const provider = new NodeTracerProvider({ - spanProcessors: [new SimpleSpanProcessor(memoryExporter)] + spanProcessors: [new SimpleSpanProcessor(memoryExporter)], }); instrumentation.setTracerProvider(provider); beforeEach(() => { diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts index 4f780e27ff8..4b9c38afd13 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/unmocked.test.ts @@ -43,7 +43,7 @@ describe('unmocked xhr', () => { beforeEach(() => { testSpans = new TestSpanProcessor(); provider = new WebTracerProvider({ - spanProcessors: [testSpans] + spanProcessors: [testSpans], }); registerInstrumentations({ instrumentations: [new XMLHttpRequestInstrumentation()], diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index 9f64b4674e8..b2fa135f9e8 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -270,14 +270,16 @@ describe('xhr', () => { ); dummySpanExporter = new DummySpanExporter(); webTracerProviderWithZone = new WebTracerProvider({ - spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)] + spanProcessors: [ + new tracing.SimpleSpanProcessor(dummySpanExporter), + ], }); registerInstrumentations({ instrumentations: [xmlHttpRequestInstrumentation], tracerProvider: webTracerProviderWithZone, }); webTracerWithZone = webTracerProviderWithZone.getTracer('xhr-test'); - + exportSpy = sinon.stub(dummySpanExporter, 'export'); clearResourceTimingsSpy = sinon.stub( performance as unknown as Performance, @@ -847,7 +849,9 @@ describe('xhr', () => { dummySpanExporter = new DummySpanExporter(); webTracerWithZoneProvider = new WebTracerProvider({ - spanProcessors: [new tracing.SimpleSpanProcessor(dummySpanExporter)] + spanProcessors: [ + new tracing.SimpleSpanProcessor(dummySpanExporter), + ], }); registerInstrumentations({ @@ -855,7 +859,6 @@ describe('xhr', () => { tracerProvider: webTracerWithZoneProvider, }); - exportSpy = sinon.stub(dummySpanExporter, 'export'); webTracerWithZone = webTracerWithZoneProvider.getTracer('xhr-test'); diff --git a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index 35a1f277f16..c80d22ad059 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -64,7 +64,11 @@ import { import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; import { NodeSDKConfiguration } from './types'; import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; -import { getResourceDetectorsFromEnv, getSpanProcessorsFromEnv, filterBlanksAndNulls } from './utils'; +import { + getResourceDetectorsFromEnv, + getSpanProcessorsFromEnv, + filterBlanksAndNulls, +} from './utils'; /** This class represents everything needed to register a fully configured OpenTelemetry Node.js SDK */ @@ -248,10 +252,10 @@ export class NodeSDK { }) ); - const spanProcessors = this._tracerProviderConfig ? - this._tracerProviderConfig.spanProcessors : - getSpanProcessorsFromEnv(); - + const spanProcessors = this._tracerProviderConfig + ? this._tracerProviderConfig.spanProcessors + : getSpanProcessorsFromEnv(); + this._tracerProvider = new NodeTracerProvider({ ...this._configuration, resource: this._resource, diff --git a/experimental/packages/opentelemetry-sdk-node/src/utils.ts b/experimental/packages/opentelemetry-sdk-node/src/utils.ts index 7f728d35505..1b654e64e6e 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/utils.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/utils.ts @@ -28,7 +28,13 @@ import { processDetectorSync, serviceInstanceIdDetectorSync, } from '@opentelemetry/resources'; -import { BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor, SpanExporter, SpanProcessor } from '@opentelemetry/sdk-trace-base'; +import { + BatchSpanProcessor, + ConsoleSpanExporter, + SimpleSpanProcessor, + SpanExporter, + SpanProcessor, +} from '@opentelemetry/sdk-trace-base'; const RESOURCE_DETECTOR_ENVIRONMENT = 'env'; const RESOURCE_DETECTOR_HOST = 'host'; @@ -117,7 +123,7 @@ function getJaegerExporter() { } export function getSpanProcessorsFromEnv(): SpanProcessor[] { - const exportersMap = new Map SpanExporter>([ + const exportersMap = new Map SpanExporter>([ ['otlp', () => getOtlpExporterFomEnv()], ['zipkin', () => new ZipkinExporter()], ['console', () => new ConsoleSpanExporter()], @@ -130,15 +136,22 @@ export function getSpanProcessorsFromEnv(): SpanProcessor[] { ); if (traceExportersList[0] === 'none') { - diag.warn('OTEL_TRACES_EXPORTER contains "none". SDK will not be initialized.'); + diag.warn( + 'OTEL_TRACES_EXPORTER contains "none". SDK will not be initialized.' + ); return []; } if (traceExportersList.length === 0) { diag.warn('OTEL_TRACES_EXPORTER is empty. Using default otlp exporter.'); traceExportersList = ['otlp']; - } else if (traceExportersList.length > 1 && traceExportersList.includes('none')) { - diag.warn('OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.'); + } else if ( + traceExportersList.length > 1 && + traceExportersList.includes('none') + ) { + diag.warn( + 'OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.' + ); traceExportersList = ['otlp']; } @@ -153,9 +166,9 @@ export function getSpanProcessorsFromEnv(): SpanProcessor[] { for (const exp of exporters) { if (exp instanceof ConsoleSpanExporter) { - processors.push(new SimpleSpanProcessor(exp)) + processors.push(new SimpleSpanProcessor(exp)); } else { - processors.push(new BatchSpanProcessor(exp)) + processors.push(new BatchSpanProcessor(exp)); } } @@ -166,4 +179,4 @@ export function getSpanProcessorsFromEnv(): SpanProcessor[] { } return processors; -} \ No newline at end of file +} diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 0bd5aef7455..28c5c214932 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -1098,10 +1098,7 @@ describe('setup exporter from env', () => { let stubLoggerError: Sinon.SinonStub; beforeEach(() => { - spyGetOtlpProtocol = Sinon.spy( - sdkUtils, - 'getOtlpProtocolFomEnv' - ); + spyGetOtlpProtocol = Sinon.spy(sdkUtils, 'getOtlpProtocolFomEnv'); stubLoggerError = Sinon.stub(diag, 'warn'); }); afterEach(() => { diff --git a/experimental/packages/shim-opencensus/test/util.ts b/experimental/packages/shim-opencensus/test/util.ts index 9bd55e83e99..69a45204e67 100644 --- a/experimental/packages/shim-opencensus/test/util.ts +++ b/experimental/packages/shim-opencensus/test/util.ts @@ -42,9 +42,9 @@ export async function withTestTracerProvider( const inMemExporter = new InMemorySpanExporter(); const tracerProvider = new BasicTracerProvider({ sampler: new AlwaysOnSampler(), - spanProcessors: [new SimpleSpanProcessor(inMemExporter)] + spanProcessors: [new SimpleSpanProcessor(inMemExporter)], }); - + await func(tracerProvider); await tracerProvider.forceFlush(); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts index ddbcb537396..57b21f874fc 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts @@ -770,7 +770,7 @@ describe('BasicTracerProvider', () => { const spanProcessorOne = new NoopSpanProcessor(); const spanProcessorTwo = new NoopSpanProcessor(); const tracerProvider = new BasicTracerProvider({ - spanProcessors: [spanProcessorOne, spanProcessorTwo] + spanProcessors: [spanProcessorOne, spanProcessorTwo], }); tracerProvider @@ -798,7 +798,7 @@ describe('BasicTracerProvider', () => { const spanProcessorOne = new NoopSpanProcessor(); const spanProcessorTwo = new NoopSpanProcessor(); const tracerProvider = new BasicTracerProvider({ - spanProcessors: [spanProcessorOne, spanProcessorTwo] + spanProcessors: [spanProcessorOne, spanProcessorTwo], }); tracerProvider diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index c59a3aa7657..997df489a4a 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -1133,7 +1133,7 @@ describe('Span', () => { }; const provider = new BasicTracerProvider({ - spanProcessors: [processor] + spanProcessors: [processor], }); provider.getTracer('default').startSpan('test'); @@ -1152,7 +1152,7 @@ describe('Span', () => { }; const provider = new BasicTracerProvider({ - spanProcessors: [processor] + spanProcessors: [processor], }); provider @@ -1173,7 +1173,7 @@ describe('Span', () => { }; const provider = new BasicTracerProvider({ - spanProcessors: [processor] + spanProcessors: [processor], }); provider.getTracer('default').startSpan('test').end(); @@ -1191,7 +1191,7 @@ describe('Span', () => { }; const provider = new BasicTracerProvider({ - spanProcessors: [processor] + spanProcessors: [processor], }); const s = provider.getTracer('default').startSpan('test') as Span; diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts index 991fb511b3d..ddf2d9ac466 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts @@ -260,7 +260,7 @@ describe('Tracer', () => { const sp: SpanProcessor = new DummySpanProcessor(); const onStartSpy = sinon.spy(sp, 'onStart'); const tp = new BasicTracerProvider({ - spanProcessors: [sp] + spanProcessors: [sp], }); const sampler: Sampler = new AlwaysOnSampler(); @@ -291,7 +291,7 @@ describe('Tracer', () => { const sp: SpanProcessor = new DummySpanProcessor(); const onStartSpy = sinon.spy(sp, 'onStart'); const tp = new BasicTracerProvider({ - spanProcessors: [sp] + spanProcessors: [sp], }); const sampler: Sampler = new AlwaysOnSampler(); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts index f24f0531e31..1e41f12b771 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts @@ -46,7 +46,7 @@ describe('ConsoleSpanExporter', () => { consoleExporter = new ConsoleSpanExporter(); const basicTracerProvider = new BasicTracerProvider({ sampler: new AlwaysOnSampler(), - spanProcessors: [new SimpleSpanProcessor(consoleExporter)] + spanProcessors: [new SimpleSpanProcessor(consoleExporter)], }); const spyConsole = sinon.spy(console, 'dir'); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts index e06e736f473..6eb1702c8c5 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/TestTracingSpanExporter.ts @@ -49,7 +49,7 @@ export class TestTracingSpanExporter extends InMemorySpanExporter { }; const tracerProvider = new BasicTracerProvider({ - spanProcessors: [spanProcessor] + spanProcessors: [spanProcessor], }); this._tracer = new Tracer( From b4250bac2eebcbe7cd3f36d34885ab6eb1982f4e Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 11 Nov 2024 12:58:02 +0100 Subject: [PATCH 4/6] chore: update examples and docs --- api/README.md | 5 ++- examples/basic-tracer-node/index.js | 15 +++---- examples/grpc-js/tracer.js | 12 ++---- examples/http/tracer.js | 14 ++----- examples/https/tracer.js | 13 ++---- .../examples/fetch-proto/index.js | 17 ++++---- .../opentelemetry-web/examples/fetch/index.js | 14 ++++--- .../examples/fetchXhr/index.js | 14 ++++--- .../examples/fetchXhrB3/index.js | 14 ++++--- .../examples/xml-http-request/index.js | 15 +++---- .../examples/zipkin/index.js | 26 +++++++----- examples/opentracing-shim/shim.js | 2 +- examples/otlp-exporter-node/tracing.js | 6 ++- .../examples/opencensus-shim/setup.js | 14 ++++--- .../exporter-trace-otlp-grpc/README.md | 15 ++++--- .../exporter-trace-otlp-http/README.md | 42 +++++++++++-------- .../exporter-trace-otlp-proto/README.md | 5 ++- .../opentelemetry-browser-detector/README.md | 12 +++++- .../README.md | 13 +++--- .../README.md | 5 ++- .../README.md | 5 ++- .../README.md | 5 ++- .../README.md | 5 ++- packages/opentelemetry-context-zone/README.md | 5 ++- .../opentelemetry-exporter-jaeger/README.md | 6 ++- .../opentelemetry-exporter-zipkin/README.md | 6 ++- .../benchmark/BatchSpanProcessor.js | 5 ++- .../opentelemetry-sdk-trace-web/README.md | 5 ++- selenium-tests/pages/tracing.js | 9 ++-- 29 files changed, 178 insertions(+), 146 deletions(-) diff --git a/api/README.md b/api/README.md index 59d4cd7a08d..40015dbf051 100644 --- a/api/README.md +++ b/api/README.md @@ -45,8 +45,9 @@ const { trace } = require("@opentelemetry/api"); const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base"); // Create and register an SDK -const provider = new BasicTracerProvider(); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); trace.setGlobalTracerProvider(provider); // Acquire a tracer from the global tracer provider which will be used to trace the application diff --git a/examples/basic-tracer-node/index.js b/examples/basic-tracer-node/index.js index 889d4ba31e4..ff50b8e8b72 100644 --- a/examples/basic-tracer-node/index.js +++ b/examples/basic-tracer-node/index.js @@ -6,19 +6,20 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); +// Configure span processor to send spans to the exporter +const exporter = new JaegerExporter({ + endpoint: 'http://localhost:14268/api/traces', +}); const provider = new BasicTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'basic-service', }), + spanProcessors: [ + new SimpleSpanProcessor(exporter), + new SimpleSpanProcessor(new ConsoleSpanExporter()), + ] }); -// Configure span processor to send spans to the exporter -const exporter = new JaegerExporter({ - endpoint: 'http://localhost:14268/api/traces', -}); -provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); - /** * Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings. * diff --git a/examples/grpc-js/tracer.js b/examples/grpc-js/tracer.js index 77d30dbd1c2..8266f407d84 100644 --- a/examples/grpc-js/tracer.js +++ b/examples/grpc-js/tracer.js @@ -13,21 +13,15 @@ const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc'); const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { + const useZipkin = EXPORTER.toLowerCase().startsWith('z'); + const exporter = useZipkin ? new ZipkinExporter() : new JaegerExporter(); const provider = new NodeTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: serviceName, }), + spanProcessors: [new SimpleSpanProcessor(exporter)] }); - let exporter; - if (EXPORTER.toLowerCase().startsWith('z')) { - exporter = new ZipkinExporter(); - } else { - exporter = new JaegerExporter(); - } - - provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); - // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); diff --git a/examples/http/tracer.js b/examples/http/tracer.js index b2351221bde..b96993446b6 100644 --- a/examples/http/tracer.js +++ b/examples/http/tracer.js @@ -13,21 +13,15 @@ const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { + const useZipkin = EXPORTER.toLowerCase().startsWith('z'); + const exporter = useZipkin ? new ZipkinExporter() : new JaegerExporter(); const provider = new NodeTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: serviceName, }), + spanProcessors: [new SimpleSpanProcessor(exporter)] }); - - let exporter; - if (EXPORTER.toLowerCase().startsWith('z')) { - exporter = new ZipkinExporter(); - } else { - exporter = new JaegerExporter(); - } - - provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); - + // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); diff --git a/examples/https/tracer.js b/examples/https/tracer.js index 9d76f30355b..d59835da96f 100644 --- a/examples/https/tracer.js +++ b/examples/https/tracer.js @@ -14,21 +14,14 @@ const EXPORTER = process.env.EXPORTER || ''; process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; module.exports = (serviceName) => { - let exporter; + const useZipkin = EXPORTER.toLowerCase().startsWith('z'); + const exporter = useZipkin ? new ZipkinExporter() : new JaegerExporter(); const provider = new NodeTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: serviceName, }), + spanProcessors: [new SimpleSpanProcessor(exporter)] }); - - if (EXPORTER.toLowerCase().startsWith('z')) { - exporter = new ZipkinExporter(); - } else { - exporter = new JaegerExporter(); - } - - provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); - // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); diff --git a/examples/opentelemetry-web/examples/fetch-proto/index.js b/examples/opentelemetry-web/examples/fetch-proto/index.js index 0faaa0149aa..346ff12163f 100644 --- a/examples/opentelemetry-web/examples/fetch-proto/index.js +++ b/examples/opentelemetry-web/examples/fetch-proto/index.js @@ -12,17 +12,16 @@ const { SEMRESATTRS_SERVICE_NAME } = require("@opentelemetry/semantic-convention const provider = new WebTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'fetch-proto-web-service' - }) + }), + // Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests + // to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the + // exporter without delay + spanProcessors: [ + new SimpleSpanProcessor(new ConsoleSpanExporter()), + new SimpleSpanProcessor(new OTLPTraceExporterProto()), + ] }); -// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests -// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the -// exporter without delay -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -provider.addSpanProcessor( - new SimpleSpanProcessor(new OTLPTraceExporterProto()) -); - provider.register({ contextManager: new ZoneContextManager(), propagator: new B3Propagator(), diff --git a/examples/opentelemetry-web/examples/fetch/index.js b/examples/opentelemetry-web/examples/fetch/index.js index 3326beb19b6..8e0ac16e03d 100644 --- a/examples/opentelemetry-web/examples/fetch/index.js +++ b/examples/opentelemetry-web/examples/fetch/index.js @@ -12,14 +12,16 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention const provider = new WebTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'fetch-web-service' - }) + }), + // Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests + // to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the + // exporter without delay + spanProcessors:[ + new SimpleSpanProcessor(new ConsoleSpanExporter()), + new SimpleSpanProcessor(new OTLPTraceExporter()), + ] }); -// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests -// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the -// exporter without delay -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); provider.register({ contextManager: new ZoneContextManager(), propagator: new B3Propagator(), diff --git a/examples/opentelemetry-web/examples/fetchXhr/index.js b/examples/opentelemetry-web/examples/fetchXhr/index.js index 1d93af4c49d..92e9b72eb3c 100644 --- a/examples/opentelemetry-web/examples/fetchXhr/index.js +++ b/examples/opentelemetry-web/examples/fetchXhr/index.js @@ -12,14 +12,16 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention const provider = new WebTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'fetch-xhr-web-service' - }) + }), + // Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests + // to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the + // exporter without delay + spanProcessors: [ + new SimpleSpanProcessor(new ConsoleSpanExporter()), + new SimpleSpanProcessor(new OTLPTraceExporter()), + ] }); -// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests -// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the -// exporter without delay -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); provider.register({ contextManager: new ZoneContextManager(), }); diff --git a/examples/opentelemetry-web/examples/fetchXhrB3/index.js b/examples/opentelemetry-web/examples/fetchXhrB3/index.js index 2923cccc6b7..368ae309f44 100644 --- a/examples/opentelemetry-web/examples/fetchXhrB3/index.js +++ b/examples/opentelemetry-web/examples/fetchXhrB3/index.js @@ -13,14 +13,16 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention const provider = new WebTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'fetch-xhr-b3-web-service' - }) + }), + // Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests + // to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the + // exporter without delay + spanProcessors: [ + new SimpleSpanProcessor(new ConsoleSpanExporter()), + new SimpleSpanProcessor(new OTLPTraceExporter()), + ] }); -// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests -// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the -// exporter without delay -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); provider.register({ contextManager: new ZoneContextManager(), propagator: new B3Propagator(), diff --git a/examples/opentelemetry-web/examples/xml-http-request/index.js b/examples/opentelemetry-web/examples/xml-http-request/index.js index 7530528ed87..908716f9a0a 100644 --- a/examples/opentelemetry-web/examples/xml-http-request/index.js +++ b/examples/opentelemetry-web/examples/xml-http-request/index.js @@ -12,15 +12,16 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention const providerWithZone = new WebTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'xml-http-web-service' - }) + }), + // Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests + // to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the + // exporter without delay + spanProcessors: [ + new SimpleSpanProcessor(new ConsoleSpanExporter()), + new SimpleSpanProcessor(new OTLPTraceExporter()), + ] }); -// Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests -// to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the -// exporter without delay -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter())); - providerWithZone.register({ contextManager: new ZoneContextManager(), propagator: new B3Propagator(), diff --git a/examples/opentelemetry-web/examples/zipkin/index.js b/examples/opentelemetry-web/examples/zipkin/index.js index d76de7e563b..a6382a1bc77 100644 --- a/examples/opentelemetry-web/examples/zipkin/index.js +++ b/examples/opentelemetry-web/examples/zipkin/index.js @@ -7,19 +7,23 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention const provider = new WebTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'zipkin-web-service' - }) + }), + // Note: For production consider using the "BatchSpanProcessor" to reduce the number of requests + // to your exporter. Using the SimpleSpanProcessor here as it sends the spans immediately to the + // exporter without delay + spanProcessors: [ + new SimpleSpanProcessor(new ConsoleSpanExporter()), + new SimpleSpanProcessor(new ZipkinExporter({ + // testing interceptor + // getExportRequestHeaders: () => { + // return { + // foo: 'bar', + // } + // } + })), + ] }); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({ - // testing interceptor - // getExportRequestHeaders: ()=> { - // return { - // foo: 'bar', - // } - // } -}))); - provider.register(); const tracer = provider.getTracer('example-tracer-web'); diff --git a/examples/opentracing-shim/shim.js b/examples/opentracing-shim/shim.js index 0f791071ee4..caea31c178c 100644 --- a/examples/opentracing-shim/shim.js +++ b/examples/opentracing-shim/shim.js @@ -11,9 +11,9 @@ const { TracerShim } = require('@opentelemetry/shim-opentracing'); function shim(serviceName) { const provider = new NodeTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: serviceName }), + spanProcessors: [new SimpleSpanProcessor(getExporter(serviceName))], }); - provider.addSpanProcessor(new SimpleSpanProcessor(getExporter(serviceName))); // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); diff --git a/examples/otlp-exporter-node/tracing.js b/examples/otlp-exporter-node/tracing.js index 475665f23cd..fb740fb2096 100644 --- a/examples/otlp-exporter-node/tracing.js +++ b/examples/otlp-exporter-node/tracing.js @@ -26,9 +26,11 @@ const provider = new BasicTracerProvider({ resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'basic-service', }), + spanProcessors: [ + new SimpleSpanProcessor(exporter), + new SimpleSpanProcessor(new ConsoleSpanExporter()), + ] }); -provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); provider.register(); const tracer = trace.getTracer('example-otlp-exporter-node'); diff --git a/experimental/examples/opencensus-shim/setup.js b/experimental/examples/opencensus-shim/setup.js index 1bb277eddb4..e3e189eae2d 100644 --- a/experimental/examples/opencensus-shim/setup.js +++ b/experimental/examples/opencensus-shim/setup.js @@ -48,12 +48,14 @@ module.exports = function setup(serviceName) { const resource = new Resource({ [SEMRESATTRS_SERVICE_NAME]: serviceName, }); - const tracerProvider = new NodeTracerProvider({ resource }); - tracerProvider.addSpanProcessor( - new BatchSpanProcessor(new OTLPTraceExporter(), { - scheduledDelayMillis: 5000, - }) - ); + const tracerProvider = new NodeTracerProvider({ + resource, + spanProcessors: [ + new BatchSpanProcessor(new OTLPTraceExporter(), { + scheduledDelayMillis: 5000, + }) + ] + }); tracerProvider.register(); const meterProvider = new MeterProvider({ resource }); diff --git a/experimental/packages/exporter-trace-otlp-grpc/README.md b/experimental/packages/exporter-trace-otlp-grpc/README.md index 09bb155c657..a18a7a787bf 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/README.md +++ b/experimental/packages/exporter-trace-otlp-grpc/README.md @@ -33,9 +33,10 @@ const collectorOptions = { url: 'http://:', }; -const provider = new BasicTracerProvider(); const exporter = new OTLPTraceExporter(collectorOptions); -provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); +const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(exporter)] +}); provider.register(); ['SIGINT', 'SIGTERM'].forEach(signal => { @@ -59,9 +60,10 @@ const collectorOptions = { credentials: grpc.credentials.createSsl(), }; -const provider = new BasicTracerProvider(); const exporter = new OTLPTraceExporter(collectorOptions); -provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); +const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(exporter)] +}); provider.register(); ['SIGINT', 'SIGTERM'].forEach(signal => { @@ -100,9 +102,10 @@ const collectorOptions = { metadata, // // an optional grpc.Metadata object to be sent with each request }; -const provider = new BasicTracerProvider(); const exporter = new OTLPTraceExporter(collectorOptions); -provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); +const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(exporter)] +}); provider.register(); ['SIGINT', 'SIGTERM'].forEach(signal => { diff --git a/experimental/packages/exporter-trace-otlp-http/README.md b/experimental/packages/exporter-trace-otlp-http/README.md index 770c8dc22dc..427792eff5a 100644 --- a/experimental/packages/exporter-trace-otlp-http/README.md +++ b/experimental/packages/exporter-trace-otlp-http/README.md @@ -36,18 +36,21 @@ const collectorOptions = { concurrencyLimit: 10, // an optional limit on pending requests }; -const provider = new WebTracerProvider(); const exporter = new OTLPTraceExporter(collectorOptions); -provider.addSpanProcessor(new BatchSpanProcessor(exporter, { - // The maximum queue size. After the size is reached spans are dropped. - maxQueueSize: 100, - // The maximum batch size of every export. It must be smaller or equal to maxQueueSize. - maxExportBatchSize: 10, - // The interval between two consecutive exports - scheduledDelayMillis: 500, - // How long the export can run before it is cancelled - exportTimeoutMillis: 30000, -})); +const provider = new WebTracerProvider({ + spanProcessors: [ + new BatchSpanProcessor(exporter, { + // The maximum queue size. After the size is reached spans are dropped. + maxQueueSize: 100, + // The maximum batch size of every export. It must be smaller or equal to maxQueueSize. + maxExportBatchSize: 10, + // The interval between two consecutive exports + scheduledDelayMillis: 500, + // How long the export can run before it is cancelled + exportTimeoutMillis: 30000, + }) + ] +}); provider.register(); @@ -67,14 +70,17 @@ const collectorOptions = { concurrencyLimit: 10, // an optional limit on pending requests }; -const provider = new BasicTracerProvider(); const exporter = new OTLPTraceExporter(collectorOptions); -provider.addSpanProcessor(new BatchSpanProcessor(exporter, { - // The maximum queue size. After the size is reached spans are dropped. - maxQueueSize: 1000, - // The interval between two consecutive exports - scheduledDelayMillis: 30000, -})); +const provider = new BasicTracerProvider({ + spanProcessors: [ + new BatchSpanProcessor(exporter, { + // The maximum queue size. After the size is reached spans are dropped. + maxQueueSize: 1000, + // The interval between two consecutive exports + scheduledDelayMillis: 30000, + }) + ] +}); provider.register(); diff --git a/experimental/packages/exporter-trace-otlp-proto/README.md b/experimental/packages/exporter-trace-otlp-proto/README.md index 9b6141b873f..984fbaa4603 100644 --- a/experimental/packages/exporter-trace-otlp-proto/README.md +++ b/experimental/packages/exporter-trace-otlp-proto/README.md @@ -32,9 +32,10 @@ const collectorOptions = { }, //an optional object containing custom headers to be sent with each request will only work with http }; -const provider = new BasicTracerProvider(); const exporter = new OTLPTraceExporter(collectorOptions); -provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); +const provider = new BasicTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(exporter)] +}); provider.register(); diff --git a/experimental/packages/opentelemetry-browser-detector/README.md b/experimental/packages/opentelemetry-browser-detector/README.md index b1cbe45e574..bc0d0ad8a54 100644 --- a/experimental/packages/opentelemetry-browser-detector/README.md +++ b/experimental/packages/opentelemetry-browser-detector/README.md @@ -24,10 +24,18 @@ async function start(){ let detectedResources= await detectResources({detectors:[browserDetector]}); resource=resource.merge(detectedResources); const provider = new WebTracerProvider({ - resource + resource, + spanProcessors: [ + new BatchSpanProcessor( + new OTLPTraceExporter({ url:CONF.url, headers: {} }), + { + exportTimeoutMillis: CONF.timeOutMillis, + scheduledDelayMillis:CONF.delayMillis + } + ) + ] }); - provider.addSpanProcessor(new BatchSpanProcessor(new OTLPTraceExporter( {url:CONF.url ,headers:{}}),{exportTimeoutMillis:CONF.timeOutMillis,scheduledDelayMillis:CONF.delayMillis})); provider.register({ // Changing default contextManager to use ZoneContextManager - supports asynchronous operations - optional contextManager: new ZoneContextManager(), diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/README.md b/experimental/packages/opentelemetry-instrumentation-fetch/README.md index 2d3c2d4aeed..bf966ef1383 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/README.md +++ b/experimental/packages/opentelemetry-instrumentation-fetch/README.md @@ -26,9 +26,9 @@ import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; -const provider = new WebTracerProvider(); - -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +const provider = new WebTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); provider.register({ contextManager: new ZoneContextManager(), @@ -40,15 +40,14 @@ registerInstrumentations({ // or plugin can be also initialised separately and then set the tracer provider or meter provider const fetchInstrumentation = new FetchInstrumentation(); -const provider = new WebTracerProvider(); +const provider = new WebTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); provider.register({ contextManager: new ZoneContextManager(), }); fetchInstrumentation.setTracerProvider(provider); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); - - // and some test fetch('http://localhost:8090/fetch.js'); diff --git a/experimental/packages/opentelemetry-instrumentation-grpc/README.md b/experimental/packages/opentelemetry-instrumentation-grpc/README.md index e887ef2e429..6d209019b8d 100644 --- a/experimental/packages/opentelemetry-instrumentation-grpc/README.md +++ b/experimental/packages/opentelemetry-instrumentation-grpc/README.md @@ -31,9 +31,10 @@ const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc'); const { registerInstrumentations } = require('@opentelemetry/instrumentation'); -const provider = new NodeTracerProvider(); +const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); provider.register(); registerInstrumentations({ diff --git a/experimental/packages/opentelemetry-instrumentation-http/README.md b/experimental/packages/opentelemetry-instrumentation-http/README.md index 030e8f327b6..794eb8de5fc 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/README.md +++ b/experimental/packages/opentelemetry-instrumentation-http/README.md @@ -35,9 +35,10 @@ const { } = require('@opentelemetry/sdk-trace-node'); const { registerInstrumentations } = require('@opentelemetry/instrumentation'); -const provider = new NodeTracerProvider(); +const provider = new NodeTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); provider.register(); registerInstrumentations({ diff --git a/experimental/packages/opentelemetry-instrumentation-xml-http-request/README.md b/experimental/packages/opentelemetry-instrumentation-xml-http-request/README.md index 8c706ffa61f..e3f0b136064 100644 --- a/experimental/packages/opentelemetry-instrumentation-xml-http-request/README.md +++ b/experimental/packages/opentelemetry-instrumentation-xml-http-request/README.md @@ -25,8 +25,9 @@ import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xm import { ZoneContextManager } from '@opentelemetry/context-zone'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; -const providerWithZone = new WebTracerProvider(); -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +const providerWithZone = new WebTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); providerWithZone.register({ contextManager: new ZoneContextManager(), diff --git a/packages/opentelemetry-context-zone-peer-dep/README.md b/packages/opentelemetry-context-zone-peer-dep/README.md index a505b9fbdda..da50eeb5fea 100644 --- a/packages/opentelemetry-context-zone-peer-dep/README.md +++ b/packages/opentelemetry-context-zone-peer-dep/README.md @@ -27,8 +27,9 @@ import { } from '@opentelemetry/sdk-trace-web'; import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep'; -const providerWithZone = new WebTracerProvider(); -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +const providerWithZone = new WebTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); providerWithZone.register({ contextManager: new ZoneContextManager() }); diff --git a/packages/opentelemetry-context-zone/README.md b/packages/opentelemetry-context-zone/README.md index 0162f9645d1..7b6d094e59b 100644 --- a/packages/opentelemetry-context-zone/README.md +++ b/packages/opentelemetry-context-zone/README.md @@ -24,8 +24,9 @@ import { } from '@opentelemetry/sdk-trace-web'; import { ZoneContextManager } from '@opentelemetry/context-zone'; -const providerWithZone = new WebTracerProvider(); -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +const providerWithZone = new WebTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); providerWithZone.register({ contextManager: new ZoneContextManager() }); diff --git a/packages/opentelemetry-exporter-jaeger/README.md b/packages/opentelemetry-exporter-jaeger/README.md index 7e662a345e8..c686af7b29e 100644 --- a/packages/opentelemetry-exporter-jaeger/README.md +++ b/packages/opentelemetry-exporter-jaeger/README.md @@ -84,7 +84,11 @@ const exporter = new JaegerExporter(options); Now, register the exporter. ```js -tracer.addSpanProcessor(new BatchSpanProcessor(exporter)); +const tracerProvider = new NodeTracerProvider({ + spanProcessors: [new BatchSpanProcessor(exporter)] +}) + +const tracer = traceProvider.getTracer('my-tracer') ``` You can use built-in `SimpleSpanProcessor` or `BatchSpanProcessor` or write your own. diff --git a/packages/opentelemetry-exporter-zipkin/README.md b/packages/opentelemetry-exporter-zipkin/README.md index dc9d57ad75c..2d4244ac823 100644 --- a/packages/opentelemetry-exporter-zipkin/README.md +++ b/packages/opentelemetry-exporter-zipkin/README.md @@ -41,7 +41,11 @@ const exporter = new ZipkinExporter(options); Now, register the exporter and start tracing. ```js -tracer.addSpanProcessor(new BatchSpanProcessor(exporter)); +const tracerProvider = new NodeTracerProvider({ + spanProcessors: [new BatchSpanProcessor(exporter)] +}) + +const tracer = traceProvider.getTracer('my-tracer') ``` You can use built-in `SimpleSpanProcessor` or `BatchSpanProcessor` or write your own. diff --git a/packages/opentelemetry-sdk-trace-base/test/performance/benchmark/BatchSpanProcessor.js b/packages/opentelemetry-sdk-trace-base/test/performance/benchmark/BatchSpanProcessor.js index ccdb82d481c..fc69043ee87 100644 --- a/packages/opentelemetry-sdk-trace-base/test/performance/benchmark/BatchSpanProcessor.js +++ b/packages/opentelemetry-sdk-trace-base/test/performance/benchmark/BatchSpanProcessor.js @@ -47,8 +47,9 @@ function createSpan() { span.end(); } -const tracerProvider = new BasicTracerProvider(); -tracerProvider.addSpanProcessor(new BatchSpanProcessor(new NoopExporter())); +const tracerProvider = new BasicTracerProvider({ + spanProcessors: [new BatchSpanProcessor(new NoopExporter())] +}); const tracer = tracerProvider.getTracer('test') const suite = new Benchmark.Suite('BatchSpanProcessor'); diff --git a/packages/opentelemetry-sdk-trace-web/README.md b/packages/opentelemetry-sdk-trace-web/README.md index 44ed1ffbe0a..92d4e93e020 100644 --- a/packages/opentelemetry-sdk-trace-web/README.md +++ b/packages/opentelemetry-sdk-trace-web/README.md @@ -40,8 +40,9 @@ import { DocumentLoad } from '@opentelemetry/plugin-document-load'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; -const provider = new WebTracerProvider(); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +const provider = new WebTracerProvider({ + spanProcessors: [new SimpleSpanProcessor(new ConsoleSpanExporter())] +}); provider.register({ // Changing default contextManager to use ZoneContextManager - supports asynchronous operations - optional diff --git a/selenium-tests/pages/tracing.js b/selenium-tests/pages/tracing.js index 6dd6e5f26f2..f88ce17618a 100644 --- a/selenium-tests/pages/tracing.js +++ b/selenium-tests/pages/tracing.js @@ -11,10 +11,13 @@ import { registerInstrumentations } from '@opentelemetry/instrumentation'; * @return {WebTracerProvider} */ export function loadOtel(instrumentations) { - const provider = new WebTracerProvider(); const memoryExporter = new InMemorySpanExporter(); - provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter)); - provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); + const provider = new WebTracerProvider({ + spanProcessors: [ + new SimpleSpanProcessor(memoryExporter), + new SimpleSpanProcessor(new ConsoleSpanExporter()), + ] + }); provider.register({ contextManager: new ZoneContextManager(), }); From d6bac25e0d6b45cce16e806be082eba2c2ada49d Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 11 Nov 2024 13:03:17 +0100 Subject: [PATCH 5/6] chore: update CHANGELOG_NEXT --- CHANGELOG_NEXT.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index 0fef0f2d1be..064956eab05 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -14,6 +14,7 @@ * refactor(resources)!: replace `ResourceAttributes` with `Attributes` [#5016](https://github.com/open-telemetry/opentelemetry-js/pull/5016) @david-luna * feat(sdk-metrics)!: drop `View` and `Aggregation` in favor of `ViewOptions` and `AggregationOption` [#4931](https://github.com/open-telemetry/opentelemetry-js/pull/4931) @pichlermarc * refactor(sdk-trace-base)!: remove `new Span` constructor in favor of `Tracer.startSpan` API [#5048](https://github.com/open-telemetry/opentelemetry-js/pull/5048) @david-luna +* refactor(sdk-trace-base)!: remove `BasicTracerProvider.addSpanProcessor` API in favor of constructor options. [#5134](https://github.com/open-telemetry/opentelemetry-js/pull/5134) @david-luna ### :rocket: (Enhancement) From d382b26f9461ffbe943e211a3eefc0d8ed9bb7b8 Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 11 Nov 2024 19:48:43 +0100 Subject: [PATCH 6/6] chore(sdk-node): review SDK tests --- .../opentelemetry-sdk-node/test/sdk.test.ts | 183 ++++++++++++++---- 1 file changed, 142 insertions(+), 41 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts index 28c5c214932..d0237fcecea 100644 --- a/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts +++ b/experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts @@ -29,6 +29,7 @@ import { AsyncLocalStorageContextManager, } from '@opentelemetry/context-async-hooks'; import { CompositePropagator } from '@opentelemetry/core'; +import { JaegerExporter } from '@opentelemetry/exporter-jaeger'; import { AggregationTemporality, ConsoleMetricExporter, @@ -54,7 +55,6 @@ import * as assert from 'assert'; import * as semver from 'semver'; import * as Sinon from 'sinon'; import { NodeSDK } from '../src'; -import * as sdkUtils from '../src/utils'; import { env } from 'process'; import { envDetector, @@ -76,10 +76,14 @@ import { import { OTLPLogExporter as OTLPProtoLogExporter } from '@opentelemetry/exporter-logs-otlp-proto'; import { OTLPLogExporter as OTLPHttpLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; import { OTLPLogExporter as OTLPGrpcLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc'; +import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; +// import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; +import { OTLPTraceExporter as OTLPGrpcTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'; import { SEMRESATTRS_HOST_NAME, SEMRESATTRS_PROCESS_PID, } from '@opentelemetry/semantic-conventions'; +import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; const DefaultContextManager = semver.gte(process.version, '14.8.0') ? AsyncLocalStorageContextManager @@ -1094,18 +1098,15 @@ describe('Node SDK', () => { }); describe('setup exporter from env', () => { - let spyGetOtlpProtocol: Sinon.SinonSpy; let stubLoggerError: Sinon.SinonStub; beforeEach(() => { - spyGetOtlpProtocol = Sinon.spy(sdkUtils, 'getOtlpProtocolFomEnv'); stubLoggerError = Sinon.stub(diag, 'warn'); }); afterEach(() => { - spyGetOtlpProtocol.restore(); stubLoggerError.restore(); }); - it('use default exporter TracerProviderWithEnvExporters when user does not provide span processor or trace exporter to sdk config', async () => { + it('should use default exporter when nor env neither SDK config is given', async () => { const sdk = new NodeSDK(); sdk.start(); const listOfProcessors = @@ -1113,9 +1114,10 @@ describe('setup exporter from env', () => { assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPProtoTraceExporter); await sdk.shutdown(); }); - it('ignore env exporter when user provides exporter to sdk config', async () => { + it('should ignore default env exporter when user provides exporter in sdk config', async () => { const traceExporter = new ConsoleSpanExporter(); const sdk = new NodeSDK({ traceExporter, @@ -1125,11 +1127,11 @@ describe('setup exporter from env', () => { sdk['_tracerProvider']!['_registeredSpanProcessors']!; assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof SimpleSpanProcessor === false); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ConsoleSpanExporter); await sdk.shutdown(); }); - it('ignores default env exporter when user provides span processor to sdk config', async () => { + it('should ignore default env exporter when user provides span processor in sdk config', async () => { const traceExporter = new ConsoleSpanExporter(); const spanProcessor = new SimpleSpanProcessor(traceExporter); const sdk = new NodeSDK({ @@ -1141,10 +1143,10 @@ describe('setup exporter from env', () => { assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof SimpleSpanProcessor); - assert(listOfProcessors[0] instanceof BatchSpanProcessor === false); + assert(listOfProcessors[0]['_exporter'] instanceof ConsoleSpanExporter); await sdk.shutdown(); }); - it('ignores env exporter when user provides tracer exporter to sdk config and sets exporter via env', async () => { + it('should ignore exporter form env if another is provided in sdk config', async () => { env.OTEL_TRACES_EXPORTER = 'console'; const traceExporter = new OTLPTraceExporter(); const sdk = new NodeSDK({ @@ -1155,8 +1157,8 @@ describe('setup exporter from env', () => { sdk['_tracerProvider']!['_registeredSpanProcessors']!; assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof SimpleSpanProcessor === false); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPTraceExporter); delete env.OTEL_TRACES_EXPORTER; await sdk.shutdown(); }); @@ -1173,10 +1175,12 @@ describe('setup exporter from env', () => { sdk['_tracerProvider']!['_config']?.sampler instanceof AlwaysOffSampler ); assert.strictEqual(listOfProcessors.length, 1); + assert(listOfProcessors[0] instanceof SimpleSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ConsoleSpanExporter); delete env.OTEL_TRACES_EXPORTER; await sdk.shutdown(); }); - it('use otlp exporter and defined exporter protocol env value', async () => { + it('should use otlp exporter and defined exporter protocol env value', async () => { env.OTEL_TRACES_EXPORTER = 'otlp'; env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; const sdk = new NodeSDK(); @@ -1187,48 +1191,49 @@ describe('setup exporter from env', () => { assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPGrpcTraceExporter); delete env.OTEL_TRACES_EXPORTER; delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; await sdk.shutdown(); }); - it('use noop span processor when user sets env exporter to none', async () => { - env.OTEL_TRACES_EXPORTER = 'none'; + it('sohuld use exporter and processor from env, signal specific env for protocol takes precedence', async () => { + env.OTEL_TRACES_EXPORTER = 'otlp'; + env.OTEL_EXPORTER_OTLP_PROTOCOL = 'http/protobuf'; + env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; const sdk = new NodeSDK(); sdk.start(); const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; - const activeProcessor = sdk['_tracerProvider']?.getActiveSpanProcessor(); - assert(listOfProcessors.length === 0); - assert(activeProcessor instanceof NoopSpanProcessor); + assert(listOfProcessors.length === 1); + assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPGrpcTraceExporter); delete env.OTEL_TRACES_EXPORTER; + delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; await sdk.shutdown(); }); - it('log warning that sdk will not be initialized when exporter is set to none', async () => { + it('should use noop span processor when user sets env exporter to none', async () => { env.OTEL_TRACES_EXPORTER = 'none'; const sdk = new NodeSDK(); sdk.start(); + // should warn assert.strictEqual( stubLoggerError.args[0][0], 'OTEL_TRACES_EXPORTER contains "none". SDK will not be initialized.' ); - delete env.OTEL_TRACES_EXPORTER; - await sdk.shutdown(); - }); - it('use default otlp exporter when user does not set exporter via env or config', async () => { - const sdk = new NodeSDK(); - sdk.start(); const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; + const activeProcessor = sdk['_tracerProvider']?.getActiveSpanProcessor(); - assert(listOfProcessors.length === 1); - assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors.length === 0); + assert(activeProcessor instanceof NoopSpanProcessor); + delete env.OTEL_TRACES_EXPORTER; await sdk.shutdown(); }); - it('use default otlp exporter when empty value is provided for exporter via env', async () => { + it('should use default otlp exporter when empty value is provided for exporter via env', async () => { env.OTEL_TRACES_EXPORTER = ''; const sdk = new NodeSDK(); sdk.start(); @@ -1238,36 +1243,33 @@ describe('setup exporter from env', () => { assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPProtoTraceExporter); env.OTEL_TRACES_EXPORTER = ''; await sdk.shutdown(); }); - it('use only default exporter when none value is provided with other exporters', async () => { + it('should use only default exporter when none value is provided with other exporters', async () => { env.OTEL_TRACES_EXPORTER = 'otlp,zipkin,none'; const sdk = new NodeSDK(); sdk.start(); + // also it should warn + assert.strictEqual( + stubLoggerError.args[0][0], + 'OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.' + ); + const listOfProcessors = sdk['_tracerProvider']!['_registeredSpanProcessors']!; assert(listOfProcessors.length === 1); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPProtoTraceExporter); delete env.OTEL_TRACES_EXPORTER; await sdk.shutdown(); }); - it('log warning that only default exporter will be used since exporter list contains none with other exports ', async () => { - env.OTEL_TRACES_EXPORTER = 'otlp,zipkin,none'; - const sdk = new NodeSDK(); - sdk.start(); - assert.strictEqual( - stubLoggerError.args[0][0], - 'OTEL_TRACES_EXPORTER contains "none" along with other exporters. Using default otlp exporter.' - ); - delete env.OTEL_TRACES_EXPORTER; - await sdk.shutdown(); - }); it('should warn that provided exporter value is unrecognized and not able to be set up', async () => { env.OTEL_TRACES_EXPORTER = 'invalid'; const sdk = new NodeSDK(); @@ -1286,7 +1288,83 @@ describe('setup exporter from env', () => { delete env.OTEL_TRACES_EXPORTER; await sdk.shutdown(); }); - it('setup zipkin, jaeger and otlp exporters', async () => { + it('should be able to setup zipkin exporter', async () => { + env.OTEL_TRACES_EXPORTER = 'zipkin'; + env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; + const sdk = new NodeSDK(); + sdk.start(); + + const listOfProcessors = + sdk['_tracerProvider']!['_registeredSpanProcessors']!; + + assert(listOfProcessors.length === 1); + assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ZipkinExporter); + + delete env.OTEL_TRACES_EXPORTER; + delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; + await sdk.shutdown(); + }); + + it('should be able to setup zipkin and otlp exporters', async () => { + env.OTEL_TRACES_EXPORTER = 'zipkin, otlp'; + env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; + const sdk = new NodeSDK(); + sdk.start(); + + const listOfProcessors = + sdk['_tracerProvider']!['_registeredSpanProcessors']!; + + assert(listOfProcessors.length === 2); + assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ZipkinExporter); + assert(listOfProcessors[1] instanceof BatchSpanProcessor); + assert(listOfProcessors[1]['_exporter'] instanceof OTLPGrpcTraceExporter); + + delete env.OTEL_TRACES_EXPORTER; + delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; + await sdk.shutdown(); + }); + + it('should be able to setup jaeger exporter', async () => { + env.OTEL_TRACES_EXPORTER = 'jaeger'; + env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; + const sdk = new NodeSDK(); + sdk.start(); + + const listOfProcessors = + sdk['_tracerProvider']!['_registeredSpanProcessors']!; + + assert(listOfProcessors.length === 1); + assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof JaegerExporter); + + delete env.OTEL_TRACES_EXPORTER; + delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; + await sdk.shutdown(); + }); + + it('should be able to setup jaeger and otlp exporters', async () => { + env.OTEL_TRACES_EXPORTER = 'otlp, jaeger'; + env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; + const sdk = new NodeSDK(); + sdk.start(); + + const listOfProcessors = + sdk['_tracerProvider']!['_registeredSpanProcessors']!; + + assert(listOfProcessors.length === 2); + assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof OTLPGrpcTraceExporter); + assert(listOfProcessors[1] instanceof BatchSpanProcessor); + assert(listOfProcessors[1]['_exporter'] instanceof JaegerExporter); + + delete env.OTEL_TRACES_EXPORTER; + delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; + await sdk.shutdown(); + }); + + it('should be able to setup zipkin, jaeger and otlp exporters', async () => { env.OTEL_TRACES_EXPORTER = 'zipkin, otlp, jaeger'; env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; const sdk = new NodeSDK(); @@ -1297,14 +1375,18 @@ describe('setup exporter from env', () => { assert(listOfProcessors.length === 3); assert(listOfProcessors[0] instanceof BatchSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ZipkinExporter); assert(listOfProcessors[1] instanceof BatchSpanProcessor); + assert(listOfProcessors[1]['_exporter'] instanceof OTLPGrpcTraceExporter); assert(listOfProcessors[2] instanceof BatchSpanProcessor); + assert(listOfProcessors[2]['_exporter'] instanceof JaegerExporter); delete env.OTEL_TRACES_EXPORTER; delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; await sdk.shutdown(); }); - it('use the console exporter', async () => { + + it('should be able to use console and otlp exporters', async () => { env.OTEL_TRACES_EXPORTER = 'console, otlp'; const sdk = new NodeSDK(); sdk.start(); @@ -1314,8 +1396,27 @@ describe('setup exporter from env', () => { assert(listOfProcessors.length === 2); assert(listOfProcessors[0] instanceof SimpleSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ConsoleSpanExporter); assert(listOfProcessors[1] instanceof BatchSpanProcessor); + assert(listOfProcessors[1]['_exporter'] instanceof OTLPProtoTraceExporter); + delete env.OTEL_TRACES_EXPORTER; + await sdk.shutdown(); + }); + + it('should ignore the protocol from env when use the console exporter', async () => { + env.OTEL_TRACES_EXPORTER = 'console'; + env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = 'grpc'; + const sdk = new NodeSDK(); + sdk.start(); + + const listOfProcessors = + sdk['_tracerProvider']!['_registeredSpanProcessors']!; + + assert(listOfProcessors.length === 1); + assert(listOfProcessors[0] instanceof SimpleSpanProcessor); + assert(listOfProcessors[0]['_exporter'] instanceof ConsoleSpanExporter); delete env.OTEL_TRACES_EXPORTER; + delete env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL; await sdk.shutdown(); }); });