metrics

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

微服务使用方法

初始化处使用
  1. 指标初始化 定义服务要使用的各种指标
var metricsValues = []*metrics.MetricValue{
  {
    ValueType: metrics.Counter,                                                                  // 提供四种指标类型Counter,Gauge,Histogram,Summary
    Name:      constant.MetricTranslateLanguage,                                                 // 本服务唯一
    Help:      "hello interface count",                                                          // 指标描述
    Labels:    []string{constant.MetricLabelSourceLanguage, constant.MetricLabelTargetLanguage}, // 标签注意顺序
  },
}
  1. metrics初始化 在应用初始化的时候调用metrics.Init, 出入gin.Engine和上文定义的指标,此函数会执行两部分初始化。
  • 为gin新增一个路径 /metrics, 此路径可以获取用户的打点内容
  • 初始化defaultMetricsManger, 并初始化应用的默认打点指标:
    • 带有method,path,status标签的http_request_count
    • 带有method,path,status标签的http_request_duration_seconds
    • 带有method,path,status标签的http_request_size_bytes
    • 带有method,path,status标签的http_response_size_bytes
业务打点使用
  1. 指标简单方法使用
// 业务打点
// metrics包维护默认的metric管理器
// 使用IncWithLabel给带有标签的指标加1
labels := map[string]string{constant.MetricLabelSourceLanguage: "cn", constant.MetricLabelTargetLanguage: "en"}
err := metrics.IncWithLabel(constant.MetricTranslateLanguage, labels)
if err != nil {
    log.Println("【Error】", err)
    c.Status(500)
    return
}

// 使用Inc给带有标签的指标加1,不过这个方法需要让初始化的label和value的顺序一样。如下所示:
err = metrics.Inc(constant.MetricTranslateLanguage, []string{"cn", "en"})
if err != nil {
    log.Println("【Error】", err)
    c.Status(500)
    return
}
  1. 指标复杂方法使用
// 如果metrics库的方法不能完全满足你, 那么可以获取Vec后转换成相应的指标并使用
metric, err := metrics.GetMetric(constant.MetricTranslateLanguage)
if err != nil {
    log.Println("【Error】", err)
    c.Status(500)
    return
}
metric.GetVec().(prometheus.Histogram).Desc()

exporter使用方法

  1. 定义Counter和Gauge类型
metrics.NewGaugeFunc(prometheus.GaugeOpts{
		Namespace: "bml",
		Subsystem: "notebook",
		Name:      "name",
		Help:      "help",
}, TestGuage)

func TestGuage() float64 {
	return 5
}
  1. 定义Histogram类型 参考微服务使用方法

注意

  1. 服务需要定义好gin.Engine,调用 metrics.Init 会为服务增加一个中间件和一个路径 /metrics
  2. 定义的metrics的全名是这样拼装的 namespace_systemname_name
  3. 全文会生成一个全局唯一的 defaultMetricsManger
  4. 所有需要的指标需要在服务初始化的时候定义清楚

实例

example

Documentation

Index

Constants

View Source
const (
	ReqCount      = "http_request_count"
	ReqDuration   = "http_request_duration_seconds"
	ReqSizeBytes  = "http_request_size_bytes"
	RespSizeBytes = "http_response_size_bytes"
)
View Source
const (
	Method = "method"
	Path   = "path"
	Status = "status"
)
View Source
const (
	DefaultNamespace = "bml"
)

Variables

This section is empty.

Functions

func Add

func Add(name string, labelValues []string, value float64) error

func AddWithLabel

func AddWithLabel(name string, labelValues map[string]string, value float64) error

func Inc

func Inc(name string, labelValues []string) error

func IncWithLabel

func IncWithLabel(name string, labelValues map[string]string) error

func Init

func Init(g *gin.Engine, namespace, systemName string) error

在应用初始化的时候调用此接口

func InitMetric

func InitMetric(metricsValues []*MetricValue) error

初始化metric指标

func MetricMiddleware

func MetricMiddleware(c *gin.Context)

func NewCounterFunc

func NewCounterFunc(opts prometheus.CounterOpts, function func() []*LabelAndValue) prometheus.CounterFunc

func NewGaugeFunc

func NewGaugeFunc(opts prometheus.GaugeOpts, function func() []*LabelAndValue) prometheus.GaugeFunc

func Observe

func Observe(name string, labelValues []string, value float64) error

