diff --git a/pkg/mqtrigger/messageQueue/kafka/consumer.go b/pkg/mqtrigger/messageQueue/kafka/consumer.go index 26b31968..a2838dc7 100644 --- a/pkg/mqtrigger/messageQueue/kafka/consumer.go +++ b/pkg/mqtrigger/messageQueue/kafka/consumer.go @@ -100,6 +100,15 @@ func (ch MqtConsumerGroupHandler) Cleanup(session sarama.ConsumerGroupSession) e // ConsumeClaims implemented to satisfy the sarama.ConsumerGroupHandler interface func (ch MqtConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error { + + trigger := ch.trigger.Name + triggerNamespace := ch.trigger.Namespace + topic := claim.Topic() + partition := string(claim.Partition()) + + // initially set metrics to -1 + mqtrigger.SetMessageLagCount(trigger, triggerNamespace, topic, partition, -1) + // Do not move the code below to a goroutine. // The `ConsumeClaim` itself is called within a goroutine for { @@ -108,8 +117,12 @@ func (ch MqtConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSessi if msg != nil { ch.kafkaMsgHandler(msg) session.MarkMessage(msg, "") - mqtrigger.IncreaseMessageCount(ch.trigger.Name, ch.trigger.Namespace) + mqtrigger.IncreaseMessageCount(trigger, triggerNamespace) } + + mqtrigger.SetMessageLagCount(trigger, triggerNamespace, topic, partition, + claim.HighWaterMarkOffset()-msg.Offset-1) + // Should return when `session.Context()` is done. case <-session.Context().Done(): return nil diff --git a/pkg/mqtrigger/metrics.go b/pkg/mqtrigger/metrics.go index b8204b4f..8ded7a3a 100644 --- a/pkg/mqtrigger/metrics.go +++ b/pkg/mqtrigger/metrics.go @@ -37,6 +37,13 @@ var ( }, labels, ) + messageLagCount = promauto.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "fission_mqt_message_lag", + Help: "Total number of messages lag per topic and partition", + }, + []string{"trigger_name", "trigger_namespace", "topic", "partition"}, + ) ) func IncreaseSubscriptionCount() { @@ -50,3 +57,7 @@ func DecreaseSubscriptionCount() { func IncreaseMessageCount(trigname, trignamespace string) { messageCount.WithLabelValues(trigname, trignamespace).Inc() } + +func SetMessageLagCount(trigname, trignamespace, topic, partition string, lag int64) { + messageLagCount.WithLabelValues(trigname, trignamespace, topic, partition).Set(float64(lag)) +}