Skip to content

Commit

Permalink
fix(metrics): Ensure string values are interpreted for metrics (#12165)
Browse files Browse the repository at this point in the history
Closes #12164
  • Loading branch information
mydea authored May 23, 2024
1 parent 6254629 commit a45bdd1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 4 additions & 3 deletions dev-packages/browser-integration-tests/suites/metrics/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ Sentry.init({
});

Sentry.metrics.increment('increment');
Sentry.metrics.increment('increment');
Sentry.metrics.increment('increment', 2);
Sentry.metrics.increment('increment', '3');
Sentry.metrics.distribution('distribution', 42);
Sentry.metrics.distribution('distribution', 45);
Sentry.metrics.distribution('distribution', '45');
Sentry.metrics.gauge('gauge', 5);
Sentry.metrics.gauge('gauge', 15);
Sentry.metrics.gauge('gauge', '15');
Sentry.metrics.set('set', 'nope');
Sentry.metrics.set('set', 'another');
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ sentryTest('collects metrics', async ({ getLocalTestUrl, page }) => {
const normalisedStatsdString = statsdString.replace(/T\d+\n?/g, 'T000000');

expect(normalisedStatsdString).toEqual(
'increment@none:2|c|T000000distribution@none:42:45|d|T000000gauge@none:15:5:15:20:2|g|T000000set@none:3387254:3443787523|s|T000000',
'increment@none:6|c|T000000distribution@none:42:45|d|T000000gauge@none:15:5:15:20:2|g|T000000set@none:3387254:3443787523|s|T000000',
);
});
11 changes: 8 additions & 3 deletions packages/core/src/metrics/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function addToMetricsAggregator(
* @experimental This API is experimental and might have breaking changes in the future.
*/
function increment(aggregator: MetricsAggregatorConstructor, name: string, value: number = 1, data?: MetricData): void {
addToMetricsAggregator(aggregator, COUNTER_METRIC_TYPE, name, value, data);
addToMetricsAggregator(aggregator, COUNTER_METRIC_TYPE, name, ensureNumber(value), data);
}

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

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

export const metrics = {
Expand All @@ -130,3 +130,8 @@ export const metrics = {
*/
getMetricsAggregatorForClient,
};

// Although this is typed to be a number, we try to handle strings as well here
function ensureNumber(number: number | string): number {
return typeof number === 'string' ? parseInt(number) : number;
}

0 comments on commit a45bdd1

Please sign in to comment.