【发布时间】:2018-08-07 14:14:54
【问题描述】:
我将如何使用以下内容来创建 function 或 class 以便我可以导入此模块以在我的应用程序中发生事件时增加计数。
每当我需要增加计数时,我不想在我的应用程序的任何地方输入这 3 行。
const metrics = request.server.app.metrics;
const metricName = 'user.additem.counter';
metrics.counter(metricName).inc();
这是我目前所拥有的,但它不起作用。
const hoek = require('hoek')
const incrementCounter = (context, metricName) => {
const appMetrics = hoek.reach(context, 'request.server.app.metrics');
if (appMetrics) {
appMetrics.counter(metricName).inc();
}
};
module.exports = {
incrementCounter
};
【问题讨论】:
-
看起来还不错。您是否收到错误或什么不起作用?你怎么称呼你的模块?
-
context 参数由于某种原因无法正常工作。我已经导入了文件,当我尝试 incrementCounter(event, testName)
-
但如果我尝试
const metrics = request.server.app.metrics; const metricName = '_testName_'; metrics.counter(metricName).inc();会起作用@
标签: javascript node.js reactjs datadog