@opentelemetry/opentelemetry-browser-detector
Version:
OpenTelemetry Resource Detector for Browser
53 lines • 2.17 kB
JavaScript
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { diag } from '@opentelemetry/api';
import { emptyResource } from '@opentelemetry/resources';
import { ATTR_USER_AGENT_ORIGINAL } from '@opentelemetry/semantic-conventions';
import { ATTR_BROWSER_LANGUAGE, ATTR_BROWSER_PLATFORM, ATTR_BROWSER_BRANDS, ATTR_BROWSER_MOBILE, } from './semconv';
/**
* BrowserDetector will be used to detect the resources related to browser.
*/
class BrowserDetector {
detect(config) {
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
if (!isBrowser) {
return emptyResource();
}
const browserResource = getBrowserAttributes();
return this._getResourceAttributes(browserResource, config);
}
/**
* Validates browser resource attribute map from browser variables
*
* @param browserResource The un-sanitized resource attributes from browser as key/value pairs.
* @param config: Config
* @returns The sanitized resource attributes.
*/
_getResourceAttributes(browserResource, _config) {
if (!browserResource[ATTR_USER_AGENT_ORIGINAL] &&
!browserResource[ATTR_BROWSER_PLATFORM]) {
diag.debug('BrowserDetector failed: Unable to find required browser resources. ');
return emptyResource();
}
else {
return { attributes: browserResource };
}
}
}
// Add Browser related attributes to resources
function getBrowserAttributes() {
const browserAttribs = {};
const userAgentData = navigator.userAgentData;
if (userAgentData) {
browserAttribs[ATTR_BROWSER_PLATFORM] = userAgentData.platform;
browserAttribs[ATTR_BROWSER_BRANDS] = userAgentData.brands.map(b => `${b.brand} ${b.version}`);
browserAttribs[ATTR_BROWSER_MOBILE] = userAgentData.mobile;
}
browserAttribs[ATTR_USER_AGENT_ORIGINAL] = navigator.userAgent;
browserAttribs[ATTR_BROWSER_LANGUAGE] = navigator.language;
return browserAttribs;
}
export const browserDetector = new BrowserDetector();
//# sourceMappingURL=BrowserDetector.js.map