metrics

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2020 License: BSD-2-Clause Imports: 12 Imported by: 3

README

go-metrics:封装云服务平台后端监控接口

go-metrics 封装了云服务平台的后端监控接口,当前仅支持腾讯云。

使用方法

定义 Metric

go-metrics 无需手动进行初始化,框架和业务可以在 go-runner 里面注册启动回调,通过 Define 接口定义监控统计参数,并使用 Metric 对象来不断更新数据。

var httpMetrics struct {
    Count, QPS, ProcTime, MaxProcTime *metrics.Metric
}

func init() {
    runner.OnStart(func(ctx context.Context) error {
        // 统计项在监控系统里面指标就是 api_count。
        // 这里统计的数据会累加。
        httpMetrics.Count = metrics.Define(&metrics.Def{
            Category: "api_count",
            Method: metrics.Sum,
        })

        // 这样设置可以计算 QPS。
        httpMetrics.QPS = metrics.Define(&metrics.Def{
            Category: "api_qps",
            Method: metrics.Sum,
            Duration: time.Second,
        })

        // 这样设置可以计算平均处理时间。
        httpMetrics.ProcTime = metrics.Define(&metrics.Def{
            Category: "api_proc_time",
            Method: metrics.Average,
        })

        // 这样设置可以找到最大处理时间。
        httpMetrics.MaxProcTime = metrics.Define(&metrics.Def{
            Category: "api_max_proc_time",
            Method: metrics.Maximum,
        })
    })
}
使用 Metric

Metric 可以并发调用,这个函数不会阻塞

// 在业务代码里面可以直接使用 metricFoo 来进行业务数据统计。
metricAPICount.Add(1)

如果我们想统计更加细分的信息,可以使用 AddForTag。这个调用不但会在细分指标里面计数,也会自动调用一次 Add 从而在总数里面也加上对应的数据。

// 也可以指定一个 tag 指标,这种用法主要用来统计一个细分指标。
// 比如,我们需要统计 /foo/bar 这个 api 的调用次数,我们可以像下面这样写,
// 这样我们会在 api_count 里面加 1,同时会在 api_count-foo_bar 这个指标项也加 1。
metricAPICount.AddForTag("/foo/bar", 1)

腾讯云配置

如果指定了 [metrics.tencentcloud] 配置,那么就会自动启动腾讯云的客户端。如果不配置,则不会启动这个客户端,数据只会统计但不会做任何的上报。

经典的配置如下。

[metrics.tencentcloud]
secret_id  = 'XXXXXXXXXXXXXXXXXXXXXXXX'
secret_key = 'XXXXXXXXXXXXXXXXXXXXXXXX'
region = 'ap-guangzhou'

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flush

func Flush(ctx context.Context)

Flush 将未发送的统计信息发出去。

Types

type Def

type Def struct {
	Category string        // 统计项的类别。
	Method   Method        // 统计方法。
	Duration time.Duration // 统计周期,如果为 0 代表不按周期进行统计。
}

Def 代表一个统计字段的定义。

当需要统计类似 qps 这样的指标时候,应该选择使用 Method = Sum, 并且将 Duration = time.Second,这样就能够实现这个功能。

type Entry

type Entry struct {
	Category string
	Tag      string
	Value    int64
}

Entry 是一个统计项。

type Method

type Method int

Method 代表统计方法。

const (
	Sum Method = iota
	Average
	Maximum
)

各种统计方法。

type Metric

type Metric struct {
	// contains filtered or unexported fields
}

Metric 是一个指标项,用于在业务代码调用,设置指标项。

func Define

func Define(def *Def) *Metric

Define 定一个监控指标。

func NewMetric

func NewMetric(value *Value) *Metric

NewMetric 创建一个指标,具体的数据记录在 value 里面。

func (*Metric) Add

func (m *Metric) Add(value int64)

Add 为当前的统计指标增加一定量的数值。

func (*Metric) AddForTag

func (m *Metric) AddForTag(tag string, value int64)

AddForTag 为指定的 tag 增加一定量的数值。 tag 一般用于给统计指标增加一些额外信息。

注意,当调用 AddForTag 时,Add 也会被自动调一次。

type Metrics

type Metrics struct {
	// contains filtered or unexported fields
}

Metrics 代表一个统计客户端。

func (*Metrics) Define

func (m *Metrics) Define(def *Def) *Metric

Define 定一个监控指标。

func (*Metrics) Send

func (m *Metrics) Send(ctx context.Context) error

Send 对外发送指标。

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value 是一类统计值。 Value 本身不保证并发安全,由调用者自己来保证。

func NewValue

func NewValue(now time.Time, def *Def) *Value

NewValue 创建一个新的 Value 用于统计。

func (*Value) Add

func (v *Value) Add(value int64)

Add 增加主要计数器的值。

func (*Value) AddForTag

func (v *Value) AddForTag(tag string, value int64)

AddForTag 增加 tag 的计数器的值,同时也会增加主计数器的值。

func (*Value) Read

func (v *Value) Read(now time.Time) (entries []Entry)

Read 读取当前最新统计值,并且重置所有的统计。

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL