Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cdp): answer billing-limited push registrations with 429 and retry-after (#117695)

Daniel Marchuk054b598b8850
diff --git a/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.test.ts b/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.test.ts
index b43fc9ec5839..cce4768e2f1e 100644
--- a/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.test.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.test.ts
@@ -215,6 +215,20 @@ describe('push subscriptions http', () => {
})
})
+ it('sends the headers the service set', async () => {
+ answer = {
+ status: 429,
+ body: { type: 'throttled_error', code: 'capture_billing_limited', detail: 'Failed.', attr: null },
+ headers: { 'Retry-After': '3600' },
+ }
+
+ const response = await post('/api/push_subscriptions/', { body: '{}' })
+
+ expect(response.status).toEqual(429)
+ expect(response.headers['retry-after']).toEqual('3600')
+ expect(response.headers['content-type']).toEqual('application/json')
+ })
+
it('counts a request the service throws on as a 500', async () => {
const failedRequests = async (): Promise<number> => {
const metric = await register.getSingleMetric('push_subscription_request_duration_seconds')!.get()
diff --git a/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.ts b/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.ts
index ab6ae8df0650..af5216b9dcf8 100644
--- a/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-subscriptions-http.ts
@@ -156,7 +156,7 @@ export function createPushSubscriptionsHandler(
res.writeHead(403, { 'Content-Type': 'text/html; charset=utf-8' }).end(REGION_BLOCKED_MESSAGE)
return
}
- res.writeHead(result.status, { 'Content-Type': 'application/json' })
+ res.writeHead(result.status, { ...result.headers, 'Content-Type': 'application/json' })
res.end(JSON.stringify(result.body))
}
}
diff --git a/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.test.ts b/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.test.ts
index 1a5143171d8f..a4d5e6e7dfa8 100644
--- a/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.test.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.test.ts
@@ -91,17 +91,23 @@ describe('PushSubscriptionsService', () => {
})
it.each([
- ['a failed capture', new Error('capture returned 503'), 'capture_failed'],
- ['a project over its billing limit', new CaptureBillingLimitError(), 'capture_billing_limited'],
- ])('reports %s rather than telling the SDK the token was stored', async (_name, error, code) => {
+ ['a failed capture', new Error('capture returned 503'), 500, 'capture_failed', undefined],
+ [
+ 'a project over its billing limit',
+ new CaptureBillingLimitError(),
+ 429,
+ 'capture_billing_limited',
+ { 'Retry-After': '3600' },
+ ],
+ ])('reports %s rather than telling the SDK the token was stored', async (_name, error, status, code, headers) => {
// The SDK marks a registration delivered on a 2xx and stops re-sending it, so a capture that
// did not happen has to surface as an error or the device is never registered.
capture.capture.mockRejectedValue(error)
const result = await service.handle(request(valid))
- expect(result.status).toEqual(500)
- expect(result.body).toMatchObject({ code })
+ expect(result).toMatchObject({ status, body: { code } })
+ expect(result.headers).toEqual(headers)
})
it('reports a capture that threw', async () => {
diff --git a/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts b/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts
index 1fce35017d4f..c07a4558ecf7 100644
--- a/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts
@@ -21,7 +21,7 @@ export type PushRejectionCode =
| 'capture_failed'
| 'capture_billing_limited'
-export type PushErrorType = 'validation_error' | 'authentication_error' | 'server_error'
+export type PushErrorType = 'validation_error' | 'authentication_error' | 'throttled_error' | 'server_error'
/** The body shape Django's `generate_exception_response` produces, which SDKs already parse. */
export type PushErrorBody = {
@@ -42,6 +42,7 @@ export type PushSuccessBody = {
export type PushHandlerResult = {
status: number
body: PushErrorBody | PushSuccessBody
+ headers?: Record<string, string>
/** Set on a rejection so the caller can log and count it with the same fields Django uses. */
rejection?: {
code: PushRejectionCode
@@ -61,6 +62,8 @@ export type PushHandlerResult = {
export const MAX_BODY_BYTES = 16 * 1024
+const BILLING_LIMITED_RETRY_AFTER_SECONDS = 3600
+
const VERIFICATION_MODE_PRECEDENCE: Record<string, number> = { disabled: 0, optional: 1, required: 2 }
type PushIntegration = { config: Record<string, any> }
@@ -259,17 +262,20 @@ export class PushSubscriptionsService {
properties: { ...properties, $process_person_profile: true },
})
} catch (error) {
+ const detail =
+ request.method === 'POST' ? 'Failed to store push subscription.' : 'Failed to remove push subscription.'
+ if (error instanceof CaptureBillingLimitError) {
+ // SDKs keep the registration or the logout for a later retry on a 429 and a 5xx, and drop it
+ // on any other 4xx. Retry-After holds the retries back for the rest of the session, because
+ // the billing limit does not lift in minutes.
+ return {
+ ...reject('capture_billing_limited', 429, 'throttled_error', detail, { teamId: team.id, appId }),
+ headers: { 'Retry-After': String(BILLING_LIMITED_RETRY_AFTER_SECONDS) },
+ identityVerification,
+ }
+ }
return {
- ...reject(
- // A code of its own lets the error-rate alert leave out projects over their billing limit.
- error instanceof CaptureBillingLimitError ? 'capture_billing_limited' : 'capture_failed',
- 500,
- 'server_error',
- request.method === 'POST'
- ? 'Failed to store push subscription.'
- : 'Failed to remove push subscription.',
- { teamId: team.id, appId, error }
- ),
+ ...reject('capture_failed', 500, 'server_error', detail, { teamId: team.id, appId, error }),
identityVerification,
}
}