Skip to content

Commit 0e82c7c

Browse files
dobrerazvanamuraru
andauthored
Collect metrics for a set of CG states (#7)
* Filter empty consumer groups * Collect metrics for a set of CG states * Update minion/config_consumer_group.go Co-authored-by: Adrian Muraru <[email protected]> * Implement review * Implement review * Update the CG state values --------- Co-authored-by: Adrian Muraru <[email protected]>
1 parent 4f2c3b0 commit 0e82c7c

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

minion/config_consumer_group.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ type ConsumerGroupConfig struct {
3131
// IgnoredGroups are regex strings of group ids that shall be ignored/skipped when exporting metrics. Ignored groups
3232
// take precedence over allowed groups.
3333
IgnoredGroupIDs []string `koanf:"ignoredGroups"`
34+
35+
// Monitor consumer group states. Empty list means all consumer groups are monitoring regardless of its state
36+
// Allowed values are: Dead, Empty, Stable, PreparingRebalance, CompletingRebalance
37+
// Source: https://github.com/apache/kafka/blob/3.4/core/src/main/scala/kafka/coordinator/group/GroupMetadata.scala
38+
AllowedConsumerGroupStates []string `koanf:"allowedConsumerGroupStates"`
3439
}
3540

3641
func (c *ConsumerGroupConfig) SetDefaults() {
@@ -76,3 +81,13 @@ func (c *ConsumerGroupConfig) Validate() error {
7681

7782
return nil
7883
}
84+
85+
// Returns a map of allowed group states for faster lookup
86+
func (c *ConsumerGroupConfig) GetAllowedConsumerGroupStates() map[string]string {
87+
// create a map for faster lookup
88+
groupStatesMap := make(map[string]string, len(c.AllowedConsumerGroupStates))
89+
for _, state := range c.AllowedConsumerGroupStates {
90+
groupStatesMap[state] = state
91+
}
92+
return groupStatesMap
93+
}

minion/consumer_group_offsets.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ func (s *Service) ListAllConsumerGroupOffsetsAdminAPI(ctx context.Context) (map[
2323
return nil, fmt.Errorf("failed to list groupsRes: %w", err)
2424
}
2525
groupIDs := make([]string, len(groupsRes.AllowedGroups.Groups))
26+
groupStatesMap := s.Cfg.ConsumerGroups.GetAllowedConsumerGroupStates()
27+
2628
for i, group := range groupsRes.AllowedGroups.Groups {
27-
if group.GroupState != "Dead" {
29+
if len(groupStatesMap) == 0 {
2830
groupIDs[i] = group.Group
31+
} else {
32+
// only add group if it's state is allowed
33+
if _, ok := groupStatesMap[group.GroupState]; ok {
34+
groupIDs[i] = group.Group
35+
}
2936
}
3037
}
3138

prometheus/collect_broker_info.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package prometheus
22

33
import (
44
"context"
5+
"strconv"
6+
57
"github.com/prometheus/client_golang/prometheus"
68
"go.uber.org/zap"
7-
"strconv"
89
)
910

1011
func (e *Exporter) collectBrokerInfo(ctx context.Context, ch chan<- prometheus.Metric) bool {

prometheus/collect_cluster_info.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package prometheus
22

33
import (
44
"context"
5+
"strconv"
6+
57
"github.com/prometheus/client_golang/prometheus"
68
"go.uber.org/zap"
7-
"strconv"
89
)
910

1011
func (e *Exporter) collectClusterInfo(ctx context.Context, ch chan<- prometheus.Metric) bool {

prometheus/collect_exporter_metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package prometheus
22

33
import (
44
"context"
5+
56
"github.com/prometheus/client_golang/prometheus"
67
)
78

prometheus/collect_log_dirs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package prometheus
22

33
import (
44
"context"
5+
"strconv"
6+
57
"github.com/prometheus/client_golang/prometheus"
68
"github.com/twmb/franz-go/pkg/kerr"
79
"github.com/twmb/franz-go/pkg/kgo"
810
"go.uber.org/zap"
9-
"strconv"
1011
)
1112

1213
func (e *Exporter) collectLogDirs(ctx context.Context, ch chan<- prometheus.Metric) bool {

prometheus/collect_topic_partition_offsets.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package prometheus
22

33
import (
44
"context"
5+
"strconv"
6+
57
"github.com/cloudhut/kminion/v2/minion"
68
"github.com/prometheus/client_golang/prometheus"
79
"github.com/twmb/franz-go/pkg/kerr"
810
"go.uber.org/zap"
9-
"strconv"
1011
)
1112

1213
func (e *Exporter) collectTopicPartitionOffsets(ctx context.Context, ch chan<- prometheus.Metric) bool {

0 commit comments

Comments
 (0)