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

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

Prometheus笔记(一)metric type

Prometheus笔记(一)metric type

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

1、Counter

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

counter主要有两个方法:

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

1.1 Counter

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

​ 1、初始化一个metric容器

​ 2、Register注册容器

​ 3、向容器中添加值

使用举例:

  1. //step1:初始一个counter
  2. pushCounter := prometheus.NewCounter(prometheus.CounterOpts{
  3. Name: "repository_pushes", // 注意: 没有help字符串
  4. })
  5. err := prometheus.Register(pushCounter) // 会返回一个错误.
  6. if err != nil {
  7. fmt.Println("Push counter couldn't be registered, no counting will happen:", err)
  8. return
  9. }
  10. // Try it once more, this time with a help string.
  11. pushCounter = prometheus.NewCounter(prometheus.CounterOpts{
  12. Name: "repository_pushes",
  13. Help: "Number of pushes to external repository.",
  14. })
  15. //setp2: 注册容器
  16. err = prometheus.Register(pushCounter)
  17. if err != nil {
  18. fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err)
  19. return
  20. }
  21. pushComplete := make(chan struct{})
  22. // TODO: Start a goroutine that performs repository pushes and reports
  23. // each completion via the channel.
  24. for range pushComplete {
  25. //step3:向容器中写入值
  26. pushCounter.Inc()
  27. }

输出:

  1. 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创建实例。

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

2、Gauge

2.1 Gauge

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

主要有以下四个方法

  1. // 将Gauge中的值设为指定值.
  2. Set(float64)
  3. // 将Gauge中的值加1.
  4. Inc()
  5. // 将Gauge中的值减1.
  6. Dec()
  7. // 将指定值加到Gauge中的值上。(指定值可以为负数)
  8. Add(float64)
  9. // 将指定值从Gauge中的值减掉。(指定值可以为负数)
  10. Sub(float64)

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

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

2.2 GaugeVec

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

  1. cpusTemprature := prometheus.NewGaugeVec(
  2. prometheus.GaugeOpts{
  3. Name: "CPUs_Temperature",
  4. Help: "the temperature of CPUs.",
  5. },
  6. []string{
  7. // Which cpu temperature?
  8. "cpuName",
  9. },
  10. )
  11. prometheus.MustRegister(cpusTemprature)
  12. cpusTemprature.WithLabelValues("cpu1").Set(temperature1)
  13. cpusTemprature.WithLabelValues("cpu2").Set(temperature2)
  14. cpusTemprature.WithLabelValues("cpu3").Set(temperature3)

3、Summary

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

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

4、Histogram

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

  1. temps := prometheus.NewHistogram(prometheus.HistogramOpts{
  2. Name: "pond_temperature_celsius",
  3. Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
  4. Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
  5. })
  6. // Simulate some observations.
  7. for i := 0; i < 1000; i++ {
  8. temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
  9. }
  10. // Just for demonstration, let's check the state of the histogram by
  11. // (ab)using its Write method (which is usually only used by Prometheus
  12. // internally).
  13. metric := &dto.Metric{}
  14. temps.Write(metric)
  15. 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笔记(二)监控go项目实时给grafana展示

    欢迎加入go语言学习交流群 636728449 Prometheus笔记(二)监控go项目实时给grafana展示 Prometheus笔记(一)metric type 文章目录 一.promethe ...

  2. prometheus 笔记

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

  3. Qt笔记之Q_DECLARE_METATYPE(Type)

    首先看一看QVariant这个类,我们可以把它当做一个万能数据类型,需要的时候能转换为一种特定的类型. 使用canConvert()函数检查是否能转换为你想要的数据类型,结构为:bool QVaria ...

  4. MyBatis笔记----@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class

    使用MyBatis 3.4.1或者其以上版本 @Intercepts({ @Signature(type = StatementHandler.class,  method = "prepa ...

  5. go学习笔记-类型转换(Type Conversion)

    类型转换(Type Conversion) 类型转换用于将一种数据类型的变量转换为另外一种类型的变,基本格式 type_name(expression) type_name 为类型,expressio ...

  6. 笔记 Activator.CreateInstance(Type)

    这段代码取自NopCommerce 3.80 的 权限列表初始化代码 dynamic provider = Activator.CreateInstance(providerType);   文件位置 ...

  7. Prometheus 四种metric类型

    Prometheus的4种metrics(指标)类型: Counter Gauge Histogram Summary 四种指标类型的数据对象都是数字,如果要监控文本类的信息只能通过指标名称或者 la ...

  8. Prometheus监控学习笔记之全面学习Prometheus

    0x00 概述 Prometheus是继Kubernetes后第2个正式加入CNCF基金会的项目,容器和云原生领域事实的监控标准解决方案.在这次分享将从Prometheus的基础说起,学习和了解Pro ...

  9. prometheus client_golang使用

    序言 Prometheus是一个开源的监控系统,拥有许多Advanced Feature,他会定期用HTTP协议来pull所监控系统状态进行数据收集,在加上timestamp等数据组织成time se ...

随机推荐

  1. python 类内部装饰器的实现 与 参数解构学习

    学习了函数的装饰器的写法,然后想到如果要在类中初始化或获取信息时能用装饰器做过滤和验证应该怎么写呢, 在网上查了下相关信息,感觉这样也是可以的,不知道会不会有什么问题class Ctj(): clas ...

  2. NuGet Package Explorer使用教程下载

    1.下载NuGet Package Explorer http://www.pc0359.cn/downinfo/91514.html 2.双击NuGet Package Explorer出现启动界面 ...

  3. IO类

    Java的IO体系分为Input/Output和Reader/Writer两类,区别在于Reader/Writer在读写文本时能自动转换内码.基本上,所有的IO类多是配对的,即有XXXInput,就有 ...

  4. nyoj 61-传纸条(一)(双向dp)

    61-传纸条(一) 内存限制:64MB 时间限制:2000ms Special Judge: No accepted:8 submit:37 题目描述: 小渊和小轩是好朋友也是同班同学,他们在一起总有 ...

  5. Lombok 使用详解,简化Java编程

    前言 在 Java 应用程序中存在许多重复相似的.生成之后几乎不对其做更改的代码,但是我们还不得不花费很多精力编写它们来满足 Java 的编译需求 比如,在 Java 应用程序开发中,我们几乎要为所有 ...

  6. var与let与const

    var与let与const都是用来声明变量,但是三者之间也有一些区别 var的使用 var a;//声明变量a var a,b,c;//声明三个变量a,b,c var a,b,c=2;//声明了三个变 ...

  7. 结合开源软件kaptcha讲解登录验证码功能的实现

    一.验证码生成之配置使用kaptcha 使用google开源的验证码实现类库kaptcha,通过maven坐标引入 <dependency> <groupId>com.gith ...

  8. Linux目录结构-中部

    第1章 /proc目录下 1.1 /proc/cpuinfo 系统cpu信息 [root@nfsnobody ~]# cat /proc/cpuinfo                  一般常用的是 ...

  9. js中this的使用及代表意义

    我们在js中经常看到this这个关键字,那么他是什么呢?它可以是全局对象.当前对象,也可以是任意对象,函数的调用方式决定了 this 的值. 1. 方法中的this. 在对象方法中, this 指向调 ...

  10. c语言博客专业05

    问题 答案 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/8655 我在 ...