Skip to content

Commit e3936c4

Browse files
committed
refactor: constants for magic numbers
1 parent 1f51bb4 commit e3936c4

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { EnvironmentVariablesService } from './config/EnvironmentVariablesServic
1010
import {
1111
COLD_START_METRIC,
1212
DEFAULT_NAMESPACE,
13+
EMF_MAX_TIMESTAMP_FUTURE_AGE,
14+
EMF_MAX_TIMESTAMP_PAST_AGE,
1315
MAX_DIMENSION_COUNT,
1416
MAX_METRICS_SIZE,
1517
MAX_METRIC_VALUES_SIZE,
@@ -975,9 +977,6 @@ class Metrics extends Utility implements MetricsInterface {
975977
* @param timestamp - Date object or epoch time in milliseconds representing the timestamp to validate.
976978
*/
977979
#validateEmfTimestamp(timestamp: number | Date): boolean {
978-
const EMF_MAX_TIMESTAMP_PAST_AGE = 14 * 24 * 60 * 60 * 1000; // 14 days in milliseconds
979-
const EMF_MAX_TIMESTAMP_FUTURE_AGE = 2 * 60 * 60 * 1000; // 2 hours in milliseconds
980-
981980
if (!isDate(timestamp) && !isIntegerNumber(timestamp)) {
982981
return false;
983982
}

packages/metrics/src/constants.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const MAX_METRIC_VALUES_SIZE = 100;
1818
* The maximum number of dimensions that can be added to a metric (0-indexed).
1919
*/
2020
const MAX_DIMENSION_COUNT = 29;
21+
/**
22+
* The maximum age of a timestamp in milliseconds that can be emitted in a metric.
23+
* This is set to 14 days.
24+
*/
25+
const EMF_MAX_TIMESTAMP_PAST_AGE = 14 * 24 * 60 * 60 * 1000;
26+
/**
27+
* The maximum age of a timestamp in milliseconds that can be emitted in a metric.
28+
* This is set to 2 hours.
29+
*/
30+
const EMF_MAX_TIMESTAMP_FUTURE_AGE = 2 * 60 * 60 * 1000;
2131

2232
/**
2333
* The unit of the metric.
@@ -73,4 +83,6 @@ export {
7383
MAX_DIMENSION_COUNT,
7484
MetricUnit,
7585
MetricResolution,
86+
EMF_MAX_TIMESTAMP_PAST_AGE,
87+
EMF_MAX_TIMESTAMP_FUTURE_AGE,
7688
};

packages/metrics/tests/unit/Metrics.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariabl
1010
import {
1111
COLD_START_METRIC,
1212
DEFAULT_NAMESPACE,
13+
EMF_MAX_TIMESTAMP_FUTURE_AGE,
14+
EMF_MAX_TIMESTAMP_PAST_AGE,
1315
MAX_DIMENSION_COUNT,
1416
MAX_METRICS_SIZE,
1517
MAX_METRIC_VALUES_SIZE,
@@ -2269,9 +2271,6 @@ describe('Class: Metrics', () => {
22692271

22702272
for (const { format, getTimestamp } of testCases) {
22712273
describe(`when timestamp is provided as ${format}`, () => {
2272-
const twoHours = 2 * 60 * 60 * 1000;
2273-
const fourteenDays = 14 * 24 * 60 * 60 * 1000;
2274-
22752274
test('should set the timestamp if provided in the future', () => {
22762275
// Prepare
22772276
const testMetric = 'test-metric';
@@ -2314,10 +2313,10 @@ describe('Class: Metrics', () => {
23142313
);
23152314
});
23162315

2317-
test('should not log a warning if the timestamp exact two hours in the future and set the timestamp', () => {
2316+
test('should not log a warning if the timestamp is withing valid future range', () => {
23182317
// Prepare
23192318
const testMetric = 'test-metric';
2320-
const timestampMs = mockDate.getTime() + twoHours;
2319+
const timestampMs = mockDate.getTime() + EMF_MAX_TIMESTAMP_FUTURE_AGE;
23212320
const customLogger = {
23222321
warn: jest.fn(),
23232322
debug: jest.fn(),
@@ -2343,10 +2342,11 @@ describe('Class: Metrics', () => {
23432342
);
23442343
});
23452344

2346-
test('should log a warning if the timestamp is more than two hours in the future but still set the timestamp', () => {
2345+
test('should log a warning if the timestamp is more than the future range but still set the timestamp', () => {
23472346
// Prepare
23482347
const testMetric = 'test-metric';
2349-
const timestampMs = mockDate.getTime() + twoHours + 1;
2348+
const timestampMs =
2349+
mockDate.getTime() + EMF_MAX_TIMESTAMP_FUTURE_AGE + 1;
23502350
const customLogger = {
23512351
warn: jest.fn(),
23522352
debug: jest.fn(),
@@ -2375,10 +2375,10 @@ describe('Class: Metrics', () => {
23752375
);
23762376
});
23772377

2378-
test('should not log a warning if the timestamp is exact 14 days in the past and set the timestamp', () => {
2378+
test('should not log a warning if the timestamp is within past range and set the timestamp', () => {
23792379
// Prepare
23802380
const testMetric = 'test-metric';
2381-
const timestampMs = mockDate.getTime() - fourteenDays;
2381+
const timestampMs = mockDate.getTime() - EMF_MAX_TIMESTAMP_PAST_AGE;
23822382
const customLogger = {
23832383
warn: jest.fn(),
23842384
debug: jest.fn(),
@@ -2404,10 +2404,11 @@ describe('Class: Metrics', () => {
24042404
);
24052405
});
24062406

2407-
test('should log a warning if the timestamp is more than 14 days in the past but still set the timestamp', () => {
2407+
test('should log a warning if the timestamp is more than past range but still set the timestamp', () => {
24082408
// Prepare
24092409
const testMetric = 'test-metric';
2410-
const timestampMs = mockDate.getTime() - fourteenDays - 1;
2410+
const timestampMs =
2411+
mockDate.getTime() - EMF_MAX_TIMESTAMP_PAST_AGE - 1;
24112412
const customLogger = {
24122413
warn: jest.fn(),
24132414
debug: jest.fn(),

0 commit comments

Comments
 (0)