使用Prometheus编写Pulsar指标查询遇阻:返回非预期指标求解答
Hey there, let's break down why you're getting JVM metrics instead of the Pulsar subscription count you're after—this is a common mix-up when starting out with these tools!
First: You're Using the Wrong Endpoint for PromQL Queries
The biggest issue here is that you're hitting the /metrics endpoint, which is where services (like the Pulsar broker) expose raw metrics data, not where you run Prometheus queries. Prometheus has a dedicated API endpoint for executing PromQL queries:
Instead of this:
GET http://localhost:8000/metrics?query=pulsar_subscriptions_count{cluster="pulsar-cluster",namespace="default",topic="measurements-combined"}
You need to use Prometheus's query API (default port for Prometheus is 9090):
GET http://localhost:9090/api/v1/query?query=pulsar_subscriptions_count{cluster="pulsar-cluster",namespace="default",topic="measurements-combined"}
Next: Double-Check Prometheus is Scraping Pulsar's Metrics
Even with the right endpoint, you won't get results unless Prometheus is actually collecting Pulsar's metrics. Here's how to confirm:
- Open your Prometheus config file (
prometheus.yml) and make sure you have a scrape job for Pulsar. It should look something like this (Pulsar brokers default to exposing metrics on port 8080):scrape_configs: - job_name: 'pulsar-broker' static_configs: - targets: ['localhost:8080'] - Restart Prometheus to apply the config.
- Head to the Prometheus UI at
http://localhost:9090/targetsand verify thepulsar-brokertarget shows as "UP".
Then: Verify the Metric and Labels Exist
Before running your filtered query, confirm the metric itself is available:
- Go to the Prometheus UI's Graph page (
http://localhost:9090/graph). - Run a basic query:
pulsar_subscriptions_count(no labels). If you see results, that means Prometheus is pulling the metric. - Check the labels attached to those results—make sure your cluster, namespace, and topic names match exactly (they're case-sensitive!). For example, if the cluster label is actually
local-clusterinstead ofpulsar-cluster, your filtered query won't return anything.
One Last Check: Confirm Pulsar's Metrics Are Enabled
Pulsar enables metrics by default, but if you've modified broker configs, double-check that metricsEnabled is set to true in your Pulsar broker config file. You can also directly hit Pulsar's metrics endpoint (e.g., http://localhost:8080/metrics) and search for pulsar_subscriptions_count to ensure it's being exposed.
Give those steps a try, and you should start seeing the subscription count you're looking for!
内容的提问来源于stack exchange,提问作者kter

