博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用golang编写prometheus metrics exporter
阅读量:6815 次
发布时间:2019-06-26

本文共 4824 字,大约阅读时间需要 16 分钟。

metrcis输出

collector.go

package mainimport (	"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 collectorfunc 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 collectorsfunc (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 mainimport (  "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 mainimport (	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 collectorfunc 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 collectorsfunc (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))}

转载地址:http://fadzl.baihongyu.com/

你可能感兴趣的文章
对react中setState的总结
查看>>
[回炉计划]-实现一个图片预加载
查看>>
正则表达式
查看>>
360前端星计划学习-html
查看>>
专注dApp高效执行和高并发的下一代公有链
查看>>
ONE-sys 整合前后端脚手架 koa2 + pm2 + vue-cli3.0 + element
查看>>
携带更方便功能全 iPone与Apple Watch球形尿袋
查看>>
行为型模式:策略模式
查看>>
实现批量数据增强 | keras ImageDataGenerator使用
查看>>
太忙女友消息未及时回复,分手吗?python微信自动消息帮你谈恋爱
查看>>
Java 多线程NIO学习
查看>>
命名实体识别
查看>>
动态切换的动态代理
查看>>
电商项目(下)
查看>>
vue 数字滚动递增效果
查看>>
vue2.0中父子,兄弟组件的传值2
查看>>
Spring Boot注解常用!!!看了就可以开发大量项目了
查看>>
音频编码 Audio Converter
查看>>
SQL - case when then else end 的用法
查看>>
web优化是http缓存(上)
查看>>