欢迎加入go语言学习交流群 636728449

Prometheus笔记(二)监控go项目实时给grafana展示
Prometheus笔记(一)metric type

文章目录

  • Prometheus笔记(一)metric type
      • 1、Counter
        • 1.1 Counter
        • 1.2 CounterVec
      • 2、Gauge
        • 2.1 Gauge
        • 2.2 GaugeVec
      • 3、Summary
      • 4、Histogram
    • 二、参考资料

Prometheus笔记(一)metric type

Prometheus客户端库提供四种核心度量标准类型。 这些目前仅在客户端库中区分(以启用针对特定类型的使用而定制的API)和有线协议。 Prometheus服务器尚未使用类型信息,并将所有数据展平为无类型时间序列。(本文所有示例代码都是使用go来举例的)

1、Counter

计数器是表示单个单调递增计数器的累积量,其值只能增加或在重启时重置为零。 例如,您可以使用计数器来表示服务的总请求数,已完成的任务或错误总数。 不要使用计数器来监控可能减少的值。 例如,不要使用计数器来处理当前正在运行的进程数,而应该用Gauge。

counter主要有两个方法:

//将counter值加1.
Inc()
// 将指定值加到counter值上,如果指定值< 0会panic.
Add(float64)

1.1 Counter

一般 metric 容器使用的步骤都是:

​ 1、初始化一个metric容器

​ 2、Register注册容器

​ 3、向容器中添加值

使用举例:

//step1:初始一个counter
pushCounter := prometheus.NewCounter(prometheus.CounterOpts{Name: "repository_pushes", // 注意: 没有help字符串
})
err := prometheus.Register(pushCounter) // 会返回一个错误.
if err != nil {fmt.Println("Push counter couldn't be registered, no counting will happen:", err)return
}// Try it once more, this time with a help string.
pushCounter = prometheus.NewCounter(prometheus.CounterOpts{Name: "repository_pushes",Help: "Number of pushes to external repository.",
})//setp2: 注册容器
err = prometheus.Register(pushCounter)
if err != nil {fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err)return
}pushComplete := make(chan struct{})
// TODO: Start a goroutine that performs repository pushes and reports
// each completion via the channel.
for range pushComplete {//step3:向容器中写入值pushCounter.Inc()
}

输出:

Push counter couldn't be registered, no counting will happen: descriptor Desc{fqName: "repository_pushes", help: "", constLabels: {}, variableLabels: []} is invalid: empty help string

1.2 CounterVec

CounterVec是一组counter,这些计数器具有相同的描述,但它们的变量标签具有不同的值。 如果要计算按各种维度划分的相同内容(例如,响应代码和方法分区的HTTP请求数),则使用此方法。使用NewCounterVec创建实例。

//step1:初始化一个容器
httpReqs := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "http_requests_total",Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",},[]string{"code", "method"},
)
//step2:注册容器
prometheus.MustRegister(httpReqs)httpReqs.WithLabelValues("404", "POST").Add(42)// If you have to access the same set of labels very frequently, it
// might be good to retrieve the metric only once and keep a handle to
// it. But beware of deletion of that metric, see below!
//step3:向容器中写入值,主要调用容器的方法如Inc()或者Add()方法
m := httpReqs.WithLabelValues("200", "GET")
for i := 0; i < 1000000; i++ {m.Inc()
}
// Delete a metric from the vector. If you have previously kept a handle
// to that metric (as above), future updates via that handle will go
// unseen (even if you re-create a metric with the same label set
// later).
httpReqs.DeleteLabelValues("200", "GET")
// Same thing with the more verbose Labels syntax.
httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})

2、Gauge

2.1 Gauge

Gauge可以用来存放一个可以任意变大变小的数值,通常用于测量值,例如温度或当前内存使用情况,或者运行的goroutine数量

主要有以下四个方法

// 将Gauge中的值设为指定值.
Set(float64)
// 将Gauge中的值加1.
Inc()
// 将Gauge中的值减1.
Dec()
// 将指定值加到Gauge中的值上。(指定值可以为负数)
Add(float64)
// 将指定值从Gauge中的值减掉。(指定值可以为负数)
Sub(float64)

示例代码(实时统计CPU的温度):

//step1:初始化容器
cpuTemprature := prometheus.NewGauge(prometheus.GaugeOpts{Name:      "CPU_Temperature",Help:      "the temperature of CPU",
})
//step2:注册容器
prometheus.MustRegister(cpuTemprature)
//定时获取cpu温度并且写入到容器
func(){tem = getCpuTemprature()//step3:向容器中写入值。调用容器的方法cpuTemprature.Set(tem)
}

2.2 GaugeVec

假设你要一次性统计四个cpu的温度,这个时候就适合使用GaugeVec了。

cpusTemprature := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name:      "CPUs_Temperature",Help:      "the temperature of CPUs.",},[]string{// Which cpu temperature?"cpuName",},
)
prometheus.MustRegister(cpusTemprature)cpusTemprature.WithLabelValues("cpu1").Set(temperature1)
cpusTemprature.WithLabelValues("cpu2").Set(temperature2)
cpusTemprature.WithLabelValues("cpu3").Set(temperature3)

3、Summary

Summary从事件或样本流中捕获单个观察,并以类似于传统汇总统计的方式对其进行汇总:1。观察总和,2。观察计数,3。排名估计。典型的用例是观察请求延迟。 默认情况下,Summary提供延迟的中位数。

