Skip to content

Commit

Permalink
fix(*): Migrate away from deprecated addSpanProcessor in tests
Browse files Browse the repository at this point in the history
This migrates from the deprecated `addSpanProcessor` method to the
constructor option in preparation for the upcoming SDK 2.0 release.

This commit migrates all usages **in tests** for each package, and
there are no changes to the runtime code.

Ref #2645
  • Loading branch information
chancancode committed Jan 17, 2025
1 parent 4cf7e6f commit 03dad02
Show file tree
Hide file tree
Showing 53 changed files with 171 additions and 137 deletions.
6 changes: 3 additions & 3 deletions plugins/node/instrumentation-cucumber/test/cucumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ import {
import { PassThrough } from 'stream';

describe('CucumberInstrumentation', () => {
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'CucumberInstrumentation',
}),
spanProcessors: [spanProcessor],
});
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
provider.addSpanProcessor(spanProcessor);
const contextManager = new AsyncHooksContextManager().enable();

before(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ describe('DataloaderInstrumentation', () => {
let contextManager: AsyncHooksContextManager;

const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
const tracer = provider.getTracer('default');

instrumentation.setTracerProvider(provider);
extraInstrumentation.setTracerProvider(provider);
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));

beforeEach(async () => {
instrumentation.enable();
Expand Down
7 changes: 4 additions & 3 deletions plugins/node/instrumentation-fs/test/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ const pluginConfig = {
createHook,
endHook,
};
const provider = new BasicTracerProvider();
const tracer = provider.getTracer('default');
const memoryExporter = new InMemorySpanExporter();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
const tracer = provider.getTracer('default');

describe('fs instrumentation', () => {
let contextManager: AsyncHooksContextManager;
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-fs/test/fsHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ const pluginConfig = {
endHook,
};

const provider = new BasicTracerProvider();
const memoryExporter = new InMemorySpanExporter();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

const assertNotHookError = (err?: Error | null) => {
assert.ok(
Expand Down
7 changes: 4 additions & 3 deletions plugins/node/instrumentation-fs/test/fsPromises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ const endHook = <EndHook>sinon.spy((fnName, { args, span }) => {
const pluginConfig = {
endHook,
};
const provider = new BasicTracerProvider();
const tracer = provider.getTracer('default');
const memoryExporter = new InMemorySpanExporter();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
const tracer = provider.getTracer('default');

describe('fs/promises instrumentation', () => {
let contextManager: AsyncHooksContextManager;
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-fs/test/fsPromisesHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ const pluginConfig = {
endHook,
};

const provider = new BasicTracerProvider();
const memoryExporter = new InMemorySpanExporter();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

const assertNotHookError = (err?: Error | null) => {
assert.ok(
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-fs/test/parent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import type { FsInstrumentationConfig } from '../src/types';
import * as api from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';

const provider = new BasicTracerProvider();
const memoryExporter = new InMemorySpanExporter();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

const tracer = provider.getTracer('default');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ describe('tedious', () => {
let tedious: any;
let contextManager: AsyncHooksContextManager;
let connection: Connection;
const provider = new BasicTracerProvider();
const memoryExporter = new InMemorySpanExporter();
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
const shouldTest = process.env.RUN_MSSQL_TESTS; // For CI: assumes local db is already available
const shouldTestLocally = process.env.RUN_MSSQL_TESTS_LOCAL; // For local: spins up local db via docker
const memoryExporter = new InMemorySpanExporter();

before(function (done) {
if (!(shouldTest || shouldTestLocally) || incompatVersions) {
Expand All @@ -101,7 +103,6 @@ describe('tedious', () => {
this.test!.parent!.pending = true;
this.skip();
}
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
if (shouldTestLocally) {
testUtils.startDocker('mssql');
// wait 15 seconds for docker container to start
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-undici/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ describe('UndiciInstrumentation `fetch` tests', function () {
const hostname = 'localhost';
const mockServer = new MockServer();
const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

before(function (done) {
// Do not test if the `fetch` global API is not available
Expand Down
5 changes: 3 additions & 2 deletions plugins/node/instrumentation-undici/test/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ describe('UndiciInstrumentation `undici` tests', function () {
const hostname = 'localhost';
const mockServer = new MockServer();
const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

before(function (done) {
// Load `undici`. It may fail if nodejs version is <18 because the module uses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ describe('force flush', () => {
});

it('should force flush NodeTracerProvider', async () => {
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new BatchSpanProcessor(traceMemoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new BatchSpanProcessor(traceMemoryExporter)],
});
provider.register();
let forceFlushed = false;
const forceFlush = () =>
Expand Down Expand Up @@ -118,10 +119,9 @@ describe('force flush', () => {
});

it('should force flush ProxyTracerProvider with NodeTracerProvider', async () => {
const nodeTracerProvider = new NodeTracerProvider();
nodeTracerProvider.addSpanProcessor(
new BatchSpanProcessor(traceMemoryExporter)
);
const nodeTracerProvider = new NodeTracerProvider({
spanProcessors: [new BatchSpanProcessor(traceMemoryExporter)],
});
nodeTracerProvider.register();
const provider = new ProxyTracerProvider();
provider.setDelegate(nodeTracerProvider);
Expand Down Expand Up @@ -183,10 +183,9 @@ describe('force flush', () => {
});

it('should callback once after force flush providers', async () => {
const nodeTracerProvider = new NodeTracerProvider();
nodeTracerProvider.addSpanProcessor(
new BatchSpanProcessor(traceMemoryExporter)
);
const nodeTracerProvider = new NodeTracerProvider({
spanProcessors: [new BatchSpanProcessor(traceMemoryExporter)],
});
nodeTracerProvider.register();
const tracerProvider = new ProxyTracerProvider();
tracerProvider.setDelegate(nodeTracerProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ describe('lambda handler', () => {
) => {
process.env._HANDLER = handler;

const provider = new NodeTracerProvider();
provider.addSpanProcessor(new BatchSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new BatchSpanProcessor(memoryExporter)],
});
provider.register();

instrumentation = new AwsLambdaInstrumentation(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ import type * as BunyanLogger from 'bunyan';
// import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const tracerProvider = new NodeTracerProvider();
const tracerProvider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(new InMemorySpanExporter())],
});
tracerProvider.register();
tracerProvider.addSpanProcessor(
new SimpleSpanProcessor(new InMemorySpanExporter())
);
const tracer = tracerProvider.getTracer('default');

const resource = new Resource({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ import {
import { ResponseHookInfo } from '../src/types';

const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
context.setGlobalContextManager(new AsyncHooksContextManager());

const testCassandra = process.env.RUN_CASSANDRA_TESTS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ const httpRequest = {
const instrumentation = new ConnectInstrumentation();
const contextManager = new AsyncHooksContextManager().enable();
const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
const provider = new NodeTracerProvider({
spanProcessors: [spanProcessor],
});
instrumentation.setTracerProvider(provider);
context.setGlobalContextManager(contextManager);

const tracer = provider.getTracer('default');

provider.addSpanProcessor(spanProcessor);
instrumentation.enable();

import * as connect from 'connect';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import * as Sinon from 'sinon';
import * as dns from 'dns';

const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
const tracer = provider.getTracer('default');
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));

describe('DnsInstrumentation', () => {
let instrumentation: DnsInstrumentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import { assertSpan } from '../utils/assertSpan';
import { SpanStatusCode } from '@opentelemetry/api';

const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

describe('dns.lookup()', () => {
let instrumentation: DnsInstrumentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import { assertSpan } from '../utils/assertSpan';
import { SpanStatusCode } from '@opentelemetry/api';

const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

const supportsPromises =
parseInt(process.versions.node.split('.')[0], 10) >= 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import { assertSpan } from '../utils/assertSpan';
import { SpanStatusCode } from '@opentelemetry/api';

const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

describe('dns.promises.lookup()', () => {
let instrumentation: DnsInstrumentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import * as express from 'express';
import * as http from 'http';

describe('ExpressInstrumentation', () => {
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
provider.addSpanProcessor(spanProcessor);
const provider = new NodeTracerProvider({
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
const tracer = provider.getTracer('default');
const contextManager = new AsyncHooksContextManager().enable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ import { RPCMetadata, getRPCMetadata } from '@opentelemetry/core';
import { Server } from 'http';

describe('ExpressInstrumentation', () => {
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
provider.addSpanProcessor(spanProcessor);
const provider = new NodeTracerProvider({
spanProcessors: [spanProcessor],
});
const tracer = provider.getTracer('default');
const contextManager = new AsyncHooksContextManager().enable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ import { httpRequest, serverWithMiddleware } from './utils';
import { RPCMetadata, getRPCMetadata } from '@opentelemetry/core';

describe('ExpressInstrumentation hooks', () => {
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
provider.addSpanProcessor(spanProcessor);
const provider = new NodeTracerProvider({
spanProcessors: [spanProcessor],
});
const tracer = provider.getTracer('default');
const contextManager = new AsyncHooksContextManager().enable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ import * as express from 'express';
import * as http from 'http';

describe('ExpressInstrumentation', () => {
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
provider.addSpanProcessor(spanProcessor);
const provider = new NodeTracerProvider({
spanProcessors: [spanProcessor],
});
const tracer = provider.getTracer('default');
const contextManager = new AsyncHooksContextManager().enable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ const httpInstrumentation = new HttpInstrumentation();
const instrumentation = new FastifyInstrumentation();
const contextManager = new AsyncHooksContextManager().enable();
const memoryExporter = new InMemorySpanExporter();
const provider = new NodeTracerProvider();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
const provider = new NodeTracerProvider({
spanProcessors: [spanProcessor],
});
instrumentation.setTracerProvider(provider);
httpInstrumentation.setTracerProvider(provider);
context.setGlobalContextManager(contextManager);

provider.addSpanProcessor(spanProcessor);
instrumentation.enable();
httpInstrumentation.enable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ const createPool = {
}[isOldVersion ? 'v2' : 'v3'];

describe('GenericPool instrumentation', () => {
const provider = new NodeTracerProvider();
const memoryExporter = new InMemorySpanExporter();
const spanProcessor = new SimpleSpanProcessor(memoryExporter);
provider.addSpanProcessor(spanProcessor);
const provider = new NodeTracerProvider({
spanProcessors: [spanProcessor],
});
plugin.setTracerProvider(provider);
const tracer = provider.getTracer('default');
let contextManager: AsyncHooksContextManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ const queryInvalid = `
`;

const exporter = new InMemorySpanExporter();
const provider = new BasicTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
const provider = new BasicTracerProvider({
spanProcessors: [new SimpleSpanProcessor(exporter)],
});
graphQLInstrumentation.setTracerProvider(provider);

describe('graphql', () => {
Expand Down
Loading

0 comments on commit 03dad02

Please sign in to comment.