📖 Scenario: You work in a company that collects client metrics data through Kafka topics. Your task is to create a simple Kafka consumer that reads client metrics from a topic and processes them step-by-step.
🎯 Goal: Build a Kafka consumer that subscribes to a topic named client_metrics, filters metrics based on a threshold, and prints the filtered metrics.
📋 What You'll Learn
Create a Kafka consumer configuration dictionary with the required settings
Set a threshold variable to filter client metrics
Consume messages from the client_metrics topic and filter metrics above the threshold
Print the filtered metrics
💡 Why This Matters
🌍 Real World
Monitoring client metrics helps companies track performance and detect issues early by processing data streams in real time.
💼 Career
Kafka is widely used in DevOps and data engineering roles to handle streaming data pipelines and real-time analytics.
Progress0 / 4 steps
1
Create Kafka consumer configuration
Create a dictionary called consumer_config with these exact entries: 'bootstrap.servers': 'localhost:9092', 'group.id': 'metrics_group', and 'auto.offset.reset': 'earliest'.
Kafka
Hint
Use a Python dictionary with the exact keys and values as specified.
2
Set the metric threshold
Create a variable called threshold and set it to the integer 50.
Kafka
Hint
Just assign the number 50 to the variable named threshold.
3
Filter client metrics above threshold
Create a list called filtered_metrics that contains only the values from the list metrics which are greater than the variable threshold. Use a list comprehension.
Kafka
Hint
Use a list comprehension with the condition m > threshold.
4
Print the filtered metrics
Write a print statement to display the variable filtered_metrics.
Kafka
Hint
Use print(filtered_metrics) to show the filtered list.
Practice
(1/5)
1. What is the main purpose of client metrics monitoring in Kafka?
easy
A. To configure Kafka brokers for better storage
B. To track real-time performance data like message counts and errors
C. To manage user access permissions
D. To backup Kafka data automatically
Solution
Step 1: Understand client metrics monitoring
Client metrics monitoring collects data about Kafka client performance such as message counts, latency, and errors.
Step 2: Identify the main purpose
The main goal is to observe client behavior in real time to improve reliability and troubleshoot issues quickly.
Final Answer:
To track real-time performance data like message counts and errors -> Option B
Quick Check:
Client metrics = performance data tracking [OK]
Hint: Client metrics = real-time client performance data [OK]
Common Mistakes:
Confusing client metrics with broker configuration
Thinking metrics manage user permissions
Assuming metrics handle data backup
2. Which of the following is the correct way to enable client metrics reporting in a Kafka producer configuration?
easy
A. "client.metrics.enabled=yes"
B. "enable.metrics=true"
C. "metrics.recording.level=DEBUG"
D. "metrics.sample.window.ms=30000"
Solution
Step 1: Review Kafka producer metric configs
Kafka uses properties like "metrics.sample.window.ms" to control metrics sampling intervals.
Step 2: Identify valid configuration syntax
"metrics.sample.window.ms=30000" is a valid setting to enable metrics sampling every 30 seconds.
Hint: Look for valid Kafka metric property names [OK]
Common Mistakes:
Using non-existent properties like enable.metrics
Wrong boolean values like 'yes' instead of true/false
Confusing logging level with metrics config
3. Given the following Kafka consumer metrics output snippet: {"records-consumed-total": 1500, "fetch-latency-avg": 25, "fetch-errors-total": 0} What does this tell you about the consumer's performance?
medium
A. The consumer has consumed 1500 records with an average fetch latency of 25 ms and no fetch errors
B. The consumer has 1500 fetch errors and average latency of 0 ms
C. The consumer has not consumed any records yet
D. The consumer is experiencing high fetch errors
Solution
Step 1: Interpret each metric value
"records-consumed-total": 1500 means 1500 messages consumed; "fetch-latency-avg": 25 means average fetch latency is 25 milliseconds; "fetch-errors-total": 0 means no fetch errors.
Step 2: Summarize consumer performance
The consumer is working well, consuming messages with reasonable latency and no errors.
Final Answer:
The consumer has consumed 1500 records with an average fetch latency of 25 ms and no fetch errors -> Option A
Quick Check:
Metrics values = The consumer has consumed 1500 records with an average fetch latency of 25 ms and no fetch errors [OK]
Hint: Check counts and latency values carefully [OK]
Common Mistakes:
Misreading zero errors as errors present
Confusing latency with error count
Assuming no records consumed when count is positive
4. You configured Kafka client metrics but see no metrics reported. Which of the following is the most likely cause?
medium
A. Metrics reporting interval is set too high, delaying output
B. Kafka broker is down
C. Client metrics are disabled by default and need explicit enabling
Kafka clients do not always enable metrics reporting by default; explicit configuration is often required.
Step 2: Evaluate other options
Broker down or firewall issues affect connectivity but not metrics reporting directly; reporting interval delays metrics but does not stop them completely.
Final Answer:
Client metrics are disabled by default and need explicit enabling -> Option C
Quick Check:
Metrics disabled by default = Client metrics are disabled by default and need explicit enabling [OK]
Hint: Check if metrics are explicitly enabled in config [OK]
Common Mistakes:
Assuming broker status affects client metrics directly
Ignoring need to enable metrics explicitly
Blaming network issues without checking configs
5. You want to monitor Kafka producer client metrics to detect high message send latency and errors in real time. Which approach is best to achieve this?
hard
A. Configure the producer to expose JMX metrics and use a monitoring tool to collect and alert on latency and error metrics
B. Increase the producer batch size to reduce latency
C. Enable debug logging on the Kafka broker
D. Use Kafka Connect to export metrics to a database
Solution
Step 1: Identify how to access client metrics
Kafka producer clients expose metrics via JMX (Java Management Extensions), which can be collected by monitoring tools.
Step 2: Choose the best monitoring approach
Using JMX with a monitoring tool allows real-time collection and alerting on latency and errors, which fits the requirement.
Step 3: Evaluate other options
Increasing batch size affects performance but not monitoring; debug logging on broker does not monitor client metrics; Kafka Connect exports data but is not the direct way to monitor client metrics.
Final Answer:
Configure the producer to expose JMX metrics and use a monitoring tool to collect and alert on latency and error metrics -> Option A
Quick Check:
JMX metrics + monitoring tool = Configure the producer to expose JMX metrics and use a monitoring tool to collect and alert on latency and error metrics [OK]
Hint: Use JMX metrics with monitoring tools for real-time alerts [OK]
Common Mistakes:
Confusing performance tuning with monitoring setup
Relying on broker logs for client metrics
Using Kafka Connect for direct client metric monitoring
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.