Skip to content

Commit 446a295

Browse files
authored
ref(types): Add type for measurement unit (#5313)
Folks gave some feedback that we should be explicit about the units we support. Let's add TypeScript types for these so we don't add any runtime logic, but it should have helpful auto-complete for users.
1 parent 8a8e489 commit 446a295

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

packages/tracing/src/browser/metrics/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function _trackCLS(): void {
5050
}
5151

5252
__DEBUG_BUILD__ && logger.log('[Measurements] Adding CLS');
53-
_measurements['cls'] = { value: metric.value, unit: 'none' };
53+
_measurements['cls'] = { value: metric.value, unit: '' };
5454
_clsEntry = entry as LayoutShift;
5555
});
5656
}

packages/tracing/src/transaction.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BaggageObj,
55
Event,
66
Measurements,
7+
MeasurementUnit,
78
Transaction as TransactionInterface,
89
TransactionContext,
910
TransactionMetadata,
@@ -77,7 +78,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
7778
/**
7879
* @inheritDoc
7980
*/
80-
public setMeasurement(name: string, value: number, unit: string = ''): void {
81+
public setMeasurement(name: string, value: number, unit: MeasurementUnit = ''): void {
8182
this._measurements[name] = { value, unit };
8283
}
8384

packages/types/src/event.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { Contexts } from './context';
44
import { DebugMeta } from './debugMeta';
55
import { Exception } from './exception';
66
import { Extras } from './extra';
7+
import { Measurements } from './measurement';
78
import { Primitive } from './misc';
89
import { Request } from './request';
910
import { CaptureContext } from './scope';
1011
import { SdkInfo } from './sdkinfo';
1112
import { Severity, SeverityLevel } from './severity';
1213
import { Span } from './span';
13-
import { Measurements } from './transaction';
1414
import { User } from './user';
1515

1616
/** JSDoc */

packages/types/src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,21 @@ export type { Stacktrace, StackParser, StackLineParser, StackLineParserFn } from
6565
export type { TextEncoderInternal } from './textencoder';
6666
export type {
6767
CustomSamplingContext,
68-
Measurements,
6968
SamplingContext,
7069
TraceparentData,
7170
Transaction,
7271
TransactionContext,
7372
TransactionMetadata,
7473
TransactionSamplingMethod,
7574
} from './transaction';
75+
export type {
76+
DurationUnit,
77+
InformationUnit,
78+
FractionUnit,
79+
MeasurementUnit,
80+
NoneUnit,
81+
Measurements,
82+
} from './measurement';
7683
export type { Thread } from './thread';
7784
export type {
7885
Transport,

packages/types/src/measurement.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Based on https://getsentry.github.io/relay/relay_metrics/enum.MetricUnit.html
2+
// For more details, see measurement key in https://develop.sentry.dev/sdk/event-payloads/transaction/
3+
4+
/**
5+
* A time duration.
6+
*/
7+
export type DurationUnit = 'nanosecond' | 'microsecond' | 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week';
8+
9+
/**
10+
* Size of information derived from bytes.
11+
*/
12+
export type InformationUnit =
13+
| 'bit'
14+
| 'byte'
15+
| 'kilobyte'
16+
| 'kibibyte'
17+
| 'megabyte'
18+
| 'mebibyte'
19+
| 'gigabyte'
20+
| 'terabyte'
21+
| 'tebibyte'
22+
| 'petabyte'
23+
| 'exabyte'
24+
| 'exbibyte';
25+
26+
/**
27+
* Fractions such as percentages.
28+
*/
29+
export type FractionUnit = 'ratio' | 'percent';
30+
31+
/**
32+
* Untyped value without a unit.
33+
*/
34+
export type NoneUnit = '' | 'none';
35+
36+
// See https://github.com/microsoft/TypeScript/issues/29729#issuecomment-1082546550
37+
// Needed to make sure auto-complete will work for the string union type while still
38+
// allowing for arbitrary strings as custom units (user-defined units without builtin conversion or default).
39+
type LiteralUnion<T extends string> = T | Omit<T, T>;
40+
41+
export type MeasurementUnit = LiteralUnion<DurationUnit | InformationUnit | FractionUnit | NoneUnit>;
42+
43+
export type Measurements = Record<string, { value: number; unit: MeasurementUnit }>;

packages/types/src/transaction.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Baggage } from './baggage';
2+
import { MeasurementUnit } from './measurement';
23
import { ExtractedNodeRequestData, Primitive, WorkerLocation } from './misc';
34
import { Span, SpanContext } from './span';
45
/**
@@ -80,7 +81,7 @@ export interface Transaction extends TransactionContext, Span {
8081
* @param value Value of the measurement
8182
* @param unit Unit of the measurement. (Defaults to an empty string)
8283
*/
83-
setMeasurement(name: string, value: number, unit: string): void;
84+
setMeasurement(name: string, value: number, unit: MeasurementUnit): void;
8485

8586
/** Returns the current transaction properties as a `TransactionContext` */
8687
toContext(): TransactionContext;
@@ -127,8 +128,6 @@ export interface SamplingContext extends CustomSamplingContext {
127128
request?: ExtractedNodeRequestData;
128129
}
129130

130-
export type Measurements = Record<string, { value: number; unit: string }>;
131-
132131
export type TransactionSamplingMethod = 'explicitly_set' | 'client_sampler' | 'client_rate' | 'inheritance';
133132

134133
export interface TransactionMetadata {

0 commit comments

Comments
 (0)