Skip to content

Commit 7d69b12

Browse files
committed
use MeasurementUnit (aka Relay's MetricUnit)
1 parent 779e053 commit 7d69b12

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

packages/core/src/attributes.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DEBUG_BUILD } from './debug-build';
2+
import type { MeasurementUnit } from './types-hoist/measurement';
23
import { debug } from './utils/debug-logger';
34

45
export type RawAttributes<T> = T & ValidatedAttributes<T>;
@@ -32,18 +33,13 @@ type AttributeUnion = {
3233
};
3334
}[keyof AttributeTypeMap];
3435

35-
export type TypedAttributeValue = AttributeUnion & { unit?: Units };
36+
export type TypedAttributeValue = AttributeUnion & { unit?: MeasurementUnit };
3637

3738
export type AttributeObject = {
3839
value: unknown;
39-
unit?: Units;
40+
unit?: MeasurementUnit;
4041
};
4142

42-
/**
43-
* Unit of measurement that can be added to an attribute.
44-
*/
45-
type Units = 'ms' | 's' | 'bytes' | 'count' | 'percent' | string;
46-
4743
/* If an attribute has either a 'value' or 'unit' property, we use the ValidAttributeObject type. */
4844
export type ValidatedAttributes<T> = {
4945
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/core/test/lib/scope.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,30 @@ describe('Scope', () => {
215215

216216
it('accepts an attribute value object with a unit', () => {
217217
const scope = new Scope();
218-
scope.setAttribute('str', { value: 1, unit: 'ms' });
218+
scope.setAttribute('str', { value: 1, unit: 'millisecond' });
219219
expect(scope['_attributes']).toEqual({
220-
str: { value: 1, unit: 'ms' },
220+
str: { value: 1, unit: 'millisecond' },
221+
});
222+
});
223+
224+
it('accepts a custom unit', () => {
225+
// mostly there for type checking purposes.
226+
const scope = new Scope();
227+
scope.setAttribute('str', { value: 3, unit: 'inch' });
228+
expect(scope['_attributes']).toEqual({
229+
str: { value: 3, unit: 'inch' },
221230
});
222231
});
223232

224233
it('accepts an array', () => {
225234
const scope = new Scope();
226235

227236
scope.setAttribute('strArray', ['a', 'b', 'c']);
228-
scope.setAttribute('intArray', { value: [1, 2, 3], unit: 'ms' });
237+
scope.setAttribute('intArray', { value: [1, 2, 3], unit: 'millisecond' });
229238

230239
expect(scope['_attributes']).toEqual({
231240
strArray: ['a', 'b', 'c'],
232-
intArray: { value: [1, 2, 3], unit: 'ms' },
241+
intArray: { value: [1, 2, 3], unit: 'millisecond' },
233242
});
234243
});
235244

@@ -266,23 +275,23 @@ describe('Scope', () => {
266275

267276
it('accepts attribute value objects with units', () => {
268277
const scope = new Scope();
269-
scope.setAttributes({ str: { value: 'b', unit: 'ms' }, int: { value: 12, unit: 's' } });
278+
scope.setAttributes({ str: { value: 'b', unit: 'millisecond' }, int: { value: 12, unit: 'second' } });
270279
expect(scope['_attributes']).toEqual({
271-
str: { value: 'b', unit: 'ms' },
272-
int: { value: 12, unit: 's' },
280+
str: { value: 'b', unit: 'millisecond' },
281+
int: { value: 12, unit: 'second' },
273282
});
274283
});
275284

276285
it('accepts arrays', () => {
277286
const scope = new Scope();
278287
scope.setAttributes({
279288
strArray: ['a', 'b', 'c'],
280-
intArray: { value: [1, 2, 3], unit: 'ms' },
289+
intArray: { value: [1, 2, 3], unit: 'millisecond' },
281290
});
282291

283292
expect(scope['_attributes']).toEqual({
284293
strArray: ['a', 'b', 'c'],
285-
intArray: { value: [1, 2, 3], unit: 'ms' },
294+
intArray: { value: [1, 2, 3], unit: 'millisecond' },
286295
});
287296
});
288297

0 commit comments

Comments
 (0)