From dbd08807628f0b5cd62963e33412b062130736e1 Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Wed, 4 Nov 2026 22:46:23 -0800 Subject: [PATCH 1/2] fix: treat an empty correlation ID header as missing An empty X-Correlation-ID header was returned as the correlation ID, so the next header was never tried and no ID was generated; records were logged with '-'. Skip empty header values. Fixes #149 --- CHANGELOG.md | 2 ++ json_logging/util.py | 3 ++- tests/test_flask.py | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83d72de..a857124 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). ### Fixed - Log records created with `stack_info=True` now carry the stack in a `stack_info` field; it was dropped before (#150). +- An empty correlation ID header (e.g. `X-Correlation-ID: `) is now treated as missing: the next header in + `CORRELATION_ID_HEADERS` is tried and an ID is generated if none is found, instead of logging `-` (#149). - `get_request_logger()` now raises the documented `RuntimeError` instead of `AttributeError` when called before `init_request_instrument()`. ## 1.6.0 - 2026-10-21 diff --git a/json_logging/util.py b/json_logging/util.py index 062bde8..55c116a 100644 --- a/json_logging/util.py +++ b/json_logging/util.py @@ -200,7 +200,8 @@ def get_request_from_call_stack(self, within_formatter=False): def _get_correlation_id_in_request_header(request_adapter, request): for header in json_logging.CORRELATION_ID_HEADERS: value = request_adapter.get_http_header(request, header) - if value is not None: + # An empty header carries no ID, try the next header or generate one + if value: return value return None diff --git a/tests/test_flask.py b/tests/test_flask.py index ecf0186..52aca51 100644 --- a/tests/test_flask.py +++ b/tests/test_flask.py @@ -147,6 +147,28 @@ def test_get_correlation_id(client_and_log_handler): assert response.json["correlation_id"] == "abc-def" +def test_empty_correlation_id_header_is_ignored(client_and_log_handler): + """An empty header carries no ID, so a new one is generated instead of logging EMPTY_VALUE""" + import json_logging + + api_client, handler = client_and_log_handler + + response = api_client.get("/get-correlation-id", headers={"X-Correlation-Id": ""}) + + assert response.status_code == 200 + assert response.json["correlation_id"] not in ("", json_logging.EMPTY_VALUE) + + +def test_empty_correlation_id_header_falls_back_to_next_header(client_and_log_handler): + """An empty X-Correlation-ID does not hide the ID in X-Request-ID""" + api_client, handler = client_and_log_handler + + response = api_client.get("/get-correlation-id", headers={"X-Correlation-Id": "", "X-Request-ID": "abc-def"}) + + assert response.status_code == 200 + assert response.json["correlation_id"] == "abc-def" + + def test_extra_property(client_and_log_handler): """Test adding an extra property to a log message""" api_client, handler = client_and_log_handler From 82d839be9cac63cd8c2b351ee79166659d120d1e Mon Sep 17 00:00:00 2001 From: Srinivas Chippagiri Date: Thu, 5 Nov 2026 23:12:38 -0800 Subject: [PATCH 2/2] docs: say the correlation ID is only generated when enabled --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a857124..27cc933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). - Log records created with `stack_info=True` now carry the stack in a `stack_info` field; it was dropped before (#150). - An empty correlation ID header (e.g. `X-Correlation-ID: `) is now treated as missing: the next header in - `CORRELATION_ID_HEADERS` is tried and an ID is generated if none is found, instead of logging `-` (#149). + `CORRELATION_ID_HEADERS` is tried, and if none is found an ID is generated when + `CREATE_CORRELATION_ID_IF_NOT_EXISTS` is enabled (#149). - `get_request_logger()` now raises the documented `RuntimeError` instead of `AttributeError` when called before `init_request_instrument()`. ## 1.6.0 - 2026-10-21