diff --git a/packages/opentelemetry-core/src/utils/environment.ts b/packages/opentelemetry-core/src/utils/environment.ts index fe5e45c6e9..0e5727966e 100644 --- a/packages/opentelemetry-core/src/utils/environment.ts +++ b/packages/opentelemetry-core/src/utils/environment.ts @@ -28,6 +28,8 @@ export interface ENVIRONMENT { OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT?: number; OTEL_SPAN_EVENT_COUNT_LIMIT?: number; OTEL_SPAN_LINK_COUNT_LIMIT?: number; + OTEL_BSP_MAX_BATCH_SIZE?: number; + OTEL_BSP_SCHEDULE_DELAY_MILLIS?: number; } const ENVIRONMENT_NUMBERS: Partial[] = [ @@ -35,6 +37,8 @@ const ENVIRONMENT_NUMBERS: Partial[] = [ 'OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT', 'OTEL_SPAN_EVENT_COUNT_LIMIT', 'OTEL_SPAN_LINK_COUNT_LIMIT', + 'OTEL_BSP_MAX_BATCH_SIZE', + 'OTEL_BSP_SCHEDULE_DELAY_MILLIS', ]; /** @@ -47,6 +51,8 @@ export const DEFAULT_ENVIRONMENT: Required = { OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 1000, OTEL_SPAN_EVENT_COUNT_LIMIT: 1000, OTEL_SPAN_LINK_COUNT_LIMIT: 1000, + OTEL_BSP_MAX_BATCH_SIZE: 512, + OTEL_BSP_SCHEDULE_DELAY_MILLIS: 5000, }; /** diff --git a/packages/opentelemetry-core/test/utils/environment.test.ts b/packages/opentelemetry-core/test/utils/environment.test.ts index 60fa403425..50d4bdb588 100644 --- a/packages/opentelemetry-core/test/utils/environment.test.ts +++ b/packages/opentelemetry-core/test/utils/environment.test.ts @@ -80,6 +80,8 @@ describe('environment', () => { OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: 10, OTEL_SPAN_EVENT_COUNT_LIMIT: 20, OTEL_SPAN_LINK_COUNT_LIMIT: 30, + OTEL_BSP_MAX_BATCH_SIZE: 40, + OTEL_BSP_SCHEDULE_DELAY_MILLIS: 50, }); const env = getEnv(); assert.strictEqual(env.OTEL_NO_PATCH_MODULES, 'a,b,c'); @@ -88,6 +90,8 @@ describe('environment', () => { assert.strictEqual(env.OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT, 10); assert.strictEqual(env.OTEL_SPAN_EVENT_COUNT_LIMIT, 20); assert.strictEqual(env.OTEL_SPAN_LINK_COUNT_LIMIT, 30); + assert.strictEqual(env.OTEL_BSP_MAX_BATCH_SIZE, 40); + assert.strictEqual(env.OTEL_BSP_SCHEDULE_DELAY_MILLIS, 50); }); it('should match invalid values to closest valid equivalent', () => { diff --git a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts b/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts index b2e308b442..3b42d1f072 100644 --- a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts +++ b/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts @@ -19,6 +19,7 @@ import { ExportResultCode, globalErrorHandler, unrefTimer, + getEnv, } from '@opentelemetry/core'; import { Span } from '../Span'; import { SpanProcessor } from '../SpanProcessor'; @@ -26,9 +27,6 @@ import { BufferConfig } from '../types'; import { ReadableSpan } from './ReadableSpan'; import { SpanExporter } from './SpanExporter'; -const DEFAULT_BUFFER_SIZE = 100; -const DEFAULT_BUFFER_TIMEOUT_MS = 20_000; - /** * Implementation of the {@link SpanProcessor} that batches spans exported by * the SDK then pushes them to the exporter pipeline. @@ -43,12 +41,15 @@ export class BatchSpanProcessor implements SpanProcessor { private _shuttingDownPromise: Promise = Promise.resolve(); constructor(private readonly _exporter: SpanExporter, config?: BufferConfig) { + const env = getEnv(); this._bufferSize = - config && config.bufferSize ? config.bufferSize : DEFAULT_BUFFER_SIZE; + config && config.bufferSize + ? config.bufferSize + : env.OTEL_BSP_MAX_BATCH_SIZE; this._bufferTimeout = config && typeof config.bufferTimeout === 'number' ? config.bufferTimeout - : DEFAULT_BUFFER_TIMEOUT_MS; + : env.OTEL_BSP_SCHEDULE_DELAY_MILLIS; } forceFlush(): Promise { diff --git a/packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts b/packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts index 10ca9e0800..6f2e2c5203 100644 --- a/packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts +++ b/packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts @@ -74,6 +74,32 @@ describe('BatchSpanProcessor', () => { assert.ok(processor instanceof BatchSpanProcessor); processor.shutdown(); }); + + it('should read defaults from environment', () => { + const bspConfig = { + OTEL_BSP_MAX_BATCH_SIZE: 256, + OTEL_BSP_SCHEDULE_DELAY_MILLIS: 2500, + }; + + let env: Record; + if (typeof process === 'undefined') { + env = (window as unknown) as Record; + } else { + env = process.env as Record; + } + + Object.entries(bspConfig).forEach(([k, v]) => { + env[k] = v; + }); + + const processor = new BatchSpanProcessor(exporter); + assert.ok(processor instanceof BatchSpanProcessor); + assert.strictEqual(processor['_bufferSize'], 256); + assert.strictEqual(processor['_bufferTimeout'], 2500); + processor.shutdown(); + + Object.keys(bspConfig).forEach(k => delete env[k]); + }); }); describe('.onStart/.onEnd/.shutdown', () => {