Skip to content

Commit e16e564

Browse files
author
Dylan Huang
committed
Enhance ElasticsearchDirectHttpHandler to prioritize rollout_id from log record extra data over environment variable. Added test to verify that the extra rollout_id correctly overrides the environment variable during logging.
1 parent e2734c2 commit e16e564

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

eval_protocol/logging/elasticsearch_direct_http_handler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ def emit(self, record: logging.LogRecord) -> None:
5151
print(f"Error preparing log for Elasticsearch: {e}")
5252

5353
def _get_rollout_id(self, record: logging.LogRecord) -> str:
54-
"""Get the rollout ID from environment variables."""
54+
"""Get the rollout ID from record extra data or environment variables."""
55+
# Check if rollout_id is provided in the extra data first
56+
if hasattr(record, "rollout_id") and record.rollout_id is not None: # type: ignore
57+
return str(record.rollout_id) # type: ignore
58+
59+
# Fall back to environment variable
5560
rollout_id = os.getenv("EP_ROLLOUT_ID")
5661
if rollout_id is None:
5762
raise ValueError(
58-
"EP_ROLLOUT_ID environment variable is not set but needed for ElasticsearchDirectHttpHandler"
63+
"EP_ROLLOUT_ID environment variable is not set and no rollout_id provided in extra data for ElasticsearchDirectHttpHandler"
5964
)
6065
return rollout_id
6166

tests/logging/test_elasticsearch_direct_http_handler.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,83 @@ def test_elasticsearch_direct_http_handler_search_by_status_code(
434434
)
435435

436436
print(f"Successfully verified search by status code {running_status.value} found {len(hits)} log messages")
437+
438+
439+
@pytest.mark.skipif(os.environ.get("CI") == "true", reason="Only run this test locally (skipped in CI)")
440+
def test_elasticsearch_direct_http_handler_rollout_id_from_extra_overrides_env(
441+
elasticsearch_client: ElasticsearchClient, test_logger: logging.Logger, rollout_id: str
442+
):
443+
"""Test that rollout_id in extra parameter overrides environment variable."""
444+
445+
# Create a different rollout_id to pass in extra
446+
extra_rollout_id = f"extra-rollout-{time.time()}"
447+
448+
# Generate a unique test message
449+
test_message = f"Rollout ID override test message at {time.time()}"
450+
451+
# Log with rollout_id in extra data (should override environment variable)
452+
test_logger.info(test_message, extra={"rollout_id": extra_rollout_id})
453+
454+
# Give Elasticsearch time to process the document
455+
time.sleep(3)
456+
457+
# Search for logs with the extra rollout_id (not the environment one)
458+
search_results = elasticsearch_client.search_by_term("rollout_id", extra_rollout_id, size=1)
459+
460+
# Assert that we found our log message with the extra rollout_id
461+
assert search_results is not None, "Search should return results"
462+
assert "hits" in search_results, "Search response should contain 'hits'"
463+
assert "total" in search_results["hits"], "Search hits should contain 'total'"
464+
465+
total_hits = search_results["hits"]["total"]
466+
if isinstance(total_hits, dict):
467+
total_count = total_hits["value"]
468+
else:
469+
total_count = total_hits
470+
471+
assert total_count > 0, f"Expected to find at least 1 log message with extra rollout_id, but found {total_count}"
472+
473+
# Verify the content of the found document
474+
hits = search_results["hits"]["hits"]
475+
assert len(hits) > 0, "Expected at least one hit"
476+
477+
found_document = hits[0]["_source"]
478+
479+
# Verify the rollout_id field matches the extra parameter (not environment variable)
480+
assert "rollout_id" in found_document, "Expected document to contain 'rollout_id' field"
481+
assert found_document["rollout_id"] == extra_rollout_id, (
482+
f"Expected rollout_id '{extra_rollout_id}', got '{found_document['rollout_id']}'"
483+
)
484+
485+
# Verify it's NOT the environment variable rollout_id
486+
assert found_document["rollout_id"] != rollout_id, (
487+
f"Expected rollout_id to be overridden, but got environment rollout_id '{rollout_id}'"
488+
)
489+
490+
# Verify other expected fields are still present
491+
assert found_document["message"] == test_message, (
492+
f"Expected message '{test_message}', got '{found_document['message']}'"
493+
)
494+
assert found_document["level"] == "INFO", f"Expected level 'INFO', got '{found_document['level']}'"
495+
assert found_document["logger_name"] == "test_elasticsearch_logger", (
496+
f"Expected logger name 'test_elasticsearch_logger', got '{found_document['logger_name']}'"
497+
)
498+
assert "@timestamp" in found_document, "Expected document to contain '@timestamp' field"
499+
500+
# Verify that searching for the original environment rollout_id doesn't find this message
501+
env_search_results = elasticsearch_client.search(
502+
{"bool": {"must": [{"term": {"rollout_id": rollout_id}}, {"match": {"message": test_message}}]}}, size=1
503+
)
504+
505+
assert env_search_results is not None, "Environment rollout_id search should return results"
506+
env_total_hits = env_search_results["hits"]["total"]
507+
if isinstance(env_total_hits, dict):
508+
env_count = env_total_hits["value"]
509+
else:
510+
env_count = env_total_hits
511+
512+
assert env_count == 0, (
513+
f"Expected 0 results when searching for message with environment rollout_id, but found {env_count}"
514+
)
515+
516+
print(f"Successfully verified rollout_id override: extra '{extra_rollout_id}' overrode environment '{rollout_id}'")

0 commit comments

Comments
 (0)