-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import prometheus client to collect metrics
Signed-off-by: Kaiyuan Hu <[email protected]>
- Loading branch information
Showing
4 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import os | ||
import time | ||
|
||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from prometheus_client import Counter, Histogram, Summary | ||
|
||
|
||
# Define metrics | ||
latencies = {} | ||
latencies['all'] = [] | ||
|
||
requests_total = Counter('requests_total', 'Cumulative requests') | ||
requests_success_total = Counter('requests_success_total', 'Cumulative successful requests') | ||
requests_failed_total = Counter('requests_failed_total', 'Cumulative failed requests') | ||
|
||
endpoint_requests_total = Counter('endpoint_requests_total', 'Cumulative requests of each endpoint', ['endpoint']) | ||
endpoint_requests_success_total = Counter('endpoint_requests_success_total', 'Cumulative successful requests of each endpoint', ['endpoint']) | ||
endpoint_requests_failed_total = Counter('endpoint_requests_failed_total', 'Cumulative failed requests of each endpoint', ['endpoint']) | ||
|
||
latency_seconds_histogram = Histogram('latency_seconds_histogram', 'Request process latency histogram') | ||
endpoint_latency_seconds_histogram = Histogram( | ||
'endpoint_latency_seconds_histogram', 'Request process latency histogram of each endpoint', ['endpoint'] | ||
) | ||
|
||
latency_seconds_summary = Summary('latency_seconds_summary', 'Request process latency summary') | ||
endpoint_latency_seconds_summary = Summary('endpoint_latency_seconds_summary', 'Request process latency summary of each endpoint', ['endpoint']) | ||
|
||
|
||
def enable_moniter(app, max_observation): | ||
|
||
# Define middleware to collect metrics | ||
class RequestMetricsMiddleware(BaseHTTPMiddleware): | ||
""" | ||
Middleware to process requests. | ||
""" | ||
async def dispatch(self, request, call_next): | ||
path = request.scope.get('path') | ||
is_req = path != '/metrics' | ||
|
||
if not is_req: | ||
try: | ||
response = await call_next(request) | ||
return response | ||
except Exception as e: | ||
raise e | ||
|
||
begin = time.time() | ||
requests_total.inc() | ||
endpoint_requests_total.labels(path).inc() | ||
try: | ||
response = await call_next(request) | ||
if response.status_code / 100 < 4: | ||
requests_success_total.inc() | ||
endpoint_requests_success_total.labels(path).inc() | ||
end = time.time() | ||
if path in latencies: | ||
latencies[path].append(end - begin) | ||
latencies[path] = latencies[path][-max_observation:] | ||
else: | ||
latencies[path] = [end - begin] | ||
latencies['all'].append(end - begin) | ||
latencies['all'] = latencies['all'][-max_observation:] | ||
latency_seconds_histogram.observe(end - begin) | ||
endpoint_latency_seconds_histogram.labels(path).observe(end - begin) | ||
latency_seconds_summary.observe(end - begin) | ||
endpoint_latency_seconds_summary.labels(path).observe(end - begin) | ||
return response | ||
else: | ||
requests_failed_total.inc() | ||
endpoint_requests_failed_total.labels(path).inc() | ||
except Exception as e: | ||
requests_failed_total.inc() | ||
endpoint_requests_failed_total.labels(path).inc() | ||
raise e | ||
|
||
app.add_middleware(RequestMetricsMiddleware) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ uvicorn | |
towhee>=1.1.0 | ||
pymilvus | ||
elasticsearch>=8.0.0 | ||
prometheus-client |