Skip to content

Commit

Permalink
fixup! add browser testing
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Nov 1, 2022
1 parent d6c5ffc commit 20c3cee
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 34 deletions.
24 changes: 24 additions & 0 deletions experimental/packages/opentelemetry-browser-detector/karma.conf.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.base');

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig
}))
};
22 changes: 16 additions & 6 deletions experimental/packages/opentelemetry-browser-detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
"version": "0.32.0",
"description": "OpenTelemetry Resource Detector for Browser",
"main": "build/src/index.js",
"module": "build/esm/index.js",
"esnext": "build/esnext/index.js",
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
"prepublishOnly": "npm run compile",
"compile": "tsc --build",
"clean": "tsc --build --clean",
"compile": "tsc --build tsconfig.all.json",
"clean": "tsc --build --clean tsconfig.all.json",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:browser": "nyc karma start --single-run",
"tdd": "npm run test -- --watch-extensions ts --watch",
"tdd:browser": "karma start",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../",
"version": "node ../../../scripts/version-update.js",
"watch": "tsc -w",
"watch": "tsc --build --watch tsconfig.all.json",
"precompile": "lerna run version --scope $(npm pkg get name) --include-dependencies",
"prewatch": "npm run precompile",
"peer-api-check": "node ../../../scripts/peer-api-check.js",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../../"
"peer-api-check": "node ../../../scripts/peer-api-check.js"
},
"keywords": [
"opentelemetry",
Expand All @@ -32,6 +36,12 @@
"node": ">=14"
},
"files": [
"build/esm/**/*.js",
"build/esm/**/*.js.map",
"build/esm/**/*.d.ts",
"build/esnext/**/*.js",
"build/esnext/**/*.js.map",
"build/esnext/**/*.d.ts",
"build/src/**/*.js",
"build/src/**/*.js.map",
"build/src/**/*.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import { diag } from '@opentelemetry/api';
// import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Detector, Resource, ResourceDetectionConfig } from '@opentelemetry/resources';
import { ResourceAttributes } from '@opentelemetry/resources';
import { BROWSER_ATTRIBUTES, UserAgentData } from './types';
Expand Down Expand Up @@ -56,11 +55,11 @@ class BrowserDetector implements Detector {
}
}

//Add Browser related attributes to resources
// Add Browser related attributes to resources
function getBrowserAttributes(): ResourceAttributes {
const browserAttribs : ResourceAttributes = {};
const userAgentData : UserAgentData | undefined = (navigator as any).userAgentData;
if(userAgentData) {
if (userAgentData) {
browserAttribs[BROWSER_ATTRIBUTES.PLATFORM] = userAgentData.platform;
browserAttribs[BROWSER_ATTRIBUTES.BRANDS] = userAgentData.brands.map(b => `${b.brand} ${b.version}`);
browserAttribs[BROWSER_ATTRIBUTES.MOBILE] = userAgentData.mobile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,26 @@ describeBrowser('browserDetector()', () => {
sinon.restore();
});

it('should return browser information', async () => {
sinon.stub(globalThis, 'navigator').value({
userAgent: 'dddd',
language:'en-US',
userAgentData:{
platform:'platform',
brands:[
{
brand: 'Chromium',
version: '106'
},
{
brand: 'Google Chrome',
version: '106'
},
{
brand: 'Not;A=Brand',
version: '99'
}
],
mobile:false
}
it('should return browser information with userAgentData', async () => {
sinon.stub(globalThis.navigator, 'userAgent').value('dddd');
sinon.stub(globalThis.navigator, 'language').value('en-US');
sinon.stub(globalThis.navigator as any, 'userAgentData').value({
platform: 'platform',
brands:[
{
brand: 'Chromium',
version: '106'
},
{
brand: 'Google Chrome',
version: '106'
},
{
brand: 'Not;A=Brand',
version: '99'
}
],
mobile: false
});

const resource: Resource = await browserDetector.detect();
Expand All @@ -61,13 +59,23 @@ describeBrowser('browserDetector()', () => {
],
mobile: false,
language: 'en-US',
});
});

it('should return browser information with userAgent', async () => {
sinon.stub(globalThis.navigator, 'userAgent').value('dddd');
sinon.stub(globalThis.navigator, 'language').value('en-US');
sinon.stub(globalThis.navigator as any, 'userAgentData').value(undefined);

const resource: Resource = await browserDetector.detect();
assertResource(resource, {
language: 'en-US',
user_agent: 'dddd'
});
});

it('should return empty resources if user agent is missing', async () => {
sinon.stub(globalThis, 'navigator').value({
userAgent: '',
});
sinon.stub(globalThis.navigator, 'userAgent').value('');
const resource: Resource = await browserDetector.detect();
assertEmptyResource(resource);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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('.', true, /test$/);
testsContext.keys().forEach(testsContext);

const srcContext = require.context('.', true, /src$/);
srcContext.keys().forEach(srcContext);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.base.json",
"files": [],
"references": [
{ "path": "./tsconfig.json" },
{ "path": "./tsconfig.esm.json" },
{ "path": "./tsconfig.esnext.json" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.base.esm.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esm",
"tsBuildInfoFile": "build/esm/tsconfig.esm.tsbuildinfo"
},
"include": [
"src/**/*.ts"
],
"references": [
{
"path": "./tsconfig.json"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.base.esnext.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esnext",
"tsBuildInfoFile": "build/esnext/tsconfig.esnext.tsbuildinfo"
},
"include": [
"src/**/*.ts"
],
"references": [
{
"path": "./tsconfig.json"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
"test/**/*.ts"
],
"references": [
{
"path": "../../../api"
},
{
"path": "../../../packages/opentelemetry-resources"
},
{
"path": "../../../packages/opentelemetry-semantic-conventions"
}
]
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
{
"path": "experimental/packages/exporter-trace-otlp-proto"
},
{
"path": "experimental/packages/opentelemetry-browser-detector"
},
{
"path": "experimental/packages/opentelemetry-exporter-metrics-otlp-grpc"
},
Expand Down

0 comments on commit 20c3cee

Please sign in to comment.