Skip to content

Commit 7649cf1

Browse files
fix(resources): fix browser compatibility for host and os detectors (#3004)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent fcef5e2 commit 7649cf1

File tree

16 files changed

+195
-15
lines changed

16 files changed

+195
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file.
1010

1111
### :bug: (Bug Fix)
1212

13+
* fix(resources): fix browser compatibility for host and os detectors [#3004](https://github.com/open-telemetry/opentelemetry-js/pull/3004) @legendecas
14+
1315
### :books: (Refine Doc)
1416

1517
### :house: (Internal)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*!
2+
* Copyright The 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+
* http://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+
const karmaWebpackConfig = require('../../karma.webpack');
18+
const karmaBaseConfig = require('../../karma.worker');
19+
20+
module.exports = (config) => {
21+
config.set(Object.assign({}, karmaBaseConfig, {
22+
webpack: karmaWebpackConfig
23+
}))
24+
};

packages/opentelemetry-resources/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
"compile": "tsc --build tsconfig.all.json",
1919
"clean": "tsc --build --clean tsconfig.all.json",
2020
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
21+
"codecov:webworker": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
2122
"lint": "eslint . --ext .ts",
2223
"lint:fix": "eslint . --ext .ts --fix",
2324
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
2425
"test:browser": "nyc karma start --single-run",
26+
"test:webworker": "nyc karma start karma.worker.js --single-run",
2527
"tdd": "npm run test -- --watch-extensions ts --watch",
2628
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
2729
"version": "node ../../scripts/version-update.js",

packages/opentelemetry-resources/src/detectors/BrowserDetector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import { ResourceAttributes } from '../types';
2424
*/
2525
class BrowserDetector implements Detector {
2626
async detect(config?: ResourceDetectionConfig): Promise<Resource> {
27-
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
27+
const isBrowser = typeof navigator !== 'undefined';
2828
if (!isBrowser) {
2929
return Resource.empty();
3030
}
3131
const browserResource: ResourceAttributes = {
3232
[SemanticResourceAttributes.PROCESS_RUNTIME_NAME]: 'browser',
3333
[SemanticResourceAttributes.PROCESS_RUNTIME_DESCRIPTION]: 'Web Browser',
34-
[SemanticResourceAttributes.PROCESS_RUNTIME_VERSION]: window.navigator.userAgent
34+
[SemanticResourceAttributes.PROCESS_RUNTIME_VERSION]: navigator.userAgent
3535
};
3636
return this._getResourceAttributes(browserResource, config);
3737
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright The 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+
import { Resource } from '../Resource';
18+
import { Detector } from '../types';
19+
20+
export class NoopDetector implements Detector {
21+
async detect(): Promise<Resource> {
22+
return new Resource({});
23+
}
24+
}
25+
26+
export const noopDetector = new NoopDetector();

packages/opentelemetry-resources/src/detectors/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@
1616

1717
export * from './BrowserDetector';
1818
export * from './EnvDetector';
19-
export * from './OSDetector';
20-
export * from './HostDetector';
2119
export * from './ProcessDetector';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright The 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+
import { noopDetector } from '../../detectors/NoopDetector';
18+
19+
export const hostDetector = noopDetector;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright The 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+
import { noopDetector } from '../../detectors/NoopDetector';
18+
19+
export const osDetector = noopDetector;

packages/opentelemetry-resources/src/platform/browser/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717
export * from './default-service-name';
1818
export * from './detect-resources';
19+
export * from './HostDetector';
20+
export * from './OSDetector';

packages/opentelemetry-resources/src/detectors/HostDetector.ts renamed to packages/opentelemetry-resources/src/platform/node/HostDetector.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
18-
import { Resource } from '../Resource';
19-
import { Detector, ResourceAttributes } from '../types';
20-
import { ResourceDetectionConfig } from '../config';
18+
import { Resource } from '../../Resource';
19+
import { Detector, ResourceAttributes } from '../../types';
20+
import { ResourceDetectionConfig } from '../../config';
2121
import { arch, hostname } from 'os';
2222

2323
/**

0 commit comments

Comments
 (0)