llm-cost-ops-api 0.1.0

REST API and data ingestion for LLM Cost Ops
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Webhook HTTP server for receiving usage data

use axum::{
    extract::State,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::{get, post},
    Json, Router,
};
use std::sync::Arc;
use tower::ServiceBuilder;
use tower_http::{
    cors::CorsLayer,
    trace::{DefaultMakeSpan, TraceLayer},
};
use tracing::{error, info, warn};

use llm_cost_ops::Result;

use super::models::{BatchIngestionRequest, IngestionResponse, UsageWebhookPayload};
use super::traits::{IngestionHandler, RateLimiter};

/// Webhook server state
#[derive(Clone)]
pub struct WebhookServerState<H: IngestionHandler> {
    handler: Arc<H>,
}

impl<H: IngestionHandler> WebhookServerState<H> {
    pub fn new(handler: H) -> Self {
        Self {
            handler: Arc::new(handler),
        }
    }
}

/// Webhook server state with rate limiting
#[derive(Clone)]
pub struct WebhookServerStateWithRateLimit<H: IngestionHandler, R: RateLimiter> {
    handler: Arc<H>,
    rate_limiter: Arc<R>,
}

impl<H: IngestionHandler, R: RateLimiter> WebhookServerStateWithRateLimit<H, R> {
    pub fn new(handler: H, rate_limiter: R) -> Self {
        Self {
            handler: Arc::new(handler),
            rate_limiter: Arc::new(rate_limiter),
        }
    }
}

/// Create webhook router with all endpoints
pub fn create_webhook_router<H: IngestionHandler + 'static>(
    handler: H,
) -> Router {
    let state = WebhookServerState::new(handler);

    Router::new()
        .route("/health", get(health_handler))
        .route("/v1/usage", post(ingest_single_handler::<H>))
        .route("/v1/usage/batch", post(ingest_batch_handler::<H>))
        .with_state(state)
        .layer(
            ServiceBuilder::new()
                .layer(axum::middleware::from_fn(llm_cost_ops::metrics::middleware::metrics_middleware))
                .layer(
                    TraceLayer::new_for_http()
                        .make_span_with(DefaultMakeSpan::new().level(tracing::Level::INFO)),
                )
                .layer(CorsLayer::permissive()),
        )
}

/// Create webhook router with rate limiting enabled
pub fn create_webhook_router_with_rate_limit<H: IngestionHandler + 'static, R: RateLimiter + Clone + 'static>(
    handler: H,
    rate_limiter: R,
) -> Router {
    let state = WebhookServerStateWithRateLimit::new(handler, rate_limiter);

    Router::new()
        .route("/health", get(health_handler))
        .route("/v1/usage", post(ingest_single_handler_with_rate_limit))
        .route("/v1/usage/batch", post(ingest_batch_handler_with_rate_limit))
        .with_state(state)
        .layer(
            ServiceBuilder::new()
                .layer(axum::middleware::from_fn(llm_cost_ops::metrics::middleware::metrics_middleware))
                .layer(
                    TraceLayer::new_for_http()
                        .make_span_with(DefaultMakeSpan::new().level(tracing::Level::INFO)),
                )
                .layer(CorsLayer::permissive()),
        )
}

/// Single usage record ingestion endpoint with rate limiting
async fn ingest_single_handler_with_rate_limit<H: IngestionHandler, R: RateLimiter>(
    State(state): State<WebhookServerStateWithRateLimit<H, R>>,
    Json(payload): Json<UsageWebhookPayload>,
) -> std::result::Result<Json<IngestionResponse>, AppError> {
    use std::time::Instant;
    let start = Instant::now();
    let org_id = payload.organization_id.clone();

    info!(
        request_id = %payload.request_id,
        organization_id = %org_id,
        "Received single usage ingestion request"
    );

    // Check rate limit
    match state.rate_limiter.check_rate_limit(&org_id).await {
        Ok(allowed) => {
            if !allowed {
                warn!(
                    organization_id = %org_id,
                    "Rate limit exceeded"
                );
                let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
                llm_cost_ops::metrics::collectors::IngestionMetrics::record_failure(&org_id, "rate_limit", duration_ms);
                return Err(AppError::RateLimitExceeded(org_id));
            }
        }
        Err(e) => {
            error!(error = %e, "Rate limit check failed, allowing request");
            // On error, allow the request (fail open for availability)
        }
    }

    // Process request
    match state.handler.handle_single(payload).await {
        Ok(response) => {
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_success(&org_id, 1, duration_ms);
            Ok(Json(response))
        }
        Err(e) => {
            error!(error = %e, "Failed to handle ingestion request");
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_failure(&org_id, "processing_error", duration_ms);
            Err(AppError::InternalError(e.to_string()))
        }
    }
}

