-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for collecting prometheus metrics
- Loading branch information
Showing
5 changed files
with
54 additions
and
6 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,42 @@ | ||
import secrets | ||
|
||
from typing import Annotated | ||
|
||
from fastapi import FastAPI, Response, Depends, HTTPException, status | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
|
||
from .dependencies import get_settings | ||
from prometheus_client import ( | ||
CONTENT_TYPE_LATEST, | ||
CollectorRegistry, | ||
generate_latest, | ||
) | ||
|
||
security = HTTPBasic() | ||
|
||
|
||
def mount_metrics(app: FastAPI, registry: CollectorRegistry): | ||
def metrics( | ||
credentials: Annotated[HTTPBasicCredentials, Depends(security)], | ||
settings=Depends(get_settings), | ||
): | ||
is_correct_username = secrets.compare_digest( | ||
credentials.username.encode("utf8"), b"prom" | ||
) | ||
is_correct_password = secrets.compare_digest( | ||
credentials.password.encode("utf8"), | ||
settings.prometheus_metrics_password.encode("utf-8"), | ||
) | ||
if not (is_correct_username and is_correct_password): | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect username or password", | ||
headers={"WWW-Authenticate": "Basic"}, | ||
) | ||
|
||
resp = Response(content=generate_latest(registry)) | ||
resp.headers["Content-Type"] = CONTENT_TYPE_LATEST | ||
return resp | ||
|
||
endpoint = "/metrics" | ||
app.get(endpoint, include_in_schema=True, tags=None)(metrics) |
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
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