
Node.js 的简约零依赖 UDP/TCP statsd 客户端
有时,您必须收集指标,并且想要一些简单的东西,而不需要编写太多样板文件, dats对您有帮助!
该客户端的目标是拥有一个简单的、符合 statsd 的 API,并具有一些可供高级使用的可选风格,例如:缓冲指标和 UDP/TCP 传输!
支持 Node.js >=14.0.0 ,如果您是 Node.js v12用户,请参阅[email protected] 。
Clientnew Client(options)Client.close([cb])Client.connect()Client.counter(string[, value, sampling])Client.timing(string, value[, sampling])Client.gauge(string, value)Client.set(string, value)该软件包可在 npm 上获取。
您可以使用npm安装它
# lastest stable version
$ npm i -S @immobiliarelabs/dats
# latest development version
$ npm i -S @immobiliarelabs/dats@next或yarn
# lastest stable version
$ yarn add @immobiliarelabs/dats
# latest development version
$ yarn @immobiliarelabs/dats@next import Client from '@immobiliarelabs/dats' ;
const stats = new Client ( {
host : 'udp://someip:someport' ,
namespace : 'myGrafanaNamespace' ,
// Optionally register a global handler to track errors.
onError : ( error ) => {
processError ( error ) ;
} ,
} ) ;
// Send counter (myGrafanaNamespace.some.toCount)
stats . counter ( 'some.toCount' , 3 ) ;
stats . counter ( 'some.toCount' ) ; // defaults to 1
stats . counter ( 'some.toCount' , 1 , 10 ) ; // set sampling to 10
// Send timing (myGrafanaNamespace.some.toTime)
stats . timing ( 'some.toTime' , 10 ) ;
stats . timing ( 'some.toTime' , 10 , 0.1 ) ; // set sampling to 0.1
// Send gauge (myGrafanaNamespace.some.toGauge)
stats . gauge ( 'some.toGauge' , 23 ) ;
// Send set (myGrafanaNamespace.some.set)
stats . set ( 'some.set' , 765 ) ; // Scope your stats per hostname and/or pid
import Client from '@immobiliarelabs/dats' ;
const stats = new Client ( {
host : 'udp://someip:someport' ,
namespace : 'myGrafanaNamespace.${hostname}.${pid}' ,
} ) ;
// Send counter (myGrafanaNamespace.myMachine.123.some.toCount)
stats . counter ( 'some.toCount' , 3 ) ;如果主机名包含任何. ,客户端会将它们替换为_ 。
import Client from '@immobiliarelabs/dats' ;
// TCP usage
const stats = new Client ( {
host : 'tcp://someip:someport' ,
namespace : 'myGrafanaNamespace.${hostname}.${pid}' ,
} ) ;
// Calling connect is required in TCP environment
await stats . connect ( ) ;
// Send counter (myGrafanaNamespace.myMachine.123.some.toCount)
stats . counter ( 'some.toCount' , 3 ) ; 该模块导出:
ClientClient统计客户端
new Client(options)options :配置对象。host : statsd 主机( udp://{ip}:{port}或tcp://{ip}:{port} ),您也可以使用 ipv6。如果你想强制使用 udp6 使用: udp6://{host}:{port} ,当使用 TCP 时,你必须调用Client.connect方法。namespace :可选。用于指标的前缀。该指标将作为namespace. + 公制字符串。您可以选择在命名空间中使用${hostname}和${pid}占位符,并将它们替换为计算机主机名和进程 ID。bufferSize :可选。默认值为0 。将此值设置为大于零的数字将激活缓冲模式,该模式不是在每次调用时发送指标,而是缓冲它们并在发生以下条件之一时发送它们:缓冲区已满或bufferFlushTimeout已过期。使用此方法的性能更高,但您必须小心使用与网络上可用的 MTU 兼容的值,否则您的数据包可能会被悄悄丢弃。请参阅多度量数据包。bufferFlushTimeout :可选。默认值为100 。刷新指标缓冲区之前等待的超时(以毫秒为单位)。debug :可选。默认debuglog('dats') 。记录器功能。udpDnsCache :可选。默认为真。激活 udp 的缓存 DNS 查找。udpDnsCacheTTL :可选。默认120 。 Dns 缓存的生存时间(以秒为单位)。onError :可选。默认(err) => void 。出现错误时调用。允许您检查发送错误。customSocket :可选。默认为null 。客户端使用的自定义套接字,这是一个用于模拟的功能,我们不建议在生产中使用它。tags :可选,默认为null 。如果提供,指标将包括#key1:value1,key2:value2形式的标签。 Client.close([cb])关闭客户端套接字
cb :可选。关闭套接字时调用的回调函数。如果没有提供cb则返回Promise 。返回:如果没有传递cb ,则Promise 。
Client.connect()连接 TCP 套接字。仅在 TCP 上才需要调用此函数。
返回:一个Promise 。
Client.counter(string[, value, sampling])发送计数器类型的度量
string : 度量字符串value :可选。指标值( Number )。默认为1 。sampling :可选。度量抽样。所有发送错误均由onError回调处理。
Client.timing(string, value[, sampling])发送时间类型的度量
string : 度量字符串value :指标值( Number )。sampling :可选。度量抽样。所有发送错误均由onError回调处理。
Client.gauge(string, value)发送规范类型的度量
string : 度量字符串value :指标值( Number )。所有发送错误均由onError回调处理。
Client.set(string, value)发送类型集的度量
string : 度量字符串value :指标值( Number )。所有发送错误均由onError回调处理。
Dats 导出他的模拟,您可以按如下方式使用它:
import ClientMock from '@immobiliarelabs/dats/dist/mock' ;
const host = new URL ( `udp://127.0.0.1:8232` ) ;
const namespace = 'ns1' ;
const client = new ClientMock ( { host , namespace } ) ;
client . gauge ( 'some.metric' , 100 ) ;
client . set ( 'some.metric' , 100 ) ;
// metrics is an array with all metrics sent
console . log ( client . metrics ) ;
/* stdout:
[
'ns1.some.metric:100|g',
'ns1.some.metric:100|s',
]
*/
// Check if a metric is in the metrics array
client . hasSent ( 'ns1.some.metric:100|s' ) ; // -> true
client . hasSent ( 'ns1.some.metric:10|s' ) ; // -> false
client . hasSent ( / ns1.some.metric:d+|s / ) ; // -> true
client . hasSent ( / ns1.some.test:d+|s / ) ; // -> false
// Clean the metrics array with
client . cleanMetrics ( ) ;
console . log ( client . metrics ) ;
/* stdout:
[]
*/ dats 还作为 CLI 公开,可以作为 npm 全局包或预编译的二进制文件安装。
预编译二进制文件可以在 Linux、MacOS 或 Windows 的发布部分找到。
$ npm i -g @immobiliarelabs/dats
dats --help
# The following are required input flags:
#
# --host {string} []
# --port {string} []
# --type {string} [Metric type can be one of: counter, timing, gauge, set]
# --prefix {string} [Metric prefix]
# --namespace {string} [Metric full namespace, use dots `.` to separate metrics]
# --value {string} [Metric value]
# --quiet {boolean} [Suppress all console output]
# --dryRun {boolean} [Metric wont be sent, use for debug]
#
# If unsure of output run the command prepended with `DRY_RUN=1`每个命令标志也可以在文件.datsrc中以 JSON 格式指定,运行时进程将在当前工作目录中搜索它,并在运行前合并文件配置和标志!
{
"host" : " 123.123.123.123 " ,
"port" : " 1234 " ,
"prefix" : " my_metric_prefix "
}如果您想使用预编译的二进制文件,请在发布部分获取适用于您的操作系统的正确链接,然后执行以下操作:
curl https://github.com/immobiliare/dats/releases/download/v{{VERSION_TAG}}/dats-cli-{{VERSION_OS}} -L -o dats-cli
chmod +x dats-cli
./dats-cli每次提交的自动基准测试可以在以下链接中找到:next 和 main。
测试是使用指向 HTTP node.js 服务器的 autocannon 完成的,该服务器在每个请求时发送计数指标。通过这种测试,我们可以评估库对应用程序性能的影响程度。
以下是最著名的 Node.js statsd 客户端的基准测试:
| 图书馆 | 请求/秒(97.5) | 请求/秒(平均) |
|---|---|---|
| 数据 | 45503 | 43174.4 |
| 热门人物 | 46975 | 43319.47 |
| 节点统计信息 | 14935 | 11632.34 |
| 统计客户端 | 42463 | 35790.67 |
| 根据 | 50271 | 43312.54 |
Base是没有指标的 HTTP 服务器。
dats 是由意大利排名第一的房地产公司 Immobiliare.it 的技术部门 ImmobiliareLabs 出色的 Node.js 团队创建的。
我们目前在我们的产品以及内部工具中使用数据。
如果您在生产中使用数据,请给我们留言。
由 ImmobiliareLabs 和贡献者使用 ❤️ 制作
我们希望您能为数据做出贡献!如果您对如何使用数据、错误和增强功能有任何疑问,请随时通过打开 GitHub 问题与我们联系。
dats 根据 MIT 许可证获得许可。有关详细信息,请参阅许可证文件。