npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-prometheus

v1.9.2

Published

A wrapper plugin for prometheus metrics.

Downloads

7,450

Readme

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",
      },

      // run metrics on seperate server / port
      server: {
        // when enabled metrics will run seperatly from the strapi instance. It will still go up / down with strapi
        // if disabled it will create /api/metrics endpoint on main strapi instance
        // when enabled install run `npm i express`
        enabled: false
        port: 9000,
        host: 'localhost',
        path: '/metrics',
      }
    }
  }
];

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.