OpenTelemetry是一套API、SDK、工具和集成,旨在创建和管理遥测数据,如跟踪、度量和日志。
OpenTelemetry(简称OTel)的主要组件之一是OpenTelemetry Collector。OpenTelemetry Collector,或称Otel Collector,是一个与厂商无关的代理,可以接收、处理和输出遥测数据。它支持接收多种格式的遥测数据(如OTLP、Jaeger、Prometheus以及许多商业/专有工具)并将数据发送到一个或多个后端。
采集器是一种强大的方式,可以从多个来源接收数据,对其进行转换,并将其发送到你喜欢的目的地。在这里,我们将研究如何用OpenTelemetry采集器刮取Prometheus指标,并将其发送到Grafana Cloud这样的远程写入目的地。
如何在Grafana Cloud中设置OpenTelemetry
为了收集指标,应用程序应该通过http以Prometheus或OpenMetrics格式公开他们的指标。对于不能这样做的应用,有一些导出器可以以正确的格式导出指标。
在我们的例子中,我将在Ubuntu服务器上设置Node Exporter。你也可以下载二进制文件并在链接的指南中进行设置:
$ apt install prometheus-node-exporter
现在应该运行node-exporter,在http://localhost:9100/metrics,暴露Linux节点的指标。
你可以通过运行查看被暴露的指标:
$ curl localhost:9100/metrics
创建一个Grafana云账户
你还需要一个远程端点来发送和查看你的指标,而存储Prometheus指标的最简单的方法之一就是使用Grafana Cloud。如果你已经有一个远程端点和凭证,你可以跳过这一步。
如果你没有账户,你可以轻松注册一个免费的Grafana Cloud账户。一旦你创建了你的账户,你就会被送入一个Grafana实例,这将帮助你开始发送指标。
向下滚动并选择Prometheus选项。
在配置选项中,选择From my local Prometheus server> 然后Send metrics from a single Prometheus instance> enter name for the API Key, as shown:
点击创建API密钥。这样你就可以得到配置了。请复制它并保存好。
现在,当我们使用OpenTelemetry Collector时,它作为一个单一的Prometheus服务器,具有相同的配置,这就是我们选择上述选项的原因。
如何配置OpenTelemetry收集器
现在我们有了可以收集指标的东西,也有了可以发送指标的地方,让我们继续设置OTel采集器。
你可以通过以下链接下载大多数架构/操作系统的二进制文件或发行版: https://github.com/open-telemetry/opentelemetry-collector-releases/releases/tag/v0.48.0
在本指南中,我使用Ubuntu作为一个例子:
$ wget
https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.46.0/otelcol_0.48.0_linux_amd64.deb
$ dpkg -i otelcol_0.48.0_linux_amd64.deb
让我们首先通过检查日志来检查收集器是否成功运行:
$ journalctl -u otelcol.service -f
这将显示收集器的日志,它应该类似于以下内容。

