goperfcounter 用于 golang 应用的业务监控。goperfcounter 需要和开源监控系统 Open-Falcon 一起使用。

概述

使用 goperfcounter 进行 golang 应用的监控,大体如下:

  1. 用户在其 golang 应用代码中,调用 goperfcounter 提供的统计函数;统计函数被调用时,perfcounter 会生成统计记录、并保存在内存中
  2. goperfcounter会自动的、定期的将这些统计记录push给Open-Falcon的收集器(agent 或 transfer)
  3. 用户在 Open-Falcon 中,查看统计数据的绘图曲线、设置实时报警

另外,goperfcounter 提供了 golang 应用的基础监控,包括runtime指标、debug指标等。默认情况下,基础监控是关闭的,用户可以通过配置文件来开启此功能。

安装

在golang项目中使用goperfcounter时,需要进行安装,操作如下

go get github.com/niean/goperfcounter

使用

Meter
package xxx

import (
	pfc "github.com/niean/goperfcounter"
)

func foo() {
	if err := bar(); err != nil {
		pfc.Meter("bar.called.error", int64(1))
	}
}

func bar() error {
	// do sth ...
	return nil
}
timestampvalueendpointHostname()steptagsname=bar.called.errorbar.called.errortagscounterTypemetric
{
    "counterType": "GAUGE",
    "endpoint": "git",
    "metric": "rate",
    "step": 20,
    "tags": "module=perfcounter,name=bar.called.error",
    "timestamp": 1451397266,
    "value": 13.14
},
{
    "counterType": "GAUGE",
    "endpoint": "git",
    "metric": "sum",
    "step": 20,
    "tags": "module=perfcounter,name=bar.called.error",
    "timestamp": 1451397266,
    "value": 1023
}

配置

默认情况下,goperfcounter不需要进行配置。如果用户需要定制goperfcounter的行为,可以通过配置文件来进行。配置文件需要满足以下的条件:

perfcounter.json

配置文件的内容,如下

{
    "debug": false, // 是否开启调制,默认为false
    "hostname": "", // 机器名(也即endpoint名称),默认为本机名称
    "tags": "", // tags标签,默认为空。一个tag形如"key=val",多个tag用逗号分隔;name为保留字段,因此不允许设置形如"name=xxx"的tag。eg. "cop=xiaomi,module=perfcounter"
    "step": 60, // 上报周期,单位s,默认为60s
    "bases":[], // gvm基础信息采集,可选值为"debug"、"runtime",默认不采集
    "push": { // push数据到Open-Falcon
        "enabled":true, // 是否开启自动push,默认开启
        "api": "" // Open-Falcon接收器地址,默认为本地agent,即"http:// 127.0.0.1:1988/v1/push"
    },
    "http": { // http服务,为了安全考虑,当前只允许本地访问
        "enabled": false, // 是否开启http服务,默认不开启
        "listen": "" // http服务监听地址,默认为空。eg. "0.0.0.0:2015"表示在2015端口开启http监听
    }
}

API

几个常用接口,如下。

// 统计页面访问次数,每来一次请求,pv加1Meter("pageView", int64(1))// 统计队列长度Gauge("queueSize", int64(len(myQueueList)))GaugeFloat64("queueSize", float64(len(myQueueList)))// 统计线程并发度Histogram("processNum", int64(326))

更详细的API介绍,请移步到这里。

数据上报

name=XXXXXX
统计器类型输出指标的名称输出指标的含义
Gaugevalue最后一次的记录值(float64)
Metersum事件发生的总次数(即所有计数的累加和)
rate一个Open-Falcon上报周期(默认60s)内,事件发生的频率,单位CPS
Histogrammax采样数据的最大值
min采样数据的最小值
mean采样数据的平均值
75th所有采样数据中,处于75%处的数值
95th所有采样数据中,处于95%处的数值
99th所有采样数据中,处于99%处的数值

Bench

请移步到这里。

TODO

  • 支持本地缓存统计数据及UI展示。