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 1ed3b2b
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 20 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
}))
};
28 changes: 19 additions & 9 deletions experimental/packages/opentelemetry-browser-detector/package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
{
"name": "@opentelemetry/opentelemetry-browser-detector",
"version": "0.32.0",
"version": "0.33.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 Expand Up @@ -60,8 +70,8 @@
"@opentelemetry/api": "^1.0.0"
},
"dependencies": {
"@opentelemetry/resources": "1.6.0",
"@opentelemetry/semantic-conventions": "^1.7.0"
"@opentelemetry/resources": "1.7.0",
"@opentelemetry/semantic-conventions": "1.7.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/browser-detector"
}
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 @@ -16,23 +16,23 @@
import * as sinon from 'sinon';
import { Resource } from '@opentelemetry/resources';
import { browserDetector } from '../src/BrowserDetector';
import { describeBrowser } from '../src/util';
import {
describeBrowser,
assertResource,
assertEmptyResource
} from '../src/util';
} from './util';

describeBrowser('browserDetector()', () => {
afterEach(() => {
sinon.restore();
});

it('should return browser information', async () => {
it('should return browser information with userAgentData', async () => {
sinon.stub(globalThis, 'navigator').value({
userAgent: 'dddd',
language:'en-US',
userAgentData:{
platform:'platform',
language: 'en-US',
userAgentData: {
platform: 'platform',
brands:[
{
brand: 'Chromium',
Expand All @@ -47,7 +47,7 @@ describeBrowser('browserDetector()', () => {
version: '99'
}
],
mobile:false
mobile: false
}
});

Expand All @@ -61,9 +61,23 @@ describeBrowser('browserDetector()', () => {
],
mobile: false,
language: 'en-US',
});
});

it('should return browser information with userAgent', async () => {
sinon.stub(globalThis, 'navigator').value({
userAgent: 'dddd',
language: 'en-US',
userAgentData: 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: '',
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
Expand Up @@ -15,7 +15,7 @@
*/
import {Suite} from 'mocha';
import * as assert from 'assert';
import {BROWSER_ATTRIBUTES} from './types';
import {BROWSER_ATTRIBUTES} from '../src/types';
import {Resource} from '@opentelemetry/resources';

export function describeBrowser(title: string, fn: (this: Suite) => void) {
Expand Down
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,11 @@
{
"extends": "../../../tsconfig.base.esm.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esm",
"tsBuildInfoFile": "build/esm/tsconfig.esm.tsbuildinfo"
},
"include": [
"src/**/*.ts"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.base.esnext.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esnext",
"tsBuildInfoFile": "build/esnext/tsconfig.esnext.tsbuildinfo"
},
"include": [
"src/**/*.ts"
]
}
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 1ed3b2b

Please sign in to comment.