Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def _format_error_detail(error_detail: Any) -> str | None:

def _build_log_fields(hit_dict: dict[str, Any]) -> dict[str, Any]:
"""Filter an ES hit to ``TASK_LOG_FIELDS`` and ensure compatibility with StructuredLogMessage."""
fields = {k: v for k, v in hit_dict.items() if k.lower() in TASK_LOG_FIELDS or k == "@timestamp"}
fields = {k: v for k, v in hit_dict.items() if k.lower() in TASK_LOG_FIELDS}

# Map @timestamp to timestamp
if "@timestamp" in fields and "timestamp" not in fields:
fields["timestamp"] = fields.pop("@timestamp")
# Map @timestamp to timestamp but not include `@timestamp` in log fields
if "@timestamp" in hit_dict and "timestamp" not in fields:
fields["timestamp"] = hit_dict["@timestamp"]

# Map levelname to level
if "levelname" in fields and "level" not in fields:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,18 @@ def test_levelname_mapped_to_level(self):
assert result["level"] == "ERROR"
assert "levelname" not in result

def test_at_timestamp_mapped_to_timestamp(self):
def test_at_timestamp_mapped_to_timestamp_if_no_timestamp_present(self):
hit = {"event": "msg", "@timestamp": "2024-01-01T00:00:00Z"}
result = _build_log_fields(hit)
assert result["timestamp"] == "2024-01-01T00:00:00Z"
assert "@timestamp" not in result

def test_at_timestamp_not_included_if_timestamp_present(self):
hit = {"event": "msg", "@timestamp": "2024-01-01T00:00:00Z", "timestamp": "2024-01-01T00:00:00Z"}
result = _build_log_fields(hit)
assert result["timestamp"] == "2024-01-01T00:00:00Z"
assert "@timestamp" not in result

def test_error_detail_is_kept_as_list(self):
error_detail = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ def _format_error_detail(error_detail: Any) -> str | None:

def _build_log_fields(hit_dict: dict[str, Any]) -> dict[str, Any]:
"""Filter an OpenSearch hit to ``TASK_LOG_FIELDS`` and ensure compatibility with StructuredLogMessage."""
fields = {k: v for k, v in hit_dict.items() if k.lower() in TASK_LOG_FIELDS or k == "@timestamp"}
fields = {k: v for k, v in hit_dict.items() if k.lower() in TASK_LOG_FIELDS}

# Map @timestamp to timestamp
if "@timestamp" in fields and "timestamp" not in fields:
fields["timestamp"] = fields.pop("@timestamp")
# Map @timestamp to timestamp but not include `@timestamp` in log fields
if "@timestamp" in hit_dict and "timestamp" not in fields:
fields["timestamp"] = hit_dict["@timestamp"]

# Map levelname to level
if "levelname" in fields and "level" not in fields:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,12 +879,18 @@ def test_levelname_mapped_to_level(self):
assert result["level"] == "ERROR"
assert "levelname" not in result

def test_at_timestamp_mapped_to_timestamp(self):
def test_at_timestamp_mapped_to_timestamp_if_no_timestamp_present(self):
hit = {"event": "msg", "@timestamp": "2024-01-01T00:00:00Z"}
result = _build_log_fields(hit)
assert result["timestamp"] == "2024-01-01T00:00:00Z"
assert "@timestamp" not in result

def test_at_timestamp_not_included_if_timestamp_present(self):
hit = {"event": "msg", "@timestamp": "2024-01-01T00:00:00Z", "timestamp": "2024-01-01T00:00:00Z"}
result = _build_log_fields(hit)
assert result["timestamp"] == "2024-01-01T00:00:00Z"
assert "@timestamp" not in result

def test_error_detail_is_kept_as_list(self):
error_detail = [
{
Expand Down