我正在努力理解 statsd 中的仪表概念。请解释它是如何工作的,并提供一些有用的例子。
doc我不是很清楚
Set a gauge value.
stat: the name of the gauge to set. value: the current value of the gauge. rate: a sample rate, a float between 0 and 1. Will only send data this percentage of the time. The statsd server does not take the sample rate into account for gauges. Use with care. delta: whether or not to consider this a delta value or an absolute value. See the gauge type for more detail.
请您参考如下方法:
仪表只是反射(reflect)您系统的状态,或者一些您根本不想聚合的指标。
让我举几个例子。
1) 在您的程序中,您可以使用一些特定于语言的 API 来了解此进程使用了多少内存。比如,在 Golang 中,我们可以这样做:
var stat runtime.MemStats
runtime.ReadMemStats(&stat)
heapAlloc := memStat.HeapAlloc
heapInuse := memStat.HeapInuse
heapObjects := memStat.HeapObjects
statsd.Gauge("machine01.memory.heap.alloc", heapAlloc)
statsd.Gauge("machine01.memory.heap.inuse", heapInuse)
statsd.Gauge("machine01.memory.heap.objects, heapObjects)
为简单起见,您可以将这些指标视为您的代码调用运行时 API 时的内存使用情况。因此,您可以将仪表发送到 StatsD,因为它们中的每一个都可以完美地向您显示 10 秒内的内存使用情况,这是 StatsD 中的默认刷新周期。而且,您不需要对这些指标使用任何聚合方法,因为聚合(如求和)没有任何意义。
除了上面的案例,它还有很多使用案例,比如 CPU 使用率、操作系统的系统负载、进程中的线程数、服务器中的在线连接数、当前的事件事务数在您的交易系统中。
2) 有时候,我们也可以用gauge来追踪某件事发生的时间。喜欢,
res, err := something()
if err != nil {
statsd.Gauge("machine01.something.error", time.Now().Unix())
}
因此,一旦错误发生,您可以通过查看 Graphite 仪表板的线条来察觉。此外,您还可以通过查看线条的形状来分析并获得出现的频率。
