Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse timestamp field safely #760

Merged
merged 1 commit into from
Feb 1, 2024
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 @@ -46,23 +46,29 @@ public static Map<String, List<Trace.Span>> parseRequest(byte[] postBody) throws
* logstash sets that) 3. Use the current time from the ingestMetadata
*/
public static long getTimestampFromIngestDocument(IngestDocument ingestDocument) {
// assumption that the provided timestamp is in millis
// at some point both th unit and field need to be configurable
// when we do that, remember to change the called to appropriately remove the field
if (ingestDocument.hasField("timestamp")) {
return ingestDocument.getFieldValue("timestamp", Long.class);
}

if (ingestDocument.hasField("_timestamp")) {
return ingestDocument.getFieldValue("_timestamp", Long.class);
}
try {
if (ingestDocument.hasField("@timestamp")) {
String dateString = ingestDocument.getFieldValue("@timestamp", String.class);
LocalDateTime localDateTime =
LocalDateTime.parse(dateString, DateTimeFormatter.ISO_DATE_TIME);
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
return instant.toEpochMilli();
}

if (ingestDocument.hasField("@timestamp")) {
String dateString = ingestDocument.getFieldValue("@timestamp", String.class);
LocalDateTime localDateTime =
LocalDateTime.parse(dateString, DateTimeFormatter.ISO_DATE_TIME);
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
return instant.toEpochMilli();
// assumption that the provided timestamp is in millis
// at some point both th unit and field need to be configurable
// when we do that, remember to change the called to appropriately remove the field
if (ingestDocument.hasField("timestamp")) {
return ingestDocument.getFieldValue("timestamp", Long.class);
}

if (ingestDocument.hasField("_timestamp")) {
return ingestDocument.getFieldValue("_timestamp", Long.class);
}
} catch (Exception e) {
LOG.warn(
"Unable to parse timestamp from ingest document. Using current time as timestamp", e);
}

return ((ZonedDateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,41 @@ public void testTimestampParsingFromIngestDocument() {
timeInMillis = BulkApiRequestParser.getTimestampFromIngestDocument(ingestDocument);
assertThat(timeInMillis).isEqualTo(providedTimeStamp.toEpochMilli());

// we put a long in the @timestamp field, which today we don't parse
// so it won't be 2024-01-01 but be the current timestamp
ingestDocument =
new IngestDocument(
"index",
"1",
"routing",
1L,
VersionType.INTERNAL,
Map.of("@timestamp", providedTimeStamp.toEpochMilli()));
timeInMillis = BulkApiRequestParser.getTimestampFromIngestDocument(ingestDocument);
ingestDocumentTime = Instant.ofEpochMilli(timeInMillis);
assertThat(oneMinuteBefore.isBefore(ingestDocumentTime)).isTrue();
assertThat(ingestDocumentTime.isBefore(oneMinuteAfter)).isTrue();

// we put a string in the timestamp field, which today we don't parse
// so it won't be 2024-01-01 but be the current timestamp
ingestDocument =
new IngestDocument(
"index", "1", "routing", 1L, VersionType.INTERNAL, Map.of("_timestamp", ts));
timeInMillis = BulkApiRequestParser.getTimestampFromIngestDocument(ingestDocument);
ingestDocumentTime = Instant.ofEpochMilli(timeInMillis);
assertThat(oneMinuteBefore.isBefore(ingestDocumentTime)).isTrue();
assertThat(ingestDocumentTime.isBefore(oneMinuteAfter)).isTrue();

// we put a string in the _timestamp field, which today we don't parse
// so it won't be 2024-01-01 but be the current timestamp
ingestDocument =
new IngestDocument(
"index", "1", "routing", 1L, VersionType.INTERNAL, Map.of("timestamp", ts));
timeInMillis = BulkApiRequestParser.getTimestampFromIngestDocument(ingestDocument);
ingestDocumentTime = Instant.ofEpochMilli(timeInMillis);
assertThat(oneMinuteBefore.isBefore(ingestDocumentTime)).isTrue();
assertThat(ingestDocumentTime.isBefore(oneMinuteAfter)).isTrue();

ingestDocument =
new IngestDocument(
"index",
Expand Down
Loading