/// Batch usage records ingestion endpoint with rate limiting
async fn ingest_batch_handler_with_rate_limit<H: IngestionHandler, R: RateLimiter>(
    State(state): State<WebhookServerStateWithRateLimit<H, R>>,
    Json(request): Json<BatchIngestionRequest>,
) -> std::result::Result<Json<IngestionResponse>, AppError> {
    use std::time::Instant;
    let start = Instant::now();
    let batch_size = request.records.len();

    info!(
        batch_id = %request.batch_id,
        batch_size = batch_size,
        source = %request.source,
        "Received batch usage ingestion request"
    );

    // Record batch size metric
    llm_cost_ops::metrics::collectors::IngestionMetrics::record_batch_size(batch_size);

    // Extract organization ID from first record
    let org_id = request.records.first()
        .map(|r| r.organization_id.clone())
        .unwrap_or_else(|| "unknown".to_string());

    // Check rate limit
    match state.rate_limiter.check_rate_limit(&org_id).await {
        Ok(allowed) => {
            if !allowed {
                warn!(
                    organization_id = %org_id,
                    "Rate limit exceeded for batch request"
                );
                let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
                llm_cost_ops::metrics::collectors::IngestionMetrics::record_failure(&org_id, "rate_limit", duration_ms);
                return Err(AppError::RateLimitExceeded(org_id));
            }
        }
        Err(e) => {
            error!(error = %e, "Rate limit check failed, allowing request");
        }
    }

    // Process request
    match state.handler.handle_batch(request.records).await {
        Ok(response) => {
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_success(&org_id, response.accepted, duration_ms);
            if response.rejected > 0 {
                llm_cost_ops::metrics::collectors::IngestionMetrics::record_rejected(&org_id, response.rejected);
            }
            Ok(Json(response))
        }
        Err(e) => {
            error!(error = %e, "Failed to handle batch ingestion request");
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_failure(&org_id, "processing_error", duration_ms);
            Err(AppError::InternalError(e.to_string()))
        }
    }
}

/// Health check endpoint
async fn health_handler() -> impl IntoResponse {
    Json(serde_json::json!({
        "status": "healthy",
        "service": "llm-cost-ops-ingestion",
        "timestamp": chrono::Utc::now().to_rfc3339()
    }))
}

/// Single usage record ingestion endpoint
async fn ingest_single_handler<H: IngestionHandler>(
    State(state): State<WebhookServerState<H>>,
    Json(payload): Json<UsageWebhookPayload>,
) -> std::result::Result<Json<IngestionResponse>, AppError> {
    use std::time::Instant;
    let start = Instant::now();
    let org_id = payload.organization_id.clone();

    info!(
        request_id = %payload.request_id,
        organization_id = %org_id,
        "Received single usage ingestion request"
    );

    match state.handler.handle_single(payload).await {
        Ok(response) => {
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_success(&org_id, 1, duration_ms);
            Ok(Json(response))
        }
        Err(e) => {
            error!(error = %e, "Failed to handle ingestion request");
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_failure(&org_id, "processing_error", duration_ms);
            Err(AppError::InternalError(e.to_string()))
        }
    }
}

