feat: Metriken, Token-Auswertung und Protokollierung am Ende der Antwort (#1050)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 09:05:20 +01:00
parent b4a3957f8a
commit 2dc8f9193c
9 changed files with 942 additions and 17 deletions

View File

@ -29,8 +29,17 @@ pub struct AccessInfo {
pub key_name: Option<String>,
pub model: Option<String>,
pub wait_ms: Option<u64>,
/// Registrierter Modellname für die Metriken (`None`: kein Modell).
pub metric_model: Option<String>,
/// Der Handler protokolliert die Anfrage selbst, sobald die Antwort zu Ende
/// ist (Streaming); die Middleware schreibt dann keine Zeile.
pub deferred: bool,
}
/// Zeitpunkt, zu dem die Anfrage eintraf.
#[derive(Debug, Clone, Copy)]
pub struct StartedAt(pub Instant);
pub type AccessSlot = Arc<Mutex<AccessInfo>>;
/// Längste Zeichenzahl eines Modellnamens im Log.
@ -77,6 +86,7 @@ pub async fn observe(
.extensions_mut()
.insert(RequestId(correlation.clone()));
request.extensions_mut().insert(slot.clone());
request.extensions_mut().insert(StartedAt(started));
let span = tracing::info_span!("request", korrelation = %correlation);
let mut response = next.run(request).instrument(span.clone()).await;
@ -124,7 +134,17 @@ pub async fn observe(
"Anfrage beantwortet"
);
}
if info.key_id.is_some() {
if !info.deferred {
state.runtime.metrics.record_request(
info.metric_model.as_deref().unwrap_or("-"),
status,
latency_ms,
None,
None,
false,
);
}
if info.key_id.is_some() && !info.deferred {
let entry = RequestLogEntry {
correlation: correlation.clone(),
api_key_id: info.key_id,
@ -132,6 +152,12 @@ pub async fn observe(
status,
wait_ms: info.wait_ms,
latency_ms: Some(latency_ms),
duration_ms: Some(latency_ms),
tokens_in: None,
tokens_out: None,
engine: None,
instance_id: None,
aborted: false,
};
if let Err(e) = state.store().log_request(&entry) {
tracing::error!(fehler = %e, "Anfrage-Log konnte nicht geschrieben werden");