Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0.0',
environment: 'test',
integrations: integrations => {
return integrations.filter(integration => integration.name !== 'BrowserSession');
},
beforeSendMetric: metric => {
if (metric.name === 'test.counter') {
return {
...metric,
attributes: {
...metric.attributes,
modified: 'by-beforeSendMetric',
original: undefined,
},
};
}
return metric;
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Store captured metrics from the afterCaptureMetric event
window.capturedMetrics = [];

const client = Sentry.getClient();

client.on('afterCaptureMetric', metric => {
window.capturedMetrics.push(metric);
});

// Capture metrics - these should be processed by beforeSendMetric
Sentry.metrics.count('test.counter', 1, { attributes: { endpoint: '/api/test', original: 'value' } });
Sentry.metrics.gauge('test.gauge', 42, { unit: 'millisecond', attributes: { server: 'test-1' } });

Sentry.flush();
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../../utils/fixtures';

sentryTest(
'should emit afterCaptureMetric event with processed metric from beforeSendMetric',
async ({ getLocalTestUrl, page }) => {
const bundle = process.env.PW_BUNDLE || '';
if (bundle.startsWith('bundle') || bundle.startsWith('loader')) {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

await page.waitForFunction(() => {
return (window as any).capturedMetrics.length >= 2;
});

const capturedMetrics = await page.evaluate(() => {
return (window as any).capturedMetrics;
});

expect(capturedMetrics).toHaveLength(2);

// Verify the counter metric was modified by beforeSendMetric
expect(capturedMetrics[0]).toMatchObject({
name: 'test.counter',
type: 'counter',
value: 1,
attributes: {
endpoint: '/api/test',
modified: 'by-beforeSendMetric',
'sentry.release': '1.0.0',
'sentry.environment': 'test',
'sentry.sdk.name': 'sentry.javascript.browser',
},
});

// Verify the 'original' attribute was removed by beforeSendMetric
expect(capturedMetrics[0].attributes.original).toBeUndefined();

// Verify the gauge metric was not modified (no beforeSendMetric processing)
expect(capturedMetrics[1]).toMatchObject({
name: 'test.gauge',
type: 'gauge',
unit: 'millisecond',
value: 42,
attributes: {
server: 'test-1',
'sentry.release': '1.0.0',
'sentry.environment': 'test',
'sentry.sdk.name': 'sentry.javascript.browser',
},
});

expect(capturedMetrics[0].attributes['sentry.sdk.version']).toBeDefined();
expect(capturedMetrics[1].attributes['sentry.sdk.version']).toBeDefined();
},
);
2 changes: 1 addition & 1 deletion packages/core/src/metrics/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function _INTERNAL_captureMetric(beforeMetric: Metric, options?: Internal

captureSerializedMetric(client, serializedMetric);

client.emit('afterCaptureMetric', enrichedMetric);
client.emit('afterCaptureMetric', processedMetric);
}

/**
Expand Down
Loading