fix(kinesis): return time-based MillisBehindLatest (#444)

AWS Kinesis GetRecords documents MillisBehindLatest as the time delta
between the consumer position and the shard tip in milliseconds.
Floci was returning the count of records remaining in the shard instead,
so downstream iterator-age metrics and lag-based alerting were wrong
(e.g. 10 records ahead surfaced as 10ms of lag).

Use the ApproximateArrivalTimestamp already stamped at PutRecord time to
compute the delta. Returns 0 when the consumer is at the shard tip, the
shard is empty, or no records were returned.
This commit is contained in:
TiM authored and GitHub committed 2026-04-14 16:24:27 -05:00
1 parent a0cd0ac511
commit e62f2a64d7
2 files changed
+92 -1

No files matched your search

@@ -393,10 +393,26 @@ public class KinesisService {
Map<String, Object> response = new HashMap<>();
response.put("Records", result);
response.put("NextShardIterator", nextIterator);
response.put("MillisBehindLatest", Math.max(0, allRecords.size() - nextIndex));
response.put("MillisBehindLatest", computeMillisBehindLatest(allRecords, nextIndex));
return response;
}
/**
* Time delta in ms between the last record returned and the shard tip.
* Zero when caught up, the shard is empty, or no records were returned.
*/
private long computeMillisBehindLatest(List<KinesisRecord> allRecords, int nextIndex) {
if (nextIndex <= 0 || nextIndex >= allRecords.size()) {
return 0L;
}
Instant lastReturned = allRecords.get(nextIndex - 1).getApproximateArrivalTimestamp();
Instant tip = allRecords.get(allRecords.size() - 1).getApproximateArrivalTimestamp();
if (lastReturned == null || tip == null) {
return 0L;
}
return Math.max(0L, tip.toEpochMilli() - lastReturned.toEpochMilli());
}
private KinesisStream resolveStream(String streamName, String region) {
return store.get(regionKey(region, streamName))
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Stream " + streamName + " not found", 400));