Repository files navigation # Prometheus Monitoring with Node.js
This project integrates **Prometheus** with a **Node.js application**, allowing you to monitor metrics both locally and on **Render**.
---
## Project Structure
```
C:\Prometheus\prometheus-3.6.0.windows-amd64\
│ prometheus.exe
│ prometheus.yml
│ ...
NodeApp\
│ server.js
│ package.json
│ ...
```
---
## Prometheus Configuration (`prometheus.yml`)
```yaml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
# Scrape Prometheus itself
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
labels:
app: "prometheus"
# Scrape Node.js app locally
- job_name: "nodejs-app"
static_configs:
- targets: ["localhost:3000"]
labels:
app: "nodejs-app"
# Scrape Node.js app on Render
- job_name: "nodejs-app-render"
scheme: https
static_configs:
- targets: ["my-app.onrender.com"]
labels:
app: "nodejs-app"
tls_config:
insecure_skip_verify: true
```
**Important:**
- Do **not** add `https://` in `targets`. Use just the domain/host.
- Use `scheme: https` for HTTPS connections.
---
## Running Prometheus
1. Open **PowerShell** and go to the Prometheus folder:
```powershell
cd C:\Prometheus\prometheus-3.6.0.windows-amd64
```
2. Start Prometheus:
```powershell
.\prometheus.exe --config.file=prometheus.yml --web.enable-lifecycle
```
3. Open the UI in your browser:
[http://localhost:9090](http://localhost:9090)
---
## Reload Prometheus Config
If you change `prometheus.yml`, reload Prometheus without restarting:
```powershell
curl.exe -X POST http://localhost:9090/-/reload
```
---
## Verify Targets
To check active targets:
```powershell
curl.exe http://localhost:9090/api/v1/targets
```
Or visit in browser:
[http://localhost:9090/targets](http://localhost:9090/targets)
You should see jobs for:
- `prometheus`
- `nodejs-app`
- `nodejs-app-render`
---
## Node.js App Setup
Example **`server.js`** with metrics using `prom-client`:
```js
import express from "express";
import client from "prom-client";
const app = express();
const port = 3000;
// Create a registry and collect default system metrics
const register = new client.Registry();
client.collectDefaultMetrics({ register });
// Example counter
const httpRequests = new client.Counter({
name: "http_requests_total",
help: "Total number of HTTP requests",
labelNames: ["method", "route", "status"],
});
register.registerMetric(httpRequests);
// Middleware to track requests
app.use((req, res, next) => {
res.on("finish", () => {
httpRequests.inc({
method: req.method,
route: req.route ? req.route.path : req.path,
status: res.statusCode,
});
});
next();
});
// Example routes
app.get("/", (req, res) => res.send("Hello from Node.js!"));
app.get("/api/test", (req, res) => res.json({ msg: "Testing API" }));
// Expose metrics
app.get("/metrics", async (req, res) => {
res.set("Content-Type", register.contentType);
res.end(await register.metrics());
});
app.listen(port, () => {
console.log(` App running on port ${port}`);
});
```
---
## Test Endpoints
- Local Prometheus: [http://localhost:9090](http://localhost:9090)
- Local metrics: [http://localhost:3000/metrics](http://localhost:3000/metrics)
- Render metrics: [https://my-app.onrender.com/metrics](https://my-app.onrender.com/metrics)
---
## Troubleshooting
- If only Prometheus is scraping, check your Node.js app is running (`npm run dev`).
- Ensure `prometheus.yml` is in the **same folder** as `prometheus.exe`.
- Only keep **one Prometheus installation folder** to avoid conflicts.
- Use `--web.enable-lifecycle` so config reload works.
---
## Example PromQL Queries
Inside Prometheus UI, try:
- Total requests:
```
http_requests_total
```
- Requests per second:
```
rate(http_requests_total[1m])
```
- Node.js CPU/memory metrics:
```
process_cpu_seconds_total
process_resident_memory_bytes
```
---
With this setup, Prometheus scrapes both your **local app** and your **Render app** successfully.
# DF-2.0-AIMA-Prometheus
About
Direct Prometheus integration
Resources
Stars
Watchers
Forks
You can’t perform that action at this time.