Prometheus笔记(一)metric type

您所在的位置:网站首页 metrictype Prometheus笔记(一)metric type

Prometheus笔记(一)metric type

2023-11-08 00:12| 来源: 网络整理| 查看: 265

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

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

文章目录Prometheus笔记(一)metric type1、Counter1.1 Counter1.2 CounterVec2、Gauge2.1 Gauge2.2 GaugeVec3、Summary4、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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3