From 651e870457fb9d5b270bff2c09bdf534cad8943a Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 30 May 2022 18:44:09 +0800 Subject: [PATCH] fix(sdk-trace-base): fix crash on environments without global document --- CHANGELOG.md | 2 ++ .../karma.conf.js | 2 -- .../karma.worker.js | 24 +++++++++++++++++++ .../opentelemetry-sdk-trace-base/package.json | 3 +++ .../browser/export/BatchSpanProcessor.ts | 14 ++++++----- .../browser/export/BatchSpanProcessor.test.ts | 15 +++++++++++- .../test/common/BasicTracerProvider.test.ts | 10 +++++--- .../test/common/Tracer.test.ts | 9 ++++--- .../test/common/config.test.ts | 9 ++++--- .../export/BatchSpanProcessorBase.test.ts | 2 +- .../test/{browser => }/index-webpack.ts | 11 ++++----- .../test/index-webpack.worker.ts | 20 ++++++++++++++++ 12 files changed, 95 insertions(+), 26 deletions(-) create mode 100644 packages/opentelemetry-sdk-trace-base/karma.worker.js rename packages/opentelemetry-sdk-trace-base/test/{browser => }/index-webpack.ts (64%) create mode 100644 packages/opentelemetry-sdk-trace-base/test/index-webpack.worker.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index cfe79dbedb6..c4775fb2c85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file. ### :bug: (Bug Fix) +* fix(sdk-trace-base): fix crash on environments without global document [#3000](https://github.com/open-telemetry/opentelemetry-js/pull/3000) @legendecas + ### :books: (Refine Doc) ### :house: (Internal) diff --git a/packages/opentelemetry-sdk-trace-base/karma.conf.js b/packages/opentelemetry-sdk-trace-base/karma.conf.js index 455b1437c87..bf2a41b7999 100644 --- a/packages/opentelemetry-sdk-trace-base/karma.conf.js +++ b/packages/opentelemetry-sdk-trace-base/karma.conf.js @@ -20,7 +20,5 @@ const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { webpack: karmaWebpackConfig, - files: ['test/browser/index-webpack.ts'], - preprocessors: { 'test/browser/index-webpack.ts': ['webpack'] } })) }; diff --git a/packages/opentelemetry-sdk-trace-base/karma.worker.js b/packages/opentelemetry-sdk-trace-base/karma.worker.js new file mode 100644 index 00000000000..a8d8e50e81b --- /dev/null +++ b/packages/opentelemetry-sdk-trace-base/karma.worker.js @@ -0,0 +1,24 @@ +/*! + * 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 + * + * http://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. + */ + +const karmaWebpackConfig = require('../../karma.webpack'); +const karmaBaseConfig = require('../../karma.worker'); + +module.exports = (config) => { + config.set(Object.assign({}, karmaBaseConfig, { + webpack: karmaWebpackConfig, + })) +}; diff --git a/packages/opentelemetry-sdk-trace-base/package.json b/packages/opentelemetry-sdk-trace-base/package.json index 4929b3ed7ff..c7f5aa7ff1e 100644 --- a/packages/opentelemetry-sdk-trace-base/package.json +++ b/packages/opentelemetry-sdk-trace-base/package.json @@ -19,12 +19,14 @@ "clean": "tsc --build --clean tsconfig.all.json", "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/browser/**/*.ts'", "test:browser": "nyc karma start --single-run", + "test:webworker": "nyc karma start karma.worker.js --single-run", "tdd": "npm run tdd:node", "tdd:node": "npm run test -- --watch-extensions ts --watch", "tdd:browser": "karma start", "lint": "eslint . --ext .ts", "lint:fix": "eslint . --ext .ts --fix", "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "codecov:webworker": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", "version": "node ../../scripts/version-update.js", "watch": "tsc --build --watch tsconfig.all.json", "precompile": "lerna run version --scope $(npm pkg get name) --include-dependencies", @@ -72,6 +74,7 @@ "karma": "6.3.16", "karma-chrome-launcher": "3.1.0", "karma-coverage-istanbul-reporter": "3.0.3", + "karma-mocha-webworker": "1.3.0", "karma-mocha": "2.0.1", "karma-spec-reporter": "0.0.32", "karma-webpack": "4.0.2", diff --git a/packages/opentelemetry-sdk-trace-base/src/platform/browser/export/BatchSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/platform/browser/export/BatchSpanProcessor.ts index d5a87cc10d0..12b6eb24a43 100644 --- a/packages/opentelemetry-sdk-trace-base/src/platform/browser/export/BatchSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/platform/browser/export/BatchSpanProcessor.ts @@ -28,7 +28,7 @@ export class BatchSpanProcessor extends BatchSpanProcessorBase { if (document.visibilityState === 'hidden') { void this.forceFlush(); @@ -45,11 +45,13 @@ export class BatchSpanProcessor extends BatchSpanProcessorBase { +const describeDocument = typeof document === 'object' ? describe : describe.skip; + +describeDocument('BatchSpanProcessor - web main context', () => { let visibilityState: VisibilityState = 'visible'; let exporter: SpanExporter; let processor: BatchSpanProcessor; @@ -100,3 +102,14 @@ describe('BatchSpanProcessor - web', () => { }); }); }); + +describe('BatchSpanProcessor', () => { + it('without exception', async () => { + const exporter = new TestTracingSpanExporter(); + const spanProcessor = new BatchSpanProcessor(exporter); + assert.ok(spanProcessor instanceof BatchSpanProcessor); + + await spanProcessor.forceFlush(); + await spanProcessor.shutdown(); + }); +}); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts index 1b5e6872e38..e2b17bb5614 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/BasicTracerProvider.test.ts @@ -47,9 +47,13 @@ import { describe('BasicTracerProvider', () => { let removeEvent: (() => void) | undefined; - const envSource = (typeof window !== 'undefined' - ? window - : process.env) as any; + + let envSource: Record; + if (typeof process === 'undefined') { + envSource = (globalThis as unknown) as Record; + } else { + envSource = process.env as Record; + } beforeEach(() => { context.disable(); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts index 71453bb260f..31652f8b6e8 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts @@ -44,9 +44,12 @@ import { invalidAttributes, validAttributes } from './util'; describe('Tracer', () => { const tracerProvider = new BasicTracerProvider(); - const envSource = (typeof window !== 'undefined' - ? window - : process.env) as any; + let envSource: Record; + if (typeof process === 'undefined') { + envSource = (globalThis as unknown) as Record; + } else { + envSource = process.env as Record; + } class TestSampler implements Sampler { shouldSample(_context: Context, _traceId: string, _spanName: string, _spanKind: SpanKind, attributes: SpanAttributes, links: Link[]) { diff --git a/packages/opentelemetry-sdk-trace-base/test/common/config.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/config.test.ts index ea27f21e9d9..f627cc18a74 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/config.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/config.test.ts @@ -23,9 +23,12 @@ import * as assert from 'assert'; import { buildSamplerFromEnv } from '../../src/config'; describe('config', () => { - const envSource = (typeof window !== 'undefined' - ? window - : process.env) as any; + let envSource: Record; + if (typeof process === 'undefined') { + envSource = (globalThis as unknown) as Record; + } else { + envSource = process.env as Record; + } describe('buildSamplerFromEnv()', () => { afterEach(() => { diff --git a/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts index 817b5bc3763..52cd832a3eb 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/export/BatchSpanProcessorBase.test.ts @@ -97,7 +97,7 @@ describe('BatchSpanProcessorBase', () => { let env: Record; if (typeof process === 'undefined') { - env = (window as unknown) as Record; + env = (globalThis as unknown) as Record; } else { env = process.env as Record; } diff --git a/packages/opentelemetry-sdk-trace-base/test/browser/index-webpack.ts b/packages/opentelemetry-sdk-trace-base/test/index-webpack.ts similarity index 64% rename from packages/opentelemetry-sdk-trace-base/test/browser/index-webpack.ts rename to packages/opentelemetry-sdk-trace-base/test/index-webpack.ts index 99100a0f6ee..802d6c4053e 100644 --- a/packages/opentelemetry-sdk-trace-base/test/browser/index-webpack.ts +++ b/packages/opentelemetry-sdk-trace-base/test/index-webpack.ts @@ -13,11 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -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); +{ + const testsContext = require.context('./', true, /test$/); + testsContext.keys().forEach(testsContext); +} diff --git a/packages/opentelemetry-sdk-trace-base/test/index-webpack.worker.ts b/packages/opentelemetry-sdk-trace-base/test/index-webpack.worker.ts new file mode 100644 index 00000000000..802d6c4053e --- /dev/null +++ b/packages/opentelemetry-sdk-trace-base/test/index-webpack.worker.ts @@ -0,0 +1,20 @@ +/* + * 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. + */ + +{ + const testsContext = require.context('./', true, /test$/); + testsContext.keys().forEach(testsContext); +}