-
Notifications
You must be signed in to change notification settings - Fork 5
[MNT] Add logging for user errors & warnings #522
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideCentralizes logging for configuration and runtime user-facing issues, replaces warnings/naive RuntimeErrors with structured logging plus a helper, and moves critical environment/config/configuration checks out of the FastAPI lifespan while updating tests to assert on logged messages instead of warnings. Sequence diagram for updated FastAPI startup configuration checkssequenceDiagram
actor Operator
participant Process as uvicorn_process
participant Main as main_module
participant Logger as logger_helper
participant Env as env_settings
participant Sec as security_module
participant Util as utility_module
participant App as FastAPI_app
Operator->>Process: start app.main:app
Process->>Main: import main module
Main->>Logger: logging.basicConfig(...)
Main->>Logger: get_logger(__name__)
Main->>Env: validate_environment_variables()
Env->>Logger: get_logger(__name__)
Env->>Logger: log_and_raise_error(RuntimeError, message) on invalid env
Logger-->>Process: log error and raise RuntimeError
Process-->>Operator: clean termination before FastAPI lifespan
Note over Main,Sec: If environment variables are valid
Main->>Sec: check_client_id()
Sec->>Logger: get_logger(__name__)
Sec->>Logger: log_and_raise_error(RuntimeError, message) on missing client_id
Logger-->>Process: log error and raise RuntimeError
Process-->>Operator: clean termination before FastAPI lifespan
Note over Main,Util: If auth config is valid
Main->>Util: request_data(vocabulary_url, err_message)
Util->>Logger: get_logger(__name__)
Util->>Util: httpx.Client().get(url)
Util-->>Main: data or
Util->>Logger: log_and_raise_error(RuntimeError, message) on HTTPError
Logger-->>Process: log error and raise RuntimeError
Note over Main,App: Only when all checks pass
Main->>App: create FastAPI app instance
App-->>Process: lifespan startup proceeds without noisy tracebacks
Class diagram for centralized logging helper and its usageclassDiagram
class LoggerHelper {
+get_logger(name: str) logging.Logger
+log_and_raise_error(logger: logging.Logger, exception_type: Type~Exception~, message: str) None
}
class MainModule {
+logger: logging.Logger
+validate_environment_variables() None
}
class UtilityModule {
+logger: logging.Logger
+request_data(url: str, err_message: str) Any
}
class SecurityModule {
+logger: logging.Logger
+check_client_id() None
}
class CrudModule {
+logger: logging.Logger
+get_terms(...) Any
}
LoggerHelper <.. MainModule : uses
LoggerHelper <.. UtilityModule : uses
LoggerHelper <.. SecurityModule : uses
LoggerHelper <.. CrudModule : uses
MainModule o-- SecurityModule : calls check_client_id
MainModule o-- UtilityModule : calls request_data
CrudModule --> UtilityModule : uses logging for term warnings
class Logging {
+basicConfig(level: int, format: str, datefmt: str) None
+getLogger(name: str) logging.Logger
}
Logging <.. LoggerHelper : wraps
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #522 +/- ##
==========================================
+ Coverage 97.23% 97.30% +0.07%
==========================================
Files 33 34 +1
Lines 1267 1301 +34
==========================================
+ Hits 1232 1266 +34
Misses 35 35 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@sourcery-ai review |
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.
We've reviewed this pull request using the Sourcery rules engine
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.
We've reviewed this pull request using the Sourcery rules engine
surchs
left a comment
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.
Thanks @alyssadai, commenting here before I have time for a full review
surchs
left a comment
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.
Thanks @alyssadai for working on this and sorry review took so long.
The actual logging looks great. I'm requesting changes for two reasons:
- 95% of this PR is about an incidental problem that wasn't part of the issue, i.e. "prevent traceback during app startup". This really should not have been in the same PR. I'm not convinced this is a major problem, but if it is, we should discuss it first as a team
- The solution you have chosen to prevent "traceback during app startup" is quite complex and introduces a number of additional side effects and workarounds for testing etc. I don't think that's proportional to the issue you are trying to prevent
So I'd say: please revert the workarounds in life-cycle so this PR is about adding consistent logging. And then we can discuss how to deal with startup tracebacks separately
|
OK, responding to myself here: The original issue was indeed about fixing the startup traceback problem, not just about logging. Sorry for missing that context. I don't know if we can find a simpler solution than what is in the PR atm to achieve this, but I'm still a bit uncomfortable with the complexity to get around the traceback. |
|
After discussion, to avoid a difficult-to-maintain workaround, we have decided to keep the old informative error raising behaviour (which will ensure that the app exits when appropriate) and simply emit an additional error log with the same message. |
|
@sourcery-ai review |
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.
We've reviewed this pull request using the Sourcery rules engine
surchs
left a comment
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.
Thanks @alyssadai, looks great!
🧑🍳
| ] | ||
| assert len(warnings) == 1 | ||
| assert ( | ||
| "API was launched without providing any values for the NB_API_ALLOWED_ORIGINS environment variable" |
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.
🍒 I actually like lifting the expected message into a constant during test setup, so you don't have to go search for the magic string in the assert.
| ] | ||
| assert len(warnings) == 1 | ||
| assert ( | ||
| "does not come from a vocabulary recognized by Neurobagel" |
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.
🍒 same here, I like the expected_message = ... bit better
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.
and more consistent if we do it in all the tests
Changes proposed in this pull request:
warnings.warn(UserWarning) app loggingChecklist
This section is for the PR reviewer
[ENH],[FIX],[REF],[TST],[CI],[MNT],[INF],[MODEL],[DOC]) (see our Contributing Guidelines for more info)skip-release(to be applied by maintainers only)Closes #XXXXFor new features:
For bug fixes:
Summary by Sourcery
Use centralized logging for configuration and runtime user-facing errors and perform configuration and auth checks before the FastAPI lifespan starts to allow clean process termination.
Bug Fixes:
Enhancements:
Summary by Sourcery
Use centralized logging for configuration and runtime user-facing errors and ensure startup failures terminate cleanly without noisy tracebacks.
Bug Fixes:
Enhancements:
Tests: