关于数据库:Metric模块源码解析

4次阅读

共计 6166 个字符,预计需要花费 16 分钟才能阅读完成。

分布式系统的监控告警及运维服务离不开指标监控,开务作为浪潮自主研发的一款分布式数据库天然也不例外。在兼顾强一致性、高可用分布式架构、在线程度扩大、企业级平安等个性下,开务的 metric 模块可提供监控指标,实现事后定义指标的周期性采集。同时,能够提供兼容 Prometheus 规范格局的 API 接口,不便与内部的 Prometheus 服务进行集成。

开务数据库 metric 模块收集各模块相干统计的 metric 信息,并将其作为 Prometheus 格局的指标储存起来用于进一步查阅,对判断开务数据库的运行状况有着重要作用,同时也是开务数据库 adminUI 指标的数据起源。本期内容将围绕下图展现的 metric 模块根本框架,率领大家深刻了解开务数据库 metric 模块的源码,图中各模块的具体介绍将继续为大家更新。

1、定义接口介绍

1.IterableIterable
IterableIterable 提供了一个同步拜访外部对象的办法。办法如下:

GetName() string 返回指标名
GetHelp() string 返回指标帮忙文本
GetMeasurement() string 返回指标的 lable
GetUnit() Unit 返回指标应用的单位
GetMetadata() Metdata 返回指标的 Metadata
Inspect(func(interface{})) Inspect 对每个蕴含的项调用给定的闭包

2.PrometheusExportable
PrometheusExportable 是规范独立指标接口,可供指标导入 Prometheus。办法如下:

GetName() string 返回指标名
GetHelp() string 返回指标帮忙文本
GetType() *prometheusgo.MetricType 返回指标的 Prometheus 类型
GetLables() []*prometheusgo.LabelPair Metadata 中的一个办法,返回指标的标签
ToPrometheusMetric() *prometheusgo.Metric 返回一个实现值填充的 Prometheus 指标

3.PrometheusIterable
PrometheusIterable 是 PrometheusExportable 的扩大,用于批示该指标由减少父标签值的子指标组成。蕴含成员:PrometheusExportable。办法如下:

Each([]*prometheusgo.LabelPair, func(metric *prometheusgo.Metric))“Each”获取与父指标相关联的标签对切片,并应用每个子指标调用所传递的函数

2、Metric Metadata 介绍

Metadata 蕴含对于指标的元数据,它必须嵌入到每个 metric object 中。它用于将无关指标的信息导出到 Promethues 和 adminUI 图表。

type Metadata struct {  
Name        string           
Help        string           
Measurement string            
Unit        Unit             
MetricType  _go.MetricType    
Labels      []*LabelPair}  

// 办法
GetName() string  
GetHelp() string  
GetMeasurement() string  
GetUnit() Unit  
GetLabels() []*prometheusgo.LabelPair  
Addlabel(name value string)// 给一个指标增加标签 / 值映射

3、指标类型介绍

1.HistogramHistogram 
在一段时间范畴内对数据进行采样(通常是申请持续时间、响应大小等),并将其计入可配置的存储桶(bucket)中,后续可通过指定区间筛选样本,也能够统计样本总数,最初个别将数据展现为直方图。

Prometheus 的 Histogram 是一种累积直方图,与下面的区间划分形式是有差异的。它的划分形式如下:假如每个 bucket 的宽度是 0.2s,那么第一个 bucket 示意响应工夫小于等于 0.2s 的申请数量,第二个 bucket 示意响应工夫小于等于 0.4s 的申请数量,以此类推。也就是说,每一个 bucket 的样本蕴含了之前所有 bucket 的样本,所以叫累积直方图。

