From 23db7f0ba383a3043964eb03be11c09df3f7453a Mon Sep 17 00:00:00 2001 From: Krystian Kruk <58699793+kkruk-sumo@users.noreply.github.com> Date: Wed, 16 Jun 2021 14:52:54 +0200 Subject: [PATCH 1/2] feat(tracing): auto flush BatchSpanProcessor on browser (#2243) --- packages/opentelemetry-tracing/karma.conf.js | 4 +- packages/opentelemetry-tracing/package.json | 2 +- .../src/BasicTracerProvider.ts | 2 +- ...Processor.ts => BatchSpanProcessorBase.ts} | 15 ++- packages/opentelemetry-tracing/src/index.ts | 2 +- .../browser/export/BatchSpanProcessor.ts | 55 ++++++++++ .../src/platform/browser/index.ts | 17 +++ .../src/platform/index.ts | 17 +++ .../node/export/BatchSpanProcessor.ts | 22 ++++ .../src/platform/node/index.ts | 17 +++ packages/opentelemetry-tracing/src/types.ts | 7 ++ .../browser/export/BatchSpanProcessor.test.ts | 102 ++++++++++++++++++ .../test/{ => browser}/index-webpack.ts | 5 +- .../{ => common}/BasicTracerProvider.test.ts | 2 +- .../{ => common}/MultiSpanProcessor.test.ts | 4 +- .../test/{ => common}/Span.test.ts | 2 +- .../test/{ => common}/Tracer.test.ts | 2 +- .../test/{ => common}/config.test.ts | 2 +- .../export/BatchSpanProcessorBase.test.ts} | 25 +++-- .../export/ConsoleSpanExporter.test.ts | 2 +- .../export/InMemorySpanExporter.test.ts | 2 +- .../export/SimpleSpanProcessor.test.ts | 2 +- .../export/TestStackContextManager.ts | 0 .../export/TestTracingSpanExporter.ts | 2 +- 24 files changed, 285 insertions(+), 27 deletions(-) rename packages/opentelemetry-tracing/src/export/{BatchSpanProcessor.ts => BatchSpanProcessorBase.ts} (94%) create mode 100644 packages/opentelemetry-tracing/src/platform/browser/export/BatchSpanProcessor.ts create mode 100644 packages/opentelemetry-tracing/src/platform/browser/index.ts create mode 100644 packages/opentelemetry-tracing/src/platform/index.ts create mode 100644 packages/opentelemetry-tracing/src/platform/node/export/BatchSpanProcessor.ts create mode 100644 packages/opentelemetry-tracing/src/platform/node/index.ts create mode 100644 packages/opentelemetry-tracing/test/browser/export/BatchSpanProcessor.test.ts rename packages/opentelemetry-tracing/test/{ => browser}/index-webpack.ts (79%) rename packages/opentelemetry-tracing/test/{ => common}/BasicTracerProvider.test.ts (99%) rename packages/opentelemetry-tracing/test/{ => common}/MultiSpanProcessor.test.ts (98%) rename packages/opentelemetry-tracing/test/{ => common}/Span.test.ts (99%) rename packages/opentelemetry-tracing/test/{ => common}/Tracer.test.ts (99%) rename packages/opentelemetry-tracing/test/{ => common}/config.test.ts (98%) rename packages/opentelemetry-tracing/test/{export/BatchSpanProcessor.test.ts => common/export/BatchSpanProcessorBase.test.ts} (95%) rename packages/opentelemetry-tracing/test/{ => common}/export/ConsoleSpanExporter.test.ts (99%) rename packages/opentelemetry-tracing/test/{ => common}/export/InMemorySpanExporter.test.ts (99%) rename packages/opentelemetry-tracing/test/{ => common}/export/SimpleSpanProcessor.test.ts (99%) rename packages/opentelemetry-tracing/test/{ => common}/export/TestStackContextManager.ts (100%) rename packages/opentelemetry-tracing/test/{ => common}/export/TestTracingSpanExporter.ts (99%) diff --git a/packages/opentelemetry-tracing/karma.conf.js b/packages/opentelemetry-tracing/karma.conf.js index 3019564a15..455b1437c8 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/package.json b/packages/opentelemetry-tracing/package.json index fb3f4657e8..a6c81483d4 100644 --- a/packages/opentelemetry-tracing/package.json +++ b/packages/opentelemetry-tracing/package.json @@ -14,7 +14,7 @@ "scripts": { "compile": "tsc --build tsconfig.json tsconfig.esm.json", "clean": "tsc --build --clean tsconfig.json tsconfig.esm.json", - "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/index-webpack.ts'", + "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/browser/**/*.ts'", "test:browser": "nyc karma start --single-run", "tdd": "npm run tdd:node", "tdd:node": "npm run test -- --watch-extensions ts --watch", diff --git a/packages/opentelemetry-tracing/src/BasicTracerProvider.ts b/packages/opentelemetry-tracing/src/BasicTracerProvider.ts index d8ca406c1c..d02d92bccb 100644 --- a/packages/opentelemetry-tracing/src/BasicTracerProvider.ts +++ b/packages/opentelemetry-tracing/src/BasicTracerProvider.ts @@ -37,7 +37,7 @@ import { SDKRegistrationConfig, TracerConfig } from './types'; // eslint-disable-next-line @typescript-eslint/no-var-requires const merge = require('lodash.merge'); import { SpanExporter } from './export/SpanExporter'; -import { BatchSpanProcessor } from './export/BatchSpanProcessor'; +import { BatchSpanProcessor } from './platform'; export type PROPAGATOR_FACTORY = () => TextMapPropagator; export type EXPORTER_FACTORY = () => SpanExporter; 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 92b596440f..03efc430bd 100644 --- a/packages/opentelemetry-tracing/src/export/BatchSpanProcessor.ts +++ b/packages/opentelemetry-tracing/src/export/BatchSpanProcessorBase.ts @@ -32,7 +32,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; @@ -43,7 +43,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' @@ -51,15 +51,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; } @@ -87,6 +87,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(); }) @@ -190,4 +193,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 298657290f..3fb4bc2248 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 0000000000..07156e7b90 --- /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 0000000000..7992546b9d --- /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 0000000000..cdaf8858ce --- /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 0000000000..8c860cfdde --- /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 0000000000..7992546b9d --- /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 be3a595490..d8329aa567 100644 --- a/packages/opentelemetry-tracing/src/types.ts +++ b/packages/opentelemetry-tracing/src/types.ts @@ -89,3 +89,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 0000000000..9c422f2245 --- /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 061a48ccfa..99100a0f6e 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 261d81b6e9..f824049a84 100644 --- a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-tracing/test/common/BasicTracerProvider.test.ts @@ -43,7 +43,7 @@ import { InMemorySpanExporter, SpanExporter, BatchSpanProcessor, -} from '../src'; +} 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 68723d2114..5c263caa4f 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 ce65b6c8d0..bd2072bf00 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/common/Span.test.ts @@ -30,7 +30,7 @@ import { } from '@opentelemetry/core'; import { SemanticAttributes } 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 99% rename from packages/opentelemetry-tracing/test/Tracer.test.ts rename to packages/opentelemetry-tracing/test/common/Tracer.test.ts index aa42daf0e8..30180ef468 100644 --- a/packages/opentelemetry-tracing/test/Tracer.test.ts +++ b/packages/opentelemetry-tracing/test/common/Tracer.test.ts @@ -32,7 +32,7 @@ import { suppressTracing } from '@opentelemetry/core'; import * as assert from 'assert'; -import { BasicTracerProvider, Span, Tracer } from '../src'; +import { BasicTracerProvider, Span, Tracer } from '../../src'; import { TestStackContextManager } from './export/TestStackContextManager'; import * as sinon from 'sinon'; diff --git a/packages/opentelemetry-tracing/test/config.test.ts b/packages/opentelemetry-tracing/test/common/config.test.ts similarity index 98% rename from packages/opentelemetry-tracing/test/config.test.ts rename to packages/opentelemetry-tracing/test/common/config.test.ts index 7f81183c13..ea27f21e9d 100644 --- a/packages/opentelemetry-tracing/test/config.test.ts +++ b/packages/opentelemetry-tracing/test/common/config.test.ts @@ -20,7 +20,7 @@ import { TraceIdRatioBasedSampler, } from '@opentelemetry/core'; import * as assert from 'assert'; -import { buildSamplerFromEnv } from '../src/config'; +import { buildSamplerFromEnv } from '../../src/config'; describe('config', () => { const envSource = (typeof window !== 'undefined' 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 4f5d734713..7015d03595 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 674efb53d6..8e5e40adfb 100644 --- a/packages/opentelemetry-tracing/test/export/ConsoleSpanExporter.test.ts +++ b/packages/opentelemetry-tracing/test/common/export/ConsoleSpanExporter.test.ts @@ -21,7 +21,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 88be64a67d..66514155f0 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, trace } 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 b3a5f1ecd5..df5fa66f81 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 758f38a26d..9415c67fe7 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'; /** From 6398bb8313b978d62c4e94fabf653e36df22dcd8 Mon Sep 17 00:00:00 2001 From: Valentin Marchaud Date: Wed, 16 Jun 2021 17:23:44 +0200 Subject: [PATCH 2/2] chore(doc): update compatibility matrix (#2287) --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dfd7d9406d..912813294e 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,8 @@ This is the JavaScript version of [OpenTelemetry](https://opentelemetry.io/), a | API Version | Core version | Contrib Version | | ----------- |--------------|-------------------------| -| 0.21.x | 0.21.x | ------ | +| 1.0.0 | ------ | ------ | +| 0.21.x | 0.21.x | 0.21.x | | 0.20.x | 0.20.x | 0.20.x | | v1.0.0-rc.3 | 0.19.x | 0.16.x | | 0.18.x | 0.18.x | 0.14.x |