最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang自定義開發(fā)Prometheus?exporter詳解

 更新時間:2023年06月15日 16:30:16   作者:臺灣省委書記  
Exporter是基于Prometheus實(shí)施的監(jiān)控系統(tǒng)中重要的組成部分,承擔(dān)數(shù)據(jù)指標(biāo)的采集工作,這篇文章主要為大家介紹了如何自定義編寫開發(fā)?Prometheus?exporter,感興趣的可以了解一下

1.介紹

Exporter是基于Prometheus實(shí)施的監(jiān)控系統(tǒng)中重要的組成部分,承擔(dān)數(shù)據(jù)指標(biāo)的采集工作,官方的exporter列表中已經(jīng)包含了常見的絕大多數(shù)的系統(tǒng)指標(biāo)監(jiān)控,比如用于機(jī)器性能監(jiān)控的node_exporter, 用于網(wǎng)絡(luò)設(shè)備監(jiān)控的snmp_exporter等等。這些已有的exporter對于監(jiān)控來說,僅僅需要很少的配置工作就能提供完善的數(shù)據(jù)指標(biāo)采集。

prometheus四種類型的指標(biāo)Counter 計數(shù),Gauge 觀測類,Histogram 直方,Summary 摘要 用golang語言如何構(gòu)造這4種類型對應(yīng)的指標(biāo),二是搞清楚修改指標(biāo)值的場景和方式。

不帶label的基本例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    cpuUsage := prometheus.NewGauge(prometheus.GaugeOpts{
        Name:        "cpu_usage",                              // 指標(biāo)名稱
        Help:        "this is test metrics cpu usage",         // 幫助信息

    })
    // 給指標(biāo)設(shè)置值
    cpuUsage.Set(29.56)
    // 注冊指標(biāo)
    prometheus.MustRegister(cpuUsage)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶有固定label指標(biāo)的例子

帶有非固定label指標(biāo)的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    //定義帶有不固定label的指標(biāo)
    mtu := prometheus.NewGaugeVec(prometheus.GaugeOpts{
        Name: "interface_mtu",
        Help: "網(wǎng)卡接口MTU",
    }, []string{"interface", "Machinetype"})

    // 給指標(biāo)設(shè)置值
    mtu.WithLabelValues("lo", "host").Set(1500)
    mtu.WithLabelValues("ens32", "host").Set(1500)
    mtu.WithLabelValues("eth0", "host").Set(1500)

    // 注冊指標(biāo)
    prometheus.MustRegister(mtu)

    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

2. Counter指標(biāo)類型

不帶label的基本例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqTotal := prometheus.NewCounter(prometheus.CounterOpts{
        Name: "current_request_total",
        Help: "當(dāng)前請求總數(shù)",
    })
    // 注冊指標(biāo)
    prometheus.MustRegister(reqTotal)

    // 設(shè)置值
    reqTotal.Add(10)

    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶有固定label指標(biāo)的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    suceReqTotal := prometheus.NewCounter(prometheus.CounterOpts{
        Name:        "sucess_request_total",
        Help:        "請求成功的總數(shù)",
        ConstLabels: prometheus.Labels{"StatusCode": "200"},
    })
    // 注冊指標(biāo)
    prometheus.MustRegister(suceReqTotal)

    // 設(shè)置值
    suceReqTotal.Add(5675)

    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶有非固定label指標(biāo)的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    pathReqTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
        Name: "path_request_total",
        Help: "path請求總數(shù)",
    }, []string{"path"})
    // 注冊指標(biāo)
    prometheus.MustRegister(pathReqTotal)

    // 設(shè)置值
    pathReqTotal.WithLabelValues("/token").Add(37)
    pathReqTotal.WithLabelValues("/auth").Add(23)
    pathReqTotal.WithLabelValues("/user").Add(90)
    pathReqTotal.WithLabelValues("/api").Add(67)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

3. Histogram指標(biāo)類型

