# Infrastructure Monitoring Setup
This template outlines a standard monitoring stack using Prometheus and Grafana, suitable for Kubernetes or Docker Compose environments.
## 1. Prometheus Configuration (`prometheus.yml`)
```yaml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "node_exporter"
static_configs:
- targets: ["node-exporter:9100"]
- job_name: "app_backend"
scrape_interval: 5s
static_configs:
- targets: ["backend:3000"]
metrics_path: "/metrics"
```
## 2. Docker Compose Setup
```yaml
version: "3.8"
services:
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana:latest
environment:
- GF_SECURITY_ADMIN_PASSWORD=secret
ports:
- "3000:3000"
depends_on:
- prometheus
```
## 3. Application Instrumentation (Node.js with `prom-client`)
```typescript
import express from "express";
import client from "prom-client";
const app = express();
const collectDefaultMetrics = client.collectDefaultMetrics;
collectDefaultMetrics();
const httpRequestDurationMicroseconds = new client.Histogram({
name: "http_request_duration_seconds",
help: "Duration of HTTP requests in seconds",
labelNames: ["method", "route", "code"],
buckets: [0.1, 0.5, 1, 1.5, 5, 10],
});
app.use((req, res, next) => {
const end = httpRequestDurationMicroseconds.startTimer();
res.on("finish", () => {
end({
method: req.method,
route: req.route?.path || req.path,
code: res.statusCode,
});
});
next();
});
app.get("/metrics", async (req, res) => {
res.set("Content-Type", client.register.contentType);
res.end(await client.register.metrics());
});
```