js css html

动手写prometheus的exporter-02-Counte

2022-08-22  本文已影响0人  玄德公笔记

概述

常见的监控指标如http_requests_total, node_cpu都是Counter类型的监控指标。

1. 不带标签的Counter

1.1 定义指标

语法

func NewCounter(opts CounterOpts) Counter
type CounterOpts 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 获取数据

语法

func (Counter) Add(float64)
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
type CounterOpts 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 获取数据

语法

func (Counter) Add(float64)
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

上一篇下一篇

猜你喜欢

热点阅读