Skip to content

Commit 661b8cf

Browse files
authored
fix: missing global in browser environments (#1067)
1 parent 05e7bb9 commit 661b8cf

File tree

7 files changed

+96
-2
lines changed

7 files changed

+96
-2
lines changed

packages/opentelemetry-api/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"description": "Public API for OpenTelemetry",
55
"main": "build/src/index.js",
66
"types": "build/src/index.d.ts",
7+
"browser": {
8+
"./src/platform/index.ts": "./src/platform/browser/index.ts",
9+
"./build/src/platform/index.js": "./build/src/platform/browser/index.js"
10+
},
711
"repository": "open-telemetry/opentelemetry-js",
812
"scripts": {
913
"test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts",

packages/opentelemetry-api/src/api/global-utils.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ContextManager } from '@opentelemetry/context-base';
1818
import { HttpTextPropagator } from '../context/propagation/HttpTextPropagator';
1919
import { MeterProvider } from '../metrics/MeterProvider';
2020
import { TracerProvider } from '../trace/tracer_provider';
21+
import { _globalThis } from '../platform';
2122

2223
export const GLOBAL_CONTEXT_MANAGER_API_KEY = Symbol.for(
2324
'io.opentelemetry.js.api.context'
@@ -31,14 +32,14 @@ export const GLOBAL_PROPAGATION_API_KEY = Symbol.for(
3132
export const GLOBAL_TRACE_API_KEY = Symbol.for('io.opentelemetry.js.api.trace');
3233

3334
type Get<T> = (version: number) => T;
34-
type MyGlobals = Partial<{
35+
type OtelGlobal = Partial<{
3536
[GLOBAL_CONTEXT_MANAGER_API_KEY]: Get<ContextManager>;
3637
[GLOBAL_METRICS_API_KEY]: Get<MeterProvider>;
3738
[GLOBAL_PROPAGATION_API_KEY]: Get<HttpTextPropagator>;
3839
[GLOBAL_TRACE_API_KEY]: Get<TracerProvider>;
3940
}>;
4041

41-
export const _global = global as typeof global & MyGlobals;
42+
export const _global = _globalThis as OtelGlobal;
4243

4344
/**
4445
* Make a function which accepts a version integer and returns the instance of an API if the version
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/** only globals that common to node and browsers are allowed */
18+
// eslint-disable-next-line node/no-unsupported-features/es-builtins, no-undef
19+
export const _globalThis = typeof globalThis === 'object' ? globalThis : window;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export * from './globalThis';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export * from './node';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/** only globals that common to node and browsers are allowed */
18+
// eslint-disable-next-line node/no-unsupported-features/es-builtins
19+
export const _globalThis = typeof globalThis === 'object' ? globalThis : global;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export * from './globalThis';

0 commit comments

Comments
 (0)