1#[cfg(feature = "serde")]
8use crate::request_id::RequestId;
9#[cfg(feature = "serde")]
10use http::{Method, StatusCode, Uri, Version};
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13#[cfg(feature = "serde")]
14use serde_json::json;
15use sha2::{Digest, Sha256};
16#[cfg(feature = "serde")]
17use std::time::{Duration, SystemTime};
18
19#[derive(Debug, Clone)]
21pub struct MLLoggingConfig {
22 pub enabled: bool,
24
25 pub sample_rate: f64,
27
28 pub hash_keys: bool,
30
31 pub target: String,
33}
34
35impl Default for MLLoggingConfig {
36 fn default() -> Self {
37 Self {
38 enabled: false,
39 sample_rate: 1.0,
40 hash_keys: true,
41 target: "tower_http_cache::ml".to_string(),
42 }
43 }
44}
45
46impl MLLoggingConfig {
47 pub fn new() -> Self {
49 Self::default()
50 }
51
52 pub fn with_enabled(mut self, enabled: bool) -> Self {
54 self.enabled = enabled;
55 self
56 }
57
58 pub fn with_sample_rate(mut self, rate: f64) -> Self {
60 self.sample_rate = rate.clamp(0.0, 1.0);
61 self
62 }
63
64 pub fn with_hash_keys(mut self, hash: bool) -> Self {
66 self.hash_keys = hash;
67 self
68 }
69
70 pub fn with_target(mut self, target: impl Into<String>) -> Self {
72 self.target = target.into();
73 self
74 }
75
76 pub fn should_sample(&self) -> bool {
78 if !self.enabled {
79 return false;
80 }
81 if self.sample_rate >= 1.0 {
82 return true;
83 }
84 use std::collections::hash_map::RandomState;
85 use std::hash::BuildHasher;
86 let hasher = RandomState::new();
87
88 let random = (hasher.hash_one(std::time::SystemTime::now()) as f64) / (u64::MAX as f64);
89 random < self.sample_rate
90 }
91}
92
93#[derive(Debug, Clone)]
95#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
96#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
97pub enum CacheEventType {
98 Hit,
100 Miss,
102 StaleHit,
104 Store,
106 Invalidate,
108 TagInvalidate,
110 TierPromote,
112 AdminAccess,
114}
115
116#[cfg(feature = "serde")]
122#[derive(Debug, Clone)]
123pub struct CacheEvent {
124 pub timestamp: SystemTime,
126
127 pub event_type: CacheEventType,
129
130 pub request_id: RequestId,
132
133 pub key: String,
135
136 pub method: Option<Method>,
138
139 pub uri: Option<Uri>,
141
142 pub version: Option<Version>,
144
145 pub status: Option<StatusCode>,
147
148 pub hit: bool,
150
151 pub latency_us: Option<u64>,
153
154 pub size_bytes: Option<usize>,
156
157 pub ttl_seconds: Option<u64>,
159
160 pub tags: Option<Vec<String>>,
162
163 pub tier: Option<String>,
165
166 pub promoted: bool,
168
169 pub metadata: serde_json::Value,
171}
172
173#[cfg(feature = "serde")]
174impl CacheEvent {
175 pub fn new(event_type: CacheEventType, request_id: RequestId, key: String) -> Self {
177 Self {
178 timestamp: SystemTime::now(),
179 event_type,
180 request_id,
181 key,
182 method: None,
183 uri: None,
184 version: None,
185 status: None,
186 hit: false,
187 latency_us: None,
188 size_bytes: None,
189 ttl_seconds: None,
190 tags: None,
191 tier: None,
192 promoted: false,
193 metadata: json!({}),
194 }
195 }
196
197 pub fn with_method(mut self, method: Method) -> Self {
199 self.method = Some(method);
200 self
201 }
202
203 pub fn with_uri(mut self, uri: Uri) -> Self {
205 self.uri = Some(uri);
206 self
207 }
208
209 pub fn with_version(mut self, version: Version) -> Self {
211 self.version = Some(version);
212 self
213 }
214
215 pub fn with_status(mut self, status: StatusCode) -> Self {
217 self.status = Some(status);
218 self
219 }
220
221 pub fn with_hit(mut self, hit: bool) -> Self {
223 self.hit = hit;
224 self
225 }
226
227 pub fn with_latency(mut self, latency: Duration) -> Self {
229 self.latency_us = Some(latency.as_micros() as u64);
230 self
231 }
232
233 pub fn with_size(mut self, size: usize) -> Self {
235 self.size_bytes = Some(size);
236 self
237 }
238
239 pub fn with_ttl(mut self, ttl: Duration) -> Self {
241 self.ttl_seconds = Some(ttl.as_secs());
242 self
243 }
244
245 pub fn with_tags(mut self, tags: Vec<String>) -> Self {
247 self.tags = Some(tags);
248 self
249 }
250
251 pub fn with_tier(mut self, tier: impl Into<String>) -> Self {
253 self.tier = Some(tier.into());
254 self
255 }
256
257 pub fn with_promoted(mut self, promoted: bool) -> Self {
259 self.promoted = promoted;
260 self
261 }
262
263 pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
265 self.metadata = metadata;
266 self
267 }
268
269 pub fn log(&self, config: &MLLoggingConfig) {
271 if !config.should_sample() {
272 return;
273 }
274
275 let key = if config.hash_keys {
276 hash_key(&self.key)
277 } else {
278 self.key.clone()
279 };
280
281 let log_data = json!({
282 "timestamp": crate::time_fmt::format_iso8601_millis(self.timestamp),
283 "level": "info",
284 "event": format!("{:?}", self.event_type).to_lowercase(),
285 "request_id": self.request_id.as_str(),
286 "key": key,
287 "method": self.method.as_ref().map(|m| m.as_str()),
288 "uri": self.uri.as_ref().map(|u| u.to_string()),
289 "version": self.version.as_ref().map(|v| format!("{:?}", v)),
290 "status": self.status.as_ref().map(|s| s.as_u16()),
291 "hit": self.hit,
292 "latency_us": self.latency_us,
293 "size_bytes": self.size_bytes,
294 "ttl_seconds": self.ttl_seconds,
295 "tags": self.tags,
296 "tier": self.tier,
297 "promoted": self.promoted,
298 "metadata": self.metadata,
299 });
300
301 #[cfg(feature = "tracing")]
302 {
303 tracing::info!(
305 target: "tower_http_cache::ml",
306 event = %log_data
307 );
308 }
309
310 #[cfg(not(feature = "tracing"))]
311 {
312 let _ = config; println!("{}", log_data);
315 }
316 }
317}
318
319pub fn hash_key(key: &str) -> String {
321 let mut hasher = Sha256::new();
322 hasher.update(key.as_bytes());
323 let result = hasher.finalize();
324 hex::encode(result)
325}
326
327#[cfg(feature = "serde")]
329pub fn log_cache_operation(
330 config: &MLLoggingConfig,
331 event_type: CacheEventType,
332 request_id: RequestId,
333 key: String,
334) {
335 if !config.enabled {
336 return;
337 }
338
339 let event = CacheEvent::new(event_type, request_id, key);
340 event.log(config);
341}
342
343#[cfg(test)]
344mod tests {
345 use super::*;
346
347 #[test]
348 fn ml_logging_config_default() {
349 let config = MLLoggingConfig::default();
350 assert!(!config.enabled);
351 assert_eq!(config.sample_rate, 1.0);
352 assert!(config.hash_keys);
353 }
354
355 #[test]
356 fn ml_logging_config_builder() {
357 let config = MLLoggingConfig::new()
358 .with_enabled(true)
359 .with_sample_rate(0.5)
360 .with_hash_keys(false)
361 .with_target("custom::target");
362
363 assert!(config.enabled);
364 assert_eq!(config.sample_rate, 0.5);
365 assert!(!config.hash_keys);
366 assert_eq!(config.target, "custom::target");
367 }
368
369 #[test]
370 fn sample_rate_clamped() {
371 let config = MLLoggingConfig::new().with_sample_rate(1.5);
372 assert_eq!(config.sample_rate, 1.0);
373
374 let config = MLLoggingConfig::new().with_sample_rate(-0.5);
375 assert_eq!(config.sample_rate, 0.0);
376 }
377
378 #[test]
379 fn should_sample_when_disabled() {
380 let config = MLLoggingConfig::new().with_enabled(false);
381 assert!(!config.should_sample());
382 }
383
384 #[test]
385 fn should_sample_when_rate_is_one() {
386 let config = MLLoggingConfig::new()
387 .with_enabled(true)
388 .with_sample_rate(1.0);
389 assert!(config.should_sample());
390 }
391
392 #[test]
393 fn hash_key_consistent() {
394 let key = "/api/users/123";
395 let hash1 = hash_key(key);
396 let hash2 = hash_key(key);
397 assert_eq!(hash1, hash2);
398 assert_ne!(hash1, key);
399 assert_eq!(hash1.len(), 64); }
401
402 #[cfg(feature = "serde")]
403 #[test]
404 fn cache_event_builder() {
405 let request_id = RequestId::new();
406 let event = CacheEvent::new(CacheEventType::Hit, request_id.clone(), "/test".to_string())
407 .with_method(Method::GET)
408 .with_status(StatusCode::OK)
409 .with_hit(true)
410 .with_latency(Duration::from_micros(150))
411 .with_size(1024)
412 .with_ttl(Duration::from_secs(300))
413 .with_tags(vec!["user:123".to_string()])
414 .with_tier("l1")
415 .with_promoted(false);
416
417 assert_eq!(event.method, Some(Method::GET));
418 assert_eq!(event.status, Some(StatusCode::OK));
419 assert!(event.hit);
420 assert_eq!(event.latency_us, Some(150));
421 assert_eq!(event.size_bytes, Some(1024));
422 assert_eq!(event.ttl_seconds, Some(300));
423 assert_eq!(event.tags, Some(vec!["user:123".to_string()]));
424 assert_eq!(event.tier, Some("l1".to_string()));
425 assert!(!event.promoted);
426 }
427
428 #[cfg(feature = "serde")]
429 #[test]
430 fn cache_event_log_disabled() {
431 let config = MLLoggingConfig::new().with_enabled(false);
432 let request_id = RequestId::new();
433 let event = CacheEvent::new(CacheEventType::Hit, request_id, "/test".to_string());
434
435 event.log(&config);
437 }
438
439 #[cfg(feature = "serde")]
440 #[test]
441 fn cache_event_log_with_hashing() {
442 let config = MLLoggingConfig::new()
443 .with_enabled(true)
444 .with_hash_keys(true);
445 let request_id = RequestId::new();
446 let event = CacheEvent::new(CacheEventType::Hit, request_id, "/api/secret".to_string());
447
448 event.log(&config);
450 }
451
452 #[cfg(feature = "serde")]
453 #[test]
454 fn log_cache_operation_helper() {
455 let config = MLLoggingConfig::new().with_enabled(true);
456 let request_id = RequestId::new();
457
458 log_cache_operation(
460 &config,
461 CacheEventType::Miss,
462 request_id,
463 "/test".to_string(),
464 );
465 }
466}