Skip to content

Commit

Permalink
parse timestamp field in a try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
vthacker committed Feb 1, 2024
1 parent abc781b commit 3a26543
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,32 @@ 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);
}

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

try {
// 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", e);
}



return ((ZonedDateTime)
ingestDocument
.getIngestMetadata()
Expand Down

0 comments on commit 3a26543

Please sign in to comment.