Skip to content

XanderD99/strapi-prometheus

Repository files navigation

Strapi prometheus plugin

strapi-prometheus-downloads strapi-prometheus-version

"Buy Me A Coffee"

A simple middleware plugin that adds prometheus metrics to strapi using prom-client;

✨ Features

  • Collect API metrics for each call
  • Process Metrics as recommended by Prometheus
  • Endpoint to retrieve the metrics - used for Prometheus scraping
  • Set custom labels

⏳ Installation

npm i strapi-prometheus

Plugin

// config/plugins.js

// enable plugin with default configuration.
module.exports = [
  prometheus: {
    enabled: true,
    config: {
      // see collectDefaultMetricsOption of prom-client
      collectDefaultMetrics: false || { prefix: 'strapi_' }
      labels: { name: "strapi-prometheus" },
      server: false || { port: 9000, host: '0.0.0.0', path: '/metrics' }
    }
  }
];

👮‍♀️ Security

Caution

Use at own risk. Metric data can sometimes hold sensitive data that should not be publically available.

To keep your information secure, the plugin by default starts a new server on port 9000 so that your metrics aren't available to the outside world.

If this is not an option for you. You can disable this by updating the server config and setting it to false. This will then create an /metrics endpoint on your strapi server. This is secured by the strapi auth middleware so you will need to create a API token to be able to access it. I well mention it again this is not the recommended way, you should try and use the separate server.

🖐 Requirements

Minimum environment requirements

  • Node.js >=14.x.x
  • NPM >=6.x.x

We are following the official Node.js releases timelines.

Supported Strapi versions

  • Strapi v4.x (strapi-prometheus v1.x.x)
  • Strapi v5.x

📊 Prometheus example

⚠️ You need to create your own prometheus instance for this. This plugin does not do that for you!

here is a basic example of prometheus config. In this example we assume that the metrics endpoint is not secured.

# prometheus.yaml

global:
  scrape_interval: 10s
scrape_configs:
  - job_name: "strapi"
    metrics_path: "/metrics"
    static_configs:
      - targets: ["localhost:9000"]

📊 Grafana dashboards

Here are some usefull dashboards you can start with. If you want to have your dashboard added feel free to open a PR.

v1 -> v2 migration guide

A lot has changed from v1 to v2 other then strapi v5 support.

Firstly check the new configuration options those have been simplified a lot. For starters the name has been updated a little bit to prometheus instead of strapi-prometheus From v2 forwards a separate server that runs disconnected from strapi is the default behaviour. This can be disabled by passing server: false to the config.

Setting a custom registry has also been removed in favor of using the default register provided by the prom-client package. This allows us to create any metric at any place in our apps, instead of having to register it using this plugin. This gives you all the freedom of what to do with your custom metrics.

People using the apollo plugin will need to look into finding a new plugin to add to their apolloServer config. I recommend this one as it was used as inspiration.

Some metric updates:

  • apollo metrics have been deleted
  • http_request_duration_s renamed to http_request_duration_seconds
  • http_{response/request}_size_bytes renamed to http_{response/request}_content_length_bytes
  • added http_requests_total
  • added http_errors_total
  • added http_active_requests