Skip to content

Commit

Permalink
feat(sdk-trace-base): add spanProcessors property in TracerConfig
Browse files Browse the repository at this point in the history
… interface (#5138)
  • Loading branch information
david-luna authored Nov 12, 2024
1 parent f1ef596 commit db14633
Show file tree
Hide file tree
Showing 38 changed files with 529 additions and 727 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
* default: `true` (no change in behavior)
* note: `false` will become the default behavior in the next major version in order to comply with [specification requirements](https://github.com/open-telemetry/opentelemetry-specification/blob/f3511a5ccda376dfd1de76dfa086fc9b35b54757/specification/resource/sdk.md?plain=1#L31-L36)

* feat(sdk-trace-base): add `spanProcessors` property in `TracerConfig` interface. [#5138](https://github.com/open-telemetry/opentelemetry-js/pull/5138) @david-luna

### :bug: (Bug Fix)

* fix(sdk-metrics): await exports in `PeriodicExportingMetricReader` when async resource attributes have not yet settled [#5119](https://github.com/open-telemetry/opentelemetry-js/pull/5119/) @pichlermarc
Expand Down
5 changes: 3 additions & 2 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions examples/basic-tracer-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 3 additions & 9 deletions examples/grpc-js/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 4 additions & 10 deletions examples/http/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 3 additions & 10 deletions examples/https/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
17 changes: 8 additions & 9 deletions examples/opentelemetry-web/examples/fetch-proto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 8 additions & 6 deletions examples/opentelemetry-web/examples/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 8 additions & 6 deletions examples/opentelemetry-web/examples/fetchXhr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand Down
14 changes: 8 additions & 6 deletions examples/opentelemetry-web/examples/fetchXhrB3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 8 additions & 7 deletions examples/opentelemetry-web/examples/xml-http-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
26 changes: 15 additions & 11 deletions examples/opentelemetry-web/examples/zipkin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion examples/opentracing-shim/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 4 additions & 2 deletions examples/otlp-exporter-node/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 8 additions & 6 deletions experimental/examples/opencensus-shim/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
15 changes: 9 additions & 6 deletions experimental/packages/exporter-trace-otlp-grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const collectorOptions = {
url: 'http://<collector-hostname>:<port>',
};

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 => {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down
Loading

0 comments on commit db14633

Please sign in to comment.