|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/google-cloud-tools/kafka-minion/kafka" |
| 5 | + "github.com/google-cloud-tools/kafka-minion/options" |
| 6 | + "github.com/prometheus/client_golang/prometheus" |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + topicPartitionCountDesc *prometheus.Desc |
| 14 | + topicMessageCountDesc *prometheus.Desc |
| 15 | + consumerGroupTopicLagDesc *prometheus.Desc |
| 16 | + latestConsumerGroupTopicLagDesc *prometheus.Desc |
| 17 | +) |
| 18 | + |
| 19 | +// Collector collects and provides all Kafka metrics |
| 20 | +type Collector struct { |
| 21 | + kafkaClient *kafka.Client |
| 22 | +} |
| 23 | + |
| 24 | +// NewKafkaCollector creates a new instance of our internal KafkaCollector |
| 25 | +func NewKafkaCollector(opts *options.Options) (*Collector, error) { |
| 26 | + kafkaClient, err := kafka.NewKafkaClient(opts) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + // Initialize all metric types |
| 32 | + topicPartitionCountDesc = prometheus.NewDesc( |
| 33 | + prometheus.BuildFQName(opts.MetricsPrefix, "topic", "partition_count"), |
| 34 | + "Number of partitions for this topic", |
| 35 | + []string{"topic"}, prometheus.Labels{}, |
| 36 | + ) |
| 37 | + topicMessageCountDesc = prometheus.NewDesc( |
| 38 | + prometheus.BuildFQName(opts.MetricsPrefix, "topic", "message_count"), |
| 39 | + "Number of expected messages on a given topic (not reliable on compacted topics)", |
| 40 | + []string{"topic"}, prometheus.Labels{}, |
| 41 | + ) |
| 42 | + consumerGroupTopicLagDesc = prometheus.NewDesc( |
| 43 | + prometheus.BuildFQName(opts.MetricsPrefix, "consumer_group", "topic_lag"), |
| 44 | + "Current approximate lag of a consumergroup for a topic", |
| 45 | + []string{"consumer_group", "consumer_group_base_name", "topic", "consumer_group_version", "is_latest_consumer_group"}, prometheus.Labels{}, |
| 46 | + ) |
| 47 | + |
| 48 | + kafkaCollector := &Collector{ |
| 49 | + kafkaClient: kafkaClient, |
| 50 | + } |
| 51 | + |
| 52 | + return kafkaCollector, nil |
| 53 | +} |
| 54 | + |
| 55 | +// Describe sends a description of all to be exposed metric types to Prometheus |
| 56 | +func (e *Collector) Describe(ch chan<- *prometheus.Desc) { |
| 57 | + ch <- topicPartitionCountDesc |
| 58 | + ch <- topicMessageCountDesc |
| 59 | + ch <- consumerGroupTopicLagDesc |
| 60 | +} |
| 61 | + |
| 62 | +// Collect is called by the Prometheus registry when collecting |
| 63 | +// metrics. The implementation sends each collected metric via the |
| 64 | +// provided channel and returns once the last metric has been sent. |
| 65 | +func (e *Collector) Collect(ch chan<- prometheus.Metric) { |
| 66 | + // 1. Get a fresh copy of all available topic names |
| 67 | + topicNames, err := e.kafkaClient.GetTopicNames() |
| 68 | + if err != nil { |
| 69 | + log.Error(err) |
| 70 | + } |
| 71 | + |
| 72 | + // 2. Get partition ids for all topics and expose the partition count by topic metric |
| 73 | + partitionIDsByTopicName := e.kafkaClient.GetPartitionIDsBulk(topicNames) |
| 74 | + for topicName, partitionIDs := range partitionIDsByTopicName { |
| 75 | + ch <- prometheus.MustNewConstMetric( |
| 76 | + topicPartitionCountDesc, |
| 77 | + prometheus.GaugeValue, |
| 78 | + float64(len(partitionIDs)), |
| 79 | + topicName, |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + // Get partition details for all partitions in all topics |
| 84 | + topicsByName := e.kafkaClient.GetPartitionOffsets(partitionIDsByTopicName) |
| 85 | + for topicName, topic := range topicsByName { |
| 86 | + ch <- prometheus.MustNewConstMetric( |
| 87 | + topicMessageCountDesc, |
| 88 | + prometheus.GaugeValue, |
| 89 | + float64(topic.MessageCount), |
| 90 | + topicName, |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + log.Debugf("Collecting consumer group metrics") |
| 95 | + consumerGroupTopicLagsByGroupName := e.kafkaClient.ConsumerGroupTopicLags(topicsByName) |
| 96 | + latestConsumerGroupsByName := getLatestConsumerGroupsByName(consumerGroupTopicLagsByGroupName) |
| 97 | + |
| 98 | + for _, topicLags := range consumerGroupTopicLagsByGroupName { |
| 99 | + for _, value := range topicLags { |
| 100 | + isLatest := "false" |
| 101 | + baseName := value.Name |
| 102 | + version := uint8(0) |
| 103 | + |
| 104 | + // If this group is the latest consumer group also add it with a different metrics description |
| 105 | + if consumerGroup, ok := latestConsumerGroupsByName[value.Name]; ok { |
| 106 | + isLatest = "true" |
| 107 | + baseName = consumerGroup.BaseName |
| 108 | + version = consumerGroup.Version |
| 109 | + } |
| 110 | + |
| 111 | + ch <- prometheus.MustNewConstMetric( |
| 112 | + consumerGroupTopicLagDesc, |
| 113 | + prometheus.GaugeValue, |
| 114 | + float64(value.TopicLag), |
| 115 | + value.Name, |
| 116 | + baseName, |
| 117 | + value.TopicName, |
| 118 | + strconv.Itoa(int(version)), |
| 119 | + isLatest, |
| 120 | + ) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// versionedConsumerGroup contains information about the consumer group's base name and version |
| 126 | +type versionedConsumerGroup struct { |
| 127 | + BaseName string |
| 128 | + Name string |
| 129 | + Version uint8 |
| 130 | +} |
| 131 | + |
| 132 | +// getLatestConsumerGroupNames returns the latest consumer group names in a map where the consumer group name is the key. |
| 133 | +func getLatestConsumerGroupsByName(groupsByTopicName map[string][]*kafka.ConsumerGroupTopicLag) map[string]*versionedConsumerGroup { |
| 134 | + latestConsumerGroupByBaseName := make(map[string]*versionedConsumerGroup) |
| 135 | + |
| 136 | + for _, groups := range groupsByTopicName { |
| 137 | + for _, group := range groups { |
| 138 | + consumerGroup := getVersionedConsumerGroup(group.Name) |
| 139 | + baseName := consumerGroup.BaseName |
| 140 | + // Check if there already is a potentially latest consumer group for this base name |
| 141 | + if _, ok := latestConsumerGroupByBaseName[baseName]; ok { |
| 142 | + // Only overwrite base consumer group if this consumergroup version is higher |
| 143 | + if latestConsumerGroupByBaseName[baseName].Version < consumerGroup.Version { |
| 144 | + latestConsumerGroupByBaseName[baseName] = consumerGroup |
| 145 | + } |
| 146 | + } else { |
| 147 | + latestConsumerGroupByBaseName[baseName] = consumerGroup |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // we already got consumer groups by base name, but now we want them in a map grouped by their actual group name as key |
| 153 | + consumerGroupByName := make(map[string]*versionedConsumerGroup) |
| 154 | + for _, group := range latestConsumerGroupByBaseName { |
| 155 | + consumerGroupByName[group.Name] = group |
| 156 | + } |
| 157 | + |
| 158 | + return consumerGroupByName |
| 159 | +} |
| 160 | + |
| 161 | +// getVersionedConsumerGroup returns the "base name" of a consumer group and it's version |
| 162 | +func getVersionedConsumerGroup(consumerGroupName string) *versionedConsumerGroup { |
| 163 | + baseName := consumerGroupName |
| 164 | + parsedVersion := 0 |
| 165 | + |
| 166 | + lastDashIndex := strings.LastIndex(consumerGroupName, "-") |
| 167 | + if lastDashIndex > 0 { |
| 168 | + // Potentially this is our base name (if the group has no trailing number at all, this is wrong though) |
| 169 | + baseName = consumerGroupName[:lastDashIndex] |
| 170 | + potentialVersion := consumerGroupName[lastDashIndex+1 : len(consumerGroupName)] |
| 171 | + var err error |
| 172 | + parsedVersion, err = strconv.Atoi(potentialVersion) |
| 173 | + if err != nil { |
| 174 | + parsedVersion = 0 |
| 175 | + baseName = consumerGroupName |
| 176 | + } |
| 177 | + } |
| 178 | + return &versionedConsumerGroup{BaseName: baseName, Name: consumerGroupName, Version: uint8(parsedVersion)} |
| 179 | +} |
0 commit comments