Skip to content

Commit a45bdd1

Browse files
authored
fix(metrics): Ensure string values are interpreted for metrics (#12165)
Closes #12164
1 parent 6254629 commit a45bdd1

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

dev-packages/browser-integration-tests/suites/metrics/init.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Sentry.init({
77
});
88

99
Sentry.metrics.increment('increment');
10-
Sentry.metrics.increment('increment');
10+
Sentry.metrics.increment('increment', 2);
11+
Sentry.metrics.increment('increment', '3');
1112
Sentry.metrics.distribution('distribution', 42);
12-
Sentry.metrics.distribution('distribution', 45);
13+
Sentry.metrics.distribution('distribution', '45');
1314
Sentry.metrics.gauge('gauge', 5);
14-
Sentry.metrics.gauge('gauge', 15);
15+
Sentry.metrics.gauge('gauge', '15');
1516
Sentry.metrics.set('set', 'nope');
1617
Sentry.metrics.set('set', 'another');

dev-packages/browser-integration-tests/suites/metrics/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ sentryTest('collects metrics', async ({ getLocalTestUrl, page }) => {
1212
const normalisedStatsdString = statsdString.replace(/T\d+\n?/g, 'T000000');
1313

1414
expect(normalisedStatsdString).toEqual(
15-
'increment@none:2|c|T000000distribution@none:42:45|d|T000000gauge@none:15:5:15:20:2|g|T000000set@none:3387254:3443787523|s|T000000',
15+
'increment@none:6|c|T000000distribution@none:42:45|d|T000000gauge@none:15:5:15:20:2|g|T000000set@none:3387254:3443787523|s|T000000',
1616
);
1717
});

packages/core/src/metrics/exports.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function addToMetricsAggregator(
9090
* @experimental This API is experimental and might have breaking changes in the future.
9191
*/
9292
function increment(aggregator: MetricsAggregatorConstructor, name: string, value: number = 1, data?: MetricData): void {
93-
addToMetricsAggregator(aggregator, COUNTER_METRIC_TYPE, name, value, data);
93+
addToMetricsAggregator(aggregator, COUNTER_METRIC_TYPE, name, ensureNumber(value), data);
9494
}
9595

9696
/**
@@ -99,7 +99,7 @@ function increment(aggregator: MetricsAggregatorConstructor, name: string, value
9999
* @experimental This API is experimental and might have breaking changes in the future.
100100
*/
101101
function distribution(aggregator: MetricsAggregatorConstructor, name: string, value: number, data?: MetricData): void {
102-
addToMetricsAggregator(aggregator, DISTRIBUTION_METRIC_TYPE, name, value, data);
102+
addToMetricsAggregator(aggregator, DISTRIBUTION_METRIC_TYPE, name, ensureNumber(value), data);
103103
}
104104

105105
/**
@@ -117,7 +117,7 @@ function set(aggregator: MetricsAggregatorConstructor, name: string, value: numb
117117
* @experimental This API is experimental and might have breaking changes in the future.
118118
*/
119119
function gauge(aggregator: MetricsAggregatorConstructor, name: string, value: number, data?: MetricData): void {
120-
addToMetricsAggregator(aggregator, GAUGE_METRIC_TYPE, name, value, data);
120+
addToMetricsAggregator(aggregator, GAUGE_METRIC_TYPE, name, ensureNumber(value), data);
121121
}
122122

123123
export const metrics = {
@@ -130,3 +130,8 @@ export const metrics = {
130130
*/
131131
getMetricsAggregatorForClient,
132132
};
133+
134+
// Although this is typed to be a number, we try to handle strings as well here
135+
function ensureNumber(number: number | string): number {
136+
return typeof number === 'string' ? parseInt(number) : number;
137+
}

0 commit comments

Comments
 (0)