@@ -30,6 +30,15 @@ func (e *Exporter) collectTopicPartitionOffsets(ctx context.Context, ch chan<- p
3030 return false
3131 }
3232
33+ // Highest Timestamp Offsets
34+ // NB: this requires Kafka Brokers 3.0+ (see https://issues.apache.org/jira/browse/KAFKA-12541)
35+ // In older versions this is returning the timestamp of the low watermarks (earliest offset)
36+ maxTimestampOffsets , err := e .minionSvc .ListOffsetsCached (ctx , - 3 )
37+ if err != nil {
38+ e .logger .Error ("failed to fetch offsets for max timestamp" , zap .Error (err ))
39+ return false
40+ }
41+
3342 // Process Low Watermarks
3443
3544 for topicName , partitions := range lowWaterMarks {
@@ -105,5 +114,47 @@ func (e *Exporter) collectTopicPartitionOffsets(ctx context.Context, ch chan<- p
105114 }
106115 }
107116
117+ // Process Max Timestamps
118+ for _ , topic := range maxTimestampOffsets .Topics {
119+ if ! e .minionSvc .IsTopicAllowed (topic .Topic ) {
120+ continue
121+ }
122+ topicMaxTimestamp := int64 (0 )
123+ hasErrors := false
124+ for _ , partition := range topic .Partitions {
125+ err := kerr .ErrorForCode (partition .ErrorCode )
126+ if err != nil {
127+ hasErrors = true
128+ isOk = false
129+ continue
130+ }
131+ if topicMaxTimestamp < partition .Timestamp {
132+ topicMaxTimestamp = partition .Timestamp
133+ }
134+ // Let's end here if partition metrics shall not be exposed
135+ if e .minionSvc .Cfg .Topics .Granularity == minion .TopicGranularityTopic {
136+ continue
137+ }
138+ if partition .Timestamp > 0 {
139+ ch <- prometheus .MustNewConstMetric (
140+ e .partitionMaxTimestamp ,
141+ prometheus .GaugeValue ,
142+ float64 (partition .Timestamp ),
143+ topic .Topic ,
144+ strconv .Itoa (int (partition .Partition )),
145+ )
146+ }
147+ }
148+ // We only want to report the max of all partition max timestamps if we receive results from all partitions
149+ // and the topic is not empty
150+ if ! hasErrors && topicMaxTimestamp > 0 {
151+ ch <- prometheus .MustNewConstMetric (
152+ e .topicMaxTimestamp ,
153+ prometheus .GaugeValue ,
154+ float64 (topicMaxTimestamp ),
155+ topic .Topic ,
156+ )
157+ }
158+ }
108159 return isOk
109160}
0 commit comments