llm-cost-ops 0.1.1

Core library for cost operations on LLM deployments
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// HTTP compression middleware for Axum

use super::{
    codec, CompressionAlgorithm, CompressionConfig, CompressionError,
    CompressionMetrics, CompressionResult,
};
use axum::{
    body::Body,
    extract::Request,
    http::{header, HeaderValue, StatusCode},
    middleware::Next,
    response::{IntoResponse, Response},
};
use http_body_util::BodyExt;
use std::str::FromStr;
use std::sync::Arc;
use tower::{Layer, Service};
use tracing::{debug, warn};

/// Compression layer for Axum
#[derive(Clone)]
pub struct CompressionLayer {
    config: Arc<CompressionConfig>,
    metrics: Arc<CompressionMetrics>,
}

impl CompressionLayer {
    /// Create a new compression layer
    pub fn new(config: CompressionConfig) -> Self {
        Self {
            config: Arc::new(config),
            metrics: super::metrics::get_metrics(),
        }
    }

    /// Create with custom metrics
    pub fn with_metrics(config: CompressionConfig, metrics: Arc<CompressionMetrics>) -> Self {
        Self {
            config: Arc::new(config),
            metrics,
        }
    }
}

impl<S> Layer<S> for CompressionLayer {
    type Service = CompressionService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        CompressionService {
            inner,
            config: self.config.clone(),
            metrics: self.metrics.clone(),
        }
    }
}

/// Compression service
#[derive(Clone)]
pub struct CompressionService<S> {
    inner: S,
    config: Arc<CompressionConfig>,
    metrics: Arc<CompressionMetrics>,
}

impl<S> Service<Request> for CompressionService<S>
where
    S: Service<Request, Response = Response> + Clone + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = Response;
    type Error = S::Error;
    type Future = std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>,
    >;

    fn poll_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, request: Request) -> Self::Future {
        let config = self.config.clone();
        let metrics = self.metrics.clone();
        let mut inner = self.inner.clone();

        Box::pin(async move {
            // Handle request decompression if enabled
            let request = if config.compress_requests {
                match decompress_request(request, &config, &metrics).await {
                    Ok(req) => req,
                    Err(e) => {
                        warn!(error = %e, "Failed to decompress request");
                        return Ok(error_response(
                            StatusCode::BAD_REQUEST,
                            "Failed to decompress request body",
                        ));
                    }
                }
            } else {
                request
            };

            // Get Accept-Encoding header before passing request
            let accept_encoding = request
                .headers()
                .get(header::ACCEPT_ENCODING)
                .and_then(|h| h.to_str().ok())
                .map(|s| s.to_string());

            // Call inner service
            let response = inner.call(request).await?;

            // Handle response compression if enabled
            let response = if config.compress_responses {
                if let Some(accept_encoding) = accept_encoding {
                    compress_response(response, &accept_encoding, &config, &metrics).await
                } else {
                    response
                }
            } else {
                response
            };

            Ok(response)
        })
    }
}

/// Decompress request body
async fn decompress_request(
    request: Request,
    _config: &CompressionConfig,
    metrics: &CompressionMetrics,
) -> CompressionResult<Request> {
    let (mut parts, body) = request.into_parts();

    // Check for Content-Encoding header
    let encoding = parts
        .headers
        .get(header::CONTENT_ENCODING)
        .and_then(|h| h.to_str().ok());

    let encoding = match encoding {
        Some(e) => e,
        None => return Ok(Request::from_parts(parts, body)), // No encoding, return as-is
    };

    // Parse algorithm
    let algorithm = match CompressionAlgorithm::from_str(encoding) {
        Ok(algo) => algo,
        Err(_) => return Ok(Request::from_parts(parts, body)), // Unknown encoding, pass through
    };

    if algorithm == CompressionAlgorithm::Identity {
        return Ok(Request::from_parts(parts, body));
    }

    // Collect body bytes
    let bytes = body
        .collect()
        .await
        .map_err(|e| CompressionError::DecompressionFailed(e.to_string()))?
        .to_bytes();

    // Decompress
    let (decompressed, stats) = codec::decompress(&bytes, algorithm)?;

    // Record metrics
    metrics.record_decompression(&stats);

    // Remove Content-Encoding header
    parts.headers.remove(header::CONTENT_ENCODING);

    // Update Content-Length
    parts.headers.insert(
        header::CONTENT_LENGTH,
        HeaderValue::from_str(&decompressed.len().to_string()).unwrap(),
    );

    debug!(
        algorithm = %algorithm,
        original_size = stats.compressed_size,
        decompressed_size = stats.original_size,
        "Request decompressed"
    );

    Ok(Request::from_parts(parts, Body::from(decompressed)))
}

