Skip to content

Commit 1b76a22

Browse files
committed
Respect telemetry setting and await PostHog flush
Check telemetryEnabled before sending exceptions and return early when disabled. Use client.flush() (awaited in a try/catch) to ensure events are flushed so callers can pass the promise to waitUntil without impacting request handling. Also harden flushAllPostHogClients by clearing the clients map in a finally block so clients are released even if shutdown rejects.
1 parent b350286 commit 1b76a22

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

packages/server/src/lib/logger.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Context } from 'hono';
22
import type { AppEnv, CloudflareBindings } from '../env.js';
3-
import { getPostHogClient } from './posthog.js';
3+
import { getPostHogClient, telemetryEnabled } from './posthog.js';
44

55
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
66
type LogFields = Record<string, unknown>;
@@ -324,22 +324,21 @@ export interface CaptureExceptionOptions {
324324
/**
325325
* Sends a `$exception` event to PostHog Error Tracking via the PostHog SDK.
326326
*
327-
* The SDK handles serialization correctly, avoiding schema mismatches like the
328-
* `timestamp` top-level field bug that raw fetch calls cause.
327+
* The returned promise resolves after the SDK has flushed the event, so call
328+
* sites can pass it to `waitUntil` to keep the isolate alive until delivery.
329329
*/
330330
export async function captureException(
331331
env: CloudflareBindings,
332332
error: unknown,
333333
options: CaptureExceptionOptions = {},
334334
): Promise<void> {
335+
if (!telemetryEnabled(env)) return;
335336
const apiKey = env.POSTHOG_API_KEY;
336337
if (!apiKey) return;
337338

338339
const exceptionList = buildExceptionList(error);
339340
const client = getPostHogClient(env, apiKey);
340341

341-
// Build a flat properties object for the SDK's captureException.
342-
// The SDK handles setting event: '$exception' and proper serialization.
343342
const additionalProperties: Record<string, unknown> = {
344343
$exception_list: exceptionList,
345344
$exception_type: exceptionList[0]?.type,
@@ -352,4 +351,9 @@ export async function captureException(
352351
};
353352

354353
client.captureException(error, options.distinctId ?? SERVICE_NAME, additionalProperties);
354+
try {
355+
await client.flush();
356+
} catch {
357+
// Best effort — never break request handling for telemetry.
358+
}
355359
}

packages/server/src/lib/posthog.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export function getPostHogClient(env: CloudflareBindings, apiKey: string): PostH
4242
export { telemetryEnabled };
4343

4444
export async function flushAllPostHogClients(): Promise<void> {
45-
await Promise.all([...clients.values()].map(({ client }) => client.shutdown()));
46-
clients.clear();
45+
try {
46+
await Promise.all([...clients.values()].map(({ client }) => client.shutdown()));
47+
} finally {
48+
clients.clear();
49+
}
4750
}

0 commit comments

Comments
 (0)