diff --git a/packages/opentelemetry-tracing/karma.conf.js b/packages/opentelemetry-tracing/karma.conf.js index 3019564a15b..455b1437c87 100644 --- a/packages/opentelemetry-tracing/karma.conf.js +++ b/packages/opentelemetry-tracing/karma.conf.js @@ -19,6 +19,8 @@ const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { - webpack: karmaWebpackConfig + webpack: karmaWebpackConfig, + files: ['test/browser/index-webpack.ts'], + preprocessors: { 'test/browser/index-webpack.ts': ['webpack'] } })) }; diff --git a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts b/packages/opentelemetry-tracing/src/export/BatchSpanProcessorBase.ts similarity index 94% rename from packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts rename to packages/opentelemetry-tracing/src/export/BatchSpanProcessorBase.ts index 8aaa15b13bc..c16f64e5704 100644 --- a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts +++ b/packages/opentelemetry-tracing/src/export/BatchSpanProcessorBase.ts @@ -31,7 +31,7 @@ import { SpanExporter } from './SpanExporter'; * Implementation of the {@link SpanProcessor} that batches spans exported by * the SDK then pushes them to the exporter pipeline. */ -export class BatchSpanProcessor implements SpanProcessor { +export abstract class BatchSpanProcessorBase implements SpanProcessor { private readonly _maxExportBatchSize: number; private readonly _maxQueueSize: number; private readonly _scheduledDelayMillis: number; @@ -42,7 +42,7 @@ export class BatchSpanProcessor implements SpanProcessor { private _isShutdown = false; private _shuttingDownPromise: Promise = Promise.resolve(); - constructor(private readonly _exporter: SpanExporter, config?: BufferConfig) { + constructor(private readonly _exporter: SpanExporter, config?: T) { const env = getEnv(); this._maxExportBatchSize = typeof config?.maxExportBatchSize === 'number' @@ -50,15 +50,15 @@ export class BatchSpanProcessor implements SpanProcessor { : env.OTEL_BSP_MAX_EXPORT_BATCH_SIZE; this._maxQueueSize = typeof config?.maxQueueSize === 'number' - ? config?.maxQueueSize + ? config.maxQueueSize : env.OTEL_BSP_MAX_QUEUE_SIZE; this._scheduledDelayMillis = typeof config?.scheduledDelayMillis === 'number' - ? config?.scheduledDelayMillis + ? config.scheduledDelayMillis : env.OTEL_BSP_SCHEDULE_DELAY; this._exportTimeoutMillis = typeof config?.exportTimeoutMillis === 'number' - ? config?.exportTimeoutMillis + ? config.exportTimeoutMillis : env.OTEL_BSP_EXPORT_TIMEOUT; } @@ -86,6 +86,9 @@ export class BatchSpanProcessor implements SpanProcessor { this._isShutdown = true; this._shuttingDownPromise = new Promise((resolve, reject) => { Promise.resolve() + .then(() => { + return this.onShutdown(); + }) .then(() => { return this._flushAll(); }) @@ -189,4 +192,6 @@ export class BatchSpanProcessor implements SpanProcessor { this._timer = undefined; } } + + protected abstract onShutdown(): void; } diff --git a/packages/opentelemetry-tracing/src/index.ts b/packages/opentelemetry-tracing/src/index.ts index 720241a3107..e1c31f2deda 100644 --- a/packages/opentelemetry-tracing/src/index.ts +++ b/packages/opentelemetry-tracing/src/index.ts @@ -16,8 +16,8 @@ export * from './Tracer'; export * from './BasicTracerProvider'; +export * from './platform'; export * from './export/ConsoleSpanExporter'; -export * from './export/BatchSpanProcessor'; export * from './export/InMemorySpanExporter'; export * from './export/ReadableSpan'; export * from './export/SimpleSpanProcessor'; diff --git a/packages/opentelemetry-tracing/src/platform/browser/export/BatchSpanProcessor.ts b/packages/opentelemetry-tracing/src/platform/browser/export/BatchSpanProcessor.ts new file mode 100644 index 00000000000..07156e7b90d --- /dev/null +++ b/packages/opentelemetry-tracing/src/platform/browser/export/BatchSpanProcessor.ts @@ -0,0 +1,55 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BatchSpanProcessorBase } from '../../../export/BatchSpanProcessorBase'; +import { SpanExporter } from '../../../export/SpanExporter'; +import { BatchSpanProcessorBrowserConfig } from '../../../types'; + +export class BatchSpanProcessor extends BatchSpanProcessorBase { + private _visibilityChangeListener?: () => void + private _pageHideListener?: () => void + + constructor(_exporter: SpanExporter, config?: BatchSpanProcessorBrowserConfig) { + super(_exporter, config) + this.onInit(config) + } + + private onInit(config?: BatchSpanProcessorBrowserConfig): void { + if (config?.disableAutoFlushOnDocumentHide !== true && document != null) { + this._visibilityChangeListener = () => { + if (document.visibilityState === 'hidden') { + void this.forceFlush(); + } + } + this._pageHideListener = () => { + void this.forceFlush() + } + document.addEventListener('visibilitychange', this._visibilityChangeListener); + + // use 'pagehide' event as a fallback for Safari; see https://bugs.webkit.org/show_bug.cgi?id=116769 + document.addEventListener('pagehide', this._pageHideListener); + } + } + + protected onShutdown(): void { + if (this._visibilityChangeListener) { + document.removeEventListener('visibilitychange', this._visibilityChangeListener); + } + if (this._pageHideListener) { + document.removeEventListener('pagehide', this._pageHideListener); + } + } +} diff --git a/packages/opentelemetry-tracing/src/platform/browser/index.ts b/packages/opentelemetry-tracing/src/platform/browser/index.ts new file mode 100644 index 00000000000..7992546b9d7 --- /dev/null +++ b/packages/opentelemetry-tracing/src/platform/browser/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './export/BatchSpanProcessor'; diff --git a/packages/opentelemetry-tracing/src/platform/index.ts b/packages/opentelemetry-tracing/src/platform/index.ts new file mode 100644 index 00000000000..cdaf8858ce5 --- /dev/null +++ b/packages/opentelemetry-tracing/src/platform/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './node'; diff --git a/packages/opentelemetry-tracing/src/platform/node/export/BatchSpanProcessor.ts b/packages/opentelemetry-tracing/src/platform/node/export/BatchSpanProcessor.ts new file mode 100644 index 00000000000..8c860cfdde9 --- /dev/null +++ b/packages/opentelemetry-tracing/src/platform/node/export/BatchSpanProcessor.ts @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BatchSpanProcessorBase } from '../../../export/BatchSpanProcessorBase'; +import { BufferConfig } from '../../../types'; + +export class BatchSpanProcessor extends BatchSpanProcessorBase { + protected onShutdown(): void {} +} diff --git a/packages/opentelemetry-tracing/src/platform/node/index.ts b/packages/opentelemetry-tracing/src/platform/node/index.ts new file mode 100644 index 00000000000..7992546b9d7 --- /dev/null +++ b/packages/opentelemetry-tracing/src/platform/node/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export * from './export/BatchSpanProcessor'; diff --git a/packages/opentelemetry-tracing/src/types.ts b/packages/opentelemetry-tracing/src/types.ts index 31561c19154..75a51e93390 100644 --- a/packages/opentelemetry-tracing/src/types.ts +++ b/packages/opentelemetry-tracing/src/types.ts @@ -83,3 +83,10 @@ export interface BufferConfig { * The default value is 2048. */ maxQueueSize?: number; } + +/** Interface configuration for BatchSpanProcessor on browser */ +export interface BatchSpanProcessorBrowserConfig extends BufferConfig { + /** Disable flush when a user navigates to a new page, closes the tab or the browser, or, + * on mobile, switches to a different app. Auto flush is enabled by default. */ + disableAutoFlushOnDocumentHide?: boolean; +} diff --git a/packages/opentelemetry-tracing/test/browser/export/BatchSpanProcessor.test.ts b/packages/opentelemetry-tracing/test/browser/export/BatchSpanProcessor.test.ts new file mode 100644 index 00000000000..9c422f2245a --- /dev/null +++ b/packages/opentelemetry-tracing/test/browser/export/BatchSpanProcessor.test.ts @@ -0,0 +1,102 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import { SpanExporter } from '../../../src'; +import { BatchSpanProcessor } from '../../../src/platform/browser/export/BatchSpanProcessor'; +import { TestTracingSpanExporter } from '../../common/export/TestTracingSpanExporter'; + +describe('BatchSpanProcessor - web', () => { + let visibilityState: VisibilityState = 'visible'; + let exporter: SpanExporter + let processor: BatchSpanProcessor; + let forceFlushSpy: sinon.SinonStub; + let visibilityChangeEvent: Event; + let pageHideEvent: Event + + beforeEach(() => { + sinon.replaceGetter(document, 'visibilityState', () => visibilityState); + visibilityState = 'visible'; + exporter = new TestTracingSpanExporter(); + processor = new BatchSpanProcessor(exporter, {}); + forceFlushSpy = sinon.stub(processor, 'forceFlush'); + visibilityChangeEvent = new Event('visibilitychange'); + pageHideEvent = new Event('pagehide'); + }); + + afterEach(async () => { + sinon.restore(); + }); + + describe('when document becomes hidden', () => { + const testDocumentHide = (hideDocument: () => void) => { + it('should force flush spans', () => { + assert.strictEqual(forceFlushSpy.callCount, 0); + hideDocument() + assert.strictEqual(forceFlushSpy.callCount, 1); + }); + + describe('AND shutdown has been called', () => { + it('should NOT force flush spans', async () => { + assert.strictEqual(forceFlushSpy.callCount, 0); + await processor.shutdown(); + hideDocument() + assert.strictEqual(forceFlushSpy.callCount, 0); + }); + }) + + describe('AND disableAutoFlushOnDocumentHide configuration option', () => { + it('set to false should force flush spans', () => { + processor = new BatchSpanProcessor(exporter, { disableAutoFlushOnDocumentHide: false }); + forceFlushSpy = sinon.stub(processor, 'forceFlush'); + assert.strictEqual(forceFlushSpy.callCount, 0); + hideDocument() + assert.strictEqual(forceFlushSpy.callCount, 1); + }) + + it('set to true should NOT force flush spans', () => { + processor = new BatchSpanProcessor(exporter, { disableAutoFlushOnDocumentHide: true }); + forceFlushSpy = sinon.stub(processor, 'forceFlush'); + assert.strictEqual(forceFlushSpy.callCount, 0); + hideDocument() + assert.strictEqual(forceFlushSpy.callCount, 0); + }) + }) + } + + describe('by the visibilitychange event', () => { + testDocumentHide(() => { + visibilityState = 'hidden'; + document.dispatchEvent(visibilityChangeEvent); + }) + }) + + describe('by the pagehide event', () => { + testDocumentHide(() => { + document.dispatchEvent(pageHideEvent); + }) + }) + }) + + describe('when document becomes visible', () => { + it('should NOT force flush spans', () => { + assert.strictEqual(forceFlushSpy.callCount, 0); + document.dispatchEvent(visibilityChangeEvent); + assert.strictEqual(forceFlushSpy.callCount, 0); + }); + }) +}); diff --git a/packages/opentelemetry-tracing/test/index-webpack.ts b/packages/opentelemetry-tracing/test/browser/index-webpack.ts similarity index 79% rename from packages/opentelemetry-tracing/test/index-webpack.ts rename to packages/opentelemetry-tracing/test/browser/index-webpack.ts index 061a48ccfa7..99100a0f6ee 100644 --- a/packages/opentelemetry-tracing/test/index-webpack.ts +++ b/packages/opentelemetry-tracing/test/browser/index-webpack.ts @@ -13,8 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -const testsContext = require.context('.', true, /test$/); +const testsContext = require.context('../browser', true, /test$/); testsContext.keys().forEach(testsContext); +const testsContextCommon = require.context('../common', true, /test$/); +testsContextCommon.keys().forEach(testsContextCommon); + const srcContext = require.context('.', true, /src$/); srcContext.keys().forEach(srcContext); diff --git a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts b/packages/opentelemetry-tracing/test/common/BasicTracerProvider.test.ts similarity index 99% rename from packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts rename to packages/opentelemetry-tracing/test/common/BasicTracerProvider.test.ts index bfef16e093b..559e5b73816 100644 --- a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-tracing/test/common/BasicTracerProvider.test.ts @@ -31,7 +31,7 @@ import { import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import * as sinon from 'sinon'; -import { BasicTracerProvider, Span } from '../src'; +import { BasicTracerProvider, Span } from '../../src'; describe('BasicTracerProvider', () => { let removeEvent: Function | undefined; diff --git a/packages/opentelemetry-tracing/test/MultiSpanProcessor.test.ts b/packages/opentelemetry-tracing/test/common/MultiSpanProcessor.test.ts similarity index 98% rename from packages/opentelemetry-tracing/test/MultiSpanProcessor.test.ts rename to packages/opentelemetry-tracing/test/common/MultiSpanProcessor.test.ts index 68723d21149..5c263caa4fc 100644 --- a/packages/opentelemetry-tracing/test/MultiSpanProcessor.test.ts +++ b/packages/opentelemetry-tracing/test/common/MultiSpanProcessor.test.ts @@ -22,12 +22,12 @@ import { SimpleSpanProcessor, Span, SpanProcessor, -} from '../src'; +} from '../../src'; import { setGlobalErrorHandler, loggingErrorHandler, } from '@opentelemetry/core'; -import { MultiSpanProcessor } from '../src/MultiSpanProcessor'; +import { MultiSpanProcessor } from '../../src/MultiSpanProcessor'; class TestProcessor implements SpanProcessor { spans: Span[] = []; diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/common/Span.test.ts similarity index 99% rename from packages/opentelemetry-tracing/test/Span.test.ts rename to packages/opentelemetry-tracing/test/common/Span.test.ts index 5597223e026..ac5b098fdd0 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/common/Span.test.ts @@ -31,7 +31,7 @@ import { } from '@opentelemetry/core'; import { ExceptionAttribute } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; -import { BasicTracerProvider, Span, SpanProcessor } from '../src'; +import { BasicTracerProvider, Span, SpanProcessor } from '../../src'; const performanceTimeOrigin = hrTime(); diff --git a/packages/opentelemetry-tracing/test/Tracer.test.ts b/packages/opentelemetry-tracing/test/common/Tracer.test.ts similarity index 98% rename from packages/opentelemetry-tracing/test/Tracer.test.ts rename to packages/opentelemetry-tracing/test/common/Tracer.test.ts index 3da1eadb62c..61776d48b46 100644 --- a/packages/opentelemetry-tracing/test/Tracer.test.ts +++ b/packages/opentelemetry-tracing/test/common/Tracer.test.ts @@ -22,12 +22,13 @@ import { ROOT_CONTEXT, suppressInstrumentation, } from '@opentelemetry/api'; -import { BasicTracerProvider, Tracer, Span } from '../src'; import { InstrumentationLibrary, AlwaysOnSampler, AlwaysOffSampler, } from '@opentelemetry/core'; +import * as assert from 'assert'; +import { BasicTracerProvider, Span, Tracer } from '../../src'; describe('Tracer', () => { const tracerProvider = new BasicTracerProvider(); diff --git a/packages/opentelemetry-tracing/test/common/config.test.ts b/packages/opentelemetry-tracing/test/common/config.test.ts new file mode 100644 index 00000000000..ea27f21e9d9 --- /dev/null +++ b/packages/opentelemetry-tracing/test/common/config.test.ts @@ -0,0 +1,102 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + AlwaysOffSampler, + AlwaysOnSampler, + ParentBasedSampler, + TraceIdRatioBasedSampler, +} from '@opentelemetry/core'; +import * as assert from 'assert'; +import { buildSamplerFromEnv } from '../../src/config'; + +describe('config', () => { + const envSource = (typeof window !== 'undefined' + ? window + : process.env) as any; + + describe('buildSamplerFromEnv()', () => { + afterEach(() => { + delete envSource.OTEL_TRACES_SAMPLER; + delete envSource.OTEL_TRACES_SAMPLER_ARG; + }); + + it('should handle always_on case', () => { + envSource.OTEL_TRACES_SAMPLER = 'always_on'; + assert.ok(buildSamplerFromEnv() instanceof AlwaysOnSampler); + assert.strictEqual(buildSamplerFromEnv().toString(), 'AlwaysOnSampler'); + }); + + it('should handle always_off case', () => { + envSource.OTEL_TRACES_SAMPLER = 'always_off'; + assert.ok(buildSamplerFromEnv() instanceof AlwaysOffSampler); + assert.strictEqual(buildSamplerFromEnv().toString(), 'AlwaysOffSampler'); + }); + + it('should handle traceidratio case', () => { + envSource.OTEL_TRACES_SAMPLER = 'traceidratio'; + envSource.OTEL_TRACES_SAMPLER_ARG = '0.1'; + assert.ok(buildSamplerFromEnv() instanceof TraceIdRatioBasedSampler); + assert.strictEqual( + buildSamplerFromEnv().toString(), + 'TraceIdRatioBased{0.1}' + ); + }); + + it('should handle parentbased_always_on case', () => { + envSource.OTEL_TRACES_SAMPLER = 'parentbased_always_on'; + assert.ok(buildSamplerFromEnv() instanceof ParentBasedSampler); + assert.strictEqual( + buildSamplerFromEnv().toString(), + 'ParentBased{root=AlwaysOnSampler, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' + ); + }); + + it('should handle parentbased_always_off case', () => { + envSource.OTEL_TRACES_SAMPLER = 'parentbased_always_off'; + assert.ok(buildSamplerFromEnv() instanceof ParentBasedSampler); + assert.strictEqual( + buildSamplerFromEnv().toString(), + 'ParentBased{root=AlwaysOffSampler, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' + ); + }); + + it('should handle parentbased_traceidratio case', () => { + envSource.OTEL_TRACES_SAMPLER = 'parentbased_traceidratio'; + envSource.OTEL_TRACES_SAMPLER_ARG = '0.2'; + assert.ok(buildSamplerFromEnv() instanceof ParentBasedSampler); + assert.strictEqual( + buildSamplerFromEnv().toString(), + 'ParentBased{root=TraceIdRatioBased{0.2}, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' + ); + }); + + it('should handle default case with probability 1', () => { + assert.ok(buildSamplerFromEnv() instanceof ParentBasedSampler); + assert.strictEqual( + buildSamplerFromEnv().toString(), + 'ParentBased{root=AlwaysOnSampler, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' + ); + }); + + it('should handle default case with probability lower than 1', () => { + assert.ok(buildSamplerFromEnv() instanceof ParentBasedSampler); + assert.strictEqual( + buildSamplerFromEnv().toString(), + 'ParentBased{root=AlwaysOnSampler, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' + ); + }); + }); +}); diff --git a/packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts b/packages/opentelemetry-tracing/test/common/export/BatchSpanProcessorBase.test.ts similarity index 95% rename from packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts rename to packages/opentelemetry-tracing/test/common/export/BatchSpanProcessorBase.test.ts index 4f5d734713c..7015d035951 100644 --- a/packages/opentelemetry-tracing/test/export/BatchSpanProcessor.test.ts +++ b/packages/opentelemetry-tracing/test/common/export/BatchSpanProcessorBase.test.ts @@ -23,15 +23,11 @@ import { } from '@opentelemetry/core'; import * as assert from 'assert'; import * as sinon from 'sinon'; -import { - BasicTracerProvider, - BatchSpanProcessor, - InMemorySpanExporter, - Span, -} from '../../src'; +import { BasicTracerProvider, BufferConfig, InMemorySpanExporter, Span } from '../../../src'; import { context } from '@opentelemetry/api'; import { TestTracingSpanExporter } from './TestTracingSpanExporter'; import { TestStackContextManager } from './TestStackContextManager'; +import { BatchSpanProcessorBase } from '../../../src/export/BatchSpanProcessorBase'; function createSampledSpan(spanName: string): Span { const tracer = new BasicTracerProvider({ @@ -42,16 +38,23 @@ function createSampledSpan(spanName: string): Span { return span as Span; } -describe('BatchSpanProcessor', () => { +class BatchSpanProcessor extends BatchSpanProcessorBase { + onInit() {} + onShutdown() {} +} + +describe('BatchSpanProcessorBase', () => { const name = 'span-name'; const defaultBufferConfig = { maxExportBatchSize: 5, scheduledDelayMillis: 2500, }; let exporter: InMemorySpanExporter; + beforeEach(() => { exporter = new InMemorySpanExporter(); }); + afterEach(() => { exporter.reset(); sinon.restore(); @@ -104,6 +107,14 @@ describe('BatchSpanProcessor', () => { }); describe('.onStart/.onEnd/.shutdown', () => { + it('should call onShutdown', async () => { + const processor = new BatchSpanProcessor(exporter, defaultBufferConfig); + const onShutdownSpy = sinon.stub(processor, 'onShutdown'); + assert.strictEqual(onShutdownSpy.callCount, 0); + await processor.shutdown(); + assert.strictEqual(onShutdownSpy.callCount, 1); + }); + it('should do nothing after processor is shutdown', async () => { const processor = new BatchSpanProcessor(exporter, defaultBufferConfig); const spy: sinon.SinonSpy = sinon.spy(exporter, 'export') as any; diff --git a/packages/opentelemetry-tracing/test/export/ConsoleSpanExporter.test.ts b/packages/opentelemetry-tracing/test/common/export/ConsoleSpanExporter.test.ts similarity index 99% rename from packages/opentelemetry-tracing/test/export/ConsoleSpanExporter.test.ts rename to packages/opentelemetry-tracing/test/common/export/ConsoleSpanExporter.test.ts index 7efac48ea6c..1dac15c0c02 100644 --- a/packages/opentelemetry-tracing/test/export/ConsoleSpanExporter.test.ts +++ b/packages/opentelemetry-tracing/test/common/export/ConsoleSpanExporter.test.ts @@ -20,7 +20,7 @@ import { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor, -} from '../../src'; +} from '../../../src'; /* eslint-disable no-console */ describe('ConsoleSpanExporter', () => { diff --git a/packages/opentelemetry-tracing/test/export/InMemorySpanExporter.test.ts b/packages/opentelemetry-tracing/test/common/export/InMemorySpanExporter.test.ts similarity index 99% rename from packages/opentelemetry-tracing/test/export/InMemorySpanExporter.test.ts rename to packages/opentelemetry-tracing/test/common/export/InMemorySpanExporter.test.ts index 3f8c916768e..9281b9f7a8b 100644 --- a/packages/opentelemetry-tracing/test/export/InMemorySpanExporter.test.ts +++ b/packages/opentelemetry-tracing/test/common/export/InMemorySpanExporter.test.ts @@ -19,7 +19,7 @@ import { InMemorySpanExporter, SimpleSpanProcessor, BasicTracerProvider, -} from '../../src'; +} from '../../../src'; import { context, setSpan } from '@opentelemetry/api'; import { ExportResult, ExportResultCode } from '@opentelemetry/core'; diff --git a/packages/opentelemetry-tracing/test/export/SimpleSpanProcessor.test.ts b/packages/opentelemetry-tracing/test/common/export/SimpleSpanProcessor.test.ts similarity index 99% rename from packages/opentelemetry-tracing/test/export/SimpleSpanProcessor.test.ts rename to packages/opentelemetry-tracing/test/common/export/SimpleSpanProcessor.test.ts index b3a5f1ecd52..df5fa66f817 100644 --- a/packages/opentelemetry-tracing/test/export/SimpleSpanProcessor.test.ts +++ b/packages/opentelemetry-tracing/test/common/export/SimpleSpanProcessor.test.ts @@ -33,7 +33,7 @@ import { InMemorySpanExporter, SimpleSpanProcessor, Span, -} from '../../src'; +} from '../../../src'; import { TestStackContextManager } from './TestStackContextManager'; import { TestTracingSpanExporter } from './TestTracingSpanExporter'; diff --git a/packages/opentelemetry-tracing/test/export/TestStackContextManager.ts b/packages/opentelemetry-tracing/test/common/export/TestStackContextManager.ts similarity index 100% rename from packages/opentelemetry-tracing/test/export/TestStackContextManager.ts rename to packages/opentelemetry-tracing/test/common/export/TestStackContextManager.ts diff --git a/packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts b/packages/opentelemetry-tracing/test/common/export/TestTracingSpanExporter.ts similarity index 99% rename from packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts rename to packages/opentelemetry-tracing/test/common/export/TestTracingSpanExporter.ts index 499faf91ebd..22a9bf1a429 100644 --- a/packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts +++ b/packages/opentelemetry-tracing/test/common/export/TestTracingSpanExporter.ts @@ -20,7 +20,7 @@ import { ReadableSpan, Tracer, SpanProcessor, -} from '../../src'; +} from '../../../src'; import { ExportResult, AlwaysOnSampler } from '@opentelemetry/core'; /**