-
Notifications
You must be signed in to change notification settings - Fork 1
Add JSON logging and Prometheus metrics from the service template #110
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,38 @@ | ||
| """ | ||
| Route reporting what is actually deployed | ||
|
|
||
| Not included in the schema: it is an operator diagnostic, not part of the public API. | ||
| The build stamps here are the same values the wide events carry. | ||
| """ | ||
|
|
||
| import os | ||
| from importlib.metadata import version | ||
|
|
||
| from fastapi import APIRouter | ||
| from pydantic import BaseModel | ||
|
|
||
| router = APIRouter(prefix="/deploy", include_in_schema=False) | ||
|
|
||
|
|
||
| class DeployInfo(BaseModel): | ||
| """ | ||
| Information about the running deployment | ||
| """ | ||
|
|
||
| version: str | ||
| git_commit: str | None | ||
| image_tag: str | None | ||
| build_time: str | None | ||
|
|
||
|
|
||
| @router.get("/info") | ||
| def deploy_info() -> DeployInfo: | ||
| """ | ||
| Return the version and build stamps baked into the image | ||
| """ | ||
| return DeployInfo( | ||
| version=version("ref-backend"), | ||
| git_commit=os.environ.get("GIT_COMMIT") or None, | ||
| image_tag=os.environ.get("IMAGE_TAG") or None, | ||
| build_time=os.environ.get("BUILD_TIME") or None, | ||
| ) |
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,52 @@ | ||
| """ | ||
| Liveness and readiness probes | ||
|
|
||
| Mounted at the root rather than under the versioned API, so an orchestrator can probe the | ||
| process without knowing anything about the API surface. | ||
| """ | ||
|
|
||
| import inspect | ||
| import logging | ||
|
|
||
| from fastapi import APIRouter, HTTPException, Request | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| router = APIRouter(include_in_schema=False) | ||
|
|
||
|
|
||
| @router.get("/livez") | ||
| def liveness_probe() -> dict[str, str]: | ||
| """ | ||
| Report process liveness without touching any dependency | ||
| """ | ||
| return {"status": "alive"} | ||
|
|
||
|
|
||
| @router.get("/readyz") | ||
| async def readiness_probe(request: Request) -> dict[str, str]: | ||
| """ | ||
| Report readiness by running the checks registered on ``app.state.readiness_checks`` | ||
|
|
||
| Each check is a callable that returns a falsy value or raises to signal it is not ready. | ||
| A check may be async, in which case what it returns is awaited. | ||
| An empty (or unset) check list means the service is always ready. | ||
| """ | ||
| checks = getattr(request.app.state, "readiness_checks", []) | ||
| failures: dict[str, str] = {} | ||
| for check in checks: | ||
| name = getattr(check, "__name__", repr(check)) | ||
| try: | ||
| ok = check() | ||
| if inspect.isawaitable(ok): | ||
| ok = await ok | ||
| except Exception as exc: | ||
| logger.warning(f"Readiness check {name} failed", exc_info=True) | ||
| failures[name] = str(exc) | ||
| continue | ||
| if not ok: | ||
| failures[name] = "check returned a falsy result" | ||
|
|
||
| if failures: | ||
| raise HTTPException(status_code=503, detail=failures) | ||
| return {"status": "ready"} | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: Climate-REF/ref-app
Length of output: 8383
🏁 Script executed:
Repository: Climate-REF/ref-app
Length of output: 5214
Information Disclosure (CWE-209): Generation of Error Message Containing Sensitive Information
Reachability: External · Exploitability: Moderate
Do not return readiness-check exception text.
When a check raises,
str(exc)is included in the/readyz503response. Keep the detailed exception in logs and return a stable generic503response body.