1.8.1 ā€¢ Published 6 months ago

strapi-prometheus v1.8.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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
    • Response time in seconds
    • Request size in bytes
    • Response size in bytes
    • Number of open connections
  • Collect apollo Graphql metrics
    • query duration
    • queries validated
    • queries parsed
  • Process Metrics as recommended by Prometheus
  • Endpoint to retrieve the metrics - used for Prometheus scraping
  • Endpoint to get specific metric
  • Set custom labels
  • custom registrer
  • mutliple registers

ā³ Installation

npm i strapi-prometheus

Plugin

// config/plugins.js

// enable plugin with default configuration.
module.exports = [
  'strapi-prometheus': {
    enabled: true,
    config: {
      // add prefix to all the prometheus metrics names.
      prefix: '',

      // use full url instead of matched url
      // true  => path label: `/api/models/1`
      // false => path label: `/api/models/:id`
      fullURL: false,

      // include url query in the url label
      // true  => path label: `/api/models?limit=1`
      // false => path label: `/api/models`
      includeQuery: false,

      // metrics that will be enabled, by default they are all enabled.
      enabledMetrics: {
        koa: true, // koa metrics
        process: true, // metrics regarding the running process
        http: true, // http metrics like response time and size
        apollo: true, // metrics regarding graphql
      },

      // interval at which rate metrics are collected in ms
      interval: 10_000,

      // set custom/default labels to all the prometheus metrics
      customLabels: {
        name: "strapi-prometheus",
      },
    }
  }
];

Graphql setup

// config/plugins.js
const { apolloPrometheusPlugin } = require('strapi-prometheus')

module.exports = [
  'strapi-prometheus': {
    enabled: true,
  },
  graphql: {
    enabled: true,
    config: {
      apolloServer: {
        plugins: [apolloPrometheusPlugin], // add the plugin to get apollo metrics
        tracing: true, // this must be true to get some of the data needed to create the metrics
      }
    }
  }
}

Metrics

Metrics are exposed at /api/metrics.

You can pass a format in the url to either get metrics in json format or plain text. Available options are json and text. Default is text. ex: /api/metrics?format=text

šŸ‘®ā€ā™€ļø Security

āš ļø Use at own risk.

By default no one can access the /api/metrics url. This is to prevent sensitive data from the metrics being exposed by default.

To access the metrics you have 2 options:

  1. create an api token with either read only access or custom access to the metrics endpoints. If you don't know how to create an api token here is a turorial
  2. using the user-permissions plugin: (NOT RECOMMENDED)
    • create a user account and allow authorized users to query the endpoints. This does give the opportunity to every user to query your metrics.
    • allow public role to query the metric endpoints. This should never be done, unless really necessary.

šŸ– 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

šŸ“Š 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: 5s
scrape_configs:
  - job_name: "api"
    metrics_path: "/api/metrics"
    static_configs:
      - targets: ["localhost:1337"]

šŸ“Š 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.

1.8.1

6 months ago

1.8.0

9 months ago

1.7.0

9 months ago

1.6.0

11 months ago

1.5.0

1 year ago

1.4.0

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

2 years ago

1.0.0

2 years ago

1.3.1

1 year ago

1.3.0

1 year ago

0.6.3

2 years ago

0.7.0

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago