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))
}

使用golang编写prometheus metrics exporter的更多相关文章

  1. Golang 基于Prometheus Node_Exporter 开发自定义脚本监控

    Golang 基于Prometheus Node_Exporter 开发自定义脚本监控 公司是今年决定将一些传统应用从虚拟机上迁移到Kubernetes上的,项目多而乱,所以迁移工作进展缓慢,为了建立 ...

  2. Prometheus Metrics 设计的最佳实践和应用实例,看这篇够了!

    Prometheus 是一个开源的监控解决方案,部署简单易使用,难点在于如何设计符合特定需求的 Metrics 去全面高效地反映系统实时状态,以助力故障问题的发现与定位.本文即基于最佳实践的 Metr ...

  3. goaccess 通过jsonpath 转换为prometheus metrics

    goaccess 是一个不错的日志分析工具,包含了json 数据同时支持基于websocket 的实时数据处理,当然我们可以通过jsonpath 的exporter 转换为支持promethues 的 ...

  4. 使用haproxy 2.0 prometheus metrics 监控系统状态

    haproxy 2.0 已经发布一段时间了,提供内部直接暴露的prometheus metrics 很方便 ,可以快速的监控系统的状态 以下是一个简单的demo 环境准备 docker-compose ...

  5. [转帖]prometheus数据采集exporter全家桶

    prometheus数据采集exporter全家桶 Rainbowhhy1人评论2731人阅读2019-04-06 15:38:32 https://blog.51cto.com/13053917/2 ...

  6. Prometheus之Exporter开发

    Prometheus开发Exporter简介 Exporter 本身是一个http 服务,其指标结果只要符合 Prometheus 规范就可以被 Prometheus 使用. Prometheus中m ...

  7. Golang编写动态库实现回调函数

    Golang编写动态库实现回调函数 我们现在要做一个动态库,但是C++实在是比较难,于是就想能不能用更简单的golang来实现,golang也就是最近的版本才支持编译成动态库,在网上也没找到可用的案例 ...

  8. 【开源监控】Prometheus+Node Exporter+Grafana监控linux服务器

    Prometheus Prometheus介绍 Prometheus新一代开源监控解决方案.github地址 Prometheus主要功能 多维 数据模型(时序由 metric 名字和 k/v 的 l ...

  9. Prometheus + Node Exporter + Grafana 监控主机运行信息

      上一篇文章中讲了如何利用Prometheus和Grafana监控SpringBoot应用的JVM信息,这次就来看看如何监控 服务器运行状态,先列出用到的工具: Prometheus node_ex ...

随机推荐

  1. docker-compose 在线安装升级

    参考:https://docs.docker.com/compose/install/ curl -L "https://github.com/docker/compose/releases ...

  2. 【微信公众号开发】【10】JSJDK相关

    前言: 1,优点:官方提供的,会调用后还算使用方便,不用费劲了解各个原生组件 缺点:使用上有限制(如:上传文件有大小限制),很容易踩坑,部分安卓手机及电脑端不支持pjax 总结:上手容易,坑很多 2, ...

  3. MySQL 处理海量数据时一些优化查询速度方法

    1.应尽量避免在where子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在where及order by设计的列上建立 ...

  4. 常用加密算法简单整理以及spring securiy使用bcrypt加密

    一.哈希加密 1.md5加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版) https://baike.baidu.com/item/MD5/212708?f ...

  5. 【转】分享前端开发中通过js设置/获取cookie的一组方法

    在前端开发中,通常都需要获取并记录用户的某些操作设置,这样可以使用户下一次访问网站时不用进行重复的调整设置同一个功能. js方法的完整代码如下: var cookie = { set:function ...

  6. 3.Liunx网络管理命令

    大纲: 1.网络信息:hostname.netstat.ifconfig ,route 2.网络配置:netconfig 3.网络测试:ping

  7. iOS 强大第三方资源库

    Github用法 git-recipesGit recipes in Chinese. 高质量的Git中文教程. lark怎样在Github上面贡献代码 my-git有关 git 的学习资料 giti ...

  8. python 豆瓣验证码识别总结

    总结:  pytesseract 识别比较标准的图片  识别成功率   还是不错的. 验证码的图片识别 需要先处理好   再用pytesseract 识别 from PIL import Image  ...

  9. Centos 7.4 源码 Nginx 安装

    一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel 二.首先要安装 PCRE ...

  10. Java IO流01-总叙

     Java IO包体系结构图: 1.流式部分――IO的主体部分: 2.非流式部分——主要包含一些辅助流式部分的类,如:File类.RandomAccessFile类和FileDescriptor等类: ...