Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dyladan
Copy link
Member

@dyladan dyladan commented Feb 5, 2025

Fixes #5364

@dyladan dyladan requested a review from a team as a code owner February 5, 2025 00:44
Copy link

codecov bot commented Feb 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.89%. Comparing base (595d0e9) to head (e75e40c).
Report is 9 commits behind head on main.

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     
Files with missing lines Coverage Δ
...ges/opentelemetry-resources/src/detectors/index.ts 100.00% <ø> (ø)

@trentm
Copy link
Contributor

trentm commented Feb 5, 2025

The two BrowserDetectors are quite different in the attributes they provide. One is doing process.runtime.* attributes. The other browser.* attributes. I've no idea if either is commonly used anywhere.

--- 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
* 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)

@pichlermarc pichlermarc added the target:next-major-release This PR targets the next major release (`next` branch) label Feb 6, 2025
@pichlermarc pichlermarc added this to the OpenTelemetry SDK 2.0 milestone Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
target:next-major-release This PR targets the next major release (`next` branch)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[v2] Remove unstable resource detectors from resource package
4 participants