temps := prometheus.NewSummary(prometheus.SummaryOpts{Name:       "pond_temperature_celsius",Help:       "The temperature of the frog pond.",Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
})// Simulate some observations.
for i := 0; i < 1000; i++ {temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
}// Just for demonstration, let's check the state of the summary by
// (ab)using its Write method (which is usually only used by Prometheus
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(proto.MarshalTextString(metric))

4、Histogram

主要用于表示一段时间范围内对数据进行采样,(通常是请求持续时间或响应大小),并能够对其指定区间以及总数进行统计,通常我们用它计算分位数的直方图。

temps := prometheus.NewHistogram(prometheus.HistogramOpts{Name:    "pond_temperature_celsius",Help:    "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.Buckets: prometheus.LinearBuckets(20, 5, 5),  // 5 buckets, each 5 centigrade wide.
})// Simulate some observations.
for i := 0; i < 1000; i++ {temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
}// Just for demonstration, let's check the state of the histogram by
// (ab)using its Write method (which is usually only used by Prometheus
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(proto.MarshalTextString(metric))

欢迎加入go语言学习交流群 636728449

二、参考资料

[1] https://godoc.org/github.com/prometheus/client_golang/prometheus
[2] https://prometheus.io/docs/introduction/overview/

Prometheus笔记(一)metric type相关推荐

  1. prometheus 笔记

    前言 prometheus 是监控应用软件类似于nagios. 安装 1.官网下载prometheus-2.2.0.linux-amd64压缩包,解压,执行./prometheus即可.这里重要的是配 ...

  2. Prometheus笔记

    https://blog.csdn.net/luanpeng825485697/article/details/82318204 https://www.cnblogs.com/yangxiaoyi/ ...

  3. Qt工作笔记-QTreeWidgetItem中type的基本用法

    这是一个很好的东西. 话不多说,运行截图如下: 代码如下: widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #in ...

  4. STM32 学习笔记 expected a type specifier

    User\main.c(16): error:  #79: expected a type specifier 1 GPIO_ResetBits(GPIOC,GPIO_Pin_13);//点亮LED小 ...

  5. Qt文档阅读笔记-ToolBar QML Type

    ToolBar主要用于应用程序的上下文控制,就像导航按钮和搜索按钮那样.ToolBar就像窗口程序的header或footer那样. ToolBar不提供自己的布局,不过需要开发者设置内容,如创建一个 ...

  6. Qt文档阅读笔记-GridLayout QML Type解析与实例

    目录 基本概念 代码与实例 基本概念 如果QGridLaout大小被调整,所有item的布局都将会重新排列.和widget的QGridLayout一样.如果想要一行或一列的布局可以使用RowLayou ...

  7. Qt文档阅读笔记-FileDialog QML Type官方解析与实例

    目录 官方解析 博主例子 官方解析 FileDialog是基于文件的选择器,可以选择文件或文件夹,创建文件,这个Dialog初始化是不可见的,得需要设置他为visible或调用open()即可. 下面 ...

  8. Qt文档阅读笔记-TextEdit QML Type官方解析及实例

    目录 官方解析 博主栗子 官方解析 TextEdit展示了一个可编辑的一块,是有格式的文本. 他同样能展示普通文本和富文本: TextEdit {width: 240text: "<b ...

  9. Qt文档阅读笔记-Text QML Type官方解析及实例

    目录 官方解析 博主例子 官方解析 Text能够展示纯文本和富文本.举个例子,红色文本以及指定的字体和大小 Text {text: "Hello World!"font.famil ...

最新文章

  1. C++ 泛型编程 -- 函数模版
  2. 2018-3-22论文一种新型的智能算法--狼群算法(笔记三)算法的步骤+收敛性分析
  3. python中if的效率_Python 代码性能优化技巧
  4. 探讨如何确保对日软件外包开发过程中的质量
  5. AIX系统root用户密码忘记
  6. mongoDB简单介绍及安装
  7. 假期七天实习参观有感
  8. spark将rdd转为string_SparkCore---RDD依赖
  9. MARQUEE 字符滚动条效果
  10. 多功能函数计算器(MATLAB实现)
  11. 基于51单片机GPS的导航系统设计(3)---毕设论文
  12. Oracle数据库update用法总结
  13. 通杀! 熬夜码的 - 八万字 - 让你一文读懂SQL注入漏洞原理及各种场景利用
  14. 【安全牛学习笔记】Kali Linux基本工具
  15. 替换读到的文件中的某一元素 pd 格式
  16. python中怎么计数_python怎么实现计数?
  17. centos6下安装配置NFS
  18. 人在外省想在老家装监控,在手机上能看,要什么条件和材料?
  19. python脚本开头怎么写_浅谈Python脚本开头及导包注释自动添加方法
  20. 菜狗的reverse学习——攻防世界xxxorrr

热门文章

  1. 你真的会除甲醛吗?除甲醛才不是通风这么简单!
  2. linux服务器实训心得体会,linux实训心得体会 linux实训总结与体会
  3. 感兴趣的很多,擅长的却没一个
  4. 六下计算机教学总结,六年级下册计算机教学工作总结.doc
  5. UE4的GamePlay框架概述
  6. 数据库系统原理--第2章作业1--习题答案
  7. java毕业生设计心灵治愈服务平台计算机源码+系统+mysql+调试部署+lw
  8. MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
  9. Office打开很慢解决办法
  10. 关于苹果审核4.3的一些猜想