diff --git a/CHANGELOG.md b/CHANGELOG.md index ed9b8f89..4d2da481 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,9 +21,14 @@ and start a new "In Progress" section above it. ## In progress + +## 0.122.0 + - `load_collection`: more consistent cube extent handling when a buffer is applied. ([#334](https://github.com/Open-EO/openeo-python-driver/issues/334)) - `load_collection`: collapse multiple `load_collection` calls into a single one in cases with buffers. ([#336](https://github.com/Open-EO/openeo-python-driver/issues/336)) - `export_workspace`: fix `KeyError: 'alternate'` upon merging into existing STAC collection ([Open-EO/openeo-geopyspark-driver#677](https://github.com/Open-EO/openeo-geopyspark-driver/issues/677)) +- Support custom default in `FlaskRequestCorrelationIdLogging.get_request_id()` + ## 0.121.0 diff --git a/openeo_driver/_version.py b/openeo_driver/_version.py index 854f51a9..22876e6e 100644 --- a/openeo_driver/_version.py +++ b/openeo_driver/_version.py @@ -1 +1 @@ -__version__ = "0.121.0a1" +__version__ = "0.122.0a1" diff --git a/openeo_driver/util/logging.py b/openeo_driver/util/logging.py index 4d0acfb7..3e16675c 100644 --- a/openeo_driver/util/logging.py +++ b/openeo_driver/util/logging.py @@ -44,6 +44,10 @@ LOG_HANDLER_ROTATING_FILE_JSON = "rotating_file_json" +# Sentinel value for unset values +_UNSET = object() + + def get_logging_config( *, root_handlers: Optional[List[str]] = None, @@ -280,12 +284,12 @@ def before_request(cls): setattr(flask.g, cls.FLASK_G_ATTR, cls._build_request_id()) @classmethod - def get_request_id(cls) -> str: + def get_request_id(cls, *, default: Union[str, None] = _UNSET) -> str: """Get request correlation id as stored in Flask request global `g`.""" if flask.has_request_context(): - return flask.g.get(cls.FLASK_G_ATTR, "n/a") + return flask.g.get(cls.FLASK_G_ATTR, default="n/a" if default is _UNSET else default) else: - return "no-request" + return "no-request" if default is _UNSET else default def filter(self, record: logging.LogRecord) -> bool: """Filter a log record (logging.Filter API).""" diff --git a/tests/util/test_logging.py b/tests/util/test_logging.py index c8212fed..04202402 100644 --- a/tests/util/test_logging.py +++ b/tests/util/test_logging.py @@ -29,7 +29,7 @@ _log = logging.getLogger(__name__) -def test_filter_flask_request_correlation_id_logging(): +def test_flask_request_correlation_id_logging(): with enhanced_logging(format="[%(req_id)s] %(message)s", context=LOGGING_CONTEXT_FLASK) as logs: app = flask.Flask(__name__) log = logging.getLogger(__name__) @@ -53,7 +53,7 @@ def hello(): assert "[123-456] Watch out!" in logs -def test_filter_flask_request_correlation_id_logging_overriding(): +def test_flask_request_correlation_id_logging_overriding(): """Don't overwrite existing request id.""" with enhanced_logging(format="[%(req_id)s] %(message)s", context=LOGGING_CONTEXT_FLASK) as logs: app = flask.Flask(__name__) @@ -78,6 +78,30 @@ def hello(): assert "[hello123] Watch out!" in logs +def test_flask_request_correlation_id_get_request_id_default_no_flask_context(): + assert FlaskRequestCorrelationIdLogging.get_request_id() == "no-request" + assert FlaskRequestCorrelationIdLogging.get_request_id(default="nope") == "nope" + assert FlaskRequestCorrelationIdLogging.get_request_id(default=None) is None + + +def test_flask_request_correlation_id_logging_default_flask_context(): + results = [] + + app = flask.Flask(__name__) + + @app.route("/hello") + def hello(): + results.append(FlaskRequestCorrelationIdLogging.get_request_id()) + results.append(FlaskRequestCorrelationIdLogging.get_request_id(default="nope")) + results.append(FlaskRequestCorrelationIdLogging.get_request_id(default=None)) + return "Hello world" + + with app.test_client() as client: + client.get("/hello") + + assert results == ["n/a", "nope", None] + + def test_filter_flask_user_id_logging(): with enhanced_logging(format="[%(user_id)s] %(message)s", context=LOGGING_CONTEXT_FLASK) as logs: app = flask.Flask(__name__)