Skip to content

Commit

Permalink
support custom default in `FlaskRequestCorrelationIdLogging.get_reque…
Browse files Browse the repository at this point in the history
…st_id`
  • Loading branch information
soxofaan committed Dec 19, 2024
1 parent 87ecfae commit ad73911
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion openeo_driver/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.121.0a1"
__version__ = "0.122.0a1"
10 changes: 7 additions & 3 deletions openeo_driver/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)."""
Expand Down
28 changes: 26 additions & 2 deletions tests/util/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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__)
Expand All @@ -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__)
Expand Down

0 comments on commit ad73911

Please sign in to comment.