Skip to content

Commit

Permalink
rm Counter, add self pfc.push.cnt
Browse files Browse the repository at this point in the history
  • Loading branch information
niean committed Jan 7, 2016
1 parent 5088fc5 commit 310125a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 98 deletions.
23 changes: 9 additions & 14 deletions README.md
Expand Up @@ -106,13 +106,13 @@ func bar() error {
API
----

几个常用接口,如下。这几个接口,可以实现大部分的业务监控需求。
几个常用接口,如下。

|接口名称|例子|使用场景|
|:----|:----|:---|
|Meter|`// 统计页面访问次数,每来一次请求,pv加1`<br/>`Meter("pageView", int64(1)) `|Meter用于***"只增"计数***。输出累加求和变化率|
|Gauge|`// 统计队列长度` <br/>`Gauge("queueSize", int64(len(myQueueList))) ` <br/> `GaugeFloat64("queueSize", float64(len(myQueueList)))`|Gauge用于记录瞬时值。支持int64、float64类型|
|Counter|`// 统计服务器当前的连接数` <br/>`Counter("ConnectionNum", int64(-1)) `|Counter也用于统计计数。相比于Meter,Counter支持增加计数、也支持减少计数;Counter只能用于累加求和、不能用于计算变化率|
|Meter|`// 统计页面访问次数,每来一次请求,pv加1`<br/>`Meter("pageView", int64(1)) `|Meter用于累加计数。输出累加求和变化率|
|Gauge|`// 统计队列长度` <br/>`Gauge("queueSize", int64(len(myQueueList))) ` <br/> `GaugeFloat64("queueSize", float64(len(myQueueList)))`|Gauge用于记录瞬时值。支持int64、float64类型|
|Histogram|`// 统计线程并发度` <br/>`Histogram("processNum", int64(326)) `| Histogram用于计算统计分布。输出最大值、最小值、平均值、75th、95th、99th等|

更详细的API介绍,请移步到[这里](https://github.com/niean/goperfcounter/blob/master/doc/API.md)

Expand All @@ -123,16 +123,11 @@ API

goperfcounter会将各种统计器的统计结果,定时发送到Open-Falcon。每种统计器,会被转换成不同的Open-Falcon指标项,转换关系如下。每条数据,至少包含一个```name=XXX```的tag,```XXX```是用户定义的统计器名称。

<table border="1">
<table>
<tr>
<th>统计器类型</th>
<td>metric名称</td>
<td>含义</td>
</tr>
<tr>
<th rowspan="1">Counter</th>
<td>sum</td>
<td>所有统计计数的累加和</td>
<th>输出指标的名称</th>
<th>输出指标的含义</th>
</tr>
<tr>
<th rowspan="1">Gauge</th>
Expand All @@ -142,11 +137,11 @@ goperfcounter会将各种统计器的统计结果,定时发送到Open-Falcon
<tr>
<th rowspan="2">Meter</th>
<td>sum</td>
<td>事件发生的总次数(即,所有计数的累加和)</td>
<td>事件发生的总次数(即所有计数的累加和)</td>
</tr>
<tr>
<td>rate</td>
<td>一个Open-Falcon上报周期(默认60s)内,事件发生的频率,单位CPS。要求Meter做"只增"计数。</td>
<td>一个Open-Falcon上报周期(默认60s)内,事件发生的频率,单位CPS</td>
</tr>
<tr>
<th rowspan="6">Histogram</th>
Expand Down
31 changes: 2 additions & 29 deletions 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
Expand Down Expand Up @@ -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
----

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions doc/BENCH.md
Expand Up @@ -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
```
9 changes: 3 additions & 6 deletions example/main.go
Expand Up @@ -15,20 +15,17 @@ 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)
pfc.Gauge("test.gauge", queueSize)

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)
}
}

Expand Down
12 changes: 6 additions & 6 deletions falcon.go
Expand Up @@ -11,8 +11,7 @@ import (
)

const (
GAUGE = "GAUGE"
COUNTER = "COUNTER"
GAUGE = "GAUGE"
)

func pushToFalcon() {
Expand All @@ -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))
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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}
}

Expand Down
4 changes: 2 additions & 2 deletions metrics.go
Expand Up @@ -8,8 +8,8 @@ import (
)

var (
gpCounter = metrics.NewRegistry()
gpGaugeFloat64 = metrics.NewRegistry()
gpCounter = metrics.NewRegistry()
gpMeter = metrics.NewRegistry()
gpHistogram = metrics.NewRegistry()
gpDebug = metrics.NewRegistry()
Expand All @@ -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
Expand Down
80 changes: 48 additions & 32 deletions perfcounter.go
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 310125a

Please sign in to comment.