-
Notifications
You must be signed in to change notification settings - Fork 79
Feature/metrics phase1: per-asset logging + dashboards #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a33ba22
docs: add metrics spec for Prometheus phase‑1
50e708a
chore: add baseline Prometheus scrape config
13b749e
Add Prometheus metrics, per-asset access logging, and Grafana panels
e9af280
Review fixes: remove user_id, monitoring profile, env ports, doc, ses…
b00657f
Expand resource type detection to all router groups
3ceb6a8
Merge branch 'develop' into feature/metrics-phase1
PGijsbers 3970c41
metrics: versioned /stats/v1/top, robust access logging via parser, t…
0eac24d
middleware:fix asset path, tests:addition of test access log
2879ae0
metrics: fix pre-commit issues, correct access_stats router typing, d…
e5dfe11
main: use importlib.metadata instead of pkg_resources (fix mypy)
bf11207
metrics: expand docs; add/default Grafana provisioning; stats router …
8f57972
Merge branch 'develop' into feature/metrics-phase1
PGijsbers 577e8f0
Pre-commit missed by GitHub merge
PGijsbers d59f3d7
Only keep the identifier of the asset, without path info
PGijsbers c894717
Do not register the middleware with subapps since it leads to duplicates
PGijsbers da3b578
type resolution from identifier prefix; grafana: default API metrics …
094d217
grafana and prometheus bind mounts
5a52286
Update monitoring information
PGijsbers 914b61a
Update table definition
PGijsbers c57fc93
Allow arbitrary url_prefix based on the server configuration
PGijsbers 88e23c9
Update path parsing to be more strict and use whitelists
PGijsbers dea0449
Add constraint to length of identifier
PGijsbers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| # Metrics & Monitoring | ||
|
|
||
| * **/metrics** – Prometheus exposition created by prometheus_fastapi_instrumentator. | ||
| * **/stats/top/{resource_type}** – JSON list '[asset_id, hits]', success only. | ||
| * **AssetAccessLog** – table schema. | ||
| * Quickstart – 'docker compose --profile monitoring up -d'. | ||
| * Queries – PromQL for per-endpoint, MySQL for per-asset popularity. |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or 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,7 @@ | ||
| global: | ||
| scrape_interval: 1s | ||
| scrape_configs: | ||
| - job_name: 'aiod_rest_api' | ||
| metrics_path: /metrics | ||
| static_configs: | ||
| - targets: ['app:8000'] |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,10 @@ | ||
| from datetime import datetime | ||
| from sqlmodel import SQLModel, Field | ||
|
|
||
|
|
||
| class AssetAccessLog(SQLModel, table=True): # type: ignore[call-arg] | ||
| id: int | None = Field(default=None, primary_key=True) | ||
| asset_id: str | ||
| resource_type: str # “datasets”, “models”, etc. | ||
| status: int # HTTP status code | ||
| accessed_at: datetime = Field(default_factory=datetime.utcnow, index=True) |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,33 @@ | ||
| from starlette.middleware.base import BaseHTTPMiddleware | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
|
|
||
| from database.session import DbSession | ||
| from database.model.access.access_log import AssetAccessLog | ||
|
|
||
| from middleware.resource_types import all_resource_types | ||
|
|
||
| VALID_TYPES = all_resource_types() | ||
|
|
||
|
|
||
| class AccessLogMiddleware(BaseHTTPMiddleware): | ||
| """Write one AssetAccessLog row for /datasets/<id> and /models/<id>.""" | ||
|
|
||
| async def dispatch(self, request: Request, call_next): | ||
| response: Response = await call_next(request) | ||
|
|
||
| segments = request.url.path.strip("/").split("/") | ||
| if len(segments) >= 2 and segments[0] in VALID_TYPES: | ||
| resource_type = segments[0] | ||
| asset_id = "/".join(segments[1:]) | ||
|
|
||
| entry = AssetAccessLog( | ||
| asset_id=asset_id, | ||
| resource_type=resource_type, | ||
| status=response.status_code, | ||
| ) | ||
| with DbSession() as sess: | ||
| sess.add(entry) | ||
| sess.commit() | ||
|
|
||
| return response |
This file contains hidden or 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,21 @@ | ||
| from typing import Set | ||
| from routers import ( | ||
| resource_routers, | ||
| parent_routers, | ||
| uploader_routers, | ||
| ) | ||
|
|
||
|
|
||
| def all_resource_types() -> Set[str]: | ||
| """ | ||
| Gather every plural resource name exposed by *any* router group, | ||
| e.g. {'datasets', 'ml_models', 'computational_assets', …}. | ||
| Uses getattr guard so it doesn’t crash when a router lacks the attribute. | ||
| """ | ||
| router_lists = resource_routers.router_list | ||
| types: Set[str] = set() | ||
| for router in router_lists: | ||
| val = getattr(router, "resource_name_plural", None) | ||
| if val: | ||
| types.add(val) | ||
| return types |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.