metrics

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: GPL-3.0 Imports: 2 Imported by: 0

README

metrics

对 prometheus 的封装,依赖环境变量 "SERVICE_NAME"。

简介

对外暴露四种 metrics: Counter, Gauge, Summary, Histogram.
Counter 用于记录某指标的总数量,对时间求导可得到每秒的增量,通常用于统计接口的 qps。
Gauge 用于记录某指标的值,每次调用会覆盖旧值,用于监控如机器内存等信息。
Summary 用于统计一组值的百分位数,如接口响应时延的 pct_50(接口响应时延的中位数,注意不是平均值), pct_99 (99% 的请求时延低于哪个值)。summary 指标可以帮助我们有目标的优化接口响应时间、优化服务稳定性。
Histogram 作用与 Summary 类似,但底层实现不一样。Histogram 计算百分位数是在 prometheus server 端完成,Summary 则是在客户端完成。更详细的对比可看这篇文档

example
reqCount = metrics.NewCounter("http_request_total", []string{"view", "status", "err_code"})
reqCount.Add(1, map[string]string{"view": "Login", "err_code": "0"})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

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

Counter is the counter-type metrics.

func NewCounter

func NewCounter(name string, labels []string) *Counter

NewCounter returns a instance of Counter.

func (*Counter) Add

func (c *Counter) Add(value float64, labels map[string]string) error

Add adds the given value to the Counter.

type Gauge

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

Gauge is the gauge-type metrics.

func NewGauge

func NewGauge(name string, labels []string) *Gauge

NewGauge returns a instance of Gauge.

func (*Gauge) Set

func (g *Gauge) Set(value float64, labels map[string]string) error

Set sets the given value to the Gauge.

type Histogram

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

Histogram is the histogram-type metrics.

func NewHistogram

func NewHistogram(name string, labels []string) *Histogram

NewHistogram returns a instance of Histogram.

func (*Histogram) Observe

func (h *Histogram) Observe(value float64, labels map[string]string) error

Observe adds the observations to the Histogram.

type Summary

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

Summary is summary-type metrics.

func NewSummary

func NewSummary(name string, labels []string) *Summary

NewSummary returns a instance of Summary.

func (*Summary) Observe

func (s *Summary) Observe(value float64, labels map[string]string) error

Observe adds the observations to the Summary.

Jump to

Keyboard shortcuts

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