Skip to content

Commit 50fe7ff

Browse files
fix(analytics): make ctx.requestId authoritative + assert single emit
CodeRabbit findings on PR #3659: - captureException additionalProperties construction let ctx.properties.requestId override ctx.requestId. Move requestId spread after ctx.properties so the explicit ctx.requestId wins. - logger.posthog.transport.integration.tests.js asserted payload shape but not call count. Add toHaveBeenCalledTimes(1). +1 unit test for ctx.requestId precedence.
1 parent 0d91834 commit 50fe7ff

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/services/analytics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ const captureException = (err, ctx = {}) => {
174174
const distinctId = ctx.distinctId || 'anonymous';
175175
const explicit = ctx.source ? { source: ctx.source } : {};
176176
const additionalProperties = {
177-
requestId: ctx.requestId,
178177
source: 'system',
179178
...(ctx.properties ?? {}),
179+
...(ctx.requestId !== undefined ? { requestId: ctx.requestId } : {}),
180180
...explicit,
181181
};
182182
client.captureException(err, distinctId, additionalProperties);

lib/services/tests/analytics.captureException.unit.tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,17 @@ describe('Analytics captureException():', () => {
108108
expect.objectContaining({ requestId: 'req-xyz' }),
109109
);
110110
});
111+
112+
test('ctx.requestId wins over ctx.properties.requestId (authoritative)', () => {
113+
const err = new Error('req-precedence');
114+
AnalyticsService.captureException(err, {
115+
requestId: 'authoritative-req',
116+
properties: { requestId: 'should-be-overridden' },
117+
});
118+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
119+
err,
120+
'anonymous',
121+
expect.objectContaining({ requestId: 'authoritative-req' }),
122+
);
123+
});
111124
});

lib/services/tests/logger.posthog.transport.integration.tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('logger.error → PostHog $exception (integration):', () => {
3737
test('logger.error(message, { error }) emits a single $exception event via SDK captureException', () => {
3838
const err = new Error('payment failed');
3939
logger.error('Charge failed for user', { error: err });
40+
expect(mockPostHogInstance.captureException).toHaveBeenCalledTimes(1);
4041
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(
4142
err,
4243
'anonymous',

0 commit comments

Comments
 (0)