|
| 1 | +"""Prometheus metrics for S3Proxy.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from prometheus_client import Counter, Gauge, Histogram |
| 6 | + |
| 7 | +# Request metrics |
| 8 | +REQUEST_COUNT = Counter( |
| 9 | + "s3proxy_requests_total", |
| 10 | + "Total number of requests", |
| 11 | + ["method", "operation", "status"], |
| 12 | +) |
| 13 | + |
| 14 | +REQUEST_DURATION = Histogram( |
| 15 | + "s3proxy_request_duration_seconds", |
| 16 | + "Request duration in seconds", |
| 17 | + ["method", "operation"], |
| 18 | + buckets=(0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0), |
| 19 | +) |
| 20 | + |
| 21 | +REQUESTS_IN_FLIGHT = Gauge( |
| 22 | + "s3proxy_requests_in_flight", |
| 23 | + "Number of requests currently being processed", |
| 24 | + ["method"], |
| 25 | +) |
| 26 | + |
| 27 | +# Memory/Concurrency metrics |
| 28 | +MEMORY_RESERVED_BYTES = Gauge( |
| 29 | + "s3proxy_memory_reserved_bytes", |
| 30 | + "Currently reserved memory in bytes", |
| 31 | +) |
| 32 | + |
| 33 | +MEMORY_LIMIT_BYTES = Gauge( |
| 34 | + "s3proxy_memory_limit_bytes", |
| 35 | + "Configured memory limit in bytes", |
| 36 | +) |
| 37 | + |
| 38 | +MEMORY_REJECTIONS = Counter( |
| 39 | + "s3proxy_memory_rejections_total", |
| 40 | + "Total number of requests rejected due to memory limits", |
| 41 | +) |
| 42 | + |
| 43 | +# Encryption metrics |
| 44 | +ENCRYPTION_OPERATIONS = Counter( |
| 45 | + "s3proxy_encryption_operations_total", |
| 46 | + "Total number of encryption/decryption operations", |
| 47 | + ["operation"], |
| 48 | +) |
| 49 | + |
| 50 | +BYTES_ENCRYPTED = Counter( |
| 51 | + "s3proxy_bytes_encrypted_total", |
| 52 | + "Total bytes encrypted", |
| 53 | +) |
| 54 | + |
| 55 | +BYTES_DECRYPTED = Counter( |
| 56 | + "s3proxy_bytes_decrypted_total", |
| 57 | + "Total bytes decrypted", |
| 58 | +) |
| 59 | + |
| 60 | + |
| 61 | +def get_operation_name(method: str, path: str, query: str) -> str: |
| 62 | + """Derive S3 operation name from request attributes. |
| 63 | +
|
| 64 | + Args: |
| 65 | + method: HTTP method (GET, PUT, POST, DELETE, HEAD). |
| 66 | + path: Request path. |
| 67 | + query: Query string. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + S3 operation name for metrics labeling. |
| 71 | + """ |
| 72 | + is_bucket_only = "/" not in path.strip("/") and bool(path.strip("/")) |
| 73 | + is_root = path.strip("/") == "" |
| 74 | + |
| 75 | + # Root path |
| 76 | + if is_root: |
| 77 | + return "ListBuckets" |
| 78 | + |
| 79 | + # Batch delete |
| 80 | + if "delete" in query and method == "POST": |
| 81 | + return "DeleteObjects" |
| 82 | + |
| 83 | + # Multipart operations |
| 84 | + if "uploadId" in query: |
| 85 | + if method == "GET" and "partNumber" not in query: |
| 86 | + return "ListParts" |
| 87 | + if method == "PUT": |
| 88 | + if "x-amz-copy-source" in query: |
| 89 | + return "UploadPartCopy" |
| 90 | + return "UploadPart" |
| 91 | + if method == "POST": |
| 92 | + return "CompleteMultipartUpload" |
| 93 | + if method == "DELETE": |
| 94 | + return "AbortMultipartUpload" |
| 95 | + |
| 96 | + # List/Create multipart uploads |
| 97 | + if "uploads" in query: |
| 98 | + if method == "GET": |
| 99 | + return "ListMultipartUploads" |
| 100 | + if method == "POST": |
| 101 | + return "CreateMultipartUpload" |
| 102 | + |
| 103 | + # Bucket operations |
| 104 | + if is_bucket_only: |
| 105 | + if "location" in query and method == "GET": |
| 106 | + return "GetBucketLocation" |
| 107 | + if method == "PUT": |
| 108 | + return "CreateBucket" |
| 109 | + if method == "DELETE": |
| 110 | + return "DeleteBucket" |
| 111 | + if method == "HEAD": |
| 112 | + return "HeadBucket" |
| 113 | + if method == "GET": |
| 114 | + return "ListObjects" |
| 115 | + |
| 116 | + # Object tagging |
| 117 | + if "tagging" in query: |
| 118 | + if method == "GET": |
| 119 | + return "GetObjectTagging" |
| 120 | + if method == "PUT": |
| 121 | + return "PutObjectTagging" |
| 122 | + if method == "DELETE": |
| 123 | + return "DeleteObjectTagging" |
| 124 | + |
| 125 | + # Standard object operations |
| 126 | + if method == "GET": |
| 127 | + return "GetObject" |
| 128 | + if method == "PUT": |
| 129 | + return "PutObject" |
| 130 | + if method == "HEAD": |
| 131 | + return "HeadObject" |
| 132 | + if method == "DELETE": |
| 133 | + return "DeleteObject" |
| 134 | + |
| 135 | + return "Unknown" |
0 commit comments