-
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.
Support metrics & health endpoints PR#820 Issue#818
Add support for genearting metrics using prometheus. Support HTTP basic auth for scraping metrics from the endpoint. * Add support for collecting prometheus metrics * Add health endpoint * Implement more test for the rest of the codebase reaching 100% code coverage * Bump to release candidate version * Run legacy api tests only on master since they take too long
- Loading branch information
Showing
13 changed files
with
331 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
name: test fastpath | ||
on: push | ||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
test: | ||
|
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
name: test legacy/ooniapi | ||
on: | ||
push: | ||
branches: | ||
- master | ||
workflow_dispatch: | ||
inputs: | ||
debug_enabled: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
VERSION = "0.5.0dev1" | ||
VERSION = "0.5.0rc0" |
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
Oops, something went wrong.