kafka_exporter

repository·master·Indexed 25 days ago

https://github.com/danielqsj/kafka_exporter

A Prometheus exporter for Apache Kafka (version 0.10.1.0 or later) that provides metrics regarding brokers, topics, and consumer groups to monitor cluster health and consumer lag. It supports deployment via binary, Docker, and Helm, with integrations for Datadog and Azure Managed Prometheus. Features include SASL authentication (SCRAM, GSSAPI, AWS IAM, OAuthBearer), TLS encryption, and the ability to collect consumer group lag from ZooKeeper.

Tokens
3.7K
Snippets
8
Records
19
Agent score
82%

What's inside kafka_exporter

  1. How to collect Consumer Group Lag from Zookeeper

    master

    By default, consumer group metrics are collected via Kafka. If you need to collect consumer lag specifically from Zookeeper, you must enable the following flags:

    1. use.consumelag.zookeeper: Set to true to enable collection from Zookeeper.
    2. zookeeper.server: Provide the address of the Zookeeper server (e.g., localhost:2181).

    This will enable the metric kafka_consumergroupzookeeper_lag_zookeeper.

  2. Install Datadog collector for Prometheus scraping

    master

    If you are using Datadog to scrape Prometheus endpoints, ensure the Datadog collector is installed with datadog.prometheusScrape.enabled=true and datadog.prometheusScrape.serviceEndpoints=true.

    helm repo add datadog https://helm.datadoghq.com
    helm repo update
    
    helm upgrade -i datadog datadog/datadog --namespace=kafka-exporter \
      --set datadog.apiKey=<key>  \
      --set targetSystem=linux \
      --set datadog.prometheusScrape.enabled=true \
      --set datadog.prometheusScrape.serviceEndpoints=true
  3. Install and Run kafka_exporter

    master

    You can run kafka_exporter as a binary, a Docker container, or via Docker Compose. It requires a Kafka server (version 0.10.1.0 or later) and exposes Prometheus metrics on port 9308 by default.

    Run as a Binary

    kafka_exporter --kafka.server=kafka:9092 [--kafka.server=another-server ...]

    Run via Docker

    Pull the latest image from Docker Hub and run it:

    docker pull danielqsj/kafka-exporter:latest
    docker run -ti --rm -p 9308:9308 danielqsj/kafka-exporter --kafka.server=kafka:9092

    Run via Docker Compose

    Create a docker-compose.yml file:

    services:
      kafka-exporter:
        image: danielqsj/kafka-exporter 
        command: ["--kafka.server=kafka:9092"]
        ports:
          - 9308:9308     

    Then run:

    docker-compose up -d
    kafka_exporter --kafka.server=kafka:9092
  4. Install kafka-exporter via Helm (Basic)

    master

    To perform a basic installation of kafka-exporter using Helm, use the helm upgrade -i command. You must specify the Kafka brokers using the kafkaExporter.kafka.servers configuration key in the format "{host:port,host:port,...}".

    helm upgrade -i kafka-exporter kafka_exporter/charts/kafka-exporter --namespace=kafka-exporter --create-namespace \
      --set kafkaExporter.kafka.servers="{kafka1:9092,kafka2:9092,.....}"
  5. Install kafka-exporter with Datadog support

    master

    To enable Datadog support, set datadog.use_datadog=true and provide a datadog.prefix. When using Datadog for metric collection, you should disable the standard Prometheus ServiceMonitor by setting prometheus.serviceMonitor.enabled=false.

    helm upgrade -i kafka-exporter kafka_exporter/charts/kafka-exporter --namespace=kafka-exporter --create-namespace \
      --set kafkaExporter.kafka.servers="{kafka1:9092,kafka2:9092,.....}" \
      --set datadog.prefix=testing-kafka-cluster \
      --set datadog.use_datadog=true \
      --set prometheus.serviceMonitor.enabled=false
  6. Install kafka-exporter with Azure Managed Prometheus support

    master

    To integrate with Azure Managed Prometheus, enable the ServiceMonitor and set azuremanagedprometheus.use_azuremanagedprometheus=true.

    helm upgrade -i kafka-exporter kafka_exporter/charts/kafka-exporter --namespace=kafka-exporter --create-namespace \
      --set kafkaExporter.kafka.servers="{kafka1:9092,kafka2:9092,.....}" \
      --set prometheus.serviceMonitor.enabled=true \
      --set azuremanagedprometheus.use_azuremanagedprometheus=true
  7. Troubleshooting: Missing Consumer Group or Lag information

    master

    If you do not see any consumer group or lag information in the metrics, it is likely because there are no active consumers using a consumer group.

    To test this, run a consumer with a consumer group using the Kafka CLI tool:

    kafka-console-consumer.sh \
        --consumer.config /path/to/client.properties \
        --bootstrap-server localhost:9092 \
        --topic test \
        --group test-conusmer-group \
        --from-beginning
  8. Configure TLS for Kafka connections

    master

    To enable TLS for the Kafka connection, set useTLS to true in kafkaOpts. You can configure the following:

    • tlsServerName: Used to verify the hostname on the returned certificates.
    • tlsCAFile: Path to the CA certificate file.
    • tlsCertFile and tlsKeyFile: Paths to the client certificate and key for mutual authentication.
    • tlsInsecureSkipTLSVerify: If true, the server's certificate validity is not checked.
  9. Configure TLS and Mutual Auth for the Exporter Server

    master

    The exporter supports serving metrics over HTTPS with optional Mutual TLS (mTLS) authentication.

    Key configuration options via opts:

    • serverUseTLS: Boolean to enable HTTPS.
    • serverTlsCertFile: Path to the server's TLS certificate file.
    • serverTlsKeyFile: Path to the server's TLS private key file.
    • serverMutualAuthEnabled: If true, sets ClientAuth to tls.RequireAndVerifyClientCert.
    • serverTlsCAFile: Path to the CA certificate file used to verify client certificates.

    When serverUseTLS is enabled, the server uses a tls.Config with a minimum version of TLS 1.2 and a specific set of secure cipher suites.

  10. Configure SASL Authentication mechanisms

    master

    The kafka_exporter supports several SASL mechanisms via the sasl.mechanism option. When configuring kafkaOpts, ensure the corresponding parameters are provided:

    • SCRAM: Use scram-sha256 or scram-sha512. Requires sasl.username and sasl.password.
    • GSSAPI (Kerberos): Requires sasl.service-name, sasl.kerberos-config-path, sasl.realm, and sasl.username. If using keytab authentication, set sasl.kerberos-auth-type to keytabAuth and provide sasl.keytab-path.
    • AWS IAM: Use awsiam. Requires sasl.aws-region (defaults to AWS_REGION env var).
    • OAuthBearer: Use oauthbearer. Requires sasl.oauthbearer-token-url (or SASL_OAUTHBEARER_TOKEN_URL env var), sasl.username, and sasl.oauthbearer-scopes (comma-separated).
  11. Configure kafka_exporter via CLI flags

    master

    The exporter is configured using various flags. Note that boolean flags use Kingpin's management: to disable a boolean flag, use the --no-<flag-name> syntax (e.g., --no-sasl.handshake).

    Connection & Authentication

    • kafka.server: Addresses (host:port) of Kafka server (Default: kafka:9092)
    • kafka.version: Kafka broker version (Default: 2.0.0)
    • sasl.enabled: Connect using SASL/PLAIN
    • sasl.mechanism: SASL SCRAM SHA algorithm (sha256 or sha512) or SASL mechanism (gssapi, awsiam or oauthbearer)
    • tls.enabled: Connect to Kafka using TLS
    • tls.insecure-skip-tls-verify: If true, the server's certificate will not be checked for validity

    Filtering & Performance

    • topic.filter: Regex that determines which topics to collect (Default: .*)
    • topic.exclude: Regex that determines which topics to exclude (Default: ^$)
    • group.filter: Regex that determines which consumer groups to collect (Default: .*)
    • group.exclude: Regex that determines which consumer groups to exclude (Default: ^$)
    • concurrent.enable: If true, all scrapes will trigger kafka operations; otherwise, they share results. Warning: Disable this on large clusters.

    Web Server

    • web.listen-address: Address to listen on for web interface and telemetry (Default: :9308)
    • web.telemetry-path: Path under which to expose metrics (Default: /metrics)
  12. Exposed Prometheus Metrics: Brokers, Topics, and Consumer Groups

    master

    The exporter provides several categories of Prometheus metrics.

    Broker Metrics

    • kafka_brokers: Number of Brokers in the Kafka Cluster
    • kafka_broker_info: Information about the Kafka Broker (labels: address, id)

    Topic Metrics

    Requires 'Describe all topics' permission.

    • kafka_topic_partitions: Number of partitions for this Topic
    • kafka_topic_partition_current_offset: Current Offset of a Broker at Topic/Partition
    • kafka_topic_partition_oldest_offset: Oldest Offset of a Broker at Topic/Partition
    • kafka_topic_partition_in_sync_replica: Number of In-Sync Replicas for this Topic/Partition
    • kafka_topic_partition_leader: Leader Broker ID of this Topic/Partition
    • kafka_topic_partition_leader_is_preferred: 1 if Topic/Partition is using the Preferred Broker
    • kafka_topic_partition_replicas: Number of Replicas for this Topic/Partition
    • kafka_topic_partition_under_replicated_partition: 1 if Topic/Partition is under Replicated

    Consumer Group Metrics

    Requires 'Describe all groups' permission.

    • kafka_consumergroup_current_offset: Current Offset of a ConsumerGroup at Topic/Partition
    • kafka_consumergroup_current_offset_sum: Current Offset of a ConsumerGroup at Topic for all partitions
    • kafka_consumergroup_lag: Current Approximate Lag of a ConsumerGroup at Topic/Partition
    • kafka_consumergroup_lag_sum: Current Approximate Lag of a ConsumerGroup at Topic for all partitions
    • kafka_consumergroup_members: Amount of members in a consumer group
    • kafka_consumergroupzookeeper_lag_zookeeper: Current Approximate Lag (from Zookeeper) of a ConsumerGroup at Topic/Partition (Requires use.consumelag.zookeeper and zookeeper.server flags)