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) 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..65a289ec3c1 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..8ba1a4c3dab 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..bd5104bdbfe 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 0d515baf46d..9ad58835c03 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 @@ -100,8 +100,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 a9f3b506a60..faced10302d 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 da19460c4eb..597dadfeda3 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/integrations/esm.test.mjs +++ b/experimental/packages/opentelemetry-instrumentation-http/test/integrations/esm.test.mjs @@ -45,9 +45,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 558bbcb8ff4..47fb01989c6 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 @@ -129,8 +129,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 ec0c35f6303..309ad0afe60 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 @@ -129,8 +129,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..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 @@ -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..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 @@ -268,21 +268,23 @@ 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 +847,19 @@ 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/experimental/packages/opentelemetry-sdk-node/src/sdk.ts b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts index bc4bf5e586c..ba4c8b48568 100644 --- a/experimental/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/experimental/packages/opentelemetry-sdk-node/src/sdk.ts @@ -61,7 +61,7 @@ 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 { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; import { @@ -251,7 +251,7 @@ export class NodeSDK { ? this._resource : this._resource.merge( new Resource({ - [SEMRESATTRS_SERVICE_NAME]: this._serviceName, + [ATTR_SERVICE_NAME]: this._serviceName, }) ); diff --git a/experimental/packages/shim-opencensus/test/util.ts b/experimental/packages/shim-opencensus/test/util.ts index 0d34bd3cca1..69a45204e67 100644 --- a/experimental/packages/shim-opencensus/test/util.ts +++ b/experimental/packages/shim-opencensus/test/util.ts @@ -39,11 +39,11 @@ 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); diff --git a/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts b/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts index a567bcebe0e..d09a8e634ba 100644 --- a/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts +++ b/packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts @@ -126,30 +126,6 @@ export class BasicTracerProvider implements TracerProvider { return this._tracers.get(key)!; } - /** - * @deprecated please use {@link TracerConfig} spanProcessors property - * 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/test/common/BasicTracerProvider.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts index 6488970ccb1..8044669dbdd 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts @@ -787,12 +787,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() @@ -816,11 +815,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..997df489a4a 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..ddf2d9ac466 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..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 @@ -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 91c4bffaf63..e4a45731b7b 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..6eb1702c8c5 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' },