Swix
Swix

Reputation: 2123

How to monitor Fastify app with Prometheus and Grafana?

I am learning to monitor my Fastify app with Prometheus and Grafana. First, I installed fastify-metrics package and registered in Fastify app.

// app.ts

import metrics from 'fastify-metrics'

...
app.register(metrics, {
  endpoint: '/metrics',
})

Then I setup Prometheus and Grafana in docker-compose.yml:

version: "3.7"
services:

  prometheus:
    image: prom/prometheus:latest
    volumes:
      - prometheus_data:/prometheus
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    network_mode: host
    ports:
      - '9090:9090'

  grafana:
    image: grafana/grafana:latest
    volumes:
      - grafana_data:/var/lib/grafana
      # - ./grafana/provisioning:/etc/grafana/provisioning
      # - ./grafana/config.ini:/etc/grafana/config.ini
      # - ./grafana/dashboards:/var/lib/grafana/dashboards
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=ohno
    depends_on:
      - prometheus
    network_mode: host
    ports:
      - '3000:3000'

volumes:
  prometheus_data: {}
  grafana_data: {}

I added network_mode=host because Fastfy app will be running at localhost:8081.

Here's the Prometheus config:

# prometheus.yml

global:
  scrape_interval: 15s
  scrape_timeout: 10s
  evaluation_interval: 1m
scrape_configs:
  - job_name: 'prometheus'
    # metrics_path: /metrics
    static_configs:
      - targets: [
        'app:8081',
      ]
  - job_name: 'node_exporter'
    static_configs:
      - targets: [
        'localhost:8081',
      ]

After docker-compose up and npm run dev, Fastify app is up and running and target localhost:8081 is UP in Prometheus dashboard, localhost:9090, I tried executing some metrics.

I imported Node Exporter Full and Node Exporter Server Metrics dashboards. And added Prometheus datasource localhost:9090, named Fastify, and saved successfully, showed Data source is working.

However, when I go to the Node Exporter Full dashboard, it shows no data. I selected Fastify in datasource but it shows None in others selections at upper left corner.

What I am doing wrong?

Upvotes: 3

Views: 3400

Answers (2)

dsalkamoses
dsalkamoses

Reputation: 280

you should specify the metrics_path in the job as defined in your 'fastify-metrics' endpoint and also update the targets param:

- job_name: 'node_exporter'
    scrape_interval: 5s
    metrics_path: /metrics
    scheme: http
    static_configs:
      - targets: ['localhost:8081']
        labels:
          group: 'node_exporter'

Upvotes: 0

dSystems
dSystems

Reputation: 21

It looks like you're using a dashboard intended for linux stats. In order to use Prometheus/Grafana with your Fastify app, you'll need a dashboard that's meant for Node.js apps. For example:

Plugging one of those in should do the trick.

Upvotes: 2

Related Questions