Skip to content

Commit

Permalink
Merge pull request #800 from Usiel/usiel/fix-metric-deletion-on-group…
Browse files Browse the repository at this point in the history
…-delete

Fix metric deletion on group delete
  • Loading branch information
bai committed Apr 29, 2024
2 parents 09f7a48 + 63514ff commit f02c934
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 14 additions & 0 deletions core/internal/httpserver/prometheus.go
Expand Up @@ -73,6 +73,7 @@ func DeleteConsumerMetrics(cluster, consumer string) {
consumerStatusGauge.Delete(labels)
consumerPartitionLagGauge.DeletePartialMatch(labels)
consumerPartitionCurrentOffset.DeletePartialMatch(labels)
partitionStatusGauge.DeletePartialMatch(labels)
}

// DeleteTopicMetrics deletes all metrics that are labeled with a topic
Expand All @@ -92,6 +93,19 @@ func DeleteTopicMetrics(cluster, topic string) {
consumerStatusGauge.DeletePartialMatch(labels)
}

// DeleteConsumerTopicMetrics deletes all metrics that are labeled with the provided consumer group AND topic
func DeleteConsumerTopicMetrics(cluster, consumer, topic string) {
labels := map[string]string{
"cluster": cluster,
"consumer_group": consumer,
"topic": topic,
}

partitionStatusGauge.DeletePartialMatch(labels)
consumerPartitionCurrentOffset.DeletePartialMatch(labels)
consumerPartitionLagGauge.DeletePartialMatch(labels)
}

func (hc *Coordinator) handlePrometheusMetrics() http.HandlerFunc {
promHandler := promhttp.Handler()

Expand Down
12 changes: 11 additions & 1 deletion core/internal/storage/inmemory.go
Expand Up @@ -667,16 +667,26 @@ func (module *InMemoryStorage) deleteGroup(request *protocol.StorageRequest, req
}

clusterMap.consumerLock.Lock()
deleteAllGroupMetrics := true
if group, ok := clusterMap.consumer[request.Group]; ok && request.Topic != "" {
delete(group.topics, request.Topic)
if len(group.topics) == 0 {
delete(clusterMap.consumer, request.Group)
} else {
// The consumer group consumes other topics, thus we need to keep its metrics
deleteAllGroupMetrics = false
}
} else {
delete(clusterMap.consumer, request.Group)
}
clusterMap.consumerLock.Unlock()
httpserver.DeleteConsumerMetrics(request.Cluster, request.Group)

if deleteAllGroupMetrics {
httpserver.DeleteConsumerMetrics(request.Cluster, request.Group)
} else {
// only a specific topic was deleted and the consumer group still exists, thus we delete only a subset of the metrics
httpserver.DeleteConsumerTopicMetrics(request.Cluster, request.Group, request.Topic)
}

requestLogger.Debug("ok")
}
Expand Down

0 comments on commit f02c934

Please sign in to comment.