-
Notifications
You must be signed in to change notification settings - Fork 842
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
Remove duplicate browser detector #5420
base: main
Are you sure you want to change the base?
Remove duplicate browser detector #5420
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5420 +/- ##
==========================================
+ Coverage 94.79% 94.89% +0.10%
==========================================
Files 310 309 -1
Lines 7967 7949 -18
Branches 1680 1674 -6
==========================================
- Hits 7552 7543 -9
+ Misses 415 406 -9
|
The two BrowserDetectors are quite different in the attributes they provide. One is doing --- packages/opentelemetry-resources/src/detectors/BrowserDetector.ts 2025-02-03 12:25:09
+++ experimental/packages/opentelemetry-browser-detector/src/BrowserDetector.ts 2025-02-03 12:25:09
@@ -16,55 +16,67 @@
import { Attributes, diag } from '@opentelemetry/api';
import {
- SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION,
- SEMRESATTRS_PROCESS_RUNTIME_NAME,
- SEMRESATTRS_PROCESS_RUNTIME_VERSION,
-} from '@opentelemetry/semantic-conventions';
-import { ResourceDetectionConfig } from '../config';
-import { DetectedResource, ResourceDetector } from '../types';
+ DetectedResource,
+ ResourceDetector,
+ Resource,
+ ResourceDetectionConfig,
+} from '@opentelemetry/resources';
+import { BROWSER_ATTRIBUTES, UserAgentData } from './types';
/**
* BrowserDetector will be used to detect the resources related to browser.
*/
class BrowserDetector implements ResourceDetector {
detect(config?: ResourceDetectionConfig): DetectedResource {
- const isBrowser =
- typeof navigator !== 'undefined' &&
- global.process?.versions?.node === undefined && // Node.js v21 adds `navigator`
- // @ts-expect-error don't have Bun types
- global.Bun?.version === undefined; // Bun (bun.sh) defines `navigator`
+ const isBrowser = typeof navigator !== 'undefined';
if (!isBrowser) {
- return { attributes: {} };
+ return Resource.EMPTY;
}
- const browserResource: Attributes = {
- [SEMRESATTRS_PROCESS_RUNTIME_NAME]: 'browser',
- [SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION]: 'Web Browser',
- [SEMRESATTRS_PROCESS_RUNTIME_VERSION]: navigator.userAgent,
- };
+ const browserResource: Attributes = getBrowserAttributes();
return this._getResourceAttributes(browserResource, config);
}
/**
- * Validates process resource attribute map from process variables
+ * Validates browser resource attribute map from browser variables
*
- * @param browserResource The un-sanitized resource attributes from process as key/value pairs.
+ * @param browserResource The un-sanitized resource attributes from browser as key/value pairs.
* @param config: Config
* @returns The sanitized resource attributes.
*/
private _getResourceAttributes(
browserResource: Attributes,
_config?: ResourceDetectionConfig
+ ): DetectedResource {
+ if (
+ !browserResource[BROWSER_ATTRIBUTES.USER_AGENT] &&
+ !browserResource[BROWSER_ATTRIBUTES.PLATFORM]
) {
- if (browserResource[SEMRESATTRS_PROCESS_RUNTIME_VERSION] === '') {
diag.debug(
'BrowserDetector failed: Unable to find required browser resources. '
);
- return { attributes: {} };
+ return Resource.EMPTY;
} else {
- return {
- attributes: browserResource,
- };
+ return { attributes: browserResource };
}
}
}
+// Add Browser related attributes to resources
+function getBrowserAttributes(): Attributes {
+ const browserAttribs: Attributes = {};
+ const userAgentData = (
+ navigator as Navigator & { userAgentData?: UserAgentData }
+ ).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;
+ } else {
+ browserAttribs[BROWSER_ATTRIBUTES.USER_AGENT] = navigator.userAgent;
+ }
+ browserAttribs[BROWSER_ATTRIBUTES.LANGUAGE] = navigator.language;
+ return browserAttribs;
+}
+
export const browserDetector = new BrowserDetector(); |
@@ -80,6 +80,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se | |||
* chore!: Raise the minimum supported Node.js version to `^18.19.0 || >=20.6.0`. Support for Node.js 14, 16, and early minor versions of 18 and 20 have been dropped. This applies to all packages except the 'api' and 'semantic-conventions' packages. [#5395](https://github.com/open-telemetry/opentelemetry-js/issues/5395) @trentm | |||
* feat(core)!: remove TracesSamplerValues from exports [#5406](https://github.com/open-telemetry/opentelemetry-js/pull/5406) @pichlermarc | |||
* (user-facing): TracesSamplerValues was only consumed internally and has been removed from exports without replacement | |||
* chore(resources)!: Remove deprecated duplicate browser detector from `@opentelemetry/resource` in favor of `@opentelemetry/opentelemetry-browser-detector` [#5420](https://github.com/open-telemetry/opentelemetry-js/pull/5420) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
* chore(resources)!: Remove deprecated duplicate browser detector from `@opentelemetry/resource` in favor of `@opentelemetry/opentelemetry-browser-detector` [#5420](https://github.com/open-telemetry/opentelemetry-js/pull/5420) | |
* chore(resources)!: Remove deprecated duplicate `browserDetector` from `@opentelemetry/resources` in favor of `@opentelemetry/opentelemetry-browser-detector` [#5420](https://github.com/open-telemetry/opentelemetry-js/pull/5420) |
Fixes #5364