auto_gen_metrics

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

用来自动生成需要上报到prometheus的metrics,使用方法很简单,参考: https://github.com/0x00b/auto-gen-metrics/blob/master/example/main.go

package sample

import (
	am "github.com/0x00b/auto-gen-metrics"
	"github.com/prometheus/client_golang/prometheus"
)

// 上报属性只需要两步操作如下:
// 1、在PromMetrics结构体中添加需要上报的属性名以及对应的类型
// 2、使用相应的属相上报方法上报属性

const (
	//最好是小写
	Module = "mod"
	App    = "app"
)

func example() {

	//am.InitMetrics("", &M, nil, nil)
	am.InitMetrics(App+"_"+Module, &M, prometheus.Labels{"Public1": "test1", "Public2": "test2"}, []string{"AppId"})
	//am.InitMetrics(App+"_"+Module, &M, nil, []string{"AppId"})

	M.RECV_TEST_TOTAL.Inc()
	
	M.DEAL_REQUEST_SUCC_TOTAL.With(
		//prometheus.Labels{"AppId": "appid1"}),
		nil).Inc()
	M.EXAMPLE_TOTAL.With(prometheus.Labels{
		//"AppId":  "appid1",
		"label1": "test1",
		"label2": "test2",
	}).Inc()
	M.RECEIVE_REQUEST_TOTAL.WithLabelValues(
		//"appid1",
		"xxx",
		"xxx").Inc()
	t, err := M.RECEIVE_REQUEST_TOTAL.GetMetricWith(prometheus.Labels{
		//"AppId": "appid1",
		"label1": "test1",
		"label2": "test2"})
	if err == nil {
		t.Inc()
	}
}

var M PromMetrics

type PromMetrics struct {
	//反射说明:`pml:"name;label1,label2...;namespace;subsystem;example(help msg)"`
	// 1、name: [prefix_][namespace_][subsystem_]name []表示可选,这里出现的"_"不需要自己声明
	// 		1) 反射中设置则使用反射设置,不设置则默认将变量名转换为小写。
	// 		2) InitMetrics第一个参数prefix是name前缀,若提供,则总会生效,
	// 		   可以使用prefix设置namespace和subsystem, 两者共存则最后metric为prefix_namespace_subsystem_name,
	// 		   只提供其中一个则为prefix_name,或者namespace_subsystem_name,
	// 		   可以用不提供namespace,subsystem,只提供prefix,让所有metric有相同的前缀
	// 		3) 如果要满足Go的命名规范,变量名用驼峰,不用下划线,则可以使用反射来设置name,变量名使用驼峰命名,
	// 		   但是这样带来一个不太便利的问题查找,比如我在prometheus视图中看到某个metric,
	// 		   要找到代码中对应的位置,需要先到这里找到对应变量名,参见下面的 ExampleTotal 定义
	// 		4) name不能重复
	// 2、labels:
	//		1) labels可以设置多个,用","隔开,想要有label效果,则要使用TypeVec(eg:CounterVec),如果只是Type(eg:Counter),则所有label不生效
	// 3、namespace、subsystem参见name
	// 4、help:
	// 		1)metric的help信息

	//上报的属性名/prometheus类型/反射
	EXAMPLE_TOTAL           prometheus.CounterVec `pml:";label1,label2;namespace;subsystem;example(help msg)"`                   //name is example_total
	ExampleTotal            prometheus.CounterVec `pml:"example_test_total;label1,label2;namespace;subsystem;example(help msg)"` //name is example_test_total
	RECEIVE_REQUEST_RATE    prometheus.GaugeVec   //目前只支持Counter ,Gauge功能还未实现
	RECEIVE_REQUEST_TOTAL   prometheus.CounterVec `pml:";label1,label2;"` //有两个标签,label1,label2
	DEAL_REQUEST_SUCC_TOTAL prometheus.CounterVec
	DEAL_REQUEST_FAIL_TOTAL prometheus.CounterVec

	//TODO: add your metrics
	RECV_TEST_TOTAL prometheus.Counter `pml:";;;;just for test"`
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttrGaugeAdd

func AttrGaugeAdd(metric AttrType)

func AttrGaugeInc

func AttrGaugeInc(metric AttrType)

func AttrGaugeSet

func AttrGaugeSet(metric AttrType)

func AttrGaugeSub

func AttrGaugeSub(metric AttrType)

func DefaultParseAttrName

func DefaultParseAttrName(name string) string

func GetCounter

func GetCounter(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) prometheus.Counter

func GetCounterVec

func GetCounterVec(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) *prometheus.CounterVec

func GetGauge

func GetGauge(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) prometheus.Gauge

func GetGaugeVec

func GetGaugeVec(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) *prometheus.GaugeVec

func GetHistogram

func GetHistogram(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) prometheus.Histogram

func GetHistogramVec

func GetHistogramVec(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) *prometheus.HistogramVec

func GetSummary

func GetSummary(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) prometheus.Summary

func GetSummaryVec

func GetSummaryVec(prefix, name string, constLabel prometheus.Labels, publicKeys, rfTags []string) *prometheus.SummaryVec

func InitMetrics

func InitMetrics(prefix string, Metrics interface{}, labels prometheus.Labels, publicTags []string)

另一种实现方式,不过不能进行功能扩展,例如公共的labels需要额外处理 不过可以直接用prometheus的metric类型

func InitMetrics2

func InitMetrics2(prefix string, Metrics interface{}, labels prometheus.Labels, publicTags []string)

Types

type AttrType

type AttrType interface {
	AttrValue() int
}

type Counter

type Counter int

define Attr Types

func (Counter) Add

func (c Counter) Add(metric AttrType, value int)

只给没有label的metric调用

func (Counter) AttrValue

func (c Counter) AttrValue() int

func (Counter) GetMetricWith

func (c Counter) GetMetricWith(labels prometheus.Labels) (prometheus.Counter, error)

func (Counter) GetMetricWithLabelValues

func (c Counter) GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error)

func (Counter) Inc

func (c Counter) Inc()

只给没有label的metric调用

func (Counter) With

func (c Counter) With(labels prometheus.Labels) prometheus.Counter

func (Counter) WithLabelValues

func (c Counter) WithLabelValues(lvs ...string) prometheus.Counter

type Gauge

type Gauge int

func (Gauge) AttrValue

func (c Gauge) AttrValue() int

type Histogram

type Histogram int

func (Histogram) AttrValue

func (c Histogram) AttrValue() int

type ParseAttrNameFunc

type ParseAttrNameFunc func(string) string
var (
	ParseAttrName ParseAttrNameFunc = nil
)

type Summary

type Summary int

func (Summary) AttrValue

func (c Summary) AttrValue() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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