Home  Devops   How to capt ...

How to capture HTTP request count, latency, and error rates with Prometheus

To capture HTTP request count, latency, and error rates with Prometheus, you need to instrument your application using a Prometheus client library. Below are examples for capturing these metrics in a Node.js application using the prom-client library.

Example Code: Node.js with express and prom-client

1. Set Up Your Project

Create a directory for your project and navigate into it:

mkdir prometheus-example
cd prometheus-example

Initialize a new Node.js project:

npm init -y

Install the required dependencies:

npm install express prom-client

2. Create the Application

Create a file named app.js with the following content:

const express = require('express');
const client = require('prom-client');
const app = express();
const register = client.register;

// Create a Histogram metric to measure request durations
const httpRequestDurationMicroseconds = new client.Histogram({
  name: 'http_request_duration_microseconds',
  help: 'Duration of HTTP requests in microseconds',
  labelNames: ['method', 'route', 'status_code'],
  buckets: [0.1, 5, 15, 50, 100, 500, 1000, 5000, 10000]  // Customize these buckets as needed
});

// Create a Counter metric to count the number of requests
const httpRequestCount = new client.Counter({
  name: 'http_request_count',
  help: 'Number of HTTP requests',
  labelNames: ['method', 'route', 'status_code']
});

// Enable the collection of default metrics
client.collectDefaultMetrics();

// Middleware to start the timer
app.use((req, res, next) => {
  res.locals.startEpoch = Date.now();
  next();
});

// Example endpoint
app.get('/', (req, res) => {
  res.send('Hello World!');
});

// Example endpoint that simulates an error
app.get('/error', (req, res) => {
  res.status(500).send('Something went wrong!');
});

// Middleware to stop the timer and record the metrics
app.use((req, res, next) => {
  const responseTimeInMs = Date.now() - res.locals.startEpoch;
  
  // Record the request duration
  httpRequestDurationMicroseconds.labels(req.method, req.route ? req.route.path : req.path, res.statusCode).observe(responseTimeInMs);
  
  // Increment the request count
  httpRequestCount.labels(req.method, req.route ? req.route.path : req.path, res.statusCode).inc();

  next();
});

// Endpoint to expose the metrics
app.get('/metrics', async (req, res) => {
  res.set('Content-Type', register.contentType);
  res.end(await register.metrics());
});

// Start the server
const PORT = 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

3. Run the Application

Run the application:

node app.js

4. Access the Metrics

Open your browser and visit http://localhost:8080/metrics. You should see the metrics exposed by the application, including the HTTP request count, duration, and error rates.

Explanation

Integration with Prometheus

To integrate this application with Prometheus, ensure that Prometheus is configured to scrape metrics from the application. Add the following job to your prometheus.yml configuration file:

scrape_configs:
  - job_name: 'example-app'
    static_configs:
      - targets: ['localhost:8080']

Restart Prometheus to apply the new configuration:

docker-compose restart prometheus

Now, Prometheus will start scraping the metrics from your application, and you can query and visualize them using the Prometheus UI or Grafana.

Published on: Jul 08, 2024, 05:17 AM  
 

Comments

Add your comment