5.3. 使用分布式追踪数据收集

5.3.1. 使用 OpenTelemetry Collector 将 trace 转发到 TempoStack

要将转发追踪配置为 TempoStack,您可以部署和配置 OpenTelemetry Collector。您可以使用指定的处理器、接收器和导出器在部署模式中部署 OpenTelemetry Collector。有关其他模式,请参阅附加资源中的 OpenTelemetry Collector 文档链接。

先决条件

  • 安装了 Red Hat OpenShift distributed tracing 数据收集 Operator。
  • 已安装 Tempo Operator。
  • 在集群中部署了 TempoStack。

流程

  1. 为 OpenTelemetry Collector 创建服务帐户。

    ServiceAccount 示例

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: otel-collector-deployment

  2. 为服务帐户创建集群角色。

    ClusterRole 示例

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: otel-collector
    rules:
      1
      2
    - apiGroups: ["", "config.openshift.io"]
      resources: ["pods", "namespaces", "infrastructures", "infrastructures/status"]
      verbs: ["get", "watch", "list"]

    1
    k8sattributesprocessor 需要 pod 和命名空间资源的权限。
    2
    resourcedetectionprocessor 需要基础架构和状态的权限。
  3. 将集群角色绑定到服务帐户。

    ClusterRoleBinding 示例

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: otel-collector
    subjects:
    - kind: ServiceAccount
      name: otel-collector-deployment
      namespace: otel-collector-example
    roleRef:
      kind: ClusterRole
      name: otel-collector
      apiGroup: rbac.authorization.k8s.io

  4. 创建 YAML 文件以定义 OpenTelemetryCollector 自定义资源(CR)。

    OpenTelemetryCollector 示例

    apiVersion: opentelemetry.io/v1alpha1
    kind: OpenTelemetryCollector
    metadata:
      name: otel
    spec:
      mode: deployment
      serviceAccount: otel-collector-deployment
      config: |
        receivers:
          jaeger:
            protocols:
              grpc:
              thrift_binary:
              thrift_compact:
              thrift_http:
          opencensus:
          otlp:
            protocols:
              grpc:
              http:
          zipkin:
        processors:
          batch:
          k8sattributes:
          memory_limiter:
            check_interval: 1s
            limit_percentage: 50
            spike_limit_percentage: 30
          resourcedetection:
            detectors: [openshift]
        exporters:
          otlp:
            endpoint: "tempo-simplest-distributor:4317" 1
            tls:
              insecure: true
        service:
          pipelines:
            traces:
              receivers: [jaeger, opencensus, otlp, zipkin] 2
              processors: [memory_limiter, k8sattributes, resourcedetection, batch]
              exporters: [otlp]

    1
    Collector exporter 配置为导出 OTLP 并指向 Tempo 经销商端点 "tempo-simplest-distributor:4317" (在这个示例中已创建)。
    2
    Collector 配置了 Jaeger trace 的接收器,OpenCensus trace over the OpenCensus 协议, Zipkin trace over the Zipkin protocol, 和 OTLP trace over the GRPC 协议。
提示

您可以将 tracegen 部署为测试:

apiVersion: batch/v1
kind: Job
metadata:
  name: tracegen
spec:
  template:
    spec:
      containers:
        - name: tracegen
          image: ghcr.io/open-telemetry/opentelemetry-collector-contrib/tracegen:latest
          command:
            - "./tracegen"
          args:
            - -otlp-endpoint=otel-collector:4317
            - -otlp-insecure
            - -duration=30s
            - -workers=1
      restartPolicy: Never
  backoffLimit: 4

5.3.2. 将 trace 和 metrics 发送到 OpenTelemetry Collector

使用或不进行 sidecar 注入功能,可以将追踪和指标发送到 OpenTelemetry Collector。

5.3.2.1. 使用 sidecar 注入向 OpenTelemetry Collector 发送 trace 和 metrics

您可以将遥测数据发送到带有 sidecar 注入的 OpenTelemetryCollector 实例。

Red Hat OpenShift distributed tracing 数据收集 Operator 允许 sidecar 注入部署工作负载,并自动配置您的检测向 OpenTelemetry Collector 发送遥测数据。

先决条件

  • 安装了 Red Hat OpenShift distributed tracing Platform (Tempo),并部署了 TempoStack 实例。
  • 您可以通过 Web 控制台或 OpenShift CLI (oc)访问集群:

    • 以集群管理员身份使用 cluster-admin 角色登录到 web 控制台。
    • 集群管理员具有 cluster-admin 角色的活跃 OpenShift CLI (oc) 会话。
    • 对于 Red Hat OpenShift Dedicated,您必须有一个具有 dedicated-admin 角色的帐户。

