1+ use std:: collections:: HashMap ;
12use std:: { sync:: LazyLock , time:: Duration } ;
23
34use opentelemetry:: KeyValue ;
@@ -12,14 +13,18 @@ use crate::{CARGO_CRATE_NAME, Config};
1213
1314static RESOURCE : LazyLock < opentelemetry_sdk:: Resource > = LazyLock :: new ( || {
1415 opentelemetry_sdk:: Resource :: builder ( )
16+ . with_service_name ( "s3_cache" )
1517 . with_service_name ( CARGO_CRATE_NAME )
1618 . build ( )
1719} ) ;
1820
1921// Prometheus registry and metrics
2022
2123pub ( crate ) static PROMETHEUS_REGISTRY : LazyLock < Registry > = LazyLock :: new ( || {
22- Registry :: new_custom ( Some ( "s3_cache" . to_string ( ) ) , None )
24+ let mut labels = HashMap :: default ( ) ;
25+ labels. insert ( "service_name" . to_string ( ) , "s3_cache" . to_string ( ) ) ;
26+
27+ Registry :: new_custom ( None , Some ( labels) )
2328 . expect ( "Failed to create Prometheus registry" )
2429} ) ;
2530
@@ -175,15 +180,15 @@ pub(crate) fn shutdown_metrics(metric_provider: opentelemetry_sdk::metrics::SdkM
175180pub ( crate ) fn record_cache_hit ( bytes : u64 ) {
176181 static CACHE_HIT_BYTES_HISTOGRAM : LazyLock < Histogram < u64 > > = LazyLock :: new ( || {
177182 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
178- . u64_histogram ( "cache .hit_bytes_histogram" )
183+ . u64_histogram ( "s3_cache .hit_bytes_histogram" )
179184 . with_description ( "Distribution of object sizes on cache hits" )
180185 . build ( )
181186 } ) ;
182187
183188 static PROM_CACHE_HIT_BYTES_HISTOGRAM : LazyLock < prometheus:: Histogram > = LazyLock :: new ( || {
184189 let histogram = prometheus:: Histogram :: with_opts (
185190 HistogramOpts :: new (
186- "cache_hit_bytes_histogram " ,
191+ "s3_cache_hit_bytes_histogram " ,
187192 "Distribution of object sizes on cache hits" ,
188193 )
189194 . buckets ( OBJECT_SIZE_BUCKETS . to_vec ( ) ) ,
@@ -197,14 +202,14 @@ pub(crate) fn record_cache_hit(bytes: u64) {
197202
198203 static CACHE_HIT_BYTES_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
199204 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
200- . u64_counter ( "cache .hit_bytes_total" )
205+ . u64_counter ( "s3_cache .hit_bytes_total" )
201206 . with_description ( "Total bytes received from cache hits" )
202207 . build ( )
203208 } ) ;
204209
205210 static PROM_CACHE_HIT_BYTES_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
206211 let counter = IntCounter :: new (
207- "cache_hit_bytes_total " ,
212+ "s3_cache_hit_bytes_total " ,
208213 "Total bytes received from cache hits" ,
209214 )
210215 . unwrap ( ) ;
@@ -225,15 +230,15 @@ pub(crate) fn record_cache_hit(bytes: u64) {
225230pub ( crate ) fn record_cache_miss ( bytes : u64 ) {
226231 static CACHE_MISS_BYTES_HISTOGRAM : LazyLock < Histogram < u64 > > = LazyLock :: new ( || {
227232 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
228- . u64_histogram ( "cache .miss_bytes_histogram" )
233+ . u64_histogram ( "s3_cache .miss_bytes_histogram" )
229234 . with_description ( "Distribution of object sizes on cache misses" )
230235 . build ( )
231236 } ) ;
232237
233238 static PROM_CACHE_MISS_BYTES_HISTOGRAM : LazyLock < prometheus:: Histogram > = LazyLock :: new ( || {
234239 let histogram = prometheus:: Histogram :: with_opts (
235240 HistogramOpts :: new (
236- "cache_miss_bytes_histogram " ,
241+ "s3_cache_miss_bytes_histogram " ,
237242 "Distribution of object sizes on cache misses" ,
238243 )
239244 . buckets ( OBJECT_SIZE_BUCKETS . to_vec ( ) ) ,
@@ -247,14 +252,14 @@ pub(crate) fn record_cache_miss(bytes: u64) {
247252
248253 static CACHE_MISS_BYTES_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
249254 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
250- . u64_counter ( "cache .miss_bytes_total" )
255+ . u64_counter ( "s3_cache .miss_bytes_total" )
251256 . with_description ( "Total bytes received from cache misses" )
252257 . build ( )
253258 } ) ;
254259
255260 static PROM_CACHE_MISS_BYTES_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
256261 let counter = IntCounter :: new (
257- "cache_miss_bytes_total " ,
262+ "s3_cache_miss_bytes_total " ,
258263 "Total bytes received from cache misses" ,
259264 )
260265 . unwrap ( ) ;
@@ -275,7 +280,7 @@ pub(crate) fn record_cache_miss(bytes: u64) {
275280pub ( crate ) fn record_cache_eviction ( bytes : u64 ) {
276281 static CACHE_EVICTION_BYTES_HISTOGRAM : LazyLock < Histogram < u64 > > = LazyLock :: new ( || {
277282 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
278- . u64_histogram ( "cache .eviction_bytes_histogram" )
283+ . u64_histogram ( "s3_cache .eviction_bytes_histogram" )
279284 . with_description ( "Distribution of object sizes on cache evictions" )
280285 . build ( )
281286 } ) ;
@@ -284,7 +289,7 @@ pub(crate) fn record_cache_eviction(bytes: u64) {
284289 LazyLock :: new ( || {
285290 let histogram = prometheus:: Histogram :: with_opts (
286291 HistogramOpts :: new (
287- "cache_eviction_bytes_histogram " ,
292+ "s3_cache_eviction_bytes_histogram " ,
288293 "Distribution of object sizes on cache evictions" ,
289294 )
290295 . buckets ( OBJECT_SIZE_BUCKETS . to_vec ( ) ) ,
@@ -298,14 +303,14 @@ pub(crate) fn record_cache_eviction(bytes: u64) {
298303
299304 static CACHE_EVICTION_BYTES_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
300305 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
301- . u64_counter ( "cache .eviction_bytes_total" )
306+ . u64_counter ( "s3_cache .eviction_bytes_total" )
302307 . with_description ( "Total bytes evicted from cache" )
303308 . build ( )
304309 } ) ;
305310
306311 static PROM_CACHE_EVICTION_BYTES_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
307312 let counter = IntCounter :: new (
308- "cache_eviction_bytes_total " ,
313+ "s3_cache_eviction_bytes_total " ,
309314 "Total bytes evicted from cache" ,
310315 )
311316 . unwrap ( ) ;
@@ -326,7 +331,7 @@ pub(crate) fn record_cache_eviction(bytes: u64) {
326331pub ( crate ) fn record_cache_eviction_age ( age_secs : f64 ) {
327332 static CACHE_EVICTION_AGE_HISTOGRAM : LazyLock < Histogram < f64 > > = LazyLock :: new ( || {
328333 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
329- . f64_histogram ( "cache .eviction_age_histogram" )
334+ . f64_histogram ( "s3_cache .eviction_age_histogram" )
330335 . with_description ( "Age of objects (in seconds) at the time of eviction, capped at TTL" )
331336 . with_unit ( "s" )
332337 . build ( )
@@ -336,7 +341,7 @@ pub(crate) fn record_cache_eviction_age(age_secs: f64) {
336341 LazyLock :: new ( || {
337342 let histogram = prometheus:: Histogram :: with_opts (
338343 HistogramOpts :: new (
339- "cache_eviction_age_histogram " ,
344+ "s3_cache_eviction_age_histogram " ,
340345 "Age of objects (in seconds) at the time of eviction, capped at TTL" ,
341346 )
342347 . buckets ( EVICTION_AGE_BUCKETS . to_vec ( ) ) ,
@@ -357,7 +362,7 @@ pub(crate) fn record_cache_eviction_age(age_secs: f64) {
357362pub ( crate ) fn record_cache_oversized ( bytes : u64 ) {
358363 static CACHE_OVERSIZED_BYTES_HISTOGRAM : LazyLock < Histogram < u64 > > = LazyLock :: new ( || {
359364 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
360- . u64_histogram ( "cache .oversized_bytes_histogram" )
365+ . u64_histogram ( "s3_cache .oversized_bytes_histogram" )
361366 . with_description ( "Distribution of object sizes that exceeded the max cacheable size" )
362367 . build ( )
363368 } ) ;
@@ -366,7 +371,7 @@ pub(crate) fn record_cache_oversized(bytes: u64) {
366371 LazyLock :: new ( || {
367372 let histogram = prometheus:: Histogram :: with_opts (
368373 HistogramOpts :: new (
369- "cache_oversized_bytes_histogram " ,
374+ "s3_cache_oversized_bytes_histogram " ,
370375 "Distribution of object sizes that exceeded the max cacheable size" ,
371376 )
372377 . buckets ( OVERSIZED_OBJECT_SIZE_BUCKETS . to_vec ( ) ) ,
@@ -380,7 +385,7 @@ pub(crate) fn record_cache_oversized(bytes: u64) {
380385
381386 static CACHE_OVERSIZED_BYTES_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
382387 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
383- . u64_counter ( "cache .oversized_bytes_total" )
388+ . u64_counter ( "s3_cache .oversized_bytes_total" )
384389 . with_description (
385390 "Total number of objects encountered exceeding the max cacheable size" ,
386391 )
@@ -389,7 +394,7 @@ pub(crate) fn record_cache_oversized(bytes: u64) {
389394
390395 static PROM_CACHE_OVERSIZED_BYTES_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
391396 let counter = IntCounter :: new (
392- "cache_oversized_bytes_total " ,
397+ "s3_cache_oversized_bytes_total " ,
393398 "Total number of objects encountered exceeding the max cacheable size" ,
394399 )
395400 . unwrap ( ) ;
@@ -410,7 +415,7 @@ pub(crate) fn record_cache_oversized(bytes: u64) {
410415pub ( crate ) fn record_unique_requested ( bytes : u64 ) {
411416 static CACHE_UNIQUE_REQUESTED_BYTES_HISTOGRAM : LazyLock < Histogram < u64 > > = LazyLock :: new ( || {
412417 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
413- . u64_histogram ( "cache .estimated_unique_bytes_histogram" )
418+ . u64_histogram ( "s3_cache .estimated_unique_bytes_histogram" )
414419 . with_description ( "Distribution of estimated unique object sizes" )
415420 . build ( )
416421 } ) ;
@@ -419,7 +424,7 @@ pub(crate) fn record_unique_requested(bytes: u64) {
419424 LazyLock :: new ( || {
420425 let histogram = prometheus:: Histogram :: with_opts (
421426 HistogramOpts :: new (
422- "cache_estimated_unique_bytes_histogram " ,
427+ "s3_cache_estimated_unique_bytes_histogram " ,
423428 "Distribution of estimated unique object sizes" ,
424429 )
425430 . buckets ( OBJECT_SIZE_BUCKETS . to_vec ( ) ) ,
@@ -433,14 +438,14 @@ pub(crate) fn record_unique_requested(bytes: u64) {
433438
434439 static CACHE_UNIQUE_REQUESTED_BYTES_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
435440 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
436- . u64_counter ( "cache .estimated_unique_bytes_total" )
441+ . u64_counter ( "s3_cache .estimated_unique_bytes_total" )
437442 . with_description ( "Estimated total bytes for unique keys accessed" )
438443 . build ( )
439444 } ) ;
440445
441446 static PROM_CACHE_UNIQUE_REQUESTED_BYTES_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
442447 let counter = IntCounter :: new (
443- "cache_estimated_unique_bytes_total " ,
448+ "s3_cache_estimated_unique_bytes_total " ,
444449 "Estimated total bytes for unique keys accessed" ,
445450 )
446451 . unwrap ( ) ;
@@ -461,14 +466,14 @@ pub(crate) fn record_unique_requested(bytes: u64) {
461466pub ( crate ) fn record_cache_invalidation ( ) {
462467 static CACHE_INVALIDATION_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
463468 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
464- . u64_counter ( "cache .invalidation_total" )
469+ . u64_counter ( "s3_cache .invalidation_total" )
465470 . with_description ( "Number of cache invalidations" )
466471 . build ( )
467472 } ) ;
468473
469474 static PROM_CACHE_INVALIDATION_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
470475 let counter =
471- IntCounter :: new ( "cache_invalidation_total " , "Number of cache invalidations" ) . unwrap ( ) ;
476+ IntCounter :: new ( "s3_cache_invalidation_total " , "Number of cache invalidations" ) . unwrap ( ) ;
472477 PROMETHEUS_REGISTRY
473478 . register ( Box :: new ( counter. clone ( ) ) )
474479 . unwrap ( ) ;
@@ -484,14 +489,14 @@ pub(crate) fn record_cache_invalidation() {
484489pub ( crate ) fn record_cache_mismatch ( ) {
485490 static CACHE_MISMATCH_ERROR_TOTAL : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
486491 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
487- . u64_counter ( "cache .mismatch_error_total" )
492+ . u64_counter ( "s3_cache .mismatch_error_total" )
488493 . with_description ( "Number of cache mismatches detected in dry-run mode" )
489494 . build ( )
490495 } ) ;
491496
492497 static PROM_CACHE_MISMATCH_ERROR_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
493498 let counter = IntCounter :: new (
494- "cache_mismatch_error_total " ,
499+ "s3_cache_mismatch_error_total " ,
495500 "Number of cache mismatches detected in dry-run mode" ,
496501 )
497502 . unwrap ( ) ;
@@ -510,14 +515,14 @@ pub(crate) fn record_cache_mismatch() {
510515pub ( crate ) fn record_upstream_error ( ) {
511516 static UPSTREAM_ERROR : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
512517 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
513- . u64_counter ( "cache .upstream_error" )
518+ . u64_counter ( "s3_cache .upstream_error" )
514519 . with_description ( "Number of upstream S3 errors" )
515520 . build ( )
516521 } ) ;
517522
518523 static PROM_UPSTREAM_ERROR : LazyLock < IntCounter > = LazyLock :: new ( || {
519524 let counter =
520- IntCounter :: new ( "cache_upstream_error_total " , "Number of upstream S3 errors" ) . unwrap ( ) ;
525+ IntCounter :: new ( "s3_cache_upstream_error_total " , "Number of upstream S3 errors" ) . unwrap ( ) ;
521526 PROMETHEUS_REGISTRY
522527 . register ( Box :: new ( counter. clone ( ) ) )
523528 . unwrap ( ) ;
@@ -533,7 +538,7 @@ pub(crate) fn record_upstream_error() {
533538pub ( crate ) fn record_buffering_error ( ) {
534539 static BUFFERING_ERROR : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
535540 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
536- . u64_counter ( "cache .buffering_error" )
541+ . u64_counter ( "s3_cache .buffering_error" )
537542 . with_description (
538543 "Number of buffering errors (object exceeded size limit during streaming)" ,
539544 )
@@ -542,7 +547,7 @@ pub(crate) fn record_buffering_error() {
542547
543548 static PROM_BUFFERING_ERROR : LazyLock < IntCounter > = LazyLock :: new ( || {
544549 let counter = IntCounter :: new (
545- "cache_buffering_error_total " ,
550+ "s3_cache_buffering_error_total " ,
546551 "Number of buffering errors (object exceeded size limit during streaming)" ,
547552 )
548553 . unwrap ( ) ;
@@ -561,14 +566,14 @@ pub(crate) fn record_buffering_error() {
561566pub ( crate ) fn record_cache_size_count ( size_count : usize ) {
562567 static CACHE_SIZE_COUNT : LazyLock < Gauge < u64 > > = LazyLock :: new ( || {
563568 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
564- . u64_gauge ( "cache .size_count" )
569+ . u64_gauge ( "s3_cache .size_count" )
565570 . with_description ( "Current number of objects in cache" )
566571 . build ( )
567572 } ) ;
568573
569574 static PROM_CACHE_SIZE_COUNT : LazyLock < IntGauge > = LazyLock :: new ( || {
570575 let gauge =
571- IntGauge :: new ( "cache_size_count " , "Current number of objects in cache" ) . unwrap ( ) ;
576+ IntGauge :: new ( "s3_cache_size_count " , "Current number of objects in cache" ) . unwrap ( ) ;
572577 PROMETHEUS_REGISTRY
573578 . register ( Box :: new ( gauge. clone ( ) ) )
574579 . unwrap ( ) ;
@@ -584,13 +589,13 @@ pub(crate) fn record_cache_size_count(size_count: usize) {
584589pub ( crate ) fn record_cache_size_bytes ( size_bytes : usize ) {
585590 static CACHE_SIZE_BYTES : LazyLock < Gauge < u64 > > = LazyLock :: new ( || {
586591 opentelemetry:: global:: meter ( CARGO_CRATE_NAME )
587- . u64_gauge ( "cache .size_bytes" )
592+ . u64_gauge ( "s3_cache .size_bytes" )
588593 . with_description ( "Current cache size in bytes" )
589594 . build ( )
590595 } ) ;
591596
592597 static PROM_CACHE_SIZE_BYTES : LazyLock < IntGauge > = LazyLock :: new ( || {
593- let gauge = IntGauge :: new ( "cache_size_bytes " , "Current cache size in bytes" ) . unwrap ( ) ;
598+ let gauge = IntGauge :: new ( "s3_cache_size_bytes " , "Current cache size in bytes" ) . unwrap ( ) ;
594599 PROMETHEUS_REGISTRY
595600 . register ( Box :: new ( gauge. clone ( ) ) )
596601 . unwrap ( ) ;
0 commit comments