/// Batch usage records ingestion endpoint
async fn ingest_batch_handler<H: IngestionHandler>(
    State(state): State<WebhookServerState<H>>,
    Json(request): Json<BatchIngestionRequest>,
) -> std::result::Result<Json<IngestionResponse>, AppError> {
    use std::time::Instant;
    let start = Instant::now();
    let batch_size = request.records.len();

    // Extract organization ID from first record
    let org_id = request.records.first()
        .map(|r| r.organization_id.clone())
        .unwrap_or_else(|| "unknown".to_string());

    info!(
        batch_id = %request.batch_id,
        batch_size = batch_size,
        source = %request.source,
        organization_id = %org_id,
        "Received batch usage ingestion request"
    );

    // Record batch size metric
    llm_cost_ops::metrics::collectors::IngestionMetrics::record_batch_size(batch_size);

    match state.handler.handle_batch(request.records).await {
        Ok(response) => {
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_success(&org_id, response.accepted, duration_ms);
            if response.rejected > 0 {
                llm_cost_ops::metrics::collectors::IngestionMetrics::record_rejected(&org_id, response.rejected);
            }
            Ok(Json(response))
        }
        Err(e) => {
            error!(error = %e, "Failed to handle batch ingestion request");
            let duration_ms = start.elapsed().as_secs_f64() * 1000.0;
            llm_cost_ops::metrics::collectors::IngestionMetrics::record_failure(&org_id, "processing_error", duration_ms);
            Err(AppError::InternalError(e.to_string()))
        }
    }
}

/// Application error types
#[derive(Debug)]
pub enum AppError {
    ValidationError(String),
    InternalError(String),
    RateLimitExceeded(String),
}

impl IntoResponse for AppError {
    fn into_response(self) -> Response {
        let (status, error_message, org_id) = match self {
            AppError::ValidationError(msg) => (StatusCode::BAD_REQUEST, msg, None),
            AppError::InternalError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg, None),
            AppError::RateLimitExceeded(org) => (
                StatusCode::TOO_MANY_REQUESTS,
                "Rate limit exceeded".to_string(),
                Some(org),
            ),
        };

        let mut body_json = serde_json::json!({
            "error": error_message,
            "timestamp": chrono::Utc::now().to_rfc3339()
        });

        if let Some(org) = org_id {
            body_json["organization_id"] = serde_json::json!(org);
        }

        let body = Json(body_json);

        let mut response = (status, body).into_response();

        // Add rate limit headers for rate limit errors
        if status == StatusCode::TOO_MANY_REQUESTS {
            response.headers_mut().insert(
                "Retry-After",
                "60".parse().unwrap(),
            );
            response.headers_mut().insert(
                "X-RateLimit-Limit",
                "1000".parse().unwrap(),
            );
            response.headers_mut().insert(
                "X-RateLimit-Remaining",
                "0".parse().unwrap(),
            );
        }

        response
    }
}

/// Start webhook server
pub async fn start_webhook_server<H: IngestionHandler + 'static>(
    bind_addr: &str,
    handler: H,
) -> Result<()> {
    info!(bind_addr = %bind_addr, "Starting webhook server");

    let app = create_webhook_router(handler);

    let listener = tokio::net::TcpListener::bind(bind_addr).await?;

    info!(
        addr = %listener.local_addr()?,
        "Webhook server listening"
    );

    axum::serve(listener, app).await?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use chrono::Utc;
    use uuid::Uuid;

    #[derive(Clone)]
    struct MockHandler;

    #[async_trait]
    impl IngestionHandler for MockHandler {
        async fn handle_single(
            &self,
            payload: UsageWebhookPayload,
        ) -> Result<IngestionResponse> {
            Ok(IngestionResponse {
                request_id: payload.request_id,
                status: super::super::models::IngestionStatus::Success,
                accepted: 1,
                rejected: 0,
                errors: vec![],
                processed_at: Utc::now(),
            })
        }

        async fn handle_batch(
            &self,
            payloads: Vec<UsageWebhookPayload>,
        ) -> Result<IngestionResponse> {
            Ok(IngestionResponse {
                request_id: Uuid::new_v4(),
                status: super::super::models::IngestionStatus::Success,
                accepted: payloads.len(),
                rejected: 0,
                errors: vec![],
                processed_at: Utc::now(),
            })
        }

        fn name(&self) -> &str {
            "mock_handler"
        }

        async fn health_check(&self) -> Result<bool> {
            Ok(true)
        }
    }

    #[tokio::test]
    async fn test_create_router() {
        let handler = MockHandler;
        let _router = create_webhook_router(handler);
        // Router creation should succeed
        assert!(true);
    }
}