Skip to content

Commit

Permalink
fix(examples): Migrate away from deprecated addSpanProcessor SDK2
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 everything in the `examples` folder and there
are no changes to the published packages.

Ref open-telemetry#2645
  • Loading branch information
chancancode committed Jan 16, 2025
1 parent e57f3e4 commit 76a15a2
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 93 deletions.
8 changes: 4 additions & 4 deletions examples/connect/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ function log() {
}

module.exports = (serviceName) => {
const exporter = new CollectorTraceExporter();
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
});
const connectInstrumentation = new ConnectInstrumentation();
registerInstrumentations({
Expand All @@ -37,10 +41,6 @@ module.exports = (serviceName) => {
],
});

const exporter = new CollectorTraceExporter();

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register({});
return {
Expand Down
8 changes: 5 additions & 3 deletions examples/dns/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const EXPORTER = process.env.EXPORTER || '';

module.exports = (serviceName) => {
const provider = new NodeTracerProvider();

let exporter;
if (EXPORTER.toLowerCase().startsWith('z')) {
exporter = new ZipkinExporter({
Expand All @@ -24,7 +22,11 @@ module.exports = (serviceName) => {
});
}

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

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();
Expand Down
8 changes: 4 additions & 4 deletions examples/express/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';

export const setupTracing = (serviceName: string) => {
const exporter = new OTLPTraceExporter({});
const provider = new NodeTracerProvider({
resource: new Resource({
[ATTR_SERVICE_NAME]: serviceName,
}),
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()),
});
registerInstrumentations({
Expand All @@ -26,10 +30,6 @@ export const setupTracing = (serviceName: string) => {
],
});

const exporter = new OTLPTraceExporter({});

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

Expand Down
6 changes: 4 additions & 2 deletions examples/graphql/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'graphql-service',
}),
spanProcessors: [
new SimpleSpanProcessor(new OTLPTraceExporter()),
new SimpleSpanProcessor(new ConsoleSpanExporter()),
],
});

provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

registerInstrumentations({
Expand Down
12 changes: 7 additions & 5 deletions examples/grpc-census-prop/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc');
* requested propagator
*/
module.exports = (serviceName, binaryPropagator) => {
const provider = new NodeTracerProvider();

// It is recommended to use this `BatchSpanProcessor` for better performance
// and optimization, especially in production.
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
const provider = new NodeTracerProvider({
spanProcessors: [
// It is recommended to use `BatchSpanProcessor` for better performance
// and optimization, especially in production.
new SimpleSpanProcessor(new ConsoleSpanExporter()),
],
});

if (binaryPropagator) {
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
Expand Down
9 changes: 6 additions & 3 deletions examples/hapi/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const EXPORTER = process.env.EXPORTER || '';

module.exports = (serviceName) => {
const provider = new NodeTracerProvider();

let exporter;
if (EXPORTER === 'jaeger') {
exporter = new JaegerExporter({ serviceName });
} else {
exporter = new ZipkinExporter({ serviceName });
}
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

const provider = new NodeTracerProvider({
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
});

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();
Expand Down
8 changes: 5 additions & 3 deletions examples/ioredis/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { IORedisInstrumentation } = require('@opentelemetry/instrumentation-ioredis');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

const provider = new NodeTracerProvider();

const exporter = new JaegerExporter({ serviceName: 'ioredis-example' });

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

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
provider.register();
Expand Down
16 changes: 9 additions & 7 deletions examples/koa/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
const EXPORTER = process.env.EXPORTER || '';

export const setupTracing = (serviceName: string) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName
})
});

let exporter;
if (EXPORTER === 'jaeger') {
exporter = new JaegerExporter();
} else {
exporter = new ZipkinExporter();
}
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName
}),
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
});

