Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Consumer groups state metrics #455

Open
Rabbit-st opened this issue Aug 27, 2024 · 0 comments
Open

Feature request: Consumer groups state metrics #455

Rabbit-st opened this issue Aug 27, 2024 · 0 comments

Comments

@Rabbit-st
Copy link

Rabbit-st commented Aug 27, 2024

There are five states, which you can see in kafka's source code. The details are:

  • Empty
  • Stable
  • PreparingRebalance
  • CompletingRebalance
  • Dead

Use different numbers to represent the state, such as:

0:Empty
1:Stable
2:PreparingRebalance
3:CompletingRebalance
4:Dead 

The code to obtain the status can be referred to:

package main

import (
    "fmt"
    "log"

    "github.com/IBM/sarama"
)

func main() {
    // Kafka broker list
    brokers := []string{"10.100.1.1:9092"}

    // Create a new Sarama config
    config := sarama.NewConfig()
    config.Version = sarama.V2_0_0_0

    // Create a new Sarama admin client
    admin, err := sarama.NewClusterAdmin(brokers, config)
    if err != nil {
        log.Fatalf("Error creating cluster admin: %v", err)
    }
    defer admin.Close()

    // Get all Consumer Groups
    groups, err := admin.ListConsumerGroups()
    if err != nil {
        log.Printf("Error listing consumer groups: %v", err)
        return
    }

    for group := range groups {
        // describe consumer group
        description, err := admin.DescribeConsumerGroups([]string{group})
        if err != nil {
            log.Printf("Error describing consumer group %s: %v", group, err)
            continue
        }

        for _, desc := range description {
            var stateValue float64
            switch desc.State {
            case "Stable":
                stateValue = 1
            case "PreparingRebalance":
                stateValue = 2
            case "CompletingRebalance":
                stateValue = 3
            case "Dead":
                stateValue = 4
            default:
                stateValue = 0
            }
            fmt.Printf("Consumer Group: %s, State: %s, State Value: %f\n", group, desc.State, stateValue)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant