使用golang編寫prometheus metrics exporter
metrcis輸出
collector.go
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
//Define a struct for you collector that contains pointers
//to prometheus descriptors for each metric you wish to expose.
//Note you can also include fields of other types if they provide utility
//but we just won't be exposing them as metrics.
type fooCollector struct {
fooMetric *prometheus.Desc
barMetric *prometheus.Desc
}
//You must create a constructor for you collector that
//initializes every descriptor and returns a pointer to the collector
func newFooCollector() *fooCollector {
return &fooCollector{
fooMetric: prometheus.NewDesc("foo_metric",
"Shows whether a foo has occurred in our cluster",
nil, nil,
),
barMetric: prometheus.NewDesc("bar_metric",
"Shows whether a bar has occurred in our cluster",
nil, nil,
),
}
}
//Each and every collector must implement the Describe function.
//It essentially writes all descriptors to the prometheus desc channel.
func (collector *fooCollector) Describe(ch chan<- *prometheus.Desc) {
//Update this section with the each metric you create for a given collector
ch <- collector.fooMetric
ch <- collector.barMetric
}
//Collect implements required collect function for all promehteus collectors
func (collector *fooCollector) Collect(ch chan<- prometheus.Metric) {
//Implement logic here to determine proper metric value to return to prometheus
//for each descriptor or call other functions that do so.
var metricValue float64
if 1 == 1 {
metricValue = 1
}
//Write latest value for each metric in the prometheus metric channel.
//Note that you can pass CounterValue, GaugeValue, or UntypedValue types here.
ch <- prometheus.MustNewConstMetric(collector.fooMetric, prometheus.CounterValue, metricValue)
ch <- prometheus.MustNewConstMetric(collector.barMetric, prometheus.CounterValue, metricValue)
}
http輸出
main.go
package main
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
//Create a new instance of the foocollector and
//register it with the prometheus client.
foo := newFooCollector()
prometheus.MustRegister(foo)
//This section will start the HTTP server and expose
//any metrics on the /metrics endpoint.
http.Handle("/metrics", promhttp.Handler())
log.Info("Beginning to serve on port :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
單檔案
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
//Define a struct for you collector that contains pointers
//to prometheus descriptors for each metric you wish to expose.
//Note you can also include fields of other types if they provide utility
//but we just won't be exposing them as metrics.
type fooCollector struct {
fooMetric *prometheus.Desc
barMetric *prometheus.Desc
}
//You must create a constructor for you collector that
//initializes every descriptor and returns a pointer to the collector
func newFooCollector() *fooCollector {
return &fooCollector{
fooMetric: prometheus.NewDesc("fff_metric",
"Shows whether a foo has occurred in our cluster",
nil, nil,
),
barMetric: prometheus.NewDesc("bbb_metric",
"Shows whether a bar has occurred in our cluster",
nil, nil,
),
}
}
//Each and every collector must implement the Describe function.
//It essentially writes all descriptors to the prometheus desc channel.
func (collector *fooCollector) Describe(ch chan<- *prometheus.Desc) {
//Update this section with the each metric you create for a given collector
ch <- collector.fooMetric
ch <- collector.barMetric
}
//Collect implements required collect function for all promehteus collectors
func (collector *fooCollector) Collect(ch chan<- prometheus.Metric) {
//Implement logic here to determine proper metric value to return to prometheus
//for each descriptor or call other functions that do so.
var metricValue float64
if 1 == 1 {
metricValue = 1
}
//Write latest value for each metric in the prometheus metric channel.
//Note that you can pass CounterValue, GaugeValue, or UntypedValue types here.
ch <- prometheus.MustNewConstMetric(collector.fooMetric, prometheus.CounterValue, metricValue)
ch <- prometheus.MustNewConstMetric(collector.barMetric, prometheus.CounterValue, metricValue)
}
func main() {
//Create a new instance of the foocollector and
//register it with the prometheus client.
foo := newFooCollector()
prometheus.MustRegister(foo)
//This section will start the HTTP server and expose
//any metrics on the /metrics endpoint.
http.Handle("/metrics", promhttp.Handler())
log.Info("Beginning to serve on port :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
相關文章
- Prometheus 整合 Node ExporterPrometheusExport
- Prometheus 新增 node_exporterPrometheusExport
- Prometheus之Exporter開發PrometheusExport
- 實現一個Prometheus exporterPrometheusExport
- prometheus node-exporter安裝PrometheusExport
- prometheus的程序監控process-exporterPrometheusExport
- Prometheus監控系統程序---process-exporterPrometheusExport
- Docker系列——Grafana+Prometheus+Node-exporter微信推送(三)DockerGrafanaPrometheusExport
- 使用Golang編寫優化演算法 (1)Golang優化演算法
- Docker系列——Grafana+Prometheus+Node-exporter釘釘推送(四)DockerGrafanaPrometheusExport
- Golang 編寫測試教程Golang
- 使用Golang語言編寫Hello World Web應用GolangWeb
- 【Prometheus學習筆記】主機監控 -node_exporterPrometheus筆記Export
- linux視覺化監控:Grafana+Prometheus+node_exporterLinux視覺化GrafanaPrometheusExport
- Golang語言之Prometheus的日誌模組使用案例GolangPrometheus
- 使用Golang的Gin框架和vue編寫web應用Golang框架VueWeb
- Golang 編寫 Tcp 伺服器GolangTCP伺服器
- k8s中使用prometheus operator監控外部伺服器部署的windows exporterK8SPrometheus伺服器WindowsExport
- 基於 RocketMQ Prometheus Exporter 打造定製化 DevOps 平臺MQPrometheusExportdev
- Docker系列——Grafana+Prometheus+Node-exporter伺服器告警中心(二)DockerGrafanaPrometheusExport伺服器
- Prometheus監控之SNMP Exporter介紹和資料展現PrometheusExport
- 使用Prometheus監控Golang服務-基於YoyoGo框架PrometheusGolang框架
- Prometheus之Dockerfile編寫、映象構建、容器啟動PrometheusDocker
- Docker部署PostgreSQL14.1以及postgres_exporter+prometheus+grafana監控DockerSQLExportPrometheusGrafana
- Prometheus和node exporter的安裝與監控資料說明PrometheusExport
- 記-Windows環境下Prometheus+alertmanager+windows_exporter+mtail監控部署WindowsPrometheusExportAI
- SSH Exporter:基於Prometheus的遠端系統效能監控神器ExportPrometheus
- 從零開始寫一個ExporterExport
- Docker系列——Grafana+Prometheus+Node-exporter伺服器監控平臺(一)DockerGrafanaPrometheusExport伺服器
- prometheus監控golang服務實踐PrometheusGolang
- golang編寫一個window定時關機Golang
- 用 GoLang 編寫類似 Apache Camel 路由引擎GolangApache路由
- 雲原生時代如何用 Prometheus 實現效能壓測可觀測-Metrics 篇Prometheus
- golang如何優雅的編寫事務程式碼Golang
- prometheus 針對blackbox exporter 設定P90,P95指標PrometheusExport指標
- 用Golang寫爬蟲(六) - 使用collyGolang爬蟲
- 使用 golang 寫一個 redis-cliGolangRedis
- Prometheus Metrics 設計的最佳實踐和應用例項,看這篇夠了!Prometheus