How to use prometheus query to count the qps of a given link?
- User
How to use prometheus query to count the qps of a given link?
- Assistant
Okay, let's figure out how to calculate the Queries Per Second (QPS) for a specific link (like a URL endpoint) using Prometheus Query Language (PromQL).
The core idea is to use the
rate()function on a counter metric that tracks the total number of HTTP requests.1. Identify the Correct Metric:
First, you need to know the name of the metric that counts the requests for your application or service. This depends heavily on the instrumentation library or exporter you are using. Common examples include:
http_requests_total(often used by custom instrumentation)http_server_requests_seconds_count(used by Spring Boot Actuator micrometer)nginx_ingress_controller_requestsornginx_http_requests_total(for NGINX)istio_requests_total(for Istio service mesh)haproxy_frontend_http_requests_total(for HAProxy)
You can find the available metrics by exploring your Prometheus instance's UI (usually at
/graph) and typinghttporrequestin the metric explorer.2. Identify the Correct Labels:
Next, you need labels that distinguish the specific link (URL path or endpoint) you care about. Common labels include:
pathurihandlerendpointjob(identifies the source application/exporter)instance(identifies the specific process/pod)method(e.g., GET, POST)statusorcode(HTTP status code, e.g., 200, 500)
3. Construct the PromQL Query:
The general formula uses the
rate()function, which calculates the per-second average rate of increase of a counter over a specified time window.rate( <metric_name> { <label1>="<value1>", <label2>="<value2>", ... } [ <time_range> ] )Example:
Let's assume:
- Your metric is
http_requests_total. - The label identifying the URL path is
path. - The specific link you want to measure is
/api/users. - You want to calculate the average QPS over the last 5 minutes (
5m).
The query would be:
rate(http_requests_total{path="/api/users"}[5m])Explanation:
http_requests_total{path="/api/users"}: This selects the time series for the counter metrichttp_requests_totalwhere thepathlabel is exactly/api/users.[5m]: This specifies the time window over which to calculate the rate. Prometheus looks at the counter's value change over the last 5 minutes.rate(...): This function calculates the average per-second increase of the selected counter(s) over the specified time window (5m). The result is the QPS.
Considering Multiple Instances/Pods:
If your service runs on multiple instances or pods, the query above will return a separate QPS value for each instance that serves the
/api/userspath. To get the total QPS for that path across all instances, you need to sum the individual rates:sum( rate( http_requests_total{path="/api/users"}[5m] ) )Using
irate()for Instantaneous QPS:If you want a more instantaneous QPS value, sensitive to rapid changes, you can use
irate()instead ofrate().irate()calculates the rate based only on the last two data points within the specified time window.# Instantaneous QPS per instance irate(http_requests_total{path="/api/users"}[1m]) # Total instantaneous QPS across all instances sum( irate( http_requests_total{path="/api/users"}[1m] ) )Note: A shorter time range like
1mis often used withirate().Steps to Implement:
- Explore Metrics: Go to your Prometheus UI. Search for metrics related to HTTP requests (e.g., search for
requestorhttp). - Find Your Metric: Identify the counter metric tracking total requests.
- Identify Labels: Examine the labels associated with that metric. Find the label(s) that identify the URL path or endpoint (e.g.,
path,uri,handler). Note any other relevant labels likejob,service,method,status. - Construct Query: Build your
rate()orsum(rate(...))query using the correct metric name, labels, and desired time range (e.g.,1m,5m,10m). - Test: Run the query in the Prometheus graph view to see the QPS result.
Remember to replace
http_requests_totalandpath="/api/users"with the actual metric name and label selectors relevant to your specific environment and the link you want to monitor.</time_range></metric_name>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.