Skip to content

Commit 88f0754

Browse files
committed
feat: add prometheus metrics for s3 cache
1 parent 9af0dcc commit 88f0754

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/server/s3_utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import boto3
1313
from botocore.exceptions import ClientError
14+
from prometheus_client import Counter
1415

1516
from server.models import S3Metadata
1617

@@ -21,6 +22,10 @@
2122
# Initialize logger for this module
2223
logger = logging.getLogger(__name__)
2324

25+
_cache_lookup_counter = Counter("gitingest_cache_lookup", "Number of cache lookups", ["url"])
26+
_cache_hit_counter = Counter("gitingest_cache_hit", "Number of cache hits", ["url"])
27+
_cache_miss_counter = Counter("gitingest_cache_miss", "Number of cache misses", ["url"])
28+
2429

2530
class S3UploadError(Exception):
2631
"""Custom exception for S3 upload failures."""
@@ -424,7 +429,7 @@ def check_s3_object_exists(s3_file_path: str) -> bool:
424429
"""
425430
if not is_s3_enabled():
426431
return False
427-
432+
_cache_lookup_counter.labels(url=s3_file_path).inc()
428433
try:
429434
s3_client = create_s3_client()
430435
bucket_name = get_s3_bucket_name()
@@ -435,13 +440,16 @@ def check_s3_object_exists(s3_file_path: str) -> bool:
435440
# Object doesn't exist if we get a 404 error
436441
error_code = err.response.get("Error", {}).get("Code")
437442
if error_code == "404":
443+
_cache_miss_counter.labels(url=s3_file_path).inc()
438444
return False
439445
# Re-raise other errors (permissions, etc.)
440446
raise
441447
except Exception:
442448
# For any other exception, assume object doesn't exist
449+
_cache_miss_counter.labels(url=s3_file_path).inc()
443450
return False
444451
else:
452+
_cache_hit_counter.labels(url=s3_file_path).inc()
445453
return True
446454

447455

0 commit comments

Comments
 (0)