背景介绍
希望能够对业务指标监控,通过业务指标监控反应业务系统的稳定性,原因是通过对基础资源,应用服务状态,性能监控,不能够反应业务系统的稳定性,举个栗子:服务程序一切正常,下单接口因参数校验,导致很多下单不成功,状态码正常返回,对于服务端来说一切正常,但是从业务角度,已经出问题了,可能是传参或某个环节导致的,这种问题就没有办法通过常规手段进行监控,不管是那个环节出问题,对我们的业务都是有影响的。
例如:单位时间段内的下单数、成单数,基于同环比进行监控和预警,先于用户发现问题并止损。
利用Grafana漂亮UI对业务监控,资源监控,绘制个性化大盘,基于Statsd+Graphite+Grafana 实现自定义监控指标Metirc
工具介绍
- Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理。常见支持的「Graphite」, 「ElasticaSearch」,「InfluxDB」 等等 ,它故宫了各种语言的客户端API,这里我们使用了jsocol/pystatsd: A Python client for statsd进行数据收集
- Graphite 是一套Python写的开源编程接口,主要是用来收集服务器的及时状态,在这里主要作为statsd的数据后端。分为了三个子项目
- carbon 守护进程,接收StatsD发送过来的原始统计数据。
- whisper 用来存储统计数据的时间序列数据库。
- graphite webapp 用来图形化展示统计数据的web项目。
- Grafana 使用Go开发,可以在界面上设计调整自己的统计图表,支持多重报警,可定制化。
利用镜像安装Graphite&Statsd
docker run -d\
--name graphite\
--restart=always\
-p 80:80\
-p 2003-2004:2003-2004\
-p 2023-2024:2023-2024\
-p 8125:8125/udp\
-p 8126:8126\
graphiteapp/graphite-statsd
Includes the following components
- Nginx - reverse proxies the graphite dashboard
- Graphite - front-end dashboard
- Carbon - back-end
- Statsd - UDP based back-end proxy
Mapped Ports
Host | Container | Service |
---|---|---|
80 | 80 | nginx |
2003 | 2003 | carbon receiver - plaintext |
2004 | 2004 | carbon receiver - pickle |
2023 | 2023 | carbon aggregator - plaintext |
2024 | 2024 | carbon aggregator - pickle |
8080 | 8080 | Graphite internal gunicorn port (without Nginx proxying). |
8125 | 8125 | statsd |
8126 | 8126 | statsd admin |
发送指标数据
while true; do echo -n "example:$((RANDOM % 100))|c" | nc -w 1 -u 127.0.0.1 8125; done
UDP发送
Message格式:
example:1|c
当然还支持更多格式了,例如:counter、timer、gauge和set
Java简单UDP投送实例:
public static void send(String message) {
LogUtil.getInstance().info("UDPClient send:" + message);
byte[] data = message.getBytes();
DatagramSocket client =null;
try{
//3.打包(发送的地点及端口)
//1.创建客户端+端口
client = new DatagramSocket(5555);
//3.打包(发送的地点及端口)
DatagramPacket packet = new DatagramPacket(data, data.length,new InetSocketAddress("127.0.0.1",8125));
//4.发送
client.send(packet);
}catch (Exception e){
LogUtil.getInstance().error(e,"UDPClient send:" + message);
}finally {
if(client==null){
//5.释放
client.close();
}
}
}
使用架构模式
异步直连式
说明:应用程序中、植入全局异步投送监控指标Metirc的异步线程或协程,每次使用异步调用异步线程进行投送即可,每个应用程序都需要建议UDP连接。
好处:简单,直接、复杂低
缺点:占用应用性能、对应用侵入性比较大
注意:因异步投送,故需要控制线程池控制异步线程数量,防止对业务产生比较大的影响。
应用场景:中小型项目,对性能和稳定性要求不高。
集中式消费
说明:应用程序中,约定Metirc指标数据格式,借助kafka,投送到kafka,中间加一层集中式消费kafka,然后到stats集群中
好处:应用程序和Stats是解耦的,提升应用程序的稳定性和性能。
缺点:复杂度较高。
注意:因异步投送,故需要控制线程池控制异步线程数量,防止对业务产生比较大的影响。
应用场景:较大型项目,对性能和稳定性有一定的要求。
可插拔Backends:
StatsD支持可插拔backends,安装包中默认带有graphite后端。可以将其他后端作为简单的npm软件包进行分发和安装:
- amqp-backend
- ganglia-backend
- librato-backend
- socket.io-backend
- statsd-backend
- mongo-backend
- mysql-backend
- datadog-backend
- monitis backend
- instrumental backend
- hosted graphite backend
- statsd aggregation backend
- zabbix-backend
- opentsdb backend
- influxdb backend
- stackdriver backend
- couchdb-backend
- elasticsearch backend
- Google BigQuery backend
2、服务端实现:
StatsD最初由Etsy的Erik Kastner编写,它基于Flickr的想法以及Cal Henderson的这篇文章:Counting and Timing。2011年该服务器用Nodejs编写重写,但是从那时起已经有其他语言的实现:
- brubeck - Server in C
- clj-statsd-svr — Clojure server
- gographite — Server in Go
- gostatsd — Server in Go
- netdata - Embedded statsd server in the netdata server, in C, with visualization
- Net::Statsd::Server — Perl server, also available on CPAN
- Py-Statsd — Server and Client
- Ruby-Statsdserver — Ruby server
- statsd-c — Server in C
- statsdaemon (bitly) — Server in Go
- statsdaemon (vimeo) — Server in Go
- statsdcc - Server in C++
- statsdpy — Python/eventlet Server
- Statsify - Server in C#
- statsite — Server in C
- bioyino — High performance multithreaded server written in Rust
3、客户端实现:
客户端主要是根据statsd协议,通过UDP/TCP向守护进程通信。常见的实现有:
Node
- lynx — Node.js client used by Mozilla, Nodejitsu, etc.
- Node-Statsd — Node.js client
- node-statsd-client — Node.js client
- node-statsd-instrument — Node.js client
- statistik - Node.js client with timers & CLI
- statsy - clean idiomatic statsd client
Java
- java-statsd-client — Lightweight (zero deps) Java client
- Statsd over SLF4J — Java client with SLF4J logging tie-in
- play-statsd — Play Framework 2.0 client for Java and Scala
- statsd-netty — Netty-based Java 8 client
Python
- Py-Statsd — Server and Client
- Python-Statsd — Python client
- pystatsd — Python client
- Django-Statsd — Django client
Ruby
- statsd-instrument — Ruby client
- statsd — Ruby client (needs new maintainer)
- Statsd-Client — Ruby client (not maintained)
Perl
- Net::Statsd — Perl client, also available on CPAN
- Net::StatsD::Client — Perl client, not available on CPAN
- Etsy::StatsD - Perl client, also available on [CPAN] (https://metacpan.org/module/Etsy::StatsD)
PHP
- Metrics
- PHP client
- php-statsd and Spark
- php-statsd-client - supports SplClassLoader
- statsd-php-client - Minimalist performant client
- phpLeague-statsd-client - Php League StatsD client
- statsd-php-client - optimized client with monolog and symfony2 integrations available
- statsd-php - PSR-4 compatible client
Clojure
- Clojure client
Io
- io-statsd — StatsD Client for Io
C
- C client — A trivial C client
CPP
- statsd-client-cpp — StatsD Client in CPP
- cpp-statsd-client — A header-only StatsD client implemented in C++
.NET
- NStatsD.Client — .NET 4.0 client
- C# client — C# client
- graphite-client — .NET client library for StatsD and Graphite
- StatsC — An asynchronous client with built-in support for batching
- JustEat.StatsD — A .NET library for publishing metrics to statsd. Targets both .NET full framework and .NET Standard 2.0.
- Statsify - .NET client
Go
- go-statsd - Go statsd client library with zero allocation overhead, great performance and reconnects
- GoE — Minimal & Performant
- go-statsd-client — Simple Go client
- g2s
- StatsD
- statsd — A simple and very fast StatsD client
Apache
- mod_statsd - StatsD client to send stats straight from Apache
Varnish
- libvmod-statsd - StatsD client to send stats straight from Varnish
PowerShell
- powershell-statsd - PowerShell client
Browser
- StatsC - Push stats to StatsD from the browser!
- StatsD HTTP Proxy - HTTP proxy to StatsD with REST interface for using in browsers
- StatsD HTTP Client - StatsD client over http for using in browsers
Objective-C
- MCStatsd - Cocoa client
ActionScript
- flash-statsd - Flash client
WordPress
- wordpress-statsd - WordPress Plugin
Drupal
- StatsD - Drupal module
Haskell
- statsd-client
R
- rstatsd
Lua
- lua-statsd
Nim
- statsd_client
- nim-metrics - supports StatsD, Carbon and Prometheus
参考:
https://github.com/statsd/statsd/wiki
https://hub.docker.com/r/graphiteapp/graphite-statsd