feat(hooks): Hook Session Lifecycle & Compression Integration (#14151)

This commit is contained in:
Edilmo Palencia
2025-12-03 09:04:13 -08:00
committed by GitHub
parent 7a6d3067c6
commit 1c12da1fad
27 changed files with 1026 additions and 302 deletions
+1
View File
@@ -16,6 +16,7 @@ export { DEFAULT_TELEMETRY_TARGET, DEFAULT_OTLP_ENDPOINT };
export {
initializeTelemetry,
shutdownTelemetry,
flushTelemetry,
isTelemetrySdkInitialized,
} from './sdk.js';
export {
+33 -5
View File
@@ -80,6 +80,8 @@ class DiagLoggerAdapter {
diag.setLogger(new DiagLoggerAdapter(), DiagLogLevel.INFO);
let sdk: NodeSDK | undefined;
let spanProcessor: BatchSpanProcessor | undefined;
let logRecordProcessor: BatchLogRecordProcessor | undefined;
let telemetryInitialized = false;
let callbackRegistered = false;
let authListener: ((newCredentials: JWTInput) => Promise<void>) | undefined =
@@ -273,10 +275,14 @@ export async function initializeTelemetry(
});
}
// Store processor references for manual flushing
spanProcessor = new BatchSpanProcessor(spanExporter);
logRecordProcessor = new BatchLogRecordProcessor(logExporter);
sdk = new NodeSDK({
resource,
spanProcessors: [new BatchSpanProcessor(spanExporter)],
logRecordProcessors: [new BatchLogRecordProcessor(logExporter)],
spanProcessors: [spanProcessor],
logRecordProcessors: [logRecordProcessor],
metricReader,
instrumentations: [new HttpInstrumentation()],
});
@@ -293,15 +299,37 @@ export async function initializeTelemetry(
console.error('Error starting OpenTelemetry SDK:', error);
}
// Note: We don't use process.on('exit') here because that callback is synchronous
// and won't wait for the async shutdownTelemetry() to complete.
// Instead, telemetry shutdown is handled in runExitCleanup() in cleanup.ts
process.on('SIGTERM', () => {
shutdownTelemetry(config);
});
process.on('SIGINT', () => {
shutdownTelemetry(config);
});
process.on('exit', () => {
shutdownTelemetry(config);
});
}
/**
* Force flush all pending telemetry data to disk.
* This is useful for ensuring telemetry is written before critical operations like /clear.
*/
export async function flushTelemetry(config: Config): Promise<void> {
if (!telemetryInitialized || !spanProcessor || !logRecordProcessor) {
return;
}
try {
// Force flush all pending telemetry to disk
await Promise.all([
spanProcessor.forceFlush(),
logRecordProcessor.forceFlush(),
]);
if (config.getDebugMode()) {
debugLogger.log('OpenTelemetry SDK flushed successfully.');
}
} catch (error) {
console.error('Error flushing SDK:', error);
}
}
export async function shutdownTelemetry(