How to Integrate Grafana & Prometheus Monitoring into Midway Applications
Learn step‑by‑step how to install Midway’s Prometheus plugin, configure Docker‑based Prometheus and Grafana, expose metrics from a Node.js app, and visualize them in Grafana dashboards, enabling effective monitoring and operations for your services.
Prometheus is an open‑source monitoring system originally built at SoundCloud and later graduated to the Cloud Native Computing Foundation (CNCF). Grafana is an open‑source analytics and visualization suite that can use Prometheus as a data source.
This article shows how to integrate Grafana and Prometheus with a Midway application.
Install Component
First install the Midway Prometheus monitoring component: npm install @midwayjs/prometheus -S Then import the component in configuration.ts:
// src/configuration.ts
import { Configuration } from '@midwayjs/decorator';
import * as prometheus from '@midwayjs/prometheus'; // import module
import { join } from 'path';
@Configuration({
imports: [prometheus], // import module
importConfigs: [join(__dirname, 'config')]
})
export class AutoConfiguration {}After starting the application, a new endpoint ${host}:${port}/metrics becomes available.
Prometheus pulls monitoring data via HTTP, so load a web framework such as Koa or Express and start the application in multi‑framework mode.
Visiting the endpoint returns the current metrics.
Other Configurations
The component provides configuration options that can be customized in config.default.ts:
import { DefaultConfig } from '@midwayjs/prometheus';
export const prometheus: DefaultConfig = {
labels: {
APP_NAME: 'demo_project'
}
};Additional settings are available in the DefaultConfig definition. For example, you can label nodes belonging to the same application to distinguish them in the metrics.
Data Collection
The Prometheus component added to Midway collects metrics inside the Node process. To let Prometheus scrape these metrics, set up a Prometheus server.
Set up Prometheus
Use docker‑compose.yml to launch Prometheus:
version: "2.2"
services:
tapi:
logging:
driver: "json-file"
options:
max-size: "50m"
image: prom/prometheus
restart: always
volumes:
- ./prometheus_data:/prometheus_data:rw
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- ./targets.json:/etc/prometheus/targets.json
command:
- '--storage.tsdb.path=/prometheus_data'
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.retention=10d'
ports:
- "9090:9090"The prometheus.yml configuration:
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds.
evaluation_interval: 15s # Evaluate rules every 15 seconds.
scrape_configs:
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/etc/prometheus/targets.json"
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']The targets.json file (replace ${ip} with the Node.js server IP):
[
{
"targets": [ "${ip}:3002" ],
"labels": {
"env": "prod",
"job": "api"
}
}
]Start Prometheus with: $ docker-compose up Prometheus will now scrape the metrics exposed by the Midway application.
Data Display
Use Grafana to visualize the collected data. Run Grafana via Docker:
$ docker run -d --name=grafana -p 3000:3000 grafana/grafanaAccess http://127.0.0.1:3000 with the default credentials admin:admin . Then add Prometheus as a data source and create dashboards as shown in the screenshots.
After importing a sample dashboard (ID 11159), developers can monitor their Node services, detect issues such as memory leaks or unexpected restarts, and perform custom analyses.
Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
