prometheus exporter

本文深入探讨了Prometheus监控系统中的Exporter概念,包括其来源、运行方式及规范,详细介绍了社区提供的各种Exporter,并阐述了如何自定义Exporter以满足特定监控需求。

广义上讲所有可以向Prometheus提供监控样本数据的程序都可以被称为一个Exporter。而Exporter的一个实例称为target,如下所示,Prometheus通过轮训的方式定期从这些target中获取样本数据:

 

Exporter的来源

从Exporter的来源上来讲,主要分为两类:

  • 社区提供的

Prometheus社区提供了丰富的Exporter实现,涵盖了从基础设施,中间件以及网络等各个方面的监控功能。这些Exporter可以实现大部分通用的监控需求。下表列举一些社区中常用的Exporter:

范围常用Exporter
数据库MySQL Exporter, Redis Exporter, MongoDB Exporter, MSSQL Exporter等
硬件Apcupsd Exporter,IoT Edison Exporter, IPMI Exporter, Node Exporter等
消息队列Beanstalkd Exporter, Kafka Exporter, NSQ Exporter, RabbitMQ Exporter等
存储Ceph Exporter, Gluster Exporter, HDFS Exporter, ScaleIO Exporter等
HTTP服务Apache Exporter, HAProxy Exporter, Nginx Exporter等
API服务AWS ECS Exporter, Docker Cloud Exporter, Docker Hub Exporter, GitHub Exporter等
日志Fluentd Exporter, Grok Exporter等
监控系统Collectd Exporter, Graphite Exporter, InfluxDB Exporter, Nagios Exporter, SNMP Exporter等
其它Blockbox Exporter, JIRA Exporter, Jenkins Exporter, Confluence Exporter等
  • 用户自定义的

除了直接使用社区提供的Exporter程序以外,用户还可以基于Prometheus提供的Client Library创建自己的Exporter程序,目前Promthues社区官方提供了对以下编程语言的支持:Go、Java/Scala、Python、Ruby。同时还有第三方实现的如:Bash、C++、Common Lisp、Erlang,、Haskeel、Lua、Node.js、PHP、Rust等。

Exporter的运行方式:

从Exporter的运行方式上来讲,又可以分为:

  • 独立使用的
  • 集成到应用中的

Exporter规范:

所有的Exporter程序都需要按照Prometheus的规范,返回监控的样本数据。以Node Exporter为例,当访问/metrics地址时会返回以下内容:

# HELP node_cpu Seconds the cpus spent in each mode.
# TYPE node_cpu counter
node_cpu{cpu="cpu0",mode="idle"} 362812.7890625
# HELP node_load1 1m load average.
# TYPE node_load1 gauge
node_load1 3.0703125

Exporter返回的样本数据,主要由三个部分组成:样本的一般注释信息(HELP),样本的类型注释信息(TYPE)和样本。

# HELP  当前的指标名称以及相应的说明信息

# TYPE  当前的指标名称以及指标类型:

TYPE注释行必须出现在指标的第一个样本之前。如果没有明确的指标类型需要返回为untyped

除了# 开头的所有行都会被视为是监控样本数据。

每一行样本需要满足以下格式规范:

metric_name [
  "{" label_name "=" `"` label_value `"` { "," label_name "=" `"` label_value `"` } [ "," ] "}"
] value [ timestamp ]

其中metric_name和label_name必须遵循PromQL的格式规范要求

需要特别注意的是对于histogram和summary类型的样本。需要按照以下约定返回样本数据:

  • 类型为summary或者histogram的指标x,该指标所有样本的值的总和需要使用一个单独的x_sum指标表示。
  • 类型为summary或者histogram的指标x,该指标所有样本的总数需要使用一个单独的x_count指标表示。

  • 对于类型为summary的指标x,其不同分位数quantile所代表的样本,需要使用单独的x{quantile="y"}表示。

  • 对于类型histogram的指标x为了表示其样本的分布情况,每一个分布需要使用x_bucket{le="y"}表示,其中y为当前分布的上位数。同时必须包含一个样本x_bucket{le="+Inf"},并且其样本值必须和x_count相同。
  • 对于histogram和summary的样本,必须按照分位数quantile和分布le的值的递增顺序排序。

以下是类型为histogram和summary的样本输出示例:

# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320

# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693

对于某些Promtheus还没有提供支持的编程语言,用户只需要按照以上规范返回响应的文本数据即可。

 

 

 

 

 

 

Prometheus exporter是一种将应用程序指标公开为Prometheus所能够接受的格式的服务。它是由Prometheus社区维护的开放源代码软件。可以使用各种编程语言和库来编写Exporter,以便在Prometheus上监视应用程序的所有方面。Exporter将应用程序的度量标准转换为Prometheus所需的格式,使得Prometheus能够定期抓取度量标准,进行存储和查询。以下是使用Python编写Prometheus exporter的一个例子: ```python from prometheus_client import start_http_server, Metric, REGISTRY import random import time class CustomCollector(object): def __init__(self): pass def collect(self): # 模拟获取应用程序指标 metric = Metric('custom_metric', 'Custom metric description', 'gauge') metric.add_sample('custom_metric', value=random.randint(0, 10), labels={}) yield metric if __name__ == '__main__': start_http_server(8000) REGISTRY.register(CustomCollector()) while True: time.sleep(1) ``` 上述Python代码中,我们定义了一个名为CustomCollector的类来生成我们自己的指标。稍后,我们将该类注册到Prometheus的默认注册表中。在collect()函数中,我们模拟生成一个名为custom_metric的指标。我们使用gauge类型来定义它,并添加了一些随机值。在主函数中,我们启动了一个HTTP服务器,监听端口号8000。然后,我们注册我们的CustomCollector类,并定期地生成一些随机的度量标准,以便Prometheus可以抓取和存储。最后,我们进入了一个无限循环,防止程序退出,以便我们可以继续生成度量标准。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值