Skip to content

Commit

Permalink
ref(measurements): Update setMeasurements to only set a single meas…
Browse files Browse the repository at this point in the history
…urement
  • Loading branch information
lforst committed Apr 12, 2022
1 parent 58b9c4e commit 605fe42
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
15 changes: 9 additions & 6 deletions packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Measurements, SpanContext } from '@sentry/types';
import { SpanContext } from '@sentry/types';
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNodeEnv, logger } from '@sentry/utils';

import { IS_DEBUG_BUILD } from '../flags';
Expand All @@ -17,7 +17,7 @@ const global = getGlobalObject<Window>();

/** Class tracking metrics */
export class MetricsInstrumentation {
private _measurements: Measurements = {};
private _measurements: Record<string, { value: number }> = {};

private _performanceCursor: number = 0;
private _lcpEntry: LargestContentfulPaint | undefined;
Expand Down Expand Up @@ -162,7 +162,10 @@ export class MetricsInstrumentation {
delete this._measurements.cls;
}

transaction.setMeasurements(this._measurements);
Object.keys(this._measurements).forEach(measurementName => {
transaction.setMeasurement(measurementName, this._measurements[measurementName].value);
});

tagMetricInfo(transaction, this._lcpEntry, this._clsEntry);
transaction.setTag('sentry_reportAllChanges', this._reportAllChanges);
}
Expand All @@ -189,11 +192,11 @@ export class MetricsInstrumentation {
}

if (isMeasurementValue(connection.rtt)) {
this._measurements['connection.rtt'] = { value: connection.rtt as number };
this._measurements['connection.rtt'] = { value: connection.rtt };
}

if (isMeasurementValue(connection.downlink)) {
this._measurements['connection.downlink'] = { value: connection.downlink as number };
this._measurements['connection.downlink'] = { value: connection.downlink };
}
}

Expand Down Expand Up @@ -392,7 +395,7 @@ export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }
/**
* Checks if a given value is a valid measurement value.
*/
function isMeasurementValue(value: any): boolean {
function isMeasurementValue(value: unknown): value is number {
return typeof value === 'number' && isFinite(value);
}

Expand Down
10 changes: 7 additions & 3 deletions packages/tracing/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getCurrentHub, Hub } from '@sentry/hub';
import {
Event,
Measurements,
MeasurementUnit,
Transaction as TransactionInterface,
TransactionContext,
TransactionMetadata,
Expand Down Expand Up @@ -68,11 +69,14 @@ export class Transaction extends SpanClass implements TransactionInterface {
}

/**
* Set observed measurements for this transaction.
* Set observed measurement for this transaction.
* @param name Name of the measurement
* @param value Value of the measurement
* @param unit Unit of the measurement. (Defaults to 'ms' - milliseconds)
* @hidden
*/
public setMeasurements(measurements: Measurements): void {
this._measurements = { ...measurements };
public setMeasurement(name: string, value: number, unit: MeasurementUnit = 'ms'): void {
this._measurements[name] = { value, unit };
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export { Stacktrace } from './stacktrace';
export {
CustomSamplingContext,
Measurements,
MeasurementUnit,
SamplingContext,
TraceparentData,
Transaction,
Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export interface SamplingContext extends CustomSamplingContext {
request?: ExtractedNodeRequestData;
}

export type Measurements = Record<string, { value: number }>;
export type MeasurementUnit = 'ns' | 'ms' | 's';
export type Measurements = Record<string, { value: number; unit: MeasurementUnit }>;

export type TransactionSamplingMethod = 'explicitly_set' | 'client_sampler' | 'client_rate' | 'inheritance';

Expand Down

0 comments on commit 605fe42

Please sign in to comment.