type Histogram {  
Metadata  
maxVal int64  
mu     struct {  
syncutil.Mutex  
cumulative *hdrhistogram.Histogram  
sliding    *slidingHistogram     
}  
//hdrhistogram.Histogram
type Histogram struct {  
lowestTrackableValue        int64  
highestTrackableValue       int64  
unitMagnitude               int64  
significantFigures          int64  
subBucketHalfCountMagnitude int32  
subBucketHalfCount          int32  
subBucketMask               int64  
subBucketCount              int32  
bucketCount                 int32  
countsLen                   int32  
totalCount                  int64  
counts                      []int64} 
//slidingHistogram
type slidingHistogram struct {  
windowed *hdrhistogram.WindowedHistogram  
nextT    time.Time  
duration time.Duration  
} 
type WindowedHistogram struct {
idx int
h  []Histogram
m  *Histogram
Current *Histogram
}

// 相干办法介绍
func (h *Histogram) Windowed() (*hdrhistogram.Histogram, time.Duration)
返回一份以后的窗口化直方图的数据和其中的工夫距离

func (h *Histogram) Snapshot() *hdrhistogram.Histogram
返回累积(即所有样本)直方图数据的正本

func (h *Histogram) RecordValue(v int64)
RecordValue 将给定的值增加到直方图。记录超过该直方图配置最大值应用办法

func (h *Histogram) TotalCount() int64
TotalCount 返回样本的 (累计) 数量

func (h *Histogram) Min() int64
返回最小值

func (h *Histogram) Inspect(f func(interface{}))
调用带有空字符串和接管方的闭包

func (h *Histogram) GetType() *prometheusgo.MetricType
返回此指标的 Prometheus 类型 enum

func (h *Histogram) ToPrometheusMetric() *prometheusgo.Metric
返回正确类型的已填充的 Prometheus 度量值

func (h *Histogram) GetMetadata() Metadata
返回指标的元数据,包含 Prometheus MetricType

func NewHistogram(metadata Metadata, duration time.Duration, maxVal int64, sigFigs int) (*Histogram)
实例化一个新 histogram

func NewLatency(metadata Metadata, histogramWindow time.Duration) *Histogram
NewLatency
返回一个带有适当默认值的直方图来跟踪提早。数值以 ns 示意,截断为距离[0,MaxLatency],并以 1 位精度记录(即误差在 100ms 时 <10ms,在 60s 时 <6s)

2.Counter
Counter 类型代表一种样本数据枯燥递增的指标,即只增不减,除非监控零碎产生了重置。例如,你能够应用 Counter 类型的指标来示意服务的申请数、已实现的工作数、谬误产生的次数等。

type Counter struct {  
Metadata  
metrics.Counter  
}  
type Counter interface {Clear()  
Count() int64  
Dec(int64)  
Inc(int64)  
Snapshot() Counter}

// 相干办法介绍
func (c *Counter) Dec(int64)
Dec 重载了 metric.Counter 的办法。不能应用这种办法,它只用于避免误用 metric 类型

func (c *Counter) GetType() *prometheusgo.MetricType
返回此指标的 Prometheus 类型 enum

func (c *Counter) Inspect(f func(interface{}))
调用带有空字符串和接管方的闭包,即返回本人 c

func (c *Counter) MarshalJSON() ([]byte, error)
MarshalJSON 将数据封装到 JSON

func (c *Counter) GetMetadata() Metadata
返回指标的元数据,包含 Prometheus MetricType

3.Gauge
Gauge 类型代表一种样本数据能够任意变动的指标,即可增可减。Guage 通常用于像温度或者内存使用率这种指标数据,也能够示意能随时减少或缩小的“总数”,例如:以后并发申请的数量。

type Gauge struct {  
Metadata  
value *int64  
fn    func() int64}

// 相干办法介绍
func (g *Gauge) Snapshot() metrics.Gauge
Snapshot 返回 Gauge 的只读正本

func (g *Gauge) Update(v int64)
更新 Gauge 的值

func (g *Gauge) Inc(i int64)
减少 Gauge 的以后值

func (g *Gauge) Dec(i int64)
缩小 Gauge 的以后值

func (g *Gauge) Value() int64
Value 返回 Gauge 的以后值

func (g *Gauge) GetType() *prometheusgo.MetricType
返回此指标的 Prometheus 类型 enum

func (g *Gauge) ToPrometheusMetric() *prometheusgo.Metric
返回此指标的 Prometheus 类型 enum

func (g *Gauge) GetMetadata() Metadata
返回指标的元数据,包含 Prometheus MetricType

4.Rate
Rate 是用来计算某个指标在最近一个区间工夫内的变化率。

type Rate struct {  
Metadata  
mu       syncutil.Mutex // protects fields below  
curSum   float64  
wrapped  ewma.MovingAverage  
interval time.Duration  
nextT    time.Time  
}  

// 相干办法介绍
func (e *Rate) GetType() *prometheusgo.MetricType
GetType 返回该指标的 Prometheus 类型 enum

func (e *Rate) Inspect(f func(interface{}))
Inspect 用本身调用给定的闭包

func (e *Rate) ToPrometheusMetric() *prometheusgo.Metric
返回此指标的 Prometheus 类型 enum

func (c *Counter) MarshalJSON() ([]byte, error)
MarshalJSON 将数据封装到 JSON

func (e *Rate) GetMetadata() Metadata
GetMetadata 返回指标的元数据,包含 Prometheus MetricType

func (e *Rate) Value() float64
Value 返回 Rate 的以后值

func (e *Rate) tick()
Rate 工夫后退

func (e *Rate) nextTick() time.Time
返回 Rate 的以后工夫。func (e *Rate) Add(v float64)
增加将给定的测量值增加到 Rate

4、注册器 Registry 介绍

Registry 是 metric 的列表,它提供了一种解决指标的办法,能够将 metric 编组成 JSON,并生成 Prometheus 格局的 metric。同时能够给注册的指标打上标签,当导出到 Prometheus 时,这些标签将利用于它的所有指标。

type Registry struct {  
syncutil.Mutex  
labels  []*prometheusgo.LabelPair  
tracked []Iterable}  

// 相干办法介绍
func (r *Registry) AddLabel(name, value string)
AddLabel 为这个注册表增加一个标签 / 值对

func (r *Registry) AddMetric(metric Iterable)
AddMetric 将传入的 metric 增加到注册表

func (r *Registry) WriteMetricsMetadata(dest map[string]Metadata)
WriteMetricsMetadata 将所有跟踪 metric 的元数据写入参数映射

func (r *Registry) Each(f func(name string, val interface{}))
每个函数对所有 metric 调用给定的闭包

func (r *Registry) MarshalJSON() ([]byte, error)
格式化到 JSON 格局

5、注册新 Registry 步骤

// 以 txnMetric 阐明  
//txn_metric.go  
// 申明定义的指标构造体类型  
type TxnMetrics struct {  
Commits         *metric.Counter  
...  
}  
// 定义指标的 metadata  
var(  
metaCommitsRates = metric.Metadata{  
Name:        "txn.commits",  
Help:        "Number of committed KV transactions (including 1PC)",  
Measurement: "KV Transactions",  
Unit:        metric.Unit_COUNT,  
}  
...  
)  
// 将定义的指标类型和 metadata 相关联  
func MakeTxnMetrics(histogramWindow time.Duration) TxnMetrics {  
return TxnMetrics{Commits:                       metric.NewCounter(metaCommitsRates),  
}  
//server.go:  
// 注册进 Registry  
txnMetrics := kvcoord.MakeTxnMetrics(cfg.HistogramWindowInterval())  
registry.AddMetricStruct(txnMetrics) 
正文完
 0