Skip to content

Commit

Permalink
fix(resources): fix browser compatibility for host and os detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed May 30, 2022
1 parent fd95d42 commit 8e816e1
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 15 deletions.
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);
}

0 comments on commit 8e816e1

Please sign in to comment.