8000 feat(rum): Capture browser navigator information (#2966) · TGTGamer/sentry-javascript@b22791e · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit b22791e

Browse files
authored
feat(rum): Capture browser navigator information (getsentry#2966)
1 parent 57eaded commit b22791e

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getFID } from './web-vitals/getFID';
1111
import { getLCP } from './web-vitals/getLCP';
1212
import { getTTFB } from './web-vitals/getTTFB';
1313
import { getFirstHidden } from './web-vitals/lib/getFirstHidden';
14+
import { NavigatorDeviceMemory, NavigatorNetworkInformation } from './web-vitals/types';
1415

1516
const global = getGlobalObject<Window>();
1617

@@ -129,6 +130,8 @@ export class MetricsInstrumentation {
129130

130131
this._performanceCursor = Math.max(performance.getEntries().length - 1, 0);
131132

133+
this._trackNavigator(transaction);
134+
132135
// Measurements are only available for pageload transactions
133136
if (transaction.op === 'pageload') {
134137
transaction.setMeasurements(this._measurements);
@@ -149,6 +152,46 @@ export class MetricsInstrumentation {
149152
});
150153
}
151154

155+
/**
156+
* Capture the information of the user agent.
157+
*/
158+
private _trackNavigator(transaction: Transaction): void {
159+
const navigator = global.navigator as null | (Navigator & NavigatorNetworkInformation & NavigatorDeviceMemory);
160+
161+
if (!navigator) {
162+
return;
163+
}
164+
165+
// track network connectivity
166+
167+
const connection = navigator.connection;
168+
if (connection) {
169+
if (connection.effectiveType) {
170+
transaction.setTag('effectiveConnectionType', connection.effectiveType);
171+
}
172+
173+
if (connection.type) {
174+
transaction.setTag('connectionType', connection.type);
175+
}
176+
177+
if (isMeasurementValue(connection.rtt)) {
178+
this._measurements['connection.rtt'] = { value: connection.rtt as number };
179+
}
180+
181+
if (isMeasurementValue(connection.downlink)) {
182+
this._measurements['connection.downlink'] = { value: connection.downlink as number };
183+
}
184+
}
185+
186+
if (isMeasurementValue(navigator.deviceMemory)) {
187+
transaction.setTag('deviceMemory', String(navigator.deviceMemory));
188+
}
189+
190+
if (isMeasurementValue(navigator.hardwareConcurrency)) {
191+
transaction.setTag('hardwareConcurrency', String(navigator.hardwareConcurrency));
192+
}
193+
}
194+
152195
/** Starts tracking the Largest Contentful Paint on the current page. */
153196
private _trackLCP(): void {
154197
getLCP(metric => {
@@ -332,3 +375,10 @@ export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }
332375
...ctx,
333376
});
334377
}
378+
379+
/**
380+
* Checks if a given value is a valid measurement value.
381+
*/
382+
function isMeasurementValue(value: any): boolean {
383+
return typeof value === 'number' && isFinite(value);
384+
}

packages/tracing/src/browser/web-vitals/types.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,42 @@ export interface Metric {
4343
export interface ReportHandler {
4444
(metric: Metric): void;
4545
}
46+
47+
// http://wicg.github.io/netinfo/#navigatornetworkinformation-interface
48+
export interface NavigatorNetworkInformation {
49+
readonly connection?: NetworkInformation;
50+
}
51+
52+
// http://wicg.github.io/netinfo/#connection-types
53+
type ConnectionType = 'bluetooth' | 'cellular' | 'ethernet' | 'mixed' | 'none' | 'other' | 'unknown' | 'wifi' | 'wimax';
54+
55+
// http://wicg.github.io/netinfo/#effectiveconnectiontype-enum
56+
type EffectiveConnectionType = '2g' | '3g' | '4g' | 'slow-2g';
57+
58+
// http://wicg.github.io/netinfo/#dom-megabit
59+
type Megabit = number;
60+
// http://wicg.github.io/netinfo/#dom-millisecond
61+
type Millisecond = number;
62+
63+
// http://wicg.github.io/netinfo/#networkinformation-interface
64+
interface NetworkInformation extends EventTarget {
65+
// http://wicg.github.io/netinfo/#type-attribute
66+
readonly type?: ConnectionType;
67+
// http://wicg.github.io/netinfo/#effectivetype-attribute
68+
readonly effectiveType?: EffectiveConnectionType;
69+
// http://wicg.github.io/netinfo/#downlinkmax-attribute
70+
readonly downlinkMax?: Megabit;
71+
// http://wicg.github.io/netinfo/#downlink-attribute
72+
readonly downlink?: Megabit;
73+
// http://wicg.github.io/netinfo/#rtt-attribute
74+
readonly rtt?: Millisecond;
75+
// http://wicg.github.io/netinfo/#savedata-attribute
76+
readonly saveData?: boolean;
77+
// http://wicg.github.io/netinfo/#handling-changes-to-the-underlying-connection
78+
onchange?: EventListener;
79+
}
80+
81+
// https://w3c.github.io/device-memory/#sec-device-memory-js-api
82+
export interface NavigatorDeviceMemory {
83+
readonly deviceMemory?: number;
84+
}

0 commit comments

Comments
 (0)
0