This article explains how to implement monitoring for HTTP requests per endpoint using counters and labels in an application.
In this article, we explain how to modify your application to record the number of HTTP requests per endpoint while still keeping track of the overall total. Initially, the application handles requests for two endpoints: /cars and /boats. Let’s start with a basic implementation that uses a global counter which increments on every call.
The following code demonstrates a simple approach where a single counter, REQUESTS, is incremented regardless of the endpoint.
While this method works to keep track of the total number of requests, it does not allow you to determine how many requests each endpoint receives. To achieve this, you could create individual counters for every endpoint.
One approach is to instantiate separate counters for each endpoint. This method is straightforward, but it requires you to manually update queries and configurations each time a new endpoint is added.
Copy
Ask AI
CAR_REQUESTS = Counter('requests_cars_total', 'Total number of requests for /cars path')BOATS_REQUESTS = Counter('requests_boats_total', 'Total number of requests for /boats path')@app.get("/cars")def get_cars(): CAR_REQUESTS.inc() return ["toyota", "honda", "mazda", "lexus"]@app.post("/cars")def create_cars(): CAR_REQUESTS.inc() return "Create Car"@app.get("/boats")def get_boats(): BOATS_REQUESTS.inc() return ["boat1", "boat2", "boat3"]@app.post("/boats")def create_boat(): BOATS_REQUESTS.inc() return "Create Boat"
As your application scales up, managing multiple counters for different endpoints can become cumbersome. Each new endpoint will require additional counter instances and updates in your monitoring queries.
A more scalable solution is to use labels. This approach allows you to define a single counter with the flexibility of differentiating request paths using labels. When initializing the counter, you specify a list of label names (in this example, we use path). Then, while handling a request, call the labels method with the appropriate path before incrementing the counter.
With this setup, you can query the metrics for a specific endpoint by filtering on the path label. For example, running the query for the /cars endpoint would look like this:
To enhance your monitoring further, you might want to track the HTTP method (e.g., GET or POST) alongside the request path. This requires adding a second label called method during counter initialization. When handling a request, provide both the path and the method to the labels method.
From the framework’s perspective (for example, in Flask), the method label corresponds to the actual HTTP method used in the request. This configuration allows you to query the metrics either by combining both labels—for instance, filtering by method="get"—or solely by the path label to obtain aggregated counts. This flexible, label-based approach enables scalable and maintainable monitoring of your application’s endpoints as it grows and evolves.