-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support aggregating metrics across workers in a Node.js worker_…
…threads
- Loading branch information
Rafal Augustyniak
committed
Oct 15, 2020
1 parent
fecd75e
commit f9dd057
Showing
2 changed files
with
143 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const metricsServer = express(); | ||
const AggregatorRegistry = require('../').AggregatorRegistry; | ||
/* eslint-disable node/no-unsupported-features/node-builtins */ | ||
const { Worker, isMainThread } = require('worker_threads'); | ||
|
||
const aggregatorRegistry = new AggregatorRegistry(); | ||
|
||
if (isMainThread) { | ||
metricsServer.get('/metrics', async (req, res) => { | ||
try { | ||
const metrics = await aggregatorRegistry.clusterMetrics(); | ||
res.set('Content-Type', aggregatorRegistry.contentType); | ||
res.send(metrics); | ||
} catch (ex) { | ||
res.statusCode = 500; | ||
res.send(ex.message); | ||
} | ||
}); | ||
|
||
metricsServer.listen(3000); | ||
|
||
const worker = new Worker(__filename); | ||
|
||
aggregatorRegistry.attachWorkers([worker]); | ||
} else { | ||
const Histogram = require('../').Histogram; | ||
const h = new Histogram({ | ||
name: 'test_histogram', | ||
help: 'Example of a histogram', | ||
labelNames: ['code'], | ||
}); | ||
|
||
h.labels('200').observe(Math.random()); | ||
} |
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