-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GITBOOK-216: change request with no subject merged in GitBook
- Loading branch information
1 parent
9938073
commit 676c6a4
Showing
3 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
description: How to use the built-in prometheus exporter | ||
--- | ||
|
||
# Prometheus | ||
|
||
BullMQ provides a simple API that can be used to export metrics to Prometheus. You just need to create an endpoint in your webserver that calls exportPrometheusMetrics() and configure prometheus to consume from this endpoint. For example using vanilla NodeJS: | ||
|
||
```typescript | ||
import http from "http"; | ||
import { Queue } from "bullmq"; | ||
|
||
const queue = new Queue("my-queue"); | ||
|
||
const server = http.createServer( | ||
async (req: http.IncomingMessage, res: http.ServerResponse) => { | ||
try { | ||
if (req.url === "/metrics" && req.method === "GET") { | ||
const metrics = await queue.exportMetrics(); | ||
|
||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
"Content-Length": Buffer.byteLength(metrics), | ||
}); | ||
res.end(metrics); | ||
} else { | ||
res.writeHead(404); | ||
res.end("Not Found"); | ||
} | ||
} catch (err: unknown) { | ||
res.writeHead(500); | ||
res.end(`Error: ${err instanceof Error ? err.message : "Unknown error"}`); | ||
} | ||
} | ||
); | ||
|
||
const PORT = process.env.PORT || 3000; | ||
|
||
server.listen(PORT, () => { | ||
console.log(`Prometheus metrics server running on port ${PORT}`); | ||
console.log(`Metrics available at http://localhost:${PORT}/metrics`); | ||
}); | ||
``` | ||
|
||
If you curl to the endpoint like this:  | ||
|
||
```bash | ||
curl http://localhost:3000/metrics | ||
``` | ||
|
||
You will get an output similar to this: | ||
|
||
``` | ||
HELP bullmq_job_count Number of jobs in the queue by state | ||
TYPE bullmq_job_count gauge | ||
bullmq_job_count{queue="my-queue", state="waiting"} 5 | ||
bullmq_job_count{queue="my-queue", state="active"} 3 | ||
bullmq_job_count{queue="my-queue", state="completed"} 12 | ||
bullmq_job_count{queue="my-queue", state="failed"} 2 | ||
``` | ||
|
||
If you use ExpressJS the code is a bit simpler: | ||
|
||
```typescript | ||
import express from 'express'; | ||
import { Queue } from './src/queue'; | ||
|
||
const app = express(); | ||
const queue = new Queue('my-queue'); | ||
|
||
app.get('/metrics', async (req, res) => { | ||
try { | ||
const metrics = await queue.exportMetrics(); | ||
res.set('Content-Type', 'text/plain'); | ||
res.send(metrics); | ||
} catch (err) { | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
const PORT = process.env.PORT || 3000; | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Prometheus metrics server running on port ${PORT}`); | ||
console.log(`Metrics available at http://localhost:${PORT}/metrics`); | ||
}); | ||
``` | ||
|