分布式系统的监控告警及运维服务离不开指标监控,开务作为浪潮自主研发的一款分布式数据库自然也不例外。在兼顾强一致性、高可用分布式架构、在线水平扩展、企业级安全等特性下,开务的 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类型enumfunc (h *Histogram) ToPrometheusMetric() *prometheusgo.Metric
返回正确类型的已填充的Prometheus度量值func (h *Histogram) GetMetadata() Metadata
返回指标的元数据,包括Prometheus MetricTypefunc NewHistogram(metadata Metadata, duration time.Duration, maxVal int64, sigFigs int) (*Histogram)
实例化一个新histogramfunc 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类型enumfunc (c *Counter) Inspect(f func(interface{}))
调用带有空字符串和接收方的闭包,即返回自己cfunc (c *Counter) MarshalJSON() ([]byte, error)
MarshalJSON将数据封装到JSONfunc (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类型enumfunc (g *Gauge) ToPrometheusMetric() *prometheusgo.Metric
返回此指标的Prometheus类型enumfunc (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类型enumfunc (e *Rate) Inspect(f func(interface{}))
Inspect用自身调用给定的闭包func (e *Rate) ToPrometheusMetric() *prometheusgo.Metric
返回此指标的Prometheus类型enumfunc (c *Counter) MarshalJSON() ([]byte, error)
MarshalJSON将数据封装到JSONfunc (e *Rate) GetMetadata() Metadata
GetMetadata返回指标的元数据,包括Prometheus MetricTypefunc (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)

Metric模块源码解析相关推荐

  1. Jedis源码解析(一):Jedis简介、Jedis模块源码解析

    一.Jedis简介 1.Jedis对应Redis的四种工作模式 对应关系如下: Jedis主要模块 Redis工作模式 Jedis Redis Standalone(单节点模式) JedisClust ...

  2. C#软件授权、注册、加密、解密模块源码解析并制作注册机生成license

    最近做了一个绿色免安装软件,领导临时要求加个注册机制,不能让现场工程师随意复制.事出突然,只能在现场开发(离开现场软件就不受我们控了).花了不到两个小时实现了简单的注册机制,稍作整理.         ...

  3. openGauss数据库源码解析系列文章—— AI技术之“自调优”

    上一篇介绍了第七章执行器解析中"7.6 向量化引擎"及"7.7 小结"的相关内容,本篇我们开启第八章 AI技术中"8.1 概述"及" ...

  4. Laravel核心解读--Session源码解析

    Session 模块源码解析 由于HTTP最初是一个匿名.无状态的请求/响应协议,服务器处理来自客户端的请求然后向客户端回送一条响应.现代Web应用程序为了给用户提供个性化的服务往往需要在请求中识别出 ...

  5. 【nodejs原理源码赏析(4)】深度剖析cluster模块源码与node.js多进程(上)

    [摘要] 集群管理模块cluster浅析 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 概述 cluster模块是node.js中用于实现和管理 ...

  6. 【nodejs原理源码赏析(4)】深度剖析cluster模块源码与node.js多线程(上)

    [摘要] 集群管理模块cluster浅析 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 概述 cluster模块是node.js中用于实现和管理 ...

  7. python处理回显_Python中getpass模块无回显输入源码解析

    本文主要讨论了python中getpass模块的相关内容,具体如下. getpass模块 昨天跟学弟吹牛b安利Python标准库官方文档的时候偶然发现了这个模块.仔细一看内容挺少的,只有两个主要api ...

  8. erlang下lists模块sort(排序)方法源码解析(二)

    上接erlang下lists模块sort(排序)方法源码解析(一),到目前为止,list列表已经被分割成N个列表,而且每个列表的元素是有序的(从大到小) 下面我们重点来看看mergel和rmergel ...

  9. thttpd源码解析 定时器模块

    thttpd源码解析 定时器模块 thttpd是非常轻量级的http服务器,可执行文件仅50kB.名称中的第一个t表示tiny, turbo, 或throttling 与lighttpd.memcac ...

  10. Libuv源码解析 - uv_loop整个初始化模块

    Libuv源码解析 - uv_loop整个初始化模块 loop_default_loop static uv_loop_t default_loop_struct; static uv_loop_t* ...

最新文章

  1. atitit.php中的dwr 设计模式
  2. Oracle 高性能SQL引擎剖析----执行计划
  3. 《途客圈创业记:不疯魔,不成活》一一2.1 创新工场初印象
  4. hdu 1147(线段相交)
  5. df命令,du命令,磁盘分区
  6. C# winform中ListView用法
  7. 89C51单片机定时器控制的流水灯
  8. Hibernate中使用Criteria查询及注解——(Emp.java)
  9. Diango博客--13.将“视图函数”类转化为“类视图”
  10. java中函数_java中的函数
  11. 小红书 “红”到翻车:你的骚操作闪了我的腰?
  12. FreeMarker语言【页面静态】
  13. 怎样使用PDF编辑器删除多余页面
  14. php++redius,【答疑】edius中快进播放的快捷键是什么啊? - 羽兔网问答
  15. easyui combobox筛选(拼音)
  16. 保姆级教程!Windows右下角扬声器有红叉,点击声音设置输出显示“未安装任何音频输出设备”?
  17. Python3对股票数据进行分析
  18. 微信小程序如何做触底加载分页功能
  19. 下载m3u8视频及在Linux下将ts合并为mp4格式
  20. 软件工程课程周学习进度报告——第三周

热门文章

  1. 小谈国内桌面浏览器占有率
  2. 新手购买基金的买入策略
  3. ((亲测有效))安卓神器Xposed框架无ROOT使用指南
  4. 中国雅虎邮箱停止服务前后帐号迁移攻略
  5. android手写计算器,MyScript Calculator(高级手写计算器) V1.2.2.479 安卓版
  6. word脚注全部放在最后一页(脚注变尾注)
  7. 计算机视觉文献综述选题,综述论文2021-计算机视觉十大领域最新综述文章分类大盘点...
  8. 蓝牙、Wifi与ZigBee无线传输技术中,谁比较占有优势
  9. mysql命令(集合)
  10. 【Spark】(task5)SparkML基础(分类 | 聚类模型)