流程

  1. 为 OpenTelemtry Collector 创建一个项目。

    apiVersion: project.openshift.io/v1
    kind: Project
    metadata:
      name: observability
  2. 创建一个服务帐户。

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: otel-collector-sidecar
      namespace: observability
  3. k8sattributesresourcedetection 处理器的服务帐户授予权限。

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: otel-collector
    rules:
    - apiGroups: ["", "config.openshift.io"]
      resources: ["pods", "namespaces", "infrastructures", "infrastructures/status"]
      verbs: ["get", "watch", "list"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: otel-collector
    subjects:
    - kind: ServiceAccount
      name: otel-collector-sidecar
      namespace: observability
    roleRef:
      kind: ClusterRole
      name: otel-collector
      apiGroup: rbac.authorization.k8s.io
  4. 将 OpenTelemetry Collector 部署为 sidecar。

    apiVersion: opentelemetry.io/v1alpha1
    kind: OpenTelemetryCollector
    metadata:
      name: otel
      namespace: observability
    spec:
      serviceAccount: otel-collector-sidecar
      mode: sidecar
      config: |
        serviceAccount: otel-collector-sidecar
        receivers:
          otlp:
            protocols:
              grpc:
              http:
        processors:
          batch:
          memory_limiter:
            check_interval: 1s
            limit_percentage: 50
            spike_limit_percentage: 30
          resourcedetection:
            detectors: [openshift]
            timeout: 2s
        exporters:
          otlp:
            endpoint: "tempo-<example>-gateway:8090" 1
            tls:
              insecure: true
        service:
          pipelines:
            traces:
              receivers: [jaeger]
              processors: [memory_limiter, resourcedetection, batch]
              exporters: [otlp]
    1
    这指向使用 Tempo Operator 部署的 <example> TempoStack 实例的网关。
  5. 使用 otel-collector-sidecar 服务帐户创建部署。
  6. 在您的 Deployment 对象中添加 sidecar.opentelemetry.io/inject: "true" 注解。这将注入所有需要的环境变量,将工作负载中的数据发送到 OpenTelemetryCollector 实例。

5.3.2.2. 在没有 sidecar 注入的情况下向 OpenTelemetry Collector 发送 trace 和 metrics

您可以在不进行 sidecar 注入的情况下将遥测数据发送到 OpenTelemetryCollector 实例,这涉及手动设置几个环境变量。

先决条件

  • 安装了 Red Hat OpenShift distributed tracing Platform (Tempo),并部署了 TempoStack 实例。
  • 您可以通过 Web 控制台或 OpenShift CLI (oc)访问集群:

    • 以集群管理员身份使用 cluster-admin 角色登录到 web 控制台。
    • 集群管理员具有 cluster-admin 角色的活跃 OpenShift CLI (oc) 会话。
    • 对于 Red Hat OpenShift Dedicated,您必须有一个具有 dedicated-admin 角色的帐户。

流程

  1. 为 OpenTelemtry Collector 创建一个项目。

    apiVersion: project.openshift.io/v1
    kind: Project
    metadata:
      name: observability
  2. 创建一个服务帐户。

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: otel-collector-deployment
      namespace: observability
  3. k8sattributesresourcedetection 处理器的服务帐户授予权限。

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: otel-collector
    rules:
    - apiGroups: ["", "config.openshift.io"]
      resources: ["pods", "namespaces", "infrastructures", "infrastructures/status"]
      verbs: ["get", "watch", "list"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: otel-collector
    subjects:
    - kind: ServiceAccount
      name: otel-collector
      namespace: observability
    roleRef:
      kind: ClusterRole
      name: otel-collector
      apiGroup: rbac.authorization.k8s.io
  4. 部署 OpenTelemetryCollector 实例。

    apiVersion: opentelemetry.io/v1alpha1
    kind: OpenTelemetryCollector
    metadata:
      name: otel
      namespace: observability
    spec:
      mode: deployment
      serviceAccount: otel-collector-deployment
      config: |
        receivers:
          jaeger:
            protocols:
              grpc:
              thrift_binary:
              thrift_compact:
              thrift_http:
          opencensus:
          otlp:
            protocols:
              grpc:
              http:
          zipkin:
        processors:
          batch:
          k8sattributes:
          memory_limiter:
            check_interval: 1s
            limit_percentage: 50
            spike_limit_percentage: 30
          resourcedetection:
            detectors: [openshift]
        exporters:
          otlp:
            endpoint: "tempo-<example>-distributor:4317" 1
            tls:
              insecure: true
        service:
          pipelines:
            traces:
              receivers: [jaeger, opencensus, otlp, zipkin]
              processors: [memory_limiter, k8sattributes, resourcedetection, batch]
              exporters: [otlp]
    1
    这指向使用 Tempo Operator 部署的 <example> TempoStack 实例的网关。
  5. 使用您的检测应用程序在容器中设置以下环境变量:

    Name描述默认值
    OTEL_SERVICE_NAME

    设置 service.name 资源属性的值。

    ""

    OTEL_EXPORTER_OTLP_ENDPOINT

    带有可选指定端口号的任何信号类型的基本端点 URL。

    https://localhost:4317

    OTEL_EXPORTER_OTLP_CERTIFICATE

    gRPC 客户端的 TLS 凭证的证书文件的路径。

    https://localhost:4317

    OTEL_TRACES_SAMPLER

    用于 trace 的 sampler。

    parentbased_always_on

    OTEL_EXPORTER_OTLP_PROTOCOL

    OTLP 导出器的传输协议。

    grpc

    OTEL_EXPORTER_OTLP_TIMEOUT

    OTLP 导出器将等待每个批处理导出的最大时间。

    10s

    OTEL_EXPORTER_OTLP_INSECURE

    为 gRPC 请求禁用客户端传输安全性 ; HTTPS 模式会覆盖它。

    False