Fix telemetry shutdown noise and improve token usage diagnostics (#444)
Co-authored-by: zuolingxuan <zuolingxuan@bytedance.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
01adc828b5
commit
995a7f070d
@@ -6,10 +6,32 @@ import { logger } from './logger';
|
||||
|
||||
const POSTHOG_API_KEY = 'phc_aGNegeJQP5FzNiF2rEoKqQbkuCpiiETMttplibXpB0n';
|
||||
const POSTHOG_HOST = 'https://us.i.posthog.com';
|
||||
const TELEMETRY_SHUTDOWN_TIMEOUT_MS = 1500;
|
||||
|
||||
let posthogClient: PostHog | null = null;
|
||||
let distinctId: string = '';
|
||||
|
||||
function isIgnorablePostHogShutdownError(error: unknown): boolean {
|
||||
if (!(error instanceof Error)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const message = `${error.name} ${error.message}`.toLowerCase();
|
||||
if (
|
||||
message.includes('posthogfetchnetworkerror') ||
|
||||
message.includes('network error while fetching posthog') ||
|
||||
message.includes('timeouterror') ||
|
||||
message.includes('aborted due to timeout') ||
|
||||
message.includes('fetch failed')
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'cause' in error && error.cause !== error
|
||||
? isIgnorablePostHogShutdownError(error.cause)
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize PostHog telemetry
|
||||
*/
|
||||
@@ -65,15 +87,51 @@ export async function initTelemetry(): Promise<void> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure PostHog flushes all pending events before shutting down
|
||||
* Best-effort telemetry shutdown that never blocks app exit on network issues.
|
||||
*/
|
||||
export async function shutdownTelemetry(): Promise<void> {
|
||||
if (posthogClient) {
|
||||
try {
|
||||
await posthogClient.shutdown();
|
||||
logger.debug('Flushed telemetry events on shutdown');
|
||||
} catch (error) {
|
||||
logger.error('Error shutting down telemetry:', error);
|
||||
const client = posthogClient;
|
||||
posthogClient = null;
|
||||
distinctId = '';
|
||||
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
let didTimeout = false;
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
|
||||
const shutdownPromise = client.shutdown().catch((error) => {
|
||||
if (isIgnorablePostHogShutdownError(error)) {
|
||||
logger.debug('Ignored telemetry shutdown network error:', error);
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
|
||||
try {
|
||||
await Promise.race([
|
||||
shutdownPromise,
|
||||
new Promise<void>((resolve) => {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
didTimeout = true;
|
||||
resolve();
|
||||
}, TELEMETRY_SHUTDOWN_TIMEOUT_MS);
|
||||
}),
|
||||
]);
|
||||
if (timeoutHandle) {
|
||||
clearTimeout(timeoutHandle);
|
||||
}
|
||||
|
||||
if (didTimeout) {
|
||||
logger.debug(`Skipped waiting for telemetry shutdown after ${TELEMETRY_SHUTDOWN_TIMEOUT_MS}ms`);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug('Flushed telemetry events on shutdown');
|
||||
} catch (error) {
|
||||
if (timeoutHandle) {
|
||||
clearTimeout(timeoutHandle);
|
||||
}
|
||||
logger.error('Error shutting down telemetry:', error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user