Skip to content

Commit

Permalink
GITBOOK-216: change request with no subject merged in GitBook
Browse files Browse the repository at this point in the history
  • Loading branch information
manast authored and gitbook-bot committed Jan 24, 2025
1 parent 9938073 commit 676c6a4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
* [Remove Dependency](guide/flows/remove-dependency.md)
* [Ignore Dependency](guide/flows/ignore-dependency.md)
* [Remove Child Dependency](guide/flows/remove-child-dependency.md)
* [Metrics](guide/metrics/metrics.md)
* [Metrics](guide/metrics/README.md)
* [Prometheus](guide/metrics/prometheus.md)
* [Rate limiting](guide/rate-limiting.md)
* [Parallelism and Concurrency](guide/parallelism-and-concurrency.md)
* [Retrying failing jobs](guide/retrying-failing-jobs.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
---
description: Built-in Metrics for your queues.
---

# Metrics

BullMQ provides a simple metrics gathering functionality that allows you to track the performance of your queues.
Workers can count the number of jobs they have processed per minute and store this data in a list to be consumed later.
BullMQ provides a simple metrics gathering functionality that allows you to track the performance of your queues. Workers can count the number of jobs they have processed per minute and store this data in a list to be consumed later.

You enable it on the worker settings by specifying how many data points you want to keep. We recommend 2 weeks of metrics data which should take a very small amount of space, just around 120Kb of RAM per queue.

Expand Down Expand Up @@ -43,5 +46,4 @@ const metrics = await queue.getMetrics('completed');
*/
```

Note that the `getMetrics` method also accepts a `start` and `end` argument (`0` and `-1` by default), that you can
use if you want to implement pagination.
Note that the `getMetrics` method also accepts a `start` and `end` argument (`0` and `-1` by default), that you can use if you want to implement pagination.
88 changes: 88 additions & 0 deletions docs/gitbook/guide/metrics/prometheus.md
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`);
});
```

0 comments on commit 676c6a4

Please sign in to comment.