How to Monitor REST APIs with Prometheus

Better Stack Team
Updated on November 29, 2024

Prometheus is an effective tool for monitoring REST APIs by collecting and analyzing metrics. By integrating Prometheus with your application or external monitoring setup, you can track key performance metrics such as response time, request count, error rates, and throughput.

1. Expose API metrics

To monitor REST APIs, you need to expose metrics in a Prometheus-compatible format. This is usually done by implementing an HTTP endpoint (e.g., /metrics) that serves these metrics in the text-based format Prometheus understands.

Example in Python using the Prometheus Client:

Install the Prometheus client library:

 
pip install prometheus-client

Add metrics exposition in your application:

 
from prometheus_client import Counter, Histogram, start_http_server
from flask import Flask, request

app = Flask(__name__)

# Define metrics
REQUEST_COUNT = Counter('api_requests_total', 'Total API requests', ['method', 'endpoint', 'status'])
REQUEST_LATENCY = Histogram('api_request_latency_seconds', 'Latency of API requests', ['endpoint'])

@app.route('/api', methods=['GET'])
def my_api():
    with REQUEST_LATENCY.labels(endpoint="/api").time():  # Track latency
        status = 200
        REQUEST_COUNT.labels(method='GET', endpoint='/api', status=status).inc()  # Increment request counter
        return {"message": "Hello, World!"}, status

if __name__ == '__main__':
    start_http_server(8000)  # Expose metrics on port 8000
    app.run(port=5000)
  • Metrics are available at http://<host>:8000/metrics.
  • Prometheus can scrape this endpoint to collect data.

2. Configure Prometheus to scrape metrics

Add a scrape job in your prometheus.yml configuration:

 
scrape_configs:
  - job_name: 'api-monitoring'
    static_configs:
      - targets: ['<your-service-host>:8000']  # Replace with your metrics endpoint

Reload Prometheus to apply the configuration.


3. Define PromQL queries for REST API monitoring

Examples of useful queries:

  1. Total requests: promql sum(api_requests_total)

  2. Requests per second: promql rate(api_requests_total[1m])

  3. Request latency (average): promql sum(rate(api_request_latency_seconds_sum[1m])) / sum(rate(api_request_latency_seconds_count[1m]))

  4. Error rate: promql sum(rate(api_requests_total{status="500"}[5m])) / sum(rate(api_requests_total[5m]))


4. Visualize metrics with Grafana

  1. Add Prometheus as a data source in Grafana.
  2. Import or create dashboards for API performance.
  3. Example visualizations:
    • Request count over time.
    • Latency heatmaps.
    • Error rates.

5. Use exporters for third-party APIs

If you're monitoring external APIs, use Prometheus exporters to fetch and expose data.

Example: Blackbox Exporter

  • Install Blackbox Exporter to monitor the availability and latency of REST APIs.
  • Add it to Prometheus configuration:
 
  scrape_configs:
    - job_name: 'blackbox'
      metrics_path: /probe
      params:
        module: [http_2xx]  # Probe for HTTP 200 responses
      static_configs:
        - targets:
            - https://api.example.com
      relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: blackbox-exporter:9115
  • The Blackbox Exporter probes the target API and provides metrics like availability and response time.

By exposing metrics, configuring Prometheus, and using effective queries and visualizations, you can monitor REST APIs comprehensively with Prometheus.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github