不帶label的基本例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqDelay := prometheus.NewHistogram(prometheus.HistogramOpts{
        Name:    "request_delay",
        Help:    "請求延遲,單位秒",
        Buckets: prometheus.LinearBuckets(0, 3, 6), // 調(diào)用LinearBuckets生成區(qū)間,從0開始,寬度3,共6個Bucket
    })

    // 注冊指標(biāo)
    prometheus.MustRegister(reqDelay)

    // 設(shè)置值
    reqDelay.Observe(6)

    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶固定label的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqDelay := prometheus.NewHistogram(prometheus.HistogramOpts{
        Name:        "request_delay",
        Help:        "請求延遲,單位秒",
        Buckets:     prometheus.LinearBuckets(0, 3, 6), // 調(diào)用LinearBuckets生成區(qū)間,從0開始,寬度3,共6個Bucket
        ConstLabels: prometheus.Labels{"path": "/api"},
    })

    // 注冊指標(biāo)
    prometheus.MustRegister(reqDelay)

    // 設(shè)置值
    reqDelay.Observe(6)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶有非固定label的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqDelay := prometheus.NewHistogramVec(prometheus.HistogramOpts{
        Name:    "request_delay",
        Help:    "請求延遲,單位秒",
        Buckets: prometheus.LinearBuckets(0, 3, 6), // 調(diào)用LinearBuckets生成區(qū)間,從0開始,寬度3,共6個Bucket
    }, []string{"path"})

    // 注冊指標(biāo)
    prometheus.MustRegister(reqDelay)

    // 設(shè)置值
    reqDelay.WithLabelValues("/api").Observe(6)
    reqDelay.WithLabelValues("/user").Observe(3)
    reqDelay.WithLabelValues("/delete").Observe(2)
    reqDelay.WithLabelValues("/get_token").Observe(13)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

4.Summary指標(biāo)類型

不帶label的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqDelay := prometheus.NewSummary(prometheus.SummaryOpts{
        Name:       "request_delay",
        Help:       "請求延遲",
        Objectives: map[float64]float64{0.5: 0.05, 0.90: 0.01, 0.99: 0.001}, // 百分比:精度
    })

    // 注冊指標(biāo)
    prometheus.MustRegister(reqDelay)

    // 設(shè)置值
    reqDelay.Observe(4)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶有固定label的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqDelay := prometheus.NewSummary(prometheus.SummaryOpts{
        Name:        "request_delay",
        Help:        "請求延遲",
        Objectives:  map[float64]float64{0.5: 0.05, 0.90: 0.01, 0.99: 0.001}, // 百分比:精度
        ConstLabels: prometheus.Labels{"path": "/api"},
    })

    // 注冊指標(biāo)
    prometheus.MustRegister(reqDelay)

    // 設(shè)置值
    reqDelay.Observe(4)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

帶有非固定label的例子

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    // 定義指標(biāo)
    reqDelay := prometheus.NewSummaryVec(prometheus.SummaryOpts{
        Name:       "request_delay",
        Help:       "請求延遲",
        Objectives: map[float64]float64{0.5: 0.05, 0.90: 0.01, 0.99: 0.001}, // 百分比:精度
    }, []string{"path"})

    // 注冊指標(biāo)
    prometheus.MustRegister(reqDelay)

    // 設(shè)置值
    reqDelay.WithLabelValues("/api").Observe(4)
    reqDelay.WithLabelValues("/token").Observe(2)
    reqDelay.WithLabelValues("/user").Observe(3)
    // 暴露指標(biāo)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

5. 值的修改

5.1 基于事件的觸發(fā)來修改值,比如每訪問1次/api就增1

基于事件的觸發(fā)對指標(biāo)的值進(jìn)行修改,通常大多數(shù)是來自業(yè)務(wù)方面的指標(biāo)需求,如自研的應(yīng)用需要暴露相關(guān)指標(biāo)給promethus進(jìn)行監(jiān)控、展示,那么指標(biāo)采集的代碼(指標(biāo)定義、設(shè)置值)就要嵌入到自研應(yīng)用代碼里了。

package main

import (
    "fmt"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "net/http"
)

func main() {
    urlRequestTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
        Name: "urlRequestTotal",
        Help: "PATH請求累計 單位 次",
    }, []string{"path"})

    http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
        urlRequestTotal.WithLabelValues(r.URL.Path).Inc() // 使用Inc函數(shù)進(jìn)行增1
        fmt.Fprintf(w, "Welcome to the api")
    })

    prometheus.MustRegister(urlRequestTotal)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

基于時間周期的觸發(fā)來修改值,比如下面的示例中,是每間隔1秒獲取cpu負(fù)載指標(biāo)

基于時間周期的觸發(fā),可以是多少秒、分、時、日、月、周。

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "github.com/shirou/gopsutil/load"
    "net/http"
    "time"
)

func main() {
    cpuUsage := prometheus.NewGaugeVec(prometheus.GaugeOpts{
        Name: "CpuLoad",
        Help: "CPU負(fù)載",
    }, []string{"time"})

    // 開啟一個子協(xié)程執(zhí)行該匿名函數(shù)內(nèi)的邏輯來給指標(biāo)設(shè)置值,且每秒獲取一次
    go func() {
        for range time.Tick(time.Second) {
            info, _ := load.Avg()
            cpuUsage.WithLabelValues("Load1").Set(float64(info.Load1))
            cpuUsage.WithLabelValues("Load5").Set(float64(info.Load5))
            cpuUsage.WithLabelValues("Load15").Set(float64(info.Load15))
        }
    }()

    prometheus.MustRegister(cpuUsage)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

基于每訪問一次獲取指標(biāo)的URI才修改值,比如每次訪問/metrics才去修改某些指標(biāo)的值

下面的這個示例是,每訪問一次/metrics就獲取一次內(nèi)存總?cè)萘?/p>

