Prometheus 支持通过 OTLP (又名 "OpenTelemetry 协议") 通过 HTTP 进行摄取。
默认情况下,OTLP 接收器是禁用的,类似于远程写入接收器。这是因为 Prometheus 可以在没有任何身份验证的情况下工作,因此除非显式配置,否则接受传入流量是不安全的。
要启用接收器,您需要切换 CLI 标志 --web.enable-otlp-receiver
。这将使 Prometheus 在 HTTP /api/v1/otlp/v1/metrics
路径上提供 OTLP 指标接收。
$ prometheus --web.enable-otlp-receiver
通常,您需要告知 OTLP 指标流量的来源关于 Prometheus 端点以及 HTTP 模式的 OTLP 应该被使用(gRPC 通常是默认模式)。
OpenTelemetry SDK 和检测库通常可以通过 标准环境变量 进行配置。以下是将 OpenTelemetry 指标发送到 localhost 上的 Prometheus 服务器所需的 OpenTelemetry 变量
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://127.0.0.1:9090/api/v1/otlp/v1/metrics
关闭追踪和日志
export OTEL_TRACES_EXPORTER=none
export OTEL_LOGS_EXPORTER=none
OpenTelemetry 指标的默认推送间隔为 60 秒。以下设置 15 秒推送间隔
export OTEL_METRIC_EXPORT_INTERVAL=15000
如果您的检测库未提供开箱即用的 service.name
和 service.instance.id
,则强烈建议您设置它们。
export OTEL_SERVICE_NAME="my-example-service"
export OTEL_RESOURCE_ATTRIBUTES="service.instance.id=$(uuidgen)"
以上假设 uuidgen
命令在您的系统上可用。确保 service.instance.id
对于每个实例都是唯一的,并且每当资源属性发生变化时都会生成新的 service.instance.id
。推荐 的方法是在实例每次启动时生成新的 UUID。
本节解释了 Prometheus 服务器的各种推荐配置方面,以启用和调整您的 OpenTelemetry 流。
请参阅示例 Prometheus 配置 文件,我们将在下面部分中使用它。
您可能希望启用乱序摄取有多种原因。
例如,OpenTelemetry 收集器鼓励批处理,您可能有多个收集器副本将数据发送到 Prometheus。由于没有机制对这些样本进行排序,它们可能会变得乱序。
要启用乱序摄取,您需要使用以下内容扩展 Prometheus 配置文件
storage:
tsdb:
out_of_order_time_window: 30m
30 分钟的乱序对于大多数情况来说已经足够了,但请随时根据您的需要调整此值。
根据经验以及与我们社区的对话,我们发现,在所有常见的资源属性中,有一些值得附加到您的所有 OTLP 指标上。
默认情况下,Prometheus 不会提升任何属性。如果您想提升任何属性,您可以在 Prometheus 配置文件的此部分中执行此操作。以下代码段分享了最佳实践的属性集以进行提升
otlp:
# Recommended attributes to be promoted to labels.
promote_resource_attributes:
- service.instance.id
- service.name
- service.namespace
- cloud.availability_zone
- cloud.region
- container.name
- deployment.environment.name
- k8s.cluster.name
- k8s.container.name
- k8s.cronjob.name
- k8s.daemonset.name
- k8s.deployment.name
- k8s.job.name
- k8s.namespace.name
- k8s.pod.name
- k8s.replicaset.name
- k8s.statefulset.name
所有未提升的、更冗长或唯一的标签都附加到特殊的 target_info
。
您可以使用此指标在查询时连接一些标签。
此类查询的示例可能如下所示
rate(http_server_request_duration_seconds_count[2m])
* on (job, instance) group_left (k8s_cluster_name)
target_info
此查询中发生的情况是,来自 rate(http_server_request_duration_seconds_count[2m])
的时间序列使用来自 target_info
系列的 k8s_cluster_name
标签进行扩充,这些系列共享相同的 job
和 instance
标签。换句话说,job
和 instance
标签在 http_server_request_duration_seconds_count
和 target_info
之间共享,类似于 SQL 外键。另一方面,k8s_cluster_name
标签对应于 OTel 资源属性 k8s.cluster.name
(Prometheus 将点转换为下划线)。
那么,target_info
指标和 OTel 资源属性之间有什么关系呢?当 Prometheus 处理 OTLP 写入请求时,并且前提是包含的资源包括属性 service.instance.id
和/或 service.name
,Prometheus 会为每个(OTel)资源生成信息指标 target_info
。它将标签 instance
添加到每个此类 target_info
系列,其值为 service.instance.id
资源属性,并将标签 job
添加到 service.name
资源属性的值。如果资源属性 service.namespace
存在,它将作为前缀添加到 job
标签值(即 <service.namespace>/<service.name>
)。
默认情况下,service.name
、service.namespace
和 service.instance.id
本身不会添加到 target_info
,因为它们会转换为 job
和 instance
。但是,可以启用以下配置参数以将它们直接添加到 target_info
(如果 otlp.translation_strategy
为 UnderscoreEscapingWithSuffixes
,则通过规范化将点替换为下划线),并在转换为 job
和 instance
的基础上进行。
otlp:
keep_identifying_resource_attributes: true
其余资源属性也作为标签添加到 target_info
系列,名称转换为 Prometheus 格式(例如,如果 otlp.translation_strategy
为 UnderscoreEscapingWithSuffixes
,则将点转换为下划线)。如果资源缺少 service.instance.id
和 service.name
属性,则不会生成相应的 target_info
系列。
对于资源的每个 OTel 指标,Prometheus 会将其转换为相应的 Prometheus 时间序列,并且(如果生成 target_info
)添加正确的 instance
和 job
标签。
从 3.x 版本开始,Prometheus 支持指标名称和标签的 UTF-8,因此可以省略 来自 OpenTelemetry 的 Prometheus 规范化转换器包。
UTF-8 默认在 Prometheus 存储和 UI 中启用,但您需要为 OTLP 指标接收器设置 translation_strategy
,默认设置为旧的规范化 UnderscoreEscapingWithSuffixes
。
将其设置为我们推荐的 NoUTF8EscapingWithSuffixes
将禁用将特殊字符更改为 _
,这允许原生使用 OpenTelemetry 指标格式,尤其是使用 语义约定。请注意,特殊后缀(如单位)和计数器的 _total
将会附加。 正在进行无后缀生成的工作,请继续关注。
otlp:
# Ingest OTLP data keeping UTF-8 characters in metric/label names.
translation_strategy: NoUTF8EscapingWithSuffixes
当前,OTLP 转换包中存在一个已知限制,如果单词之间连接了多个 UTF-8 字符,则字符将从指标/标签名称中删除,例如
my___metric
变为my_metric
。有关更多详细信息,请参阅 https://github.com/prometheus/prometheus/issues/15362。
OpenTelemetry 规范说 支持 Delta 时序性和 Cumulative 时序性。
虽然 Delta 时序性在 statsd 和 graphite 等系统中很常见,但 Cumulative 时序性是 Prometheus 的默认时序性。
今天 Prometheus 不支持 delta 时序性,但我们正在向 OpenTelemetry 社区学习,并且我们正在考虑在未来添加对其的支持。
如果您来自 delta 时序性系统,我们建议您在您的 OTel 管道中使用 delta to cumulative processor。
本文档是 开源的。请通过提交 issue 或 pull request 帮助改进它。