registerInstrumentations({
instrumentations: [
Expand Down
8 changes: 4 additions & 4 deletions examples/memcached/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-convention
const { MemcachedInstrumentation } = require('@opentelemetry/instrumentation-memcached');

module.exports = (serviceName) => {
const exporter = new ConsoleSpanExporter();
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
});
registerInstrumentations({
tracerProvider: provider,
Expand All @@ -26,10 +30,6 @@ module.exports = (serviceName) => {
],
});

const exporter = new ConsoleSpanExporter();

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

Expand Down
7 changes: 5 additions & 2 deletions examples/meta-node/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ module.exports = () => {
serviceName: 'basic-service',
});

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

registerInstrumentations({
Expand Down
7 changes: 4 additions & 3 deletions examples/mongodb/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const setupTracing = (serviceName: string): api.Tracer => {
resource: new Resource({
[ATTR_SERVICE_NAME]: serviceName,
}),
spanProcessors: [
new SimpleSpanProcessor(new ZipkinExporter()),
new SimpleSpanProcessor(new OTLPTraceExporter()),
],
});

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
Expand All @@ -30,8 +34,5 @@ export const setupTracing = (serviceName: string): api.Tracer => {
tracerProvider: provider,
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter()));

return api.trace.getTracer('mongodb-example');
};
16 changes: 10 additions & 6 deletions examples/mysql/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import opentelemetry from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { SimpleSpanProcessor, SpanProcessor } from '@opentelemetry/sdk-trace-base';
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
Expand Down Expand Up @@ -30,15 +30,19 @@ export const setupTracing = (serviceName: string) => {
meterProvider.addMetricReader(metricReader);

//traces:
const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),});
const spanProcessors: SpanProcessor[] = [];

if (EXPORTER.toLowerCase().startsWith('z')) {
tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter()));
spanProcessors.push(new SimpleSpanProcessor(new ZipkinExporter()));
}

const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),
spanProcessors,
});

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
tracerProvider.register();

Expand Down
15 changes: 8 additions & 7 deletions examples/network/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');

const provider = new NodeTracerProvider();

provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({
serviceName: 'http-client',
})));

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
const provider = new NodeTracerProvider({
spanProcessors: [
new SimpleSpanProcessor(new JaegerExporter({
serviceName: 'http-client',
})),
new SimpleSpanProcessor(new ConsoleSpanExporter()),
],
});

provider.register();

Expand Down
10 changes: 6 additions & 4 deletions examples/react-load/preact/src/web-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';

export default (serviceName) => {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
const provider = new WebTracerProvider();
const exporter = new OTLPTraceExporter({
url: 'http://localhost:4318/v1/traces',
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
const provider = new WebTracerProvider({
spanProcessors: [
new SimpleSpanProcessor(new ConsoleSpanExporter()),
new SimpleSpanProcessor(exporter),
],
});

provider.register({
contextManager: new ZoneContextManager(),
Expand Down
15 changes: 8 additions & 7 deletions examples/react-load/react/src/web-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'

export default (serviceName) => {
const exporter = new CollectorTraceExporter({
url: 'http://localhost:55678/v1/trace',
});

const provider = new WebTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: "react-load-example"
}),
spanProcessors: [
new SimpleSpanProcessor(new ConsoleSpanExporter()),
new SimpleSpanProcessor(exporter),
],
});

const exporter = new CollectorTraceExporter({
url: 'http://localhost:55678/v1/trace',
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register({
contextManager: new ZoneContextManager(),
});
Expand Down
14 changes: 8 additions & 6 deletions examples/redis/src/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
const EXPORTER = process.env.EXPORTER || '';

export const setupTracing = (serviceName: string) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),});

let exporter;
if (EXPORTER.toLowerCase().startsWith('z')) {
exporter = new ZipkinExporter();
} else {
exporter = new JaegerExporter();
}

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),
spanProcessors: [
new SimpleSpanProcessor(exporter),
]
});

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();
Expand Down
17 changes: 10 additions & 7 deletions examples/restify/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ const Exporter = ((exporterParam) => {
})(process.env.EXPORTER);

module.exports = (serviceName) => {
const provider = new NodeTracerProvider();
const exporter = new Exporter({
serviceName,
});

const provider = new NodeTracerProvider({
spanProcessors: [
new SimpleSpanProcessor(exporter),
],
});

registerInstrumentations({
tracerProvider: provider,
instrumentations: [
Expand All @@ -37,12 +46,6 @@ module.exports = (serviceName) => {
],
});

const exporter = new Exporter({
serviceName,
});

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

Expand Down
Loading

0 comments on commit 76a15a2

Please sign in to comment.