/// Compress response body
async fn compress_response(
    response: Response,
    accept_encoding: &str,
    config: &CompressionConfig,
    metrics: &CompressionMetrics,
) -> Response {
    // Select compression algorithm
    let algorithm = match config.select_algorithm(Some(accept_encoding)) {
        Some(algo) if algo != CompressionAlgorithm::Identity => algo,
        _ => return response, // No compression
    };

    let (mut parts, body) = response.into_parts();

    // Check if content type should be compressed
    let content_type = parts
        .headers
        .get(header::CONTENT_TYPE)
        .and_then(|h| h.to_str().ok())
        .unwrap_or("");

    if !config.should_compress_mime_type(content_type) {
        debug!(
            content_type = content_type,
            "Content type not compressible"
        );
        return Response::from_parts(parts, body);
    }

    // Collect body bytes
    let bytes = match body.collect().await {
        Ok(collected) => collected.to_bytes(),
        Err(e) => {
            warn!(error = %e, "Failed to collect response body");
            return error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error");
        }
    };

    // Check size thresholds
    if !config.should_compress_size(bytes.len()) {
        debug!(
            size = bytes.len(),
            min_size = config.min_size,
            "Response too small to compress"
        );
        return Response::from_parts(parts, Body::from(bytes));
    }

    // Compress
    let (compressed, stats) = match codec::compress(&bytes, algorithm, config.level) {
        Ok(result) => result,
        Err(e) => {
            warn!(error = %e, algorithm = %algorithm, "Compression failed");
            metrics.record_error(Some(algorithm), "compress");
            return Response::from_parts(parts, Body::from(bytes));
        }
    };

    // Check if compression was beneficial
    if compressed.len() >= bytes.len() {
        debug!(
            original_size = bytes.len(),
            compressed_size = compressed.len(),
            "Compression not beneficial, using original"
        );
        return Response::from_parts(parts, Body::from(bytes));
    }

    // Record metrics
    metrics.record_compression(&stats);

    // Update headers
    parts.headers.insert(
        header::CONTENT_ENCODING,
        HeaderValue::from_str(algorithm.as_str()).unwrap(),
    );

    parts.headers.insert(
        header::CONTENT_LENGTH,
        HeaderValue::from_str(&compressed.len().to_string()).unwrap(),
    );

    // Add Vary header to indicate content negotiation
    parts
        .headers
        .entry(header::VARY)
        .or_insert(HeaderValue::from_static("Accept-Encoding"));

    debug!(
        algorithm = %algorithm,
        original_size = stats.original_size,
        compressed_size = stats.compressed_size,
        ratio = stats.compression_ratio,
        savings_pct = stats.compression_percentage(),
        "Response compressed"
    );

    Response::from_parts(parts, Body::from(compressed))
}

/// Create error response
fn error_response(status: StatusCode, message: &str) -> Response {
    (status, message.to_string()).into_response()
}

/// Create compression layer with default config
pub fn compression_layer() -> CompressionLayer {
    CompressionLayer::new(CompressionConfig::default())
}

