golang定义prometheus exporter

素白流殇 · · 1078 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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)

}


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:素白流殇

查看原文:golang定义prometheus exporter

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1078 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传