mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-28 14:04:41 -07:00
feat(hooks): Hook Session Lifecycle & Compression Integration (#14151)
This commit is contained in:
@@ -16,6 +16,7 @@ export { DEFAULT_TELEMETRY_TARGET, DEFAULT_OTLP_ENDPOINT };
|
||||
export {
|
||||
initializeTelemetry,
|
||||
shutdownTelemetry,
|
||||
flushTelemetry,
|
||||
isTelemetrySdkInitialized,
|
||||
} from './sdk.js';
|
||||
export {
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user