Learn all about the Kafka exporter, one of the best-fit exporters for monitoring metrics used by NexClipper. Kafka is an open source system developed by the Apache Software Foundation written in Java and Scala.
Kafka is an open-source system developed by the Apache Software Foundation written in Java and Scala. It is a distributed event store and stream-processing platform. You can also call it a queue. It is a distributed system consisting of servers and clients that communicate via a high-performance TCP network protocol. It can be deployed on bare-metal hardware, virtual machines, and containers in on-premise as well as cloud environments.
Streaming data is continuously generated by thousands of data sources, which typically send the data records simultaneously. A streaming platform needs to handle this constant influx of data and process it sequentially and incrementally.
Kafka provides three main functions to its users:
Kafka is primarily used to build real-time streaming data pipelines and applications that adapt to the data streams. It combines messaging, storage, and stream processing to allow storage and analysis of both historical and real-time data.
For this setup, we are using bitnami Kafka helm charts to start the Kafka server/cluster.
With the latest version of Prometheus (2.33 as of February 2022), these are the ways to set up a Prometheus exporter:
Supported by Prometheus since the beginning
To set up an exporter in the native way a Prometheus config needs to be updated to add the target.
A sample configuration:
# scrape_config job
scrape_configs:
- job_name: kafka
scrape_interval: 45s
scrape_timeout: 30s
metrics_path: "/metrics"
static_configs:
- targets:
- <Kafka exporter endpoint>
Code language: YAML (yaml)
This method is applicable for Kubernetes deployment only.
A default scrap config can be added to the prometheus.yaml file and an annotation can be added to the exporter service. With this, Prometheus will automatically start scrapping the data from the services with the mentioned path.
Prometheus.yaml
- job_name: kubernetes-services
scrape_interval: 15s
scrape_timeout: 10s
kubernetes_sd_configs:
- role: service
relabel_configs:
# Example relabel to scrape only endpoints that have
# prometheus.io/scrape: "true" annotation.
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
# prometheus.io/path: "/scrape/path" annotation.
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
# prometheus.io/port: "80" annotation.
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: (.+)(?::\d+);(\d+)
replacement: $1:$2
Code language: YAML (yaml)
Exporter service annotations:
annotations:
prometheus.io/path: /metrics
prometheus.io/scrape: "true"
Code language: YAML (yaml)
Setting up a service monitor
The Prometheus operator supports an automated way of scraping data from the exporters by setting up a service monitor Kubernetes object. For reference, a sample service monitor for Kafka can be found here.
These are the necessary steps:
Step 1
Add/update Prometheus operator’s selectors. By default, the Prometheus operator comes with empty selectors which will select every service monitor available in the cluster for scrapping the data.
To check your Prometheus configuration:
Kubectl get prometheus -n <namespace> -o yaml
Code language: Bash (bash)
A sample output will look like this.
ruleNamespaceSelector: {}
ruleSelector:
matchLabels:
app: kube-prometheus-stack
release: kps
scrapeInterval: 1m
scrapeTimeout: 10s
securityContext:
fsGroup: 2000
runAsGroup: 2000
runAsNonRoot: true
runAsUser: 1000
serviceAccountName: kps-kube-prometheus-stack-prometheus
serviceMonitorNamespaceSelector: {}
serviceMonitorSelector:
matchLabels:
release: kps
Code language: YAML (yaml)
Here you can see that this Prometheus configuration is selecting all the service monitors with the label release = kps
So with this, if you are modifying the default Prometheus operator configuration for service monitor scrapping, make sure you use the right labels in your service monitor as well.
Step 2
Add a service monitor and make sure it has a matching label and namespace for the Prometheus service monitor selectors (serviceMonitorNamespaceSelector & serviceMonitorSelector).
Sample configuration:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
annotations:
meta.helm.sh/release-name: kafka-exporter
meta.helm.sh/release-namespace: monitor
labels:
app: prometheus-kafka-exporter
app.kubernetes.io/managed-by: Helm
chart: prometheus-kafka-exporter-1.1.0
heritage: Helm
release: kps
name: kafka-exporter-prometheus-kafka-exporter
namespace: monitor
spec:
endpoints:
- interval: 15s
port: kafka-exporter
selector:
matchLabels:
app: prometheus-kafka-exporter
release: kafka-exporter
Code language: YAML (yaml)
As you can see, a matching label on the service monitor release = kps is used that is specified in the Prometheus operator scrapping configuration.
Below are handpicked metrics that will provide insights into Kafka operations.
Kafka exporter for Prometheus. For other metrics from Kafka, have a look at the JMX exporter.
Support Apache Kafka version 0.10.1.0 (and later).
Binary can be downloaded from Releases page.
make
make docker
docker pull danielqsj/kafka-exporter:latest
It can be used directly instead of having to build the image yourself. (Docker Hub danielqsj/kafka-exporter)
kafka_exporter --kafka.server=kafka:9092 [--kafka.server=another-server ...]
docker run -ti --rm -p 9308:9308 danielqsj/kafka-exporter --kafka.server=kafka:9092 [--kafka.server=another-server ...]
This image is configurable using different flags
Flag name | Default | Description |
---|---|---|
kafka.server | kafka:9092 | Addresses (host:port) of Kafka server |
kafka.version | 2.0.0 | Kafka broker version |
sasl.enabled | false | Connect using SASL/PLAIN |
sasl.handshake | true | Only set this to false if using a non-Kafka SASL proxy |
sasl.username | SASL user name | |
sasl.password | SASL user password | |
sasl.mechanism | SASL mechanism can be plain, scram-sha512, scram-sha256 | |
sasl.service-name | Service name when using Kerberos Auth | |
sasl.kerberos-config-path | Kerberos config path | |
sasl.realm | Kerberos realm | |
sasl.keytab-path | Kerberos keytab file path | |
sasl.kerberos-auth-type | Kerberos auth type. Either 'keytabAuth' or 'userAuth' | |
tls.enabled | false | Connect to Kafka using TLS |
tls.server-name | Used to verify the hostname on the returned certificates unless tls.insecure-skip-tls-verify is given. The kafka server's name should be given | |
tls.ca-file | The optional certificate authority file for Kafka TLS client authentication | |
tls.cert-file | The optional certificate file for Kafka client authentication | |
tls.key-file | The optional key file for Kafka client authentication | |
tls.insecure-skip-tls-verify | false | If true, the server's certificate will not be checked for validity |
server.tls.enabled | false | Enable TLS for web server |
server.tls.mutual-auth-enabled | false | Enable TLS client mutual authentication |
server.tls.ca-file | The certificate authority file for the web server | |
server.tls.cert-file | The certificate file for the web server | |
server.tls.key-file | The key file for the web server | |
topic.filter | .* | Regex that determines which topics to collect |
group.filter | .* | Regex that determines which consumer groups to collect |
web.listen-address | :9308 | Address to listen on for web interface and telemetry |
web.telemetry-path | /metrics | Path under which to expose metrics |
log.enable-sarama | false | Turn on Sarama logging |
use.consumelag.zookeeper | false | if you need to use a group from zookeeper |
zookeeper.server | localhost:2181 | Address (hosts) of zookeeper server |
kafka.labels | Kafka cluster name | |
refresh.metadata | 30s | Metadata refresh interval |
offset.show-all | true | Whether show the offset/lag for all consumer group, otherwise, only show connected consumer groups |
concurrent.enable | false | If true, all scrapes will trigger kafka operations otherwise, they will share results. WARN: This should be disabled on large clusters |
topic.workers | 100 | Number of topic workers |
verbosity | 0 | Verbosity log level |
Boolean values are uniquely managed by Kingpin. Each boolean flag will have a negative complement:--<name>
and --no-<name>
.
For example:
If you need to disable sasl.handshake
, you could add flag --no-sasl.handshake
Documents about exposed Prometheus metrics.
For details on the underlying metrics please see Apache Kafka.
Metrics details
Name | Exposed informations |
---|---|
kafka_brokers | Number of Brokers in the Kafka Cluster |
Metrics output example
# HELP kafka_brokers Number of Brokers in the Kafka Cluster.
# TYPE kafka_brokers gauge
kafka_brokers 3
Metrics details
Name | Exposed informations |
---|---|
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 |
Metrics output example
# HELP kafka_topic_partitions Number of partitions for this Topic
# TYPE kafka_topic_partitions gauge
kafka_topic_partitions{topic="__consumer_offsets"} 50
# HELP kafka_topic_partition_current_offset Current Offset of a Broker at Topic/Partition
# TYPE kafka_topic_partition_current_offset gauge
kafka_topic_partition_current_offset{partition="0",topic="__consumer_offsets"} 0
# HELP kafka_topic_partition_oldest_offset Oldest Offset of a Broker at Topic/Partition
# TYPE kafka_topic_partition_oldest_offset gauge
kafka_topic_partition_oldest_offset{partition="0",topic="__consumer_offsets"} 0
# HELP kafka_topic_partition_in_sync_replica Number of In-Sync Replicas for this Topic/Partition
# TYPE kafka_topic_partition_in_sync_replica gauge
kafka_topic_partition_in_sync_replica{partition="0",topic="__consumer_offsets"} 3
# HELP kafka_topic_partition_leader Leader Broker ID of this Topic/Partition
# TYPE kafka_topic_partition_leader gauge
kafka_topic_partition_leader{partition="0",topic="__consumer_offsets"} 0
# HELP kafka_topic_partition_leader_is_preferred 1 if Topic/Partition is using the Preferred Broker
# TYPE kafka_topic_partition_leader_is_preferred gauge
kafka_topic_partition_leader_is_preferred{partition="0",topic="__consumer_offsets"} 1
# HELP kafka_topic_partition_replicas Number of Replicas for this Topic/Partition
# TYPE kafka_topic_partition_replicas gauge
kafka_topic_partition_replicas{partition="0",topic="__consumer_offsets"} 3
# HELP kafka_topic_partition_under_replicated_partition 1 if Topic/Partition is under Replicated
# TYPE kafka_topic_partition_under_replicated_partition gauge
kafka_topic_partition_under_replicated_partition{partition="0",topic="__consumer_offsets"} 0
Metrics details
Name | Exposed informations |
---|---|
kafka_consumergroup_current_offset | Current Offset of a ConsumerGroup at Topic/Partition |
kafka_consumergroup_lag | Current Approximate Lag of a ConsumerGroup at Topic/Partition |
Metrics output example
# HELP kafka_consumergroup_current_offset Current Offset of a ConsumerGroup at Topic/Partition
# TYPE kafka_consumergroup_current_offset gauge
kafka_consumergroup_current_offset{consumergroup="KMOffsetCache-kafka-manager-3806276532-ml44w",partition="0",topic="__consumer_offsets"} -1
# HELP kafka_consumergroup_lag Current Approximate Lag of a ConsumerGroup at Topic/Partition
# TYPE kafka_consumergroup_lag gauge
kafka_consumergroup_lag{consumergroup="KMOffsetCache-kafka-manager-3806276532-ml44w",partition="0",topic="__consumer_offsets"} 1
Grafana Dashboard ID: 7589, name: Kafka Exporter Overview.
For details of the dashboard please see Kafka Exporter Overview.
If you like Kafka Exporter, please give me a star. This will help more people know Kafka Exporter.
Please feel free to send me pull requests.
Thanks goes to these wonderful people:
Your donation will encourage me to continue to improve Kafka Exporter. Support Alipay donation.
Code is licensed under the Apache License 2.0.
The exporter, alert rule, and dashboard can be deployed in Kubernetes using the Helm chart. The Helm chart used for deployment is taken from the Prometheus community, which can be found here.
If your Kafka cluster is not up and ready you can start it using Helm:
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm install my-release bitnami/kafka
Note that bitnami charts allow you to deploy a Kafka exporter as part of the Helm chart. You can enable it by adding “--set metrics.kafka.enabled=true”
helm repo add Prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install my-release prometheus-community/prometheus-kafka-exporter
Some of the common parameters that must be changed in the values file include:
kafkaServer: "IP/Hostname:9092"
All these parameters can be tuned via the values.yaml file here.
There are multiple ways to scrape the metrics as discussed above. In addition to the native way of setting up Prometheus monitoring, a service monitor can be deployed (if a Prometheus operator is being used) to scrap the data from the Kafka exporter. With this approach, multiple Kafka servers can be scrapped without altering the Prometheus configuration. Every Kafka exporter comes with its own service monitor.
In the above-mentioned chart, a service monitor can be deployed by turning it on from the values.yaml file here.
prometheus:
serviceMonitor:
enabled: true
namespace: monitoring
interval: "30s"
# If serviceMonitor is enabled and you want prometheus to automatically register
# target using serviceMonitor, add additionalLabels with prometheus release name
# e.g. If you have deployed kube-prometheus-stack with release name kube-prometheus
# then additionalLabels will be
# additionalLabels:
# release: kube-prometheus
additionalLabels: {}
targetLabels: []
Update the annotation section here if you are not using the Prometheus Operator.
service:
annotations:
prometheus.io/path: /metrics
prometheus.io/scrape: "true"
And with this, we conclude our discussion of the Kafka exporter. If you have any questions about the content of this article or our other exporter reviews, you can reach our team via support@nexclipper.io. Stay tuned for more useful exporter reviews in the near future!
After digging into all the valuable metrics, this section explains in detail how we can get critical alerts.
PromQL is a query language for the Prometheus monitoring system. It is designed for building powerful yet simple queries for graphs, alerts, or derived time series (aka recording rules). PromQL is designed from scratch and has zero common grounds with other query languages used in time series databases, such as SQL in TimescaleDB, InfluxQL, or Flux. More details can be found here.
Prometheus comes with a built-in Alert Manager that is responsible for sending alerts (could be email, Slack, or any other supported channel) when any of the trigger conditions is met. Alerting rules allow users to define alerts based on Prometheus query expressions. They are defined based on the available metrics scraped by the exporter. Click here for a good source for community-defined alerts.
A general alert looks as follows:
- alert:(Alert Name)
expr: (Metric exported from exporter) >/</==/<=/=> (Value)
for: (wait for a certain duration between first encountering a new expression output vector element and counting an alert as firing for this element)
labels: (allows specifying a set of additional labels to be attached to the alert)
annotation: (specifies a set of informational labels that can be used to store longer additional information)
Some of the recommended Kafka alerts are:
- alert: KafkaTopicsReplicas
expr: sum(kafka_topic_partition_in_sync_replica) by (topic) < 3
for: 0m
labels:
severity: critical
annotations:
summary: Kafka topics replicas (instance {{ $labels.instance }})
description: "Kafka topic in-sync partition\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: KafkaConsumersGroup
expr: sum(kafka_consumergroup_lag) by (consumergroup) > 50
for: 1m
labels:
severity: critical
annotations:
summary: Kafka consumers group (instance {{ $labels.instance }})
description: "Kafka consumers group\n VALUE = {{ $value }}\n LABELS = {{ $labels }}"
- alert: KafkaBrokerDown
expr: kafka_brokers < 3
for: 0m
labels:
severity: critical
annotations:
Summary: "Kafka broker *{{ $labels.instance }}* alert status"
description: "One of the Kafka broker *{{ $labels.instance }}* is down."
Graphs are easier to understand and more user-friendly than a row of numbers. For this purpose, users can plot their time series data in visualized format using Grafana.
Grafana is an open-source dashboarding tool used for visualizing metrics with the help of customizable and illustrative charts and graphs. It connects very well with Prometheus and makes monitoring easy and informative. Dashboards in Grafana are made up of panels, with each panel running a PromQL query to fetch metrics from Prometheus.
Grafana supports community-driven graphs for most of the widely used software, which can be directly imported to the Grafana Community.
NexClipper uses the Kafka by the jack chen dashboard, which is widely accepted and has a lot of useful panels.
What is a Panel?
Panels are the most basic component of a dashboard and can display information in various ways, such as gauge, text, bar chart, graph, and so on. They provide information in a very interactive way. Users can view every panel separately and check the value of metrics within a specific time range.
The values on the panel are queried using PromQL, which is Prometheus Query Language. PromQL is a simple query language used to query metrics within Prometheus. It enables users to query data, aggregate and apply arithmetic functions to the metrics, and then further visualize them on panels.
Here are some examples of panels: