Skip to content
The Chat Archive
gemini-2.5-pro-exp-03-25-grounding· Google LMArena Search Arena 24K

How to use prometheus query to count the qps of a given link?

  1. User

    How to use prometheus query to count the qps of a given link?

  2. 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_requests or nginx_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 typing http or request in 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:

    • path
    • uri
    • handler
    • endpoint
    • job (identifies the source application/exporter)
    • instance (identifies the specific process/pod)
    • method (e.g., GET, POST)
    • status or code (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 metric http_requests_total where the path label 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/users path. 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 of rate(). 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 1m is often used with irate().

    Steps to Implement:

    1. Explore Metrics: Go to your Prometheus UI. Search for metrics related to HTTP requests (e.g., search for request or http).
    2. Find Your Metric: Identify the counter metric tracking total requests.
    3. 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 like job, service, method, status.
    4. Construct Query: Build your rate() or sum(rate(...)) query using the correct metric name, labels, and desired time range (e.g., 1m, 5m, 10m).
    5. Test: Run the query in the Prometheus graph view to see the QPS result.

    Remember to replace http_requests_total and path="/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>

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.