From 310125a128163ea6bea9c6ea34853d5c4fe3f615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nieanan=20=5B=E8=81=82=E5=AE=89=E5=AE=89=5D?= Date: Tue, 5 Jan 2016 05:55:27 +0000 Subject: [PATCH] rm Counter, add self pfc.push.cnt --- README.md | 23 ++++++-------- doc/API.md | 31 ++----------------- doc/BENCH.md | 16 +++++----- example/main.go | 9 ++---- falcon.go | 12 ++++---- metrics.go | 4 +-- perfcounter.go | 80 +++++++++++++++++++++++++++++-------------------- 7 files changed, 77 insertions(+), 98 deletions(-) diff --git a/README.md b/README.md index f026236..35904bd 100644 --- a/README.md +++ b/README.md @@ -106,13 +106,13 @@ func bar() error { API ---- -几个常用接口,如下。这几个接口,可以实现大部分的业务监控需求。 +几个常用接口,如下。 |接口名称|例子|使用场景| |:----|:----|:---| -|Meter|`// 统计页面访问次数,每来一次请求,pv加1`
`Meter("pageView", int64(1)) `|Meter用于***"只增"计数***。输出累加求和 及 变化率| -|Gauge|`// 统计队列长度`
`Gauge("queueSize", int64(len(myQueueList))) `
`GaugeFloat64("queueSize", float64(len(myQueueList)))`|Gauge用于记录瞬时值。支持int64、float64类型。| -|Counter|`// 统计服务器当前的连接数`
`Counter("ConnectionNum", int64(-1)) `|Counter也用于统计计数。相比于Meter,Counter支持增加计数、也支持减少计数;Counter只能用于累加求和、不能用于计算变化率| +|Meter|`// 统计页面访问次数,每来一次请求,pv加1`
`Meter("pageView", int64(1)) `|Meter用于累加计数。输出累加求和、变化率| +|Gauge|`// 统计队列长度`
`Gauge("queueSize", int64(len(myQueueList))) `
`GaugeFloat64("queueSize", float64(len(myQueueList)))`|Gauge用于记录瞬时值。支持int64、float64类型| +|Histogram|`// 统计线程并发度`
`Histogram("processNum", int64(326)) `| Histogram用于计算统计分布。输出最大值、最小值、平均值、75th、95th、99th等| 更详细的API介绍,请移步到[这里](https://github.com/niean/goperfcounter/blob/master/doc/API.md)。 @@ -123,16 +123,11 @@ API goperfcounter会将各种统计器的统计结果,定时发送到Open-Falcon。每种统计器,会被转换成不同的Open-Falcon指标项,转换关系如下。每条数据,至少包含一个```name=XXX```的tag,```XXX```是用户定义的统计器名称。 - +
- - - - - - - + + @@ -142,11 +137,11 @@ goperfcounter会将各种统计器的统计结果,定时发送到Open-Falcon - + - + diff --git a/doc/API.md b/doc/API.md index a8ba104..4da4d24 100644 --- a/doc/API.md +++ b/doc/API.md @@ -1,7 +1,7 @@ API ==== -gopfercounter提供了4种类型的统计器,分比为Gauge、Counter、Meter、Histogram。统计器的含义,参见[java-metrics](http://metrics.dropwizard.io/3.1.0/getting-started/)。 +gopfercounter提供了几种类型的统计器,分比为Gauge、Meter、Histogram。统计器的含义,参见[java-metrics](http://metrics.dropwizard.io/3.1.0/getting-started/)。 Gauge @@ -37,33 +37,6 @@ SetGaugeValue("requestRate", float64(13.14)) reqRate := GetGaugeValue("requestRate") ``` -Counter ----- - -An incrementing and decrementing gauge metric - -##### 设置 -+ 接口: SetCounterCount(name string, count int64) -+ Alias: Counter(name string, count int64) -+ 参数: count - 记录该值的相对变化量 -+ 例子: - -```go -// 新增一条链接 -SetCounterCount("ConnectionNum", int64(1)) -// 断开一条链接 -SetCounterCount("ConnectionNum", int64(-1)) -``` - -##### 获取累计的值 -+ 接口: GetCounterCount(name string) int64 -+ 例子: - -```go -// 获取当前链接数 -cNum := GetCounterCount("ConnectionNum") -``` - Meter ---- @@ -139,7 +112,7 @@ pvRate15Min := GetMeterRate15("pageView") Histogram ---- -A histogram measures the [statistical distribution](http://www.johndcook.com/standard_deviation.html) of values in a stream of data. In addition to minimum, maximum, mean, etc., it also measures median, 75th, 90th, 95th, 98th, 99th, and 99.9th percentiles +A histogram measures the [statistical distribution](http://www.johndcook.com/standard_deviation.html) of values in a stream of data. In addition to minimum, maximum, mean, etc., it also measures median, 75th, 90th, 95th, 98th, and 99th percentiles ##### 设置 + 接口: SetHistogramCount(name string, count int64) diff --git a/doc/BENCH.md b/doc/BENCH.md index c903afc..b932657 100644 --- a/doc/BENCH.md +++ b/doc/BENCH.md @@ -14,14 +14,12 @@ Result ``` PASS -BenchmarkMeter 2000000 892 ns/op -BenchmarkMeterMulti 2000000 873 ns/op -BenchmarkGauge 5000000 338 ns/op -BenchmarkGaugeMulti 5000000 350 ns/op -BenchmarkCounter 10000000 186 ns/op -BenchmarkCounterMulti 10000000 178 ns/op -BenchmarkHistogram 2000000 758 ns/op -BenchmarkHistogramMulti 2000000 751 ns/op -ok github.com/niean/goperfcounter 18.383s +BenchmarkMeter 2000000 918 ns/op +BenchmarkMeterMulti 2000000 916 ns/op +BenchmarkGauge 5000000 342 ns/op +BenchmarkGaugeMulti 5000000 333 ns/op +BenchmarkHistogram 2000000 800 ns/op +BenchmarkHistogramMulti 2000000 780 ns/op +ok github.com/niean/goperfcounter 14.568s ``` \ No newline at end of file diff --git a/example/main.go b/example/main.go index 1a27b31..62de143 100644 --- a/example/main.go +++ b/example/main.go @@ -15,9 +15,10 @@ func main() { func basic() { for _ = range time.Tick(time.Second * time.Duration(10)) { - // (常用) Meter,用于累加求和、计算变化率。Meter只能增加计数、不能减少计数,使用场景如,统计首页访问次数、gvm的CG次数等。 + // (常用) Meter,用于累加求和、计算变化率。使用场景如,统计首页访问次数、gvm的CG次数等。 pv := int64(rand.Int() % 100) - pfc.Meter("bar.called.error", pv) + pfc.Meter("test.meter", pv) + pfc.Meter("test.meter.2", pv-50) // (常用) Gauge,用于保存数值类型的瞬时记录值。使用场景如,统计队列长度、统计CPU使用率等 queueSize := int64(rand.Int()%100 - 50) @@ -25,10 +26,6 @@ func basic() { cpuUtil := float64(rand.Int()%10000) / float64(100) pfc.GaugeFloat64("test.gauge.float64", cpuUtil) - - // Counter,用于累加求和。Counter可以增加计数,也可以减少计数,使用场景如,统计已登录用户数 - users := int64(rand.Int()%100 - 50) - pfc.Counter("test.counter", users) } } diff --git a/falcon.go b/falcon.go index 23446e2..1aa3f8c 100644 --- a/falcon.go +++ b/falcon.go @@ -11,8 +11,7 @@ import ( ) const ( - GAUGE = "GAUGE" - COUNTER = "COUNTER" + GAUGE = "GAUGE" ) func pushToFalcon() { @@ -22,8 +21,9 @@ func pushToFalcon() { debug := cfg.Debug for _ = range time.Tick(time.Duration(step) * time.Second) { - fms := falconMetrics() + selfMeter("pfc.push.cnt", 1) + fms := falconMetrics() start := time.Now() err := push(fms, api, debug) selfGauge("pfc.push.ms", int64(time.Since(start)/time.Millisecond)) @@ -32,9 +32,9 @@ func pushToFalcon() { if debug { log.Printf("[perfcounter] send to %s error: %v", api, err) } - selfGauge("pfc.push.cnt", int64(0)) // statistics + selfGauge("pfc.push.size", int64(0)) // statistics } else { - selfGauge("pfc.push.cnt", int64(len(fms))) // statistics + selfGauge("pfc.push.size", int64(len(fms))) // statistics } } } @@ -107,7 +107,7 @@ func gaugeFloat64MetricValue(metric metrics.GaugeFloat64, metricName, endpoint, func counterMetricValue(metric metrics.Counter, metricName, endpoint, oldtags string, step, ts int64) []*MetricValue { tags := getTags(metricName, oldtags) - c1 := newMetricValue(endpoint, "sum", metric.Count(), step, GAUGE, tags, ts) + c1 := newMetricValue(endpoint, "count", metric.Count(), step, GAUGE, tags, ts) return []*MetricValue{c1} } diff --git a/metrics.go b/metrics.go index 4f730bb..f3f2164 100644 --- a/metrics.go +++ b/metrics.go @@ -8,8 +8,8 @@ import ( ) var ( - gpCounter = metrics.NewRegistry() gpGaugeFloat64 = metrics.NewRegistry() + gpCounter = metrics.NewRegistry() gpMeter = metrics.NewRegistry() gpHistogram = metrics.NewRegistry() gpDebug = metrics.NewRegistry() @@ -19,8 +19,8 @@ var ( ) func init() { - values["counter"] = gpCounter values["gauge"] = gpGaugeFloat64 + values["counter"] = gpCounter values["meter"] = gpMeter values["histogram"] = gpHistogram values["debug"] = gpDebug diff --git a/perfcounter.go b/perfcounter.go index d05207d..be02608 100644 --- a/perfcounter.go +++ b/perfcounter.go @@ -30,38 +30,6 @@ func init() { } } -// counter -func Counter(name string, count int64) { - SetCounterCount(name, count) -} - -func SetCounterCount(name string, count int64) { - rr := gpCounter.Get(name) - if rr != nil { - if r, ok := rr.(metrics.Counter); ok { - r.Inc(count) - } - return - } - - r := metrics.NewCounter() - r.Inc(count) - if err := gpCounter.Register(name, r); isDuplicateMetricError(err) { - r := gpCounter.Get(name).(metrics.Counter) - r.Inc(count) - } -} - -func GetCounterCount(name string) int64 { - rr := gpCounter.Get(name) - if rr != nil { - if r, ok := rr.(metrics.Counter); ok { - return r.Count() - } - } - return 0 -} - // gauge func Gauge(name string, value int64) { SetGaugeValue(name, float64(value)) @@ -302,6 +270,37 @@ func GetHistogram999th(name string) float64 { return 0.0 } +// senior +func Counter(name string, count int64) { + SetCounterCount(name, count) +} +func SetCounterCount(name string, count int64) { + rr := gpCounter.Get(name) + if rr != nil { + if r, ok := rr.(metrics.Counter); ok { + r.Inc(count) + } + return + } + + r := metrics.NewCounter() + r.Inc(count) + if err := gpCounter.Register(name, r); isDuplicateMetricError(err) { + r := gpCounter.Get(name).(metrics.Counter) + r.Inc(count) + } +} + +func GetCounterCount(name string) int64 { + rr := gpCounter.Get(name) + if rr != nil { + if r, ok := rr.(metrics.Counter); ok { + return r.Count() + } + } + return 0 +} + // self func selfGauge(name string, value int64) { rr := gpSelf.Get(name) @@ -320,6 +319,23 @@ func selfGauge(name string, value int64) { } } +func selfMeter(name string, value int64) { + rr := gpSelf.Get(name) + if rr != nil { + if r, ok := rr.(metrics.Meter); ok { + r.Mark(value) + } + return + } + + r := metrics.NewMeter() + r.Mark(value) + if err := gpSelf.Register(name, r); isDuplicateMetricError(err) { + r := gpSelf.Get(name).(metrics.Meter) + r.Mark(value) + } +} + // internal func isDuplicateMetricError(err error) bool { if err == nil {
统计器类型metric名称含义
Countersum所有统计计数的累加和输出指标的名称输出指标的含义
Gauge
Meter sum事件发生的总次数(即,所有计数的累加和)事件发生的总次数(即所有计数的累加和)
rate一个Open-Falcon上报周期(默认60s)内,事件发生的频率,单位CPS。要求Meter做"只增"计数。一个Open-Falcon上报周期(默认60s)内,事件发生的频率,单位CPS
Histogram