为监控堆栈配置指标


Red Hat build of OpenTelemetry 3.8

将 OpenTelemetry Collector 与 Prometheus 监控集成

Red Hat OpenShift Documentation Team

摘要

本文档论述了如何配置 OpenTelemetry Collector 和 OpenShift 监控堆栈之间的指标集成。了解如何使用 ServiceMonitor 和 PodMonitor 自定义资源将 Collector 指标发送到 Prometheus,以及如何使用 Prometheus 接收器从集群内监控堆栈接收指标。包括提取内部 Collector 指标、Prometheus 导出器端点、RBAC 设置、TLS 配置以及访问联邦端点的配置。

第 1 章 为监控堆栈配置指标

作为集群管理员,您可以配置 OpenTelemetry Collector 自定义资源 (CR) 来执行以下任务:

  • 创建 Prometheus ServiceMonitor CR,以提取 Collector 的管道指标并启用 Prometheus exporter。
  • 配置 Prometheus 接收器,以从集群内监控堆栈中提取指标。

1.1. 将指标发送到监控堆栈的配置

您可以配置 OpenTelemetryCollector 自定义资源 (CR),为 sidecar 部署创建 Prometheus ServiceMonitor CR 或 PodMonitor CR。ServiceMonitor 可以提取 Collector 的内部指标端点和 Prometheus exporter 指标端点。

带有 Prometheus exporter 的 OpenTelemetry Collector CR 示例

apiVersion: opentelemetry.io/v1beta1
kind: OpenTelemetryCollector
spec:
  mode: deployment
  observability:
    metrics:
      enableMetrics: true 
1

  config:
    exporters:
      prometheus:
        endpoint: 0.0.0.0:8889
        resource_to_telemetry_conversion:
          enabled: true # by default resource attributes are dropped
    service:
      telemetry:
        metrics:
          readers:
          - pull:
              exporter:
                prometheus:
                  host: 0.0.0.0
                  port: 8888
      pipelines:
        metrics:
          exporters: [prometheus]
Copy to Clipboard Toggle word wrap

1
配置红帽构建的 OpenTelemetry Operator,以创建 Prometheus ServiceMonitor CR 或 PodMonitor CR,以提取 Collector 的内部指标端点和 Prometheus exporter 指标端点。
注意

enableMetrics 设置为 true 会创建以下两个 ServiceMonitor 实例:

  • 一个 ServiceMonitor 实例用于 <instance_name>-collector-monitoring 服务。此 ServiceMonitor 实例提取 Collector 的内部指标。
  • 一个 ServiceMonitor 实例用于 <instance_name>-collector 服务。此 ServiceMonitor 实例提取 Prometheus exporter 实例公开的指标。

另外,手动创建 Prometheus PodMonitor CR 可以提供精细的控制,例如删除 Prometheus 提取过程中添加的重复标签。

配置监控堆栈以提取 Collector 指标的 PodMonitor CR 示例

apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: otel-collector
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: <cr_name>-collector 
1

  podMetricsEndpoints:
  - port: metrics 
2

  - port: promexporter 
3

    relabelings:
    - action: labeldrop
      regex: pod
    - action: labeldrop
      regex: container
    - action: labeldrop
      regex: endpoint
    metricRelabelings:
    - action: labeldrop
      regex: instance
    - action: labeldrop
      regex: job
Copy to Clipboard Toggle word wrap

1
OpenTelemetry Collector CR 的名称。
2
OpenTelemetry Collector 的内部指标端口的名称。此端口名称始终是 metrics
3
OpenTelemetry Collector 的 Prometheus exporter 端口的名称。

1.2. 配置用于从监控堆栈接收指标

配置的 OpenTelemetry Collector 自定义资源(CR)可以设置 Prometheus 接收器从集群监控堆栈中提取指标。

用于从集群监控堆栈中提取指标的 OpenTelemetry Collector CR 示例

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: otel-collector
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-monitoring-view 
1

subjects:
  - kind: ServiceAccount
    name: otel-collector
    namespace: observability
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: cabundle
  namespace: observability
  annotations:
    service.beta.openshift.io/inject-cabundle: "true" 
2

---
apiVersion: opentelemetry.io/v1beta1
kind: OpenTelemetryCollector
metadata:
  name: otel
  namespace: observability
spec:
  volumeMounts:
    - name: cabundle-volume
      mountPath: /etc/pki/ca-trust/source/service-ca
      readOnly: true
  volumes:
    - name: cabundle-volume
      configMap:
        name: cabundle
  mode: deployment
  config:
    receivers:
      prometheus: 
3

        config:
          scrape_configs:
            - job_name: 'federate'
              scrape_interval: 15s
              scheme: https
              tls_config:
                ca_file: /etc/pki/ca-trust/source/service-ca/service-ca.crt
              bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
              honor_labels: false
              params:
                'match[]':
                  - '{__name__="<metric_name>"}' 
4

              metrics_path: '/federate'
              static_configs:
                - targets:
                  - "prometheus-k8s.openshift-monitoring.svc.cluster.local:9091"
    exporters:
      debug: 
5

        verbosity: detailed
    service:
      pipelines:
        metrics:
          receivers: [prometheus]
          processors: []
          exporters: [debug]
Copy to Clipboard Toggle word wrap

1
cluster-monitoring-view 集群角色分配给 OpenTelemetry Collector 的服务帐户,以便它可以访问指标数据。
2
注入 OpenShift 服务 CA,用于在 Prometheus 接收器中配置 TLS。
3
配置 Prometheus 接收器,以从集群内监控堆栈中提取 federate 端点。
4
使用 Prometheus 查询语言选择要提取的指标。有关联合端点的详情和限制,请参阅集群监控文档。
5
配置 debug exporter,将指标输出到标准输出。

法律通告

Copyright © 2025 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部