Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(metrics): Ensure string values are interpreted for metrics #12165

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading