Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk-trace-base): fix crash on environments without global document #3000

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
### :bug: (Bug Fix)

* fix(resources): fix browser compatibility for host and os detectors [#3004](https://github.com/open-telemetry/opentelemetry-js/pull/3004) @legendecas
* 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)

Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry-sdk-trace-base/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }
}))
};
24 changes: 24 additions & 0 deletions packages/opentelemetry-sdk-trace-base/karma.worker.js
Original file line number Diff line number Diff line change
@@ -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,
}))
};
3 changes: 3 additions & 0 deletions packages/opentelemetry-sdk-trace-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class BatchSpanProcessor extends BatchSpanProcessorBase<BatchSpanProcesso
}

private onInit(config?: BatchSpanProcessorBrowserConfig): void {
if (config?.disableAutoFlushOnDocumentHide !== true && document != null) {
if (config?.disableAutoFlushOnDocumentHide !== true && typeof document !== 'undefined') {
this._visibilityChangeListener = () => {
if (document.visibilityState === 'hidden') {
void this.forceFlush();
Expand All @@ -45,11 +45,13 @@ export class BatchSpanProcessor extends BatchSpanProcessorBase<BatchSpanProcesso
}

protected onShutdown(): void {
if (this._visibilityChangeListener) {
document.removeEventListener('visibilitychange', this._visibilityChangeListener);
}
if (this._pageHideListener) {
document.removeEventListener('pagehide', this._pageHideListener);
if (typeof document !== 'undefined') {
if (this._visibilityChangeListener) {
document.removeEventListener('visibilitychange', this._visibilityChangeListener);
}
if (this._pageHideListener) {
document.removeEventListener('pagehide', this._pageHideListener);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import { SpanExporter } from '../../../src';
import { BatchSpanProcessor } from '../../../src/platform/browser/export/BatchSpanProcessor';
import { TestTracingSpanExporter } from '../../common/export/TestTracingSpanExporter';

describe('BatchSpanProcessor - web', () => {
const describeDocument = typeof document === 'object' ? describe : describe.skip;

describeDocument('BatchSpanProcessor - web main context', () => {
let visibilityState: VisibilityState = 'visible';
let exporter: SpanExporter;
let processor: BatchSpanProcessor;
Expand Down Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ import {

describe('BasicTracerProvider', () => {
let removeEvent: (() => void) | undefined;
const envSource = (typeof window !== 'undefined'
? window
: process.env) as any;

let envSource: Record<string, any>;
if (typeof process === 'undefined') {
envSource = (globalThis as unknown) as Record<string, any>;
} else {
envSource = process.env as Record<string, any>;
}

beforeEach(() => {
context.disable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
if (typeof process === 'undefined') {
envSource = (globalThis as unknown) as Record<string, any>;
} else {
envSource = process.env as Record<string, any>;
}

class TestSampler implements Sampler {
shouldSample(_context: Context, _traceId: string, _spanName: string, _spanKind: SpanKind, attributes: SpanAttributes, links: Link[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
if (typeof process === 'undefined') {
envSource = (globalThis as unknown) as Record<string, any>;
} else {
envSource = process.env as Record<string, any>;
}

describe('buildSamplerFromEnv()', () => {
afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('BatchSpanProcessorBase', () => {

let env: Record<string, any>;
if (typeof process === 'undefined') {
env = (window as unknown) as Record<string, any>;
env = (globalThis as unknown) as Record<string, any>;
} else {
env = process.env as Record<string, any>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}