Skip to content

Commit

Permalink
feat(node-sdk): add spanProcessors option
Browse files Browse the repository at this point in the history
Since span processors can be chained together, it makes sense to expose
this option as an array of span processors. This change also deprecates
the `spanProcessor` option in favor of `spanProcessors`.
  • Loading branch information
naseemkullah committed Jan 31, 2024
1 parent 2df6310 commit 921c71e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
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
14 changes: 9 additions & 5 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 @@ -147,9 +147,11 @@ export class NodeSDK {
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 +194,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 +336,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
19 changes: 16 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 a 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,16 @@ describe('Node SDK', () => {
const apiTracerProvider =
trace.getTracerProvider() as ProxyTracerProvider;
assert.ok(apiTracerProvider.getDelegate() instanceof NodeTracerProvider);


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

assert(sdk['_tracerProvider'] instanceof TracerProviderWithEnvExporters);
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 921c71e

Please sign in to comment.