feat: JSON-Logging mit Correlation-ID, Anfrage-Log und Audit (M2.4)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -4,12 +4,13 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::body::{Body, Bytes};
|
||||
use axum::extract::State;
|
||||
use axum::extract::{Extension, State};
|
||||
use axum::http::header::{CACHE_CONTROL, CONTENT_TYPE};
|
||||
use axum::http::{HeaderMap, StatusCode};
|
||||
use axum::response::Response;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::access::{AccessSlot, RequestId, clip};
|
||||
use crate::auth::{Authed, requested_priority};
|
||||
use crate::error::ApiError;
|
||||
use crate::state::AppState;
|
||||
@ -17,6 +18,8 @@ use crate::state::AppState;
|
||||
pub async fn completions(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Authed(key): Authed,
|
||||
Extension(slot): Extension<AccessSlot>,
|
||||
Extension(RequestId(correlation)): Extension<RequestId>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Result<Response, ApiError> {
|
||||
@ -30,6 +33,8 @@ pub async fn completions(
|
||||
.ok_or_else(|| ApiError::bad_request("Das Feld \"model\" fehlt."))?
|
||||
.to_string();
|
||||
|
||||
slot.lock().unwrap_or_else(|e| e.into_inner()).model = Some(clip(&model));
|
||||
|
||||
if !key.rights.allows(&model) {
|
||||
return Err(ApiError::model_not_found(&model));
|
||||
}
|
||||
@ -37,25 +42,27 @@ pub async fn completions(
|
||||
.store()
|
||||
.start_plan(&model)
|
||||
.map_err(|e| {
|
||||
eprintln!("FEHLER Startplan für \"{model}\": {e}");
|
||||
tracing::error!(fehler = %e, "Startplan nicht lesbar");
|
||||
ApiError::internal()
|
||||
})?
|
||||
.ok_or_else(|| ApiError::model_not_found(&model))?;
|
||||
|
||||
let endpoint = state.runtime.ensure(&plan).await.map_err(|e| {
|
||||
eprintln!("FEHLER {e}");
|
||||
ApiError::engine_unavailable()
|
||||
})?;
|
||||
let waited = std::time::Instant::now();
|
||||
let ensured = state.runtime.ensure(&plan).await;
|
||||
slot.lock().unwrap_or_else(|e| e.into_inner()).wait_ms =
|
||||
Some(waited.elapsed().as_millis() as u64);
|
||||
let endpoint = ensured.map_err(|_| ApiError::engine_unavailable())?;
|
||||
|
||||
let upstream = state
|
||||
.http
|
||||
.post(format!("{}/v1/chat/completions", endpoint.base_url))
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.header("x-request-id", &correlation)
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("FEHLER Weiterleitung an {}: {e}", endpoint.base_url);
|
||||
tracing::error!(ziel = %endpoint.base_url, fehler = %e, "Weiterleitung an die Engine fehlgeschlagen");
|
||||
ApiError::upstream()
|
||||
})?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user