如何详细解析Prometheus搭建Exporter中间件步骤?
- 内容介绍
- 文章标签
- 相关推荐
本文共计406个文字,预计阅读时间需要2分钟。
Prometheus为开发者提供客户端工具,可用于开发自己的中间件Exporter,方便对接Prometheus。目前支持的客户端包括Go、Java、Python、Ruby等。以下以Go为例,开发自己的Exporter:
依赖包的引入:goimport ( github.com/prometheus/client_golang/prometheus net/http)
工程结构:[root@node1 d]├── main.go└── metrics.go
main.go:gopackage main
import ( log net/http github.com/prometheus/client_golang/prometheus)
func main() { http.Handle(/metrics, promhttp.Handler()) log.Fatal(http.ListenAndServe(:9115, nil))}
metrics.go:gopackage main
import ( github.com/prometheus/client_golang/prometheus)
var ( // 自定义指标 myGauge=prometheus.NewGauge(prometheus.GaugeOpts{ Name: my_gauge, Help: A gauge for my application, }))
func init() { prometheus.MustRegister(myGauge)}
func handler(w http.ResponseWriter, r *http.Request) { // 更新指标值 myGauge.Set(1.0) w.Write([]byte(ok))}
Prometheus 为开发这提供了客户端工具,用于为自己的中间件开发Exporter,对接Prometheus 。
目前支持的客户端
- Go
- Java
- Python
- Ruby
以go为例开发自己的Exporter
依赖包的引入
工程结构
[root@node1 data]# tree exporter/
exporter/
├── collector
│ └── node.go
├── go.mod
└── main.go
引入依赖包
require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/prometheus/client_golang v1.1.0 //借助gopsutil 采集主机指标 github.com/shirou/gopsutil v0.0.0-20190731134726-d80c43f9c984 )
main.go
package main import ( "cloud.io/exporter/collector" "fmt" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/prom127.0.0.1:8080/metrics
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计406个文字,预计阅读时间需要2分钟。
Prometheus为开发者提供客户端工具,可用于开发自己的中间件Exporter,方便对接Prometheus。目前支持的客户端包括Go、Java、Python、Ruby等。以下以Go为例,开发自己的Exporter:
依赖包的引入:goimport ( github.com/prometheus/client_golang/prometheus net/http)
工程结构:[root@node1 d]├── main.go└── metrics.go
main.go:gopackage main
import ( log net/http github.com/prometheus/client_golang/prometheus)
func main() { http.Handle(/metrics, promhttp.Handler()) log.Fatal(http.ListenAndServe(:9115, nil))}
metrics.go:gopackage main
import ( github.com/prometheus/client_golang/prometheus)
var ( // 自定义指标 myGauge=prometheus.NewGauge(prometheus.GaugeOpts{ Name: my_gauge, Help: A gauge for my application, }))
func init() { prometheus.MustRegister(myGauge)}
func handler(w http.ResponseWriter, r *http.Request) { // 更新指标值 myGauge.Set(1.0) w.Write([]byte(ok))}
Prometheus 为开发这提供了客户端工具,用于为自己的中间件开发Exporter,对接Prometheus 。
目前支持的客户端
- Go
- Java
- Python
- Ruby
以go为例开发自己的Exporter
依赖包的引入
工程结构
[root@node1 data]# tree exporter/
exporter/
├── collector
│ └── node.go
├── go.mod
└── main.go
引入依赖包
require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/prometheus/client_golang v1.1.0 //借助gopsutil 采集主机指标 github.com/shirou/gopsutil v0.0.0-20190731134726-d80c43f9c984 )
main.go
package main import ( "cloud.io/exporter/collector" "fmt" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/prom127.0.0.1:8080/metrics
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

