美文网首页js css html
动手写prometheus的exporter-02-Counte

动手写prometheus的exporter-02-Counte

作者: 玄德公笔记 | 来源:发表于2022-08-22 21:02 被阅读0次

概述

  • 特点:只增不减(除非系统发生重启,或者用户进程有异常)。

常见的监控指标如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

相关文章

网友评论

    本文标题:动手写prometheus的exporter-02-Counte

    本文链接:https://www.haomeiwen.com/subject/bnfbgrtx.html