Skip to content

Commit dd5dafd

Browse files
authored
Fix crash on TimeParserException during preprocessing caused by invalid timestamps (#862)
* Handle TimeParserException in get_next() to catch invalid time format values
1 parent 165d311 commit dd5dafd

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
### Bugfix
4646

4747
* add `@timestamp` field to error documents
48+
* fix crash on `TimeParserException` during preprocessing caused by invalid timestamps
4849

4950
## 17.0.0
5051
### Breaking

logprep/abc/input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from logprep.metrics.metrics import Metric
2222
from logprep.processor.base.exceptions import FieldExistsWarning
2323
from logprep.util.helper import add_fields_to, get_dotted_field_value
24-
from logprep.util.time import UTC, TimeParser
24+
from logprep.util.time import UTC, TimeParser, TimeParserException
2525
from logprep.util.validators import dict_structure_validator
2626

2727

@@ -338,7 +338,7 @@ def get_next(self, timeout: float) -> dict | None:
338338
self._add_arrival_timedelta_information_to_event(event)
339339
if self._add_env_enrichment:
340340
self._add_env_enrichment_to_event(event)
341-
except FieldExistsWarning as error:
341+
except (FieldExistsWarning, TimeParserException) as error:
342342
raise CriticalInputError(self, error.args[0], event) from error
343343
return event
344344

tests/unit/connector/base.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,34 @@ def test_pipeline_preprocessing_does_not_add_timestamp_delta_if_configured_but_l
582582
result = connector.get_next(0.01)
583583
assert result == {"any": "content"}
584584

585+
@pytest.mark.parametrize(
586+
["timestamp", "expected_error_message", "message"],
587+
[
588+
("0000-00-00 00:00:00", "out of range", "Timestamp is out of range"),
589+
("invalid", "Invalid isoformat", "Timestamp is invalid"),
590+
],
591+
)
592+
def test_pipeline_preprocessing_raises_criticalinputerror_on_timeparser_error(
593+
self, timestamp, expected_error_message, message
594+
):
595+
preprocessing_config = {
596+
"preprocessing": {
597+
"log_arrival_time_target_field": "arrival_time",
598+
"log_arrival_timedelta": {
599+
"target_field": "log_arrival_timedelta",
600+
"reference_field": "@timestamp",
601+
},
602+
}
603+
}
604+
connector_config = deepcopy(self.CONFIG)
605+
connector_config.update(preprocessing_config)
606+
connector = Factory.create({"test connector": connector_config})
607+
test_event = {"any": "content", "@timestamp": timestamp}
608+
connector._get_event = mock.MagicMock(return_value=(test_event, None))
609+
with pytest.raises(CriticalInputError, match=expected_error_message) as error:
610+
_, _ = connector.get_next(0.01)
611+
assert error.value.raw_input, message
612+
585613
def test_preprocessing_enriches_by_env_variable(self):
586614
preprocessing_config = {
587615
"preprocessing": {

0 commit comments

Comments
 (0)