metrics

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package metrics 提供 Prometheus 指标收集和暴露功能

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector 指标收集器

func Default

func Default() *Collector

Default 返回默认指标收集器

func NewCollector

func NewCollector(namespace string) *Collector

NewCollector 创建新的指标收集器

func (*Collector) AddCounter

func (c *Collector) AddCounter(name string, value float64, labels prometheus.Labels)

AddCounter 增加计数器值

func (*Collector) DecGauge

func (c *Collector) DecGauge(name string, labels prometheus.Labels)

DecGauge 减少 Gauge 值

func (*Collector) Handler

func (c *Collector) Handler() http.Handler

Handler 返回 HTTP Handler

func (*Collector) IncCounter

func (c *Collector) IncCounter(name string, labels prometheus.Labels)

IncCounter 增加计数器

func (*Collector) IncGauge

func (c *Collector) IncGauge(name string, labels prometheus.Labels)

IncGauge 增加 Gauge 值

func (*Collector) MustRegister

func (c *Collector) MustRegister(collector prometheus.Collector)

MustRegister 注册 Prometheus Collector 到 registry

func (*Collector) ObserveHistogram

func (c *Collector) ObserveHistogram(name string, value float64, labels prometheus.Labels)

ObserveHistogram 观察 Histogram 值

func (*Collector) ObserveSummary

func (c *Collector) ObserveSummary(name string, value float64, labels prometheus.Labels)

ObserveSummary 观察 Summary 值

func (*Collector) Register

func (c *Collector) Register(desc MetricDesc) error

Register 注册指标

func (*Collector) Registry

func (c *Collector) Registry() *prometheus.Registry

Registry 返回底层的 Prometheus Registry

func (*Collector) SetGauge

func (c *Collector) SetGauge(name string, value float64, labels prometheus.Labels)

SetGauge 设置 Gauge 值

type ComponentMetrics

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

ComponentMetrics 组件级别的指标

func (*ComponentMetrics) AddCounter

func (m *ComponentMetrics) AddCounter(name string, value float64, labels prometheus.Labels)

AddCounter 增加自定义计数器指标

func (*ComponentMetrics) ObserveHistogram

func (m *ComponentMetrics) ObserveHistogram(name string, value float64, labels prometheus.Labels)

ObserveHistogram 观察自定义 Histogram 指标

func (*ComponentMetrics) ObserveSummary

func (m *ComponentMetrics) ObserveSummary(name string, value float64, labels prometheus.Labels)

ObserveSummary 观察自定义 Summary 指标

func (*ComponentMetrics) RecordDuration

func (m *ComponentMetrics) RecordDuration(seconds float64)

RecordDuration 记录处理耗时

func (*ComponentMetrics) RecordError

func (m *ComponentMetrics) RecordError(count int64)

RecordError 记录错误数

func (*ComponentMetrics) RecordIn

func (m *ComponentMetrics) RecordIn(count int64)

RecordIn 记录输入记录数

func (*ComponentMetrics) RecordOut

func (m *ComponentMetrics) RecordOut(count int64)

RecordOut 记录输出记录数

func (*ComponentMetrics) SetGauge

func (m *ComponentMetrics) SetGauge(name string, value float64, labels prometheus.Labels)

SetGauge 设置自定义 Gauge 指标

func (*ComponentMetrics) Snapshot

func (m *ComponentMetrics) Snapshot() (in, out, errs int64, avgSec float64)

Snapshot 返回指标快照

type FlowMetrics

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

FlowMetrics 流程级别的指标聚合

func NewFlowMetrics

func NewFlowMetrics(flowName string) *FlowMetrics

NewFlowMetrics 创建流程级别的指标聚合器

func (*FlowMetrics) GetOrCreate

func (fm *FlowMetrics) GetOrCreate(name string) *ComponentMetrics

GetOrCreate 获取或创建组件指标

func (*FlowMetrics) SetPrometheusCollector

func (fm *FlowMetrics) SetPrometheusCollector(collector PrometheusCollector)

SetPrometheusCollector 设置 Prometheus 收集器

func (*FlowMetrics) Snapshot

func (fm *FlowMetrics) Snapshot() []MetricSnapshot

Snapshot 返回所有组件的指标快照

type MetricDesc

type MetricDesc struct {
	Name   string
	Type   MetricType
	Help   string
	Labels []string
}

MetricDesc 指标描述

type MetricSnapshot

type MetricSnapshot struct {
	ComponentName  string
	RecordsIn      int64
	RecordsOut     int64
	RecordsError   int64
	AvgDurationSec float64
	ProcessingTime time.Duration
}

MetricSnapshot 指标快照

type MetricType

type MetricType int

MetricType 指标类型

const (
	// MetricTypeCounter 计数器指标类型
	MetricTypeCounter MetricType = iota
	// MetricTypeGauge Gauge 指标类型
	MetricTypeGauge
	// MetricTypeHistogram Histogram 指标类型
	MetricTypeHistogram
	// MetricTypeSummary Summary 指标类型
	MetricTypeSummary
)

type PrometheusCollector

type PrometheusCollector interface {
	// AddCounter 增加计数器值
	AddCounter(name string, value float64, labels prometheus.Labels)
	// ObserveHistogram 观察 Histogram 值
	ObserveHistogram(name string, value float64, labels prometheus.Labels)
}

PrometheusCollector Prometheus 指标收集器接口

type Recorder

type Recorder interface {
	// RecordIn 记录进入组件的记录数
	RecordIn(count int64)
	// RecordOut 记录离开组件的记录数
	RecordOut(count int64)
	// RecordError 记录错误数
	RecordError(count int64)
	// RecordDuration 记录处理耗时
	RecordDuration(seconds float64)

	// AddCounter 增加自定义计数器指标
	AddCounter(name string, value float64, labels prometheus.Labels)
	// SetGauge 设置自定义 Gauge 指标
	SetGauge(name string, value float64, labels prometheus.Labels)
	// ObserveHistogram 观察自定义 Histogram 指标
	ObserveHistogram(name string, value float64, labels prometheus.Labels)
	// ObserveSummary 观察自定义 Summary 指标
	ObserveSummary(name string, value float64, labels prometheus.Labels)
}

Recorder 记录处理过程中的指标

type Server

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

Server Prometheus 指标服务器

func NewServer

func NewServer(config ServerConfig) *Server

NewServer 创建新的 metrics 服务器

func (*Server) Collector

func (s *Server) Collector() *Collector

Collector 返回指标收集器

func (*Server) Start

func (s *Server) Start() error

Start 启动服务器

func (*Server) Stop

func (s *Server) Stop() error

Stop 停止服务器

type ServerConfig

type ServerConfig struct {
	// Addr 监听地址,默认 :9090
	Addr string `json:"addr"`
	// Path metrics 路径,默认 /metrics
	Path string `json:"path"`
	// Namespace 指标命名空间
	Namespace string `json:"namespace"`
}

ServerConfig 服务器配置

type Summary

type Summary struct {
	StartTime  time.Time
	EndTime    time.Time
	Duration   time.Duration
	TotalIn    int64
	TotalOut   int64
	TotalError int64
	Components []MetricSnapshot
}

Summary 指标汇总

Jump to

Keyboard shortcuts

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