---
title: "folsom_metrics使用备忘"
author: "cryolite"
date: "2013-06-07T15:41:25Z"
tags: []
categories: ["开源软件"]
slug: "folsom-metrics"
draft: false
lastmod: "2013-06-11T23:06:46Z"
id: 1883852
url: "https://cryolite.iteye.com/blog/1883852"
---

folsom是一个通用的统计度量工具。使用很简单，关键是搞清它的度量方式的含义。


有如下几种度量器（metrics）：
counter, gauge, histogram, histories, meter
有些还可以往下细分。

使用方法都是先new一个度量器（metric），然后每来一个数据，notify一下这个metric：
例如gauge类型的时速表的使用。
1. 新建一个时速表：
folsom_metrics:new_gauge(velocity).

2. 更新当前车速：
folsom_metrics:notify(velocity, 40).
folsom_metrics:notify(velocity, 43).
folsom_metrics:notify(velocity, 42).

3. 获取当前时速：
folsom_metrics:get_metric_value(velocity).
42

其它metrics使用类似。

gauge

A gauge is a device that measures the amount or quantity of something and shows the amount measured.
最简单，度量一个时间点的数值，例如时速表就是一种典型的gauge，测量的是当前的车速。

当然gauge的value还可以是其它，例如一个term：
folsom_metrics:notify(velocity, {42, 120}).
表示当前时速42码，最高时速120码。

不过gauge的缺省初始值都是0。

gauge不涉及到统计计算。

counter
A counter is a mechanical or electronic device which keeps a count of something and displays the total. 
计数器，测量的数字来累加得来的，也可减少。例如，可以用counter记录家里滤水器里水的容量，接入新水，counter的记录会增加，以后每天会减少，减到一定数量又接入新水，counter又增加。counter不能记录家庭用水量，只是记录滤水器的当前水量。


meter
  A meter is a device that measures and records something such as the amount of gas or electricity that you have used.

对应煤气表或者电表来说，meter记录的数据似乎是积累的。那它和counter有什么区别？
meter除了提供记录数据的累加（count），还提供了记录数据（例如负载数据）的在最近一段时间内的平均值，和变化趋势。

meter类似于更高级的counter，但是记录的数据量是不能减少的，例如水表/电表就是这种meter，有这样一个高级水表，用于记录用户的用水量，当打开水龙头放水，用meter最近一段时间内放的水量。
meter会算出放水总量，以及放水速度。放水速度类似unix load。

meter提供的最近时间内平均值采用了类似unix load的方式，unix 平均负载有3个值：最近1分钟、最近五分钟和最近15分钟的平均负载。对应的是：
 {one,0.9985352996242468},
 {five,0.9251875710795834},
 {fifteen,0.8558953763671191},

这些测量值不是恒定的，即使没有新的数据，在停止记录后，随着时间的流逝，这些值也跟着变化。
http://en.wikipedia.org/wiki/Load_(computing)#Unix-style_load_calculation