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(resources): fix browser compatibility for host and os detectors #3004

Merged
merged 2 commits into from
Jun 1, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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:/open-telemetry/opentelemetry-js/pull/3004) @legendecas

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
24 changes: 24 additions & 0 deletions packages/opentelemetry-resources/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
}))
};
2 changes: 2 additions & 0 deletions packages/opentelemetry-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
"compile": "tsc --build tsconfig.all.json",
"clean": "tsc --build --clean tsconfig.all.json",
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"codecov:webworker": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:browser": "nyc karma start --single-run",
"test:webworker": "nyc karma start karma.worker.js --single-run",
"tdd": "npm run test -- --watch-extensions ts --watch",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"version": "node ../../scripts/version-update.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { ResourceAttributes } from '../types';
*/
class BrowserDetector implements Detector {
async detect(config?: ResourceDetectionConfig): Promise<Resource> {
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
const isBrowser = typeof navigator !== 'undefined';
if (!isBrowser) {
return Resource.empty();
}
const browserResource: ResourceAttributes = {
[SemanticResourceAttributes.PROCESS_RUNTIME_NAME]: 'browser',
[SemanticResourceAttributes.PROCESS_RUNTIME_DESCRIPTION]: 'Web Browser',
[SemanticResourceAttributes.PROCESS_RUNTIME_VERSION]: window.navigator.userAgent
[SemanticResourceAttributes.PROCESS_RUNTIME_VERSION]: navigator.userAgent
};
return this._getResourceAttributes(browserResource, config);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/opentelemetry-resources/src/detectors/NoopDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

import { Resource } from '../Resource';
import { Detector } from '../types';

export class NoopDetector implements Detector {
async detect(): Promise<Resource> {
return new Resource({});
}
}

export const noopDetector = new NoopDetector();
2 changes: 0 additions & 2 deletions packages/opentelemetry-resources/src/detectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@

export * from './BrowserDetector';
export * from './EnvDetector';
export * from './OSDetector';
export * from './HostDetector';
export * from './ProcessDetector';
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/

import { noopDetector } from '../../detectors/NoopDetector';

export const hostDetector = noopDetector;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/

import { noopDetector } from '../../detectors/NoopDetector';

export const osDetector = noopDetector;
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

export * from './default-service-name';
export * from './detect-resources';
export * from './HostDetector';
export * from './OSDetector';
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Resource } from '../Resource';
import { Detector, ResourceAttributes } from '../types';
import { ResourceDetectionConfig } from '../config';
import { Resource } from '../../Resource';
import { Detector, ResourceAttributes } from '../../types';
import { ResourceDetectionConfig } from '../../config';
import { arch, hostname } from 'os';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Resource } from '../Resource';
import { Detector, ResourceAttributes } from '../types';
import { ResourceDetectionConfig } from '../config';
import { Resource } from '../../Resource';
import { Detector, ResourceAttributes } from '../../types';
import { ResourceDetectionConfig } from '../../config';
import { platform, release } from 'os';

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-resources/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

export * from './default-service-name';
export * from './detect-resources';
export * from './HostDetector';
export * from './OSDetector';
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import {
assertEmptyResource,
} from '../../util/resource-assertions';


describeBrowser('browserDetector()', () => {

afterEach(() => {
sinon.restore();
});

it('should return browser information', async () => {
sinon.stub(window, 'navigator').value({
sinon.stub(globalThis, 'navigator').value({
userAgent: 'dddd',
});

Expand All @@ -42,11 +40,10 @@ describeBrowser('browserDetector()', () => {
});
});
it('should return empty resources if version is missing', async () => {
sinon.stub(window, 'navigator').value({
sinon.stub(globalThis, 'navigator').value({
userAgent: '',
});
const resource: Resource = await browserDetector.detect();
assertEmptyResource(resource);
});
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/
import * as sinon from 'sinon';
import { hostDetector, Resource } from '../../../src';
import {
assertEmptyResource,
} from '../../util/resource-assertions';
import { describeBrowser } from '../../util';

describeBrowser('hostDetector() on web browser', () => {
afterEach(() => {
sinon.restore();
});

it('should return empty resource', async () => {
const resource: Resource = await hostDetector.detect();
assertEmptyResource(resource);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/
import * as sinon from 'sinon';
import { osDetector, Resource } from '../../../src';
import {
assertEmptyResource,
} from '../../util/resource-assertions';
import { describeBrowser } from '../../util';

describeBrowser('osDetector() on web browser', () => {
afterEach(() => {
sinon.restore();
});

it('should return empty resource', async () => {
const resource: Resource = await osDetector.detect();
assertEmptyResource(resource);
});
});
25 changes: 25 additions & 0 deletions packages/opentelemetry-resources/test/index-webpack.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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('./', false, /test$/);
testsContext.keys().forEach(testsContext);
}

{
const testsContext = require.context('./detectors/browser', false, /test$/);
testsContext.keys().forEach(testsContext);
}