From 412c7549e8c6d1c711300b5008bdc943adb6b4f1 Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 18 May 2020 13:24:51 +0800 Subject: [PATCH] fix: missing `global` in browser environments --- packages/opentelemetry-api/package.json | 4 ++++ .../opentelemetry-api/src/api/global-utils.ts | 5 +++-- .../src/platform/browser/globalThis.ts | 19 +++++++++++++++++++ .../src/platform/browser/index.ts | 17 +++++++++++++++++ .../opentelemetry-api/src/platform/index.ts | 17 +++++++++++++++++ .../src/platform/node/globalThis.ts | 19 +++++++++++++++++++ .../src/platform/node/index.ts | 17 +++++++++++++++++ 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 packages/opentelemetry-api/src/platform/browser/globalThis.ts create mode 100644 packages/opentelemetry-api/src/platform/browser/index.ts create mode 100644 packages/opentelemetry-api/src/platform/index.ts create mode 100644 packages/opentelemetry-api/src/platform/node/globalThis.ts create mode 100644 packages/opentelemetry-api/src/platform/node/index.ts diff --git a/packages/opentelemetry-api/package.json b/packages/opentelemetry-api/package.json index 40195286d4..859f47501c 100644 --- a/packages/opentelemetry-api/package.json +++ b/packages/opentelemetry-api/package.json @@ -4,6 +4,10 @@ "description": "Public API for OpenTelemetry", "main": "build/src/index.js", "types": "build/src/index.d.ts", + "browser": { + "./src/platform/index.ts": "./src/platform/browser/index.ts", + "./build/src/platform/index.js": "./build/src/platform/browser/index.js" + }, "repository": "open-telemetry/opentelemetry-js", "scripts": { "test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts", diff --git a/packages/opentelemetry-api/src/api/global-utils.ts b/packages/opentelemetry-api/src/api/global-utils.ts index 3114d1b6d0..00b73f8ce8 100644 --- a/packages/opentelemetry-api/src/api/global-utils.ts +++ b/packages/opentelemetry-api/src/api/global-utils.ts @@ -18,6 +18,7 @@ import { ContextManager } from '@opentelemetry/context-base'; import { HttpTextPropagator } from '../context/propagation/HttpTextPropagator'; import { MeterProvider } from '../metrics/MeterProvider'; import { TracerProvider } from '../trace/tracer_provider'; +import { _globalThis } from '../platform'; export const GLOBAL_CONTEXT_MANAGER_API_KEY = Symbol.for( 'io.opentelemetry.js.api.context' @@ -31,14 +32,14 @@ export const GLOBAL_PROPAGATION_API_KEY = Symbol.for( export const GLOBAL_TRACE_API_KEY = Symbol.for('io.opentelemetry.js.api.trace'); type Get = (version: number) => T; -type MyGlobals = Partial<{ +type OtelGlobal = Partial<{ [GLOBAL_CONTEXT_MANAGER_API_KEY]: Get; [GLOBAL_METRICS_API_KEY]: Get; [GLOBAL_PROPAGATION_API_KEY]: Get; [GLOBAL_TRACE_API_KEY]: Get; }>; -export const _global = global as typeof global & MyGlobals; +export const _global = _globalThis as OtelGlobal; /** * Make a function which accepts a version integer and returns the instance of an API if the version diff --git a/packages/opentelemetry-api/src/platform/browser/globalThis.ts b/packages/opentelemetry-api/src/platform/browser/globalThis.ts new file mode 100644 index 0000000000..df2b5b247f --- /dev/null +++ b/packages/opentelemetry-api/src/platform/browser/globalThis.ts @@ -0,0 +1,19 @@ +/*! + * Copyright 2020, 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. + */ + +/** only globals that common to node and browsers are allowed */ +// eslint-disable-next-line node/no-unsupported-features/es-builtins, no-undef +export const _globalThis = typeof globalThis === 'object' ? globalThis : window; diff --git a/packages/opentelemetry-api/src/platform/browser/index.ts b/packages/opentelemetry-api/src/platform/browser/index.ts new file mode 100644 index 0000000000..c3608dbed7 --- /dev/null +++ b/packages/opentelemetry-api/src/platform/browser/index.ts @@ -0,0 +1,17 @@ +/*! + * Copyright 2020, 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 './globalThis'; diff --git a/packages/opentelemetry-api/src/platform/index.ts b/packages/opentelemetry-api/src/platform/index.ts new file mode 100644 index 0000000000..e4361f1724 --- /dev/null +++ b/packages/opentelemetry-api/src/platform/index.ts @@ -0,0 +1,17 @@ +/*! + * Copyright 2020, 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-api/src/platform/node/globalThis.ts b/packages/opentelemetry-api/src/platform/node/globalThis.ts new file mode 100644 index 0000000000..8e24cf207e --- /dev/null +++ b/packages/opentelemetry-api/src/platform/node/globalThis.ts @@ -0,0 +1,19 @@ +/*! + * Copyright 2020, 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. + */ + +/** only globals that common to node and browsers are allowed */ +// eslint-disable-next-line node/no-unsupported-features/es-builtins +export const _globalThis = typeof globalThis === 'object' ? globalThis : global; diff --git a/packages/opentelemetry-api/src/platform/node/index.ts b/packages/opentelemetry-api/src/platform/node/index.ts new file mode 100644 index 0000000000..c3608dbed7 --- /dev/null +++ b/packages/opentelemetry-api/src/platform/node/index.ts @@ -0,0 +1,17 @@ +/*! + * Copyright 2020, 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 './globalThis';