Skip to content

Commit

Permalink
feat(sdk-trace-base)!: remove addSpanProcessor API (#5152)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna authored Nov 14, 2024
1 parent a833d9c commit b7343ef
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 143 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions examples/esm-http-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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,
Expand All @@ -868,7 +873,6 @@ export const runTests = (
ServerStreamMethod: true,
BidiStreamMethod: false,
};
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
beforeEach(() => {
memoryExporter.reset();
});
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -251,7 +251,7 @@ export class NodeSDK {
? this._resource
: this._resource.merge(
new Resource({
[SEMRESATTRS_SERVICE_NAME]: this._serviceName,
[ATTR_SERVICE_NAME]: this._serviceName,
})
);

Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/shim-opencensus/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export async function withTestTracer(
export async function withTestTracerProvider(
func: (otelTracerProvider: TracerProvider) => void | Promise<void>
): Promise<ReadableSpan[]> {
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);

Expand Down
24 changes: 0 additions & 24 deletions packages/opentelemetry-sdk-trace-base/src/BasicTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit b7343ef

Please sign in to comment.