现在让我们通过查看配置文件来弄清楚所有的日志是关于什么的:
/etc/otelcol/config.yaml
$ cat /etc/otelcol/config.yaml
extensions:
health_check:
pprof:
endpoint: 0.0.0.0:1777
zpages:
endpoint: 0.0.0.0:55679
receivers:
otlp:
protocols:
grpc:
http:
opencensus:
# Collect own metrics
prometheus:
config:
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 10s
static_configs:
- targets: ['0.0.0.0:8888']
jaeger:
protocols:
grpc:
thrift_binary:
thrift_compact:
thrift_http:
zipkin:
processors:
batch:
exporters:
logging:
logLevel: debug
service:
pipelines:
traces:
receivers: [otlp, opencensus, jaeger, zipkin]
processors: [batch]
exporters: [logging]
metrics:
receivers: [otlp, opencensus, prometheus]
processors: [batch]
exporters: [logging]
extensions: [health_check, pprof, zpages]
所以Otel收集器的管道有3个步骤。
接收器 -> 处理器 -> 输出器
它通过接收器收集遥测数据,并在处理器阶段应用转换,然后通过出口商将其发送到各种输出。
你可以在配置文件末尾的管道部分看到管道的最终定义。默认的配置有2条管道,一条用于追踪,一条用于度量。而度量衡管道看起来是这样的:
metrics:
receivers: [otlp, opencensus, prometheus]
processors: [batch]
exporters: [logging]
这基本上意味着我们可以通过OTLP、OpenCensus和Prometheus接收度量,批处理程序将几个度量分批在一起,然后发送给日志出口商(基本上是记录到stdout)。
鉴于我们不需要任何跟踪、OTLP或OpenCensus,我们可以将配置简化为以下内容:
receivers:
# Collect own metrics
prometheus:
config:
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 10s
static_configs:
- targets: ['0.0.0.0:8888']
processors:
batch:
exporters:
logging:
logLevel: debug
service:
pipelines:
metrics:
receivers: [prometheus]
processors: [batch]
exporters: [logging]
一旦你将/etc/otelcol/config.yaml 的配置文件更新为上述配置,让我们重新启动收集器以确保一切正常 :)
$ systemctl restart otelcol.service
$ systemctl status otelcol.service
现在已经完成了,让我们看看Prometheus的配置:
prometheus:
config:
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 10s
static_configs:
- targets: ['0.0.0.0:8888']
普罗米修斯接收器支持开箱即用的普罗米修斯配置文件,只有很少的限制。
默认的配置是刮取收集器本身的指标,以了解收集器的性能如何。添加更多的来源来刮取是很容易的,就像添加更多的目标/工作一样简单:
prometheus:
config:
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 10s
static_configs:
- targets: ['0.0.0.0:8888']
- job_name: 'node'
scrape_interval: 10s
static_configs:
- targets: ['localhost:9100']
现在我们已经添加了我们的出口商,让我们开始将它们发送到Grafana Cloud。为此,我们需要一个Prometheus远程写入导出器:
exporters:
prometheusremotewrite:
endpoint: https://USER:PASSWORD@URL
现在,这就是Grafana Cloud配置发挥作用的地方。例如,对我来说,端点看起来像这样:
exporters:
prometheusremotewrite:
endpoint: https://361398:eyJrIjoiYTNlZTFiOTc2NjA2ODJlOGY1ZGRlNGJkNWMwODRkMDY2M2U2MTE3NiIsIm4iOiJtZXRyaWNzLWtleSIsImlkIjo2MTU4ODJ9@prometheus-prod-01-eu-west-0.grafana.net/api/prom/push
(别担心,我撤销了API Key;)
当你添加导出器时,也要更新管道,最后的配置会是这样的:
receivers:
# Collect own metrics
prometheus:
config:
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 10s
static_configs:
- targets: ['0.0.0.0:8888']
- job_name: 'node'
scrape_interval: 10s
static_configs:
- targets: ['localhost:9100']
processors:
batch:
exporters:
logging:
logLevel: debug
prometheusremotewrite:
endpoint: https://361398:eyJrIjoiYTNlZTFiOTc2NjA2ODJlOGY1ZGRlNGJkNWMwODRkMDY2M2U2MTE3NiIsIm4iOiJtZXRyaWNzLWtleSIsImlkIjo2MTU4ODJ9@prometheus-prod-01-eu-west-0.grafana.net/api/prom/push
service:
pipelines:
metrics:
receivers: [prometheus]
processors: [batch]
exporters: [prometheusremotewrite]
一旦你用上述内容更新了/etc/otelcol/config.yaml (确保你使用了正确的USER、PASSWORD和URL),重新启动并验证采集器是否成功运行。
$ systemctl restart otelcol.service
$ systemctl status otelcol.service
现在在Grafana云的Grafana实例中,你应该看到指标开始流入。让我们导入这个流行的Node Exporter仪表盘来可视化这些指标:


这将加载仪表板。给它几分钟时间,你应该看到面板上弹出了关于你的Linux节点表现的数据
在未来,我们将介绍如何将收集器与操作员一起部署到Kubernetes,以及如何在Kubernetes中刮取指标。我们还将研究如何更好地使用采集器内的不同Prometheus组件