Add opt-in application observability#19
Merged
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR introduces an opt-in, application-scoped observability layer for Guardette, centered on safe structured stdout events (request/response metadata + low-cardinality “metric” events) and a FastAPI middleware for request correlation IDs, while keeping exporter/deployment concerns out of this repo.
Changes:
- Added
guardette.observabilitypackage (env-driven config, safe header allowlist + bounding, event/metric emitters, and FastAPI middleware with request IDs). - Integrated observability into the proxy FastAPI wiring and tightened JSON logging to a safe allowlist of extra fields (plus dedicated
observability_eventpayloads). - Added documentation and examples for new environment flags and included focused observability tests.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_observability.py | Adds tests for disabled-by-default behavior, env flag parsing, safe request/response logging, request-id correlation, and header sanitization. |
| src/guardette/proxy.py | Removes ad-hoc request lifecycle logging, wires observability into FastAPI setup, and records auth/upstream metrics. |
| src/guardette/observability/middleware.py | Implements request-boundary middleware for request IDs, safe event emission, and per-request metrics recording. |
| src/guardette/observability/headers.py | Adds strict safe-header allowlist and value bounding for logged headers. |
| src/guardette/observability/events.py | Defines structured event logging and low-cardinality metric-event emission helpers. |
| src/guardette/observability/config.py | Adds explicit boolean env parsing and derived “active” flags for observability components. |
| src/guardette/observability/init.py | Provides configure_observability() entrypoint to install middleware and initialize logging/recorders once per app. |
| src/guardette/logging.py | Restricts serialized extra fields and supports structured observability events. |
| src/guardette/constants.py | Adds X-Guardette-Request-Id header constant for correlation. |
| README.md | Documents new observability env vars and the high-level safety/behavior contract. |
| .env.example | Adds example observability environment variables. |
| .client/adr/observability-options.md | Adds ADR documenting observability goals, constraints, and chosen approach. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+144
to
+148
| logger.info( | ||
| "Guardette policy loaded", | ||
| extra={ | ||
| "source_count": len(self.policy.sources), | ||
| "rule_count": sum(len(source.rules) for source in self.policy.sources), |
Comment on lines
+299
to
+303
| def _record_auth_failure(request: Request) -> None: | ||
| observability = getattr(request.app.state, "observability", None) | ||
| if observability is not None: | ||
| observability.metrics.record_auth_failure("client") | ||
|
|
Comment on lines
+305
to
+308
| def _record_upstream(request: Request, outcome: str, status_code: int | None) -> None: | ||
| observability = getattr(request.app.state, "observability", None) | ||
| if observability is not None: | ||
| observability.metrics.record_upstream(outcome, status_code) |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Validation
This PR is based on the synchronized
upstream-maindefault branch.