Skip to content

Commit

Permalink
feat(node-sdk): add spanProcessors option (#4454)
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah authored Feb 5, 2024
1 parent 2df6310 commit 72c60f1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat(exporter-metrics-otlp-http): add option to set the exporter aggregation preference [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas
* feat(node-sdk): add spanProcessors option [#4454](https://github.com/open-telemetry/opentelemetry-js/pull/4454) @naseemkullah

### :bug: (Bug Fix)

Expand Down
6 changes: 6 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ Configure a custom sampler. By default, all traces will be sampled.

### spanProcessor

Deprecated, please use [spanProcessors](#spanprocessors) instead.

### spanProcessors

An array of span processors to register to the tracer provider.

### traceExporter

Configure a trace exporter. If an exporter is configured, it will be used with a [BatchSpanProcessor](../../../packages/opentelemetry-sdk-trace-base/src/platform/node/export/BatchSpanProcessor.ts). If an exporter OR span processor is not configured programatically, this package will auto setup the default `otlp` exporter with `http/protobuf` protocol with a `BatchSpanProcessor`.
Expand Down
26 changes: 20 additions & 6 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type LoggerProviderConfig = {
export class NodeSDK {
private _tracerProviderConfig?: {
tracerConfig: NodeTracerConfig;
spanProcessor: SpanProcessor;
spanProcessors: SpanProcessor[];
contextManager?: ContextManager;
textMapPropagator?: TextMapPropagator;
};
Expand Down Expand Up @@ -130,7 +130,11 @@ export class NodeSDK {
this._autoDetectResources = configuration.autoDetectResources ?? true;

// If a tracer provider can be created from manual configuration, create it
if (configuration.traceExporter || configuration.spanProcessor) {
if (
configuration.traceExporter ||
configuration.spanProcessor ||
configuration.spanProcessors
) {
const tracerProviderConfig: NodeTracerConfig = {};

if (configuration.sampler) {
Expand All @@ -143,13 +147,21 @@ export class NodeSDK {
tracerProviderConfig.idGenerator = configuration.idGenerator;
}

if (configuration.spanProcessor) {
diag.warn(
"The 'spanProcessor' option is deprecated. Please use 'spanProcessors' instead."
);
}

const spanProcessor =
configuration.spanProcessor ??
new BatchSpanProcessor(configuration.traceExporter!);

const spanProcessors = configuration.spanProcessors ?? [spanProcessor];

this.configureTracerProvider(
tracerProviderConfig,
spanProcessor,
spanProcessors,
configuration.contextManager,
configuration.textMapPropagator
);
Expand Down Expand Up @@ -192,13 +204,13 @@ export class NodeSDK {
*/
public configureTracerProvider(
tracerConfig: NodeTracerConfig,
spanProcessor: SpanProcessor,
spanProcessors: SpanProcessor[],
contextManager?: ContextManager,
textMapPropagator?: TextMapPropagator
): void {
this._tracerProviderConfig = {
tracerConfig,
spanProcessor,
spanProcessors,
contextManager,
textMapPropagator,
};
Expand Down Expand Up @@ -334,7 +346,9 @@ export class NodeSDK {
this._tracerProvider = tracerProvider;

if (this._tracerProviderConfig) {
tracerProvider.addSpanProcessor(this._tracerProviderConfig.spanProcessor);
for (const spanProcessor of this._tracerProviderConfig.spanProcessors) {
tracerProvider.addSpanProcessor(spanProcessor);
}
}

tracerProvider.register({
Expand Down
4 changes: 3 additions & 1 deletion experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export interface NodeSDKConfiguration {
resourceDetectors: Array<Detector | DetectorSync>;
sampler: Sampler;
serviceName?: string;
spanProcessor: SpanProcessor;
/** @deprecated use spanProcessors instead*/
spanProcessor?: SpanProcessor;
spanProcessors?: SpanProcessor[];
traceExporter: SpanExporter;
spanLimits: SpanLimits;
idGenerator: IdGenerator;
Expand Down
18 changes: 15 additions & 3 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,15 @@ describe('Node SDK', () => {
delete env.OTEL_TRACES_EXPORTER;
});

it('should register a tracer provider if a span processor is provided', async () => {
it('should register a tracer provider if span processors are provided', async () => {
const exporter = new ConsoleSpanExporter();
const spanProcessor = new SimpleSpanProcessor(exporter);

const sdk = new NodeSDK({
spanProcessor,
spanProcessors: [
new NoopSpanProcessor(),
new SimpleSpanProcessor(exporter),
new BatchSpanProcessor(exporter),
],
autoDetectResources: false,
});

Expand All @@ -223,6 +226,15 @@ describe('Node SDK', () => {
const apiTracerProvider =
trace.getTracerProvider() as ProxyTracerProvider;
assert.ok(apiTracerProvider.getDelegate() instanceof NodeTracerProvider);

const listOfProcessors =
sdk['_tracerProvider']!['_registeredSpanProcessors']!;

assert(sdk['_tracerProvider'] instanceof NodeTracerProvider);
assert(listOfProcessors.length === 3);
assert(listOfProcessors[0] instanceof NoopSpanProcessor);
assert(listOfProcessors[1] instanceof SimpleSpanProcessor);
assert(listOfProcessors[2] instanceof BatchSpanProcessor);
});

it('should register a meter provider if a reader is provided', async () => {
Expand Down

0 comments on commit 72c60f1

Please sign in to comment.