package main

import (
    "fmt"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"

    "github.com/shirou/gopsutil/mem"
    "net/http"
)

func main() {
    MemTotal := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
        Name: "MemTotal",
        Help: "內(nèi)存總?cè)萘?單位 GB",
    }, func() float64 {
        fmt.Println("call MemTotal ...")
        info, _ := mem.VirtualMemory()
        return float64(info.Total / 1024 / 1024 / 1024)
    })
    prometheus.MustRegister(MemTotal)
    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe("localhost:9100", nil)
}

到此這篇關(guān)于Golang自定義開發(fā)Prometheus exporter詳解的文章就介紹到這了,更多相關(guān)Golang Prometheus exporter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go外部依賴包從vendor,$GOPATH和$GOPATH/pkg/mod查找順序

    Go外部依賴包從vendor,$GOPATH和$GOPATH/pkg/mod查找順序

    這篇文章主要介紹了Go外部依賴包vendor,$GOPATH和$GOPATH/pkg/mod下查找順序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本詳解

    golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本詳解

    這篇文章主要給大家介紹了關(guān)于golang如何實(shí)現(xiàn)mapreduce單進(jìn)程版本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • golang將切片或數(shù)組根據(jù)某個字段進(jìn)行分組操作

    golang將切片或數(shù)組根據(jù)某個字段進(jìn)行分組操作

    這篇文章主要介紹了golang將切片或數(shù)組根據(jù)某個字段進(jìn)行分組操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 基于Go語言實(shí)現(xiàn)Base62編碼的三種方式以及對比分析

    基于Go語言實(shí)現(xiàn)Base62編碼的三種方式以及對比分析

    Base62 編碼是一種在字符編碼中使用62個字符的編碼方式,在計算機(jī)科學(xué)中,,Go語言是一種靜態(tài)類型、編譯型語言,它由Google開發(fā)并開源,本文給大家介紹了Go語言實(shí)現(xiàn)Base62編碼的三種方式以及對比分析,需要的朋友可以參考下
    2025-05-05
  • Go語言學(xué)習(xí)之操作MYSQL實(shí)現(xiàn)CRUD

    Go語言學(xué)習(xí)之操作MYSQL實(shí)現(xiàn)CRUD

    Go官方提供了database包,database包下有sql/driver。該包用來定義操作數(shù)據(jù)庫的接口,這保證了無論使用哪種數(shù)據(jù)庫,操作方式都是相同的。本文就來和大家聊聊Go語言如何操作MYSQL實(shí)現(xiàn)CRUD,希望對大家有所幫助
    2023-02-02
  • Go語言死鎖與goroutine泄露問題的解決

    Go語言死鎖與goroutine泄露問題的解決

    最近在工作中使用golang編程,今天的文章給大家分享一下Go語言死鎖與goroutine泄露問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Golang與其他語言不同的九個特性

    Golang與其他語言不同的九個特性

    近來關(guān)于對Golang的討論有很多,七牛的幾個大牛們也斷定Go語言在未來將會快速發(fā)展,并且很可能會取代Java成為互聯(lián)網(wǎng)時代最受歡迎的編程語言。本文將帶你了解它不同于其他語言的九個特性
    2021-09-09
  • Golang 語言高效使用字符串的方法

    Golang 語言高效使用字符串的方法

    這篇文章主要介紹了Golang 語言高效使用字符串的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Go語言hello world實(shí)例

    Go語言hello world實(shí)例

    這篇文章主要介紹了Go語言hello world實(shí)例,本文先是給出了hello world的代碼實(shí)例,然后對一些知識點(diǎn)和技巧做了解釋,需要的朋友可以參考下
    2014-10-10
  • Go語言reflect.TypeOf()和reflect.Type通過反射獲取類型信息

    Go語言reflect.TypeOf()和reflect.Type通過反射獲取類型信息

    這篇文章主要介紹了Go語言reflect.TypeOf()和reflect.Type通過反射獲取類型信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評論

福泉市| 吉木乃县| 孙吴县| 万全县| 泽库县| 玉林市| 乐陵市| 西充县| 民权县| 黑山县| 云龙县| 宾川县| 嘉义市| 邢台县| 平阳县| 高碑店市| 寿阳县| 保定市| 光泽县| 惠水县| 若尔盖县| 环江| 二连浩特市| 万载县| 丰都县| 连山| 贵德县| 罗江县| 常德市| 田林县| 广东省| 南汇区| 西乡县| 绿春县| 枝江市| 乌兰察布市| 内丘县| 应城市| 长治县| 景德镇市| 万盛区|