/// Middleware function for manual application
pub async fn compression_middleware(
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    let config = Arc::new(CompressionConfig::default());
    let metrics = super::metrics::get_metrics();

    // Handle request decompression
    let request = if config.compress_requests {
        match decompress_request(request, &config, &metrics).await {
            Ok(req) => req,
            Err(e) => {
                warn!(error = %e, "Failed to decompress request");
                return Ok(error_response(
                    StatusCode::BAD_REQUEST,
                    "Failed to decompress request body",
                ));
            }
        }
    } else {
        request
    };

    // Get Accept-Encoding before passing request
    let accept_encoding = request
        .headers()
        .get(header::ACCEPT_ENCODING)
        .and_then(|h| h.to_str().ok())
        .map(|s| s.to_string());

    // Call next middleware/handler
    let response = next.run(request).await;

    // Handle response compression
    let response = if config.compress_responses {
        if let Some(accept_encoding) = accept_encoding {
            compress_response(response, &accept_encoding, &config, &metrics).await
        } else {
            response
        }
    } else {
        response
    };

    Ok(response)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::CompressionLevel;
    use axum::{routing::get, Router};
    use tower::ServiceExt;

    async fn test_handler() -> ([(header::HeaderName, &'static str); 1], &'static str) {
        ([
            (header::CONTENT_TYPE, "text/plain"),
        ], "Hello, World! This is a test response that should be compressed. Adding more text to ensure it compresses well and the compressed size is smaller than the original.")
    }

    #[tokio::test]
    async fn test_compression_layer_creation() {
        let config = CompressionConfig::default();
        let layer = CompressionLayer::new(config);
        // Should not panic
        drop(layer);
    }

    #[tokio::test]
    async fn test_compression_response() {
        let config = CompressionConfig {
            enabled: true,
            level: CompressionLevel::Default,
            algorithms: vec![CompressionAlgorithm::Gzip],
            min_size: 10, // Low threshold for testing
            max_size: None,
            mime_types: vec!["text/*".to_string()],
            compress_requests: false,
            compress_responses: true,
            buffer_size: 8192,
        };

        let app = Router::new()
            .route("/", get(test_handler))
            .layer(CompressionLayer::new(config));

        let request = Request::builder()
            .uri("/")
            .header(header::ACCEPT_ENCODING, "gzip")
            .body(Body::empty())
            .unwrap();

        let response = app.oneshot(request).await.unwrap();

        // Should have Content-Encoding header
        assert_eq!(
            response.headers().get(header::CONTENT_ENCODING).unwrap(),
            "gzip"
        );
    }

    #[tokio::test]
    async fn test_no_compression_without_accept_encoding() {
        let config = CompressionConfig::default();

        let app = Router::new()
            .route("/", get(test_handler))
            .layer(CompressionLayer::new(config));

        let request = Request::builder()
            .uri("/")
            .body(Body::empty())
            .unwrap();

        let response = app.oneshot(request).await.unwrap();

        // Should not have Content-Encoding header
        assert!(response.headers().get(header::CONTENT_ENCODING).is_none());
    }

    #[tokio::test]
    async fn test_compression_with_brotli() {
        let config = CompressionConfig {
            enabled: true,
            level: CompressionLevel::Default,
            algorithms: vec![CompressionAlgorithm::Brotli],
            min_size: 10,
            max_size: None,
            mime_types: vec!["text/*".to_string()],
            compress_requests: false,
            compress_responses: true,
            buffer_size: 8192,
        };

        let app = Router::new()
            .route("/", get(test_handler))
            .layer(CompressionLayer::new(config));

        let request = Request::builder()
            .uri("/")
            .header(header::ACCEPT_ENCODING, "br")
            .body(Body::empty())
            .unwrap();

        let response = app.oneshot(request).await.unwrap();

        // Should use brotli
        assert_eq!(
            response.headers().get(header::CONTENT_ENCODING).unwrap(),
            "br"
        );
    }
}