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.

feat(cdp): label push registrations rejected by a capture billing limit (#115625)

Daniel Marchuke1cfe3bd9ea9
diff --git a/nodejs/src/messaging/push-subscriptions/push-capture.test.ts b/nodejs/src/messaging/push-subscriptions/push-capture.test.ts
index 7ccac173cd31..02ed10117817 100644
--- a/nodejs/src/messaging/push-subscriptions/push-capture.test.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-capture.test.ts
@@ -3,7 +3,7 @@ import { AddressInfo } from 'net'
import { parseJSON } from '~/common/utils/json-parse'
-import { PushCaptureService } from './push-capture'
+import { CaptureBillingLimitError, PushCaptureService } from './push-capture'
describe('PushCaptureService', () => {
let server: Server
@@ -108,15 +108,18 @@ describe('PushCaptureService', () => {
})
it.each([
- ['a client error', 400, 1],
- ['an auth error', 401, 1],
- ['a rate limit, which django does not retry either', 429, 1],
- ['a server error that persists', 500, 4],
- ['an unavailable capture that persists', 503, 4],
- ])('throws on %s so the SDK is not told it stored', async (_name, code, requests) => {
+ ['a client error', 400, 1, Error],
+ ['an auth error', 401, 1, Error],
+ ['a billing limit', 402, 1, CaptureBillingLimitError],
+ ['a rate limit, which django does not retry either', 429, 1, Error],
+ ['a server error that persists', 500, 4, Error],
+ ['an unavailable capture that persists', 503, 4, Error],
+ ])('throws on %s so the SDK is not told it stored', async (_name, code, requests, errorClass) => {
status = code
- await expect(send()).rejects.toThrow(`capture returned ${code}`)
+ const error = await send().catch((e: unknown) => e)
+ expect(error).toBeInstanceOf(errorClass)
+ expect(error).toMatchObject({ message: `capture returned ${code}` })
expect(received).toHaveLength(requests)
})
diff --git a/nodejs/src/messaging/push-subscriptions/push-capture.ts b/nodejs/src/messaging/push-subscriptions/push-capture.ts
index 2cfe75052b77..9d800c199327 100644
--- a/nodejs/src/messaging/push-subscriptions/push-capture.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-capture.ts
@@ -45,6 +45,14 @@ export type PushCaptureEvent = {
properties: Record<string, any>
}
+/** Capture refuses every event for a project over its billing limit, until the limit lifts. */
+export class CaptureBillingLimitError extends Error {
+ constructor() {
+ super('capture returned 402')
+ this.name = 'CaptureBillingLimitError'
+ }
+}
+
export class PushCaptureService {
private origin: string
@@ -84,6 +92,10 @@ export class PushCaptureService {
for (let attempt = 1; ; attempt++) {
const response = await this.post(event.token, body, attempt)
+ if (response.status === 402) {
+ captureCounter.inc({ outcome: 'billing_limited' })
+ throw new CaptureBillingLimitError()
+ }
if (response.status < 200 || response.status >= 300) {
captureCounter.inc({ outcome: 'rejected' })
logger.warn('push_subscription_capture_rejected', { status: response.status })
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 4aff986f5662..1a5143171d8f 100644
--- a/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.test.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.test.ts
@@ -1,5 +1,6 @@
import { gzipSync } from 'zlib'
+import { CaptureBillingLimitError } from './push-capture'
import { PushSubscriptionsService } from './push-subscriptions.service'
describe('PushSubscriptionsService', () => {
@@ -89,15 +90,18 @@ describe('PushSubscriptionsService', () => {
})
})
- it('reports a rejected capture rather than telling the SDK the token was stored', async () => {
+ 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) => {
// 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(new Error('capture returned 503'))
+ capture.capture.mockRejectedValue(error)
const result = await service.handle(request(valid))
expect(result.status).toEqual(500)
- expect(result.body).toMatchObject({ code: 'capture_failed' })
+ expect(result.body).toMatchObject({ code })
})
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 4d65ddcb7b07..1fce35017d4f 100644
--- a/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts
+++ b/nodejs/src/messaging/push-subscriptions/push-subscriptions.service.ts
@@ -7,7 +7,7 @@ import { PostgresRouter, PostgresUse } from '~/common/utils/db/postgres'
import { verifyPushIdentityToken } from './identity-token'
import { ProjectTokenLookup } from './project-token-lookup'
-import { PushCaptureService } from './push-capture'
+import { CaptureBillingLimitError, PushCaptureService } from './push-capture'
import { InflatedBodyTooLargeError, RawRequest, decodeRequest } from './request-decoding'
export type PushRejectionCode =
@@ -19,6 +19,7 @@ export type PushRejectionCode =
| 'missing_fields'
| 'identity_verification_failed'
| 'capture_failed'
+ | 'capture_billing_limited'
export type PushErrorType = 'validation_error' | 'authentication_error' | 'server_error'
@@ -260,7 +261,8 @@ export class PushSubscriptionsService {
} catch (error) {
return {
...reject(
- 'capture_failed',
+ // 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'