Kee*_*asa 3 node.js npm prometheus prom-client
我有一个 NodeJS 应用程序,我想公开默认指标。我有以下实现。我正在使用舞会客户端包。
let express = require('express');
let app = express();
const client = require('prom-client');
// Create a Registry which registers the metrics
const register = new client.Registry()
// Add a default label which is added to all metrics
register.setDefaultLabels({
app: 'example-nodejs-app'
})
// Enable the collection of default metrics
client.collectDefaultMetrics({ register })
app.get('/metrics', function (req, res) {
// Return all metrics the Prometheus exposition format
res.set('Content-Type', register.contentType)
res.send(register.metrics())
})
let server = app.listen(8080, function () {
let port = server.address().port
console.log("Application running on port: %s", port)
})
Run Code Online (Sandbox Code Playgroud)
当我导航到 http://localhost:8080/metrics 时,我得到一个空对象 ( {}) 作为响应。当我检查我的 Prometheus 目标时,我看到以下错误。
"INVALID" is not a valid start token
Run Code Online (Sandbox Code Playgroud)
我最近使用 npm 的舞会客户端。我怎样才能做到这一点?
发现问题了。你需要意识到的是,register.metrics()返回一个承诺。因此,使用await register.metrics()将返回默认指标的预期响应。下面给出了更新后的代码片段。
let express = require('express');
let app = express();
const client = require('prom-client');
// Create a Registry which registers the metrics
const register = new client.Registry()
// Add a default label which is added to all metrics
register.setDefaultLabels({
app: 'example-nodejs-app'
})
// Enable the collection of default metrics
client.collectDefaultMetrics({ register })
app.get('/metrics', async function (req, res) {
// Return all metrics the Prometheus exposition format
res.set('Content-Type', register.contentType);
let metrics = await register.metrics();
res.send(metrics);
})
let server = app.listen(8080, function () {
let port = server.address().port
console.log("Application running on port: %s", port)
})
Run Code Online (Sandbox Code Playgroud)
现在,在浏览器中导航到http://localhost:8080/metrics并查看默认指标。
| 归档时间: |
|
| 查看次数: |
5102 次 |
| 最近记录: |