mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-18 09:11:55 -07:00
feat(telemetry): Add telemetry and metrics for model routing (#8518)
This commit is contained in:
@@ -16,6 +16,9 @@ import { CompositeStrategy } from './strategies/compositeStrategy.js';
|
||||
import { FallbackStrategy } from './strategies/fallbackStrategy.js';
|
||||
import { OverrideStrategy } from './strategies/overrideStrategy.js';
|
||||
|
||||
import { logModelRouting } from '../telemetry/loggers.js';
|
||||
import { ModelRoutingEvent } from '../telemetry/types.js';
|
||||
|
||||
/**
|
||||
* A centralized service for making model routing decisions.
|
||||
*/
|
||||
@@ -49,12 +52,55 @@ export class ModelRouterService {
|
||||
* @returns A promise that resolves to a RoutingDecision.
|
||||
*/
|
||||
async route(context: RoutingContext): Promise<RoutingDecision> {
|
||||
const decision = await this.strategy.route(
|
||||
context,
|
||||
this.config,
|
||||
this.config.getBaseLlmClient(),
|
||||
);
|
||||
const startTime = Date.now();
|
||||
let decision: RoutingDecision;
|
||||
|
||||
return decision;
|
||||
try {
|
||||
decision = await this.strategy.route(
|
||||
context,
|
||||
this.config,
|
||||
this.config.getBaseLlmClient(),
|
||||
);
|
||||
|
||||
const event = new ModelRoutingEvent(
|
||||
decision.model,
|
||||
decision.metadata.source,
|
||||
decision.metadata.latencyMs,
|
||||
decision.metadata.reasoning,
|
||||
false, // failed
|
||||
undefined, // error_message
|
||||
);
|
||||
logModelRouting(this.config, event);
|
||||
|
||||
return decision;
|
||||
} catch (e) {
|
||||
const failed = true;
|
||||
const error_message = e instanceof Error ? e.message : String(e);
|
||||
// Create a fallback decision for logging purposes
|
||||
// We do not actually route here. This should never happen so we should
|
||||
// fail loudly to catch any issues where this happens.
|
||||
decision = {
|
||||
model: this.config.getModel(),
|
||||
metadata: {
|
||||
source: 'router-exception',
|
||||
latencyMs: Date.now() - startTime,
|
||||
reasoning: 'An exception occurred during routing.',
|
||||
error: error_message,
|
||||
},
|
||||
};
|
||||
|
||||
const event = new ModelRoutingEvent(
|
||||
decision.model,
|
||||
decision.metadata.source,
|
||||
decision.metadata.latencyMs,
|
||||
decision.metadata.reasoning,
|
||||
failed,
|
||||
error_message,
|
||||
);
|
||||
|
||||
logModelRouting(this.config, event);
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user