Skip to content

Commit 8efeeeb

Browse files
authored
fix(core): Emit processed metric (#18222)
We were emitting the non-processed metric in the hook before – I changed this behaviour + added a test to verify.
1 parent ad0ce51 commit 8efeeeb

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
release: '1.0.0',
8+
environment: 'test',
9+
integrations: integrations => {
10+
return integrations.filter(integration => integration.name !== 'BrowserSession');
11+
},
12+
beforeSendMetric: metric => {
13+
if (metric.name === 'test.counter') {
14+
return {
15+
...metric,
16+
attributes: {
17+
...metric.attributes,
18+
modified: 'by-beforeSendMetric',
19+
original: undefined,
20+
},
21+
};
22+
}
23+
return metric;
24+
},
25+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Store captured metrics from the afterCaptureMetric event
2+
window.capturedMetrics = [];
3+
4+
const client = Sentry.getClient();
5+
6+
client.on('afterCaptureMetric', metric => {
7+
window.capturedMetrics.push(metric);
8+
});
9+
10+
// Capture metrics - these should be processed by beforeSendMetric
11+
Sentry.metrics.count('test.counter', 1, { attributes: { endpoint: '/api/test', original: 'value' } });
12+
Sentry.metrics.gauge('test.gauge', 42, { unit: 'millisecond', attributes: { server: 'test-1' } });
13+
14+
Sentry.flush();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../utils/fixtures';
3+
4+
sentryTest(
5+
'should emit afterCaptureMetric event with processed metric from beforeSendMetric',
6+
async ({ getLocalTestUrl, page }) => {
7+
const bundle = process.env.PW_BUNDLE || '';
8+
if (bundle.startsWith('bundle') || bundle.startsWith('loader')) {
9+
sentryTest.skip();
10+
}
11+
12+
const url = await getLocalTestUrl({ testDir: __dirname });
13+
await page.goto(url);
14+
15+
await page.waitForFunction(() => {
16+
return (window as any).capturedMetrics.length >= 2;
17+
});
18+
19+
const capturedMetrics = await page.evaluate(() => {
20+
return (window as any).capturedMetrics;
21+
});
22+
23+
expect(capturedMetrics).toHaveLength(2);
24+
25+
// Verify the counter metric was modified by beforeSendMetric
26+
expect(capturedMetrics[0]).toMatchObject({
27+
name: 'test.counter',
28+
type: 'counter',
29+
value: 1,
30+
attributes: {
31+
endpoint: '/api/test',
32+
modified: 'by-beforeSendMetric',
33+
'sentry.release': '1.0.0',
34+
'sentry.environment': 'test',
35+
'sentry.sdk.name': 'sentry.javascript.browser',
36+
},
37+
});
38+
39+
// Verify the 'original' attribute was removed by beforeSendMetric
40+
expect(capturedMetrics[0].attributes.original).toBeUndefined();
41+
42+
// Verify the gauge metric was not modified (no beforeSendMetric processing)
43+
expect(capturedMetrics[1]).toMatchObject({
44+
name: 'test.gauge',
45+
type: 'gauge',
46+
unit: 'millisecond',
47+
value: 42,
48+
attributes: {
49+
server: 'test-1',
50+
'sentry.release': '1.0.0',
51+
'sentry.environment': 'test',
52+
'sentry.sdk.name': 'sentry.javascript.browser',
53+
},
54+
});
55+
56+
expect(capturedMetrics[0].attributes['sentry.sdk.version']).toBeDefined();
57+
expect(capturedMetrics[1].attributes['sentry.sdk.version']).toBeDefined();
58+
},
59+
);

packages/core/src/metrics/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export function _INTERNAL_captureMetric(beforeMetric: Metric, options?: Internal
243243

244244
captureSerializedMetric(client, serializedMetric);
245245

246-
client.emit('afterCaptureMetric', enrichedMetric);
246+
client.emit('afterCaptureMetric', processedMetric);
247247
}
248248

249249
/**

0 commit comments

Comments
 (0)