-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a metrics endpoint that computes updated metric values on each request. On each request we: - fetch runner groups from Redis - get all runners for each group - update a Gauge for the runners_count on each group - render out the prometheus metrics in standard format Resolves #625
- Loading branch information
1 parent
94e45c1
commit 86ae1ba
Showing
5 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter | ||
from fastapi.responses import PlainTextResponse | ||
from prometheus_client import Gauge, generate_latest | ||
|
||
from runner_manager import RunnerGroup | ||
from runner_manager.models.runner import Runner | ||
|
||
router = APIRouter(prefix="/metrics") | ||
|
||
runners_count = Gauge("runners_count", "Number of runners", ["runner_group"]) | ||
|
||
|
||
@router.get("/", response_class=PlainTextResponse) | ||
def compute_metrics() -> PlainTextResponse: | ||
groups: List[RunnerGroup] = RunnerGroup.find().all() | ||
for group in groups: | ||
runners: List[Runner] = group.get_runners() | ||
runners_count.labels(runner_group=group.name).set(len(runners)) | ||
metrics = generate_latest().decode() | ||
return PlainTextResponse(content=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
from runner_manager import RunnerGroup | ||
from runner_manager.clients.github import GitHub | ||
|
||
|
||
def test_metrics_endpoint(client: TestClient, runner_group: RunnerGroup): | ||
runner_group.save() | ||
response = client.get("/metrics") | ||
assert response.status_code == 200 | ||
runner_lines = [ | ||
line for line in response.text.splitlines() if line.startswith("runners_") | ||
] | ||
assert f'runners_count{{runner_group="{runner_group.name}"}} 0.0' in runner_lines | ||
|
||
|
||
def test_runner_count(client: TestClient, runner_group: RunnerGroup, github: GitHub): | ||
runner_group.save() | ||
want = len(runner_group.get_runners()) | ||
|
||
response = client.get("/metrics") | ||
assert response.status_code == 200 | ||
got = [line for line in response.text.splitlines() if line.startswith("runners_")] | ||
print(want) | ||
assert f'runners_count{{runner_group="{runner_group.name}"}} {want:.1f}' in got | ||
|
||
runner_group.max = 2 | ||
runner_group.min = 1 | ||
runner_group.save() | ||
|
||
want = 0.0 | ||
response = client.get("/metrics") | ||
assert response.status_code == 200 | ||
before = [ | ||
line for line in response.text.splitlines() if line.startswith("runners_") | ||
] | ||
assert f'runners_count{{runner_group="{runner_group.name}"}} {want:.1f}' in before | ||
|
||
runner = runner_group.create_runner(github) | ||
assert runner is not None | ||
|
||
want = 1.0 | ||
response = client.get("/metrics") | ||
assert response.status_code == 200 | ||
after_create = [ | ||
line for line in response.text.splitlines() if line.startswith("runners_") | ||
] | ||
assert ( | ||
f'runners_count{{runner_group="{runner_group.name}"}} {want:.1f}' | ||
in after_create | ||
) | ||
|
||
runner = runner_group.delete_runner(runner, github) | ||
assert runner is not None | ||
|
||
want = 0.0 | ||
response = client.get("/metrics") | ||
assert response.status_code == 200 | ||
after_delete = [ | ||
line for line in response.text.splitlines() if line.startswith("runners_") | ||
] | ||
assert ( | ||
f'runners_count{{runner_group="{runner_group.name}"}} {want:.1f}' | ||
in after_delete | ||
) |