func ObserveWithLabel

func ObserveWithLabel(name string, labelValues map[string]string, value float64) error

func SetGaugeValue

func SetGaugeValue(name string, labelValues []string, value float64) error

func SetGaugeValueWithLabel

func SetGaugeValueWithLabel(name string, labelValues map[string]string, value float64) error

Types

type Collector

type Collector interface {
	Describe(chan<- *prometheus.Desc)
	Collect(chan<- prometheus.Metric)
}

type LabelAndValue

type LabelAndValue struct {
	Labels map[string]string
	Value  float64
}

type Metric

type Metric struct {
	Type       MetricType
	Name       string
	Help       string
	Labels     []string
	Buckets    []float64
	Objectives map[float64]float64
	// contains filtered or unexported fields
}

Metric defines a metric object. Users can use it to save metric data. Every metric should be globally unique by name.

func GenerateCounter

func GenerateCounter(metricValue *MetricValue) (*Metric, error)

func GenerateGauge

func GenerateGauge(metricValue *MetricValue) (*Metric, error)

func GenerateHistogram

func GenerateHistogram(metricValue *MetricValue) (*Metric, error)

func GenerateSummary

func GenerateSummary(metricValue *MetricValue) (*Metric, error)

func GetMetric

func GetMetric(name string) (*Metric, error)

func (*Metric) Add

func (m *Metric) Add(labelValues []string, value float64) error

Add adds the given value to the Metric object. Only for Counter/Gauge type metric.

func (*Metric) AddWithLabel

func (m *Metric) AddWithLabel(labelValues map[string]string, value float64) error

func (*Metric) GetVec

func (m *Metric) GetVec() prometheus.Collector

func (*Metric) Inc

func (m *Metric) Inc(labelValues []string) error

Inc increases value for Counter/Gauge type metric, increments the counter by 1

func (*Metric) IncWithLabel

func (m *Metric) IncWithLabel(labelValues map[string]string) error

func (*Metric) Observe

func (m *Metric) Observe(labelValues []string, value float64) error

Observe is used by Histogram and Summary type metric to add observations.

func (*Metric) ObserveWithLabel

func (m *Metric) ObserveWithLabel(labelValues map[string]string, value float64) error

func (*Metric) SetGaugeValue

func (m *Metric) SetGaugeValue(labelValues []string, value float64) error

SetGaugeValue set data for Gauge type Metric with values.

func (*Metric) SetGaugeValueWithLabel

func (m *Metric) SetGaugeValueWithLabel(labelValues map[string]string, value float64) error

type MetricManager

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

func (*MetricManager) GenerateCounter

func (p *MetricManager) GenerateCounter(metricValue *MetricValue) (*Metric, error)

Generate Counter metrics

func (*MetricManager) GenerateGauge

func (p *MetricManager) GenerateGauge(metricValue *MetricValue) (*Metric, error)

Generate Gauge metric

func (*MetricManager) GenerateHistogram

func (p *MetricManager) GenerateHistogram(metricValue *MetricValue) (*Metric, error)

Generate Histogram metric

func (*MetricManager) GenerateSummary

func (p *MetricManager) GenerateSummary(metricValue *MetricValue) (*Metric, error)

Generate Summary metric

func (*MetricManager) GetMetric

func (p *MetricManager) GetMetric(name string) (*Metric, error)

type MetricType

type MetricType string
const (
	Counter   MetricType = "counter"
	Gauge     MetricType = "gauge"
	Histogram MetricType = "histogram"
	Summary   MetricType = "summary"
	Untyped   MetricType = "untyped"
)

type MetricValue

type MetricValue struct {
	ValueType MetricType
	Name      string
	Help      string
	Labels    []string
	Buckets   []float64
}

type SelfCollector

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

func NewSelfCollector

func NewSelfCollector(desc *prometheus.Desc, valueType prometheus.ValueType, function func() []*LabelAndValue) *SelfCollector

func (*SelfCollector) Collect

func (c *SelfCollector) Collect(ch chan<- prometheus.Metric)

func (*SelfCollector) Desc

func (v *SelfCollector) Desc() *prometheus.Desc

func (*SelfCollector) Describe

func (c *SelfCollector) Describe(ch chan<- *prometheus.Desc)

func (*SelfCollector) Write

func (v *SelfCollector) Write(out *dto.Metric) error

Jump to

Keyboard shortcuts

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