使用 StatsD + Grafana 做前端监控报警

1,198 阅读2分钟

一、知识储备

1、StatsD 是什么?

StatsD 就是一个简单的网络守护进程,基于 Node.js 平台,通过 UDP 或者 TCP 方式侦听各种统计信息,包括计数器和定时器,并发送聚合信息到后端服务,如 Graphite。它基于两大功能:计数和计时。Statsd 通过发送 UDP 数据包来调用每个 Statsd 服务器。

2、node-statsd 是什么?

node-statsd 是 StatsD 服务器的客户端。此客户端将在 Node.js 应用程序中允许你在 Statsd 服务器上默认统计信息。

3、lru-cache 是什么?

lru-cache 是一个缓存对象,可删除最少使用的项目。

4、StatsC 是什么?

StatsC 允许你直接从浏览器登录到 Graphite / Statsd 服务器的统计信息。

二、技术方案

1、在服务端做监控,可以使用的客户端工具是 node-statsd

安装

npm install node-statsd

使用

var StatsD = require('node-statsd'),
    client = new StatsD({
      host: 'localhost', // 发送统计信息的主机 default: localhost
      port: 8125, // 发送统计信息的端口 default: 8125
      prefix: '', // 每个统计名称的前缀是什么 default: ''
      suffix: '', // 每个统计名称的后缀 default: ''
      globalize: false, // 全局公开这个 StatsD 实例? default: false
      cacheDns: false,// 缓存初始 dns 查找到主机 default: false
      mock: false, // 创建一个模拟 StatsD 实例,不向服务器发送任何统计信息? default: false
      global_tags:[], // 将添加到每个指标的可选标签 default: []
    });
 
// Timing: 发送指定毫秒的计时命令。sends a timing command with the specified milliseconds
client.timing('response_time', 42);
 
// Increment: 递增,将统计值增加一个值(默认为 1)。Increments a stat by a value (default is 1)
client.increment('my_counter');
​
// Decrement: 递减:将统计数据递减一个值(默认为 -1)。Decrements a stat by a value (default is -1)
client.decrement('my_counter');
​
// Histogram: 直方图:发送直方图统计数据。send data for histogram stat
client.histogram('my_histogram', 42);
​
// Gauge: 计量:按指定的数量计量统计数据。Gauge a stat by a specified amount
client.gauge('my_gauge', 123.45);
​
// Set: 设置:统计统计数据的唯一出现次数(唯一的别名)。Counts unique occurrences of a stat (alias of unique)
client.set('my_unique', 'foobar');
client.unique('my_unique', 'foobarbaz');
​
// 增加多个项目。Incrementing multiple items
client.increment(['these', 'are', 'different', 'stats']);
​
// 采样,这将采样 StatsD 守护进程补偿采样的 25% 的时间。Sampling, this will sample 25% of the time the StatsD Daemon will compensate for sampling
client.increment('my_counter', 1, 0.25);
​
// 标签,这会将用户定义的标签添加到数据中。Tags, this will add user-defined tags to the data
client.histogram('my_histogram', 42, ['foo', 'bar']);
​
// 使用回调。Using the callback
client.set(['foo', 'bar'], 42, function(error, bytes){
  //this only gets called once after all messages have been sent
  if(error){
    console.error('Oh noes! There was an error:', error);
  } else {
    console.log('Successfully sent', bytes, 'bytes');
  }
});
​
// 采样、标签和回调是可选的,可以任意组合使用。Sampling, tags and callback are optional and could be used in any combination
client.histogram('my_histogram', 42, 0.25); // 25% Sample Rate
client.histogram('my_histogram', 42, ['tag']); // User-defined tag
client.histogram('my_histogram', 42, next); // Callback
client.histogram('my_histogram', 42, 0.25, ['tag']);
client.histogram('my_histogram', 42, 0.25, next);
client.histogram('my_histogram', 42, ['tag'], next);
client.histogram('my_histogram', 42, 0.25, ['tag'], next);

Errors 监听链接异常:node-stated 允许此错误冒泡。如果想要捕获异常,只需将监听器附加到实例的 socket 属性即可。

client.socket.on('error', function(error) {
  return console.error("Error in socket: ", error);
});

在客户端做