当前位置:网站首页>动手写prometheus的exporter-02-Counter(计数器)
动手写prometheus的exporter-02-Counter(计数器)
2022-08-10 05:16:00 【开发运维玄德公】
概述
- 特点:只增不减(除非系统发生重启,或者用户进程有异常)。
常见的监控指标如http_requests_total, node_cpu都是Counter类型的监控指标。
- 标记:一般以
_total
作为后缀。
1. 不带标签的Counter
1.1 定义指标
语法
- 实例化
func NewCounter(opts CounterOpts) Counter
- CounterOpts类型
type CounterOpts Opts
- Opts结构体
type Opts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels Labels
}
完整示例
- 代码
package main
import (
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
var (
numOfBuns = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "xi_shu",
Subsystem: "jing_zhou_team",
Name: "guanyu_eaten_num_all",
Help: "Number of steamed buns eaten by Guan Yu",
})
)
func init() {
prometheus.MustRegister(numOfBuns)
}
func main() {
flag.Parse()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}
- 结果显示
# HELP xi_shu_jing_zhou_team_guanyu_eaten_num Number of steamed buns eaten by Guan Yu
# TYPE xi_shu_jing_zhou_team_guanyu_eaten_num counter
xi_shu_jing_zhou_team_guanyu_eaten_num_all 0
1.2 获取数据
语法
- Add() 增加N
func (Counter) Add(float64)
- Inc() 增加1
func (Counter) Inc()
完整示例
package main
import (
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
var (
numOfBuns = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "xi_shu",
Subsystem: "jing_zhou_team",
Name: "guanyu_eaten_num_all",
Help: "Number of steamed buns eaten by Guan Yu",
})
)
func init() {
prometheus.MustRegister(numOfBuns)
}
func main() {
flag.Parse()
go func() {
for true {
numOfBuns.Add(2)
time.Sleep(time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}
- 结果显示
# HELP xi_shu_jing_zhou_team_guanyu_eaten_num_all Number of steamed buns eaten by Guan Yu
# TYPE xi_shu_jing_zhou_team_guanyu_eaten_num_all counter
xi_shu_jing_zhou_team_guanyu_eaten_num_all 74
2. 带标签的Counter
2.1 定义指标
语法
- 实例化
func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec
- CounterOpts类型
type CounterOpts Opts
- Opts结构体
type Opts struct {
Namespace string
Subsystem string
Name string
Help string
ConstLabels Labels
}
完整示例
package main
import (
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
var (
numOfBuns = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "xi_shu",
Subsystem: "jing_zhou_team",
Name: "eaten_num_all",
Help: "Number of steamed buns eaten by Guan Yu",
},[]string{
"name","age"})
)
func init() {
prometheus.MustRegister(numOfBuns)
}
func main() {
flag.Parse()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}
- 输出
带标签的指标,没有打标签之前,不会显示。
2.2 获取数据
语法
- Add() 增加N
func (Counter) Add(float64)
- Inc() 增加1
func (Counter) Inc()
完整示例
- 代码
package main
import (
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
var addr = flag.String("listen-address", ":1840", "The address to listen on for HTTP requests")
var (
numOfBuns = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "xi_shu",
Subsystem: "jing_zhou_team",
Name: "eaten_num_all",
Help: "Number of steamed buns eaten by Guan Yu",
},[]string{
"name","age"})
)
func init() {
prometheus.MustRegister(numOfBuns)
}
func main() {
flag.Parse()
go func() {
for true {
numOfBuns.WithLabelValues("guan_yu","25").Add(2)
numOfBuns.WithLabelValues("zhang_fei","22").Add(3)
time.Sleep(time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}
- 显示结果
# HELP xi_shu_jing_zhou_team_eaten_num Number of steamed buns eaten by Guan Yu
# TYPE xi_shu_jing_zhou_team_eaten_num gauge
xi_shu_jing_zhou_team_eaten_num{
age="22",name="zhang_fei"} 33
xi_shu_jing_zhou_team_eaten_num{
age="25",name="guan_yu"} 22
边栏推荐
- How does flinksql write that the value of redis has only the last field?
- Arduino框架下合宙ESP32C3 +1.8“tft 网络时钟
- 【论文笔记】Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation
- 深度学习之-01
- leetcode每天5题-Day11
- Ask you guys.The FlinkCDC2.2.0 version in the CDC community has a description of the supported sqlserver version, please
- FPGA engineer interview questions collection 11~20
- Abstract problem methodology
- Stacks and Queues | Valid parentheses, delete all adjacent elements in a string, reverse Polish expression evaluation, maximum sliding window, top K high frequency elements | leecode brush questions
- canvas 画布绘制时钟
猜你喜欢
随机推荐
FPGA工程师面试试题集锦1~10
Flutter development: error The following assertion was thrown resolving an image codec: Solution for Unable to...
flinkcdc 读取pgsql 的时间被放大了 有大佬知道咋回事吗 gmt_create':1
线性模型中的高级特征选择技术——基于R
leetcode每天5题-Day10
Rpc interface stress test
`id` bigint(20) unsigned NOT NULL COMMENT '数据库主键',
【论文笔记】Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation
SQL Server query optimization
oracle cdc时,设置并行度2插槽数1,最终任务只有一个tm,是不是因为oracle不支持并发
Arduino框架下合宙ESP32C3 +1.8“tft 网络时钟
Nexus_Warehouse Type
How does flinksql write that the value of redis has only the last field?
我用这一招让团队的开发效率提升了 100%!
Guys, is it normal that the oracle archive log grows by 3G in 20 minutes after running cdc?
FPGA engineer interview questions collection 31~40
Introduction to curl command
【Pei Shu Theorem】CF1055C Lucky Days
conda创建虚拟环境方法和pqi使用国内镜像源安装第三方库的方法教程
SQL Server查询优化