fmetric

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

业务指标监控接入

fmetric 只是对 prometheus 的简单封装, 仅仅将初始化和注册指标包装成一个函数而已. 如果有疑惑请先学习 prometheus 相关知识.

接入需要注意的是:

  1. 非业务相关 label 不需要关注, 例如: app, projectEnv (k8s prometheus operator 会在收集时自动注入 k8s pod 相关信息)
  2. 注意 labels 组合种类总数必须是常数级别有限的

对于业务指标, 绝大多数情况 Counter 类型就够了, 别的类型也是同理(请确保你在充分了解不同指标类型后选择合适的类型使用). 下面用此类型为例.


// 1. 在项目里初始化指标
var SomeBizCounter = fmetric.CounterVecOpts{
	    // 指标名称命名要清晰, 并且是下划线形式
		Name:      "monitor_some_biz_total",
		// help 字段增加指标说明
		Help:      "Total number of some biz logic on the server",
		// 业务 label 名称
		Labels:    []string{"channel", "status"},
	}.Build()


// 2. 在适当的业务逻辑中上报指标
// WithLabelValues 值顺序必须和 Labels 声明顺序一致
// 真正的业务使用 label 值可以定义成全局常量, 避免拼写错误
SomeBizCounter.WithLabelValues("channel1", "OK").Inc()
// other case
SomeBizCounter.WithLabelValues("channel2", "ERROR").Inc()

Documentation

Index

Constants

View Source
const (
	CodeOK    = "OK"
	CodeError = "Error"
)

Variables

View Source
var (
	// TypeHTTP ...
	TypeHTTP = "http"
	// TypeGRPCClient ...
	TypeGRPCClient = "grpc_client"
	// TypeGRPCServer ...
	TypeGRPCServer = "grpc_server"
	// TypeRedis ...
	TypeRedis = "redis"
	// TypeGorm ...
	TypeGorm = "gorm"
	// TypeMySQL ...
	TypeMySQL = "mysql"

	// DefaultNamespace ...
	DefaultNamespace = ""
)

Functions

func SplitGrpcMethodName

func SplitGrpcMethodName(fullMethodName string) (service string, method string)

SplitGrpcMethodName split grpc full method into service and method.

Types

type CounterVec

type CounterVec struct {
	*prometheus.CounterVec
}

CounterVec ...

func NewCounterVec

func NewCounterVec(name string, labels []string) *CounterVec

NewCounterVec ...

func (*CounterVec) Add

func (counter *CounterVec) Add(v float64, labels ...string)

Add ...

func (*CounterVec) Inc

func (counter *CounterVec) Inc(labels ...string)

Inc ...

type CounterVecOpts

type CounterVecOpts struct {
	Namespace string
	Subsystem string
	Name      string
	Help      string
	Labels    []string
}

CounterVecOpts ...

func (CounterVecOpts) Build

func (opts CounterVecOpts) Build() *CounterVec

Build ...

type GaugeVec

type GaugeVec struct {
	*prometheus.GaugeVec
}

GaugeVec ...

func NewGaugeVec

func NewGaugeVec(name string, labels []string) *GaugeVec

NewGaugeVec ...

func (*GaugeVec) Add

func (gv *GaugeVec) Add(v float64, labels ...string)

Add ...

func (*GaugeVec) Inc

func (gv *GaugeVec) Inc(labels ...string)

Inc ...

func (*GaugeVec) Set

func (gv *GaugeVec) Set(v float64, labels ...string)

Set ...

type GaugeVecOpts

type GaugeVecOpts struct {
	Namespace string
	Subsystem string
	Name      string
	Help      string
	Labels    []string
}

GaugeVecOpts ...

func (GaugeVecOpts) Build

func (opts GaugeVecOpts) Build() *GaugeVec

Build ...

type HistogramVec

type HistogramVec struct {
	*prometheus.HistogramVec
}

HistogramVec ...

func (*HistogramVec) Observe

func (histogram *HistogramVec) Observe(v float64, labels ...string)

Observe ...

func (*HistogramVec) ObserveWithExemplar

func (histogram *HistogramVec) ObserveWithExemplar(v float64, exemplar prometheus.Labels, labels ...string)

type HistogramVecOpts

type HistogramVecOpts struct {
	Namespace string
	Subsystem string
	Name      string
	Help      string
	Labels    []string
	Buckets   []float64
}

HistogramVecOpts ...

func (HistogramVecOpts) Build

func (opts HistogramVecOpts) Build() *HistogramVec

Build ...

type SummaryVec

type SummaryVec struct {
	*prometheus.SummaryVec
}

SummaryVec ...

func (*SummaryVec) Observe

func (summary *SummaryVec) Observe(v float64, labels ...string)

Observe ...

type SummaryVecOpts

type SummaryVecOpts struct {
	Namespace  string
	Subsystem  string
	Name       string
	Help       string
	Objectives map[float64]float64
	Labels     []string
}

SummaryVecOpts ...

func (SummaryVecOpts) Build

func (opts SummaryVecOpts) Build() *SummaryVec

Build ...

Jump to

Keyboard shortcuts

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