使用 MeshConfig 和 Pod 注解配置跟踪

Istio 提供了配置高级跟踪选项的功能,例如采样率和向报告的跨度添加自定义标签。采样是一个 Beta 功能,但添加自定义标签和跟踪标签长度在此版本中被认为是开发中的功能。

开始之前

  1. 确保您的应用程序按此处所述传播跟踪头。

  2. 根据您首选的跟踪后端,遵循位于集成下的跟踪安装指南,以安装相应的插件并配置您的 Istio 代理将跟踪发送到跟踪部署。

可用的跟踪配置

您可以在 Istio 中配置以下跟踪选项

  1. 随机采样率,用于选择将生成跟踪的请求的百分比。

  2. 请求路径的最大长度,超过此长度将截断路径以进行报告。这在限制跟踪数据存储方面很有用,尤其是在您在入口网关收集跟踪时。

  3. 在跨度中添加自定义标签。这些标签可以根据静态字面值、环境值或请求头中的字段添加。这可以用于在特定于您的环境的跨度中注入其他信息。

您可以通过两种方式配置跟踪选项

  1. 通过MeshConfig选项全局配置。

  2. 针对工作负载特定自定义的每个 Pod 注解。

安装

使用这些功能为在您的环境中管理跟踪打开了新的可能性。

在此示例中,我们将对所有跟踪进行采样,并使用注入到您的 Pod 中的ISTIO_META_CLUSTER_ID环境变量添加名为clusterID的标签。仅使用该值的最初 256 个字符。

$ cat <<EOF > ./tracing.yaml apiVersion: install.istio.io/v1alpha1 kind: IstioOperator spec: meshConfig: enableTracing: true defaultConfig: tracing: sampling: 100.0 max_path_tag_length: 256 custom_tags: clusterID: environment: name: ISTIO_META_CLUSTER_ID EOF $ istioctl install -f ./tracing.yaml

使用 MeshConfig 进行跟踪设置

所有跟踪选项都可以通过MeshConfig全局配置。为了简化配置,建议创建一个单一的 YAML 文件,您可以将其传递给istioctl install -f命令。

cat <<'EOF' > tracing.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  meshConfig:
    enableTracing: true
    defaultConfig:
      tracing:
        sampling: 10
        custom_tags:
          my_tag_header:
            header:
              name: host
EOF

使用proxy.istio.io/config注解配置跟踪设置

您可以将proxy.istio.io/config注解添加到您的 Pod 元数据规范中,以覆盖任何网格范围的跟踪设置。例如,要修改与 Istio 一起提供的curl部署,您需要将以下内容添加到samples/curl/curl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: curl
spec:
  ...
  template:
    metadata:
      ...
      annotations:
        ...
        proxy.istio.io/config: |
          tracing:
            sampling: 10
            custom_tags:
              my_tag_header:
                header:
                  name: host
    spec:
      ...

自定义跟踪采样

采样率选项可用于控制向跟踪系统报告的请求百分比。应根据网格中的流量和您想要收集的跟踪数据量来配置此选项。默认速率为 1%。

要将默认随机采样修改为 50,请将以下选项添加到您的tracing.yaml文件中。

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  meshConfig:
    enableTracing: true
    defaultConfig:
      tracing:
        sampling: 50

采样率应在 0.0 到 100.0 之间,精度为 0.01。例如,要跟踪每 10000 个请求中的 5 个请求,请在此处使用 0.05 作为值。

自定义跟踪标签

可以根据字面量、环境变量和客户端请求头向跨度添加自定义标签,以便在特定于您环境的跨度中提供其他信息。

您可以使用以下三个支持的选项中的任何一个自定义标签。

  1. 字面量表示添加到每个跨度的静态值。

    apiVersion: install.istio.io/v1alpha1
    kind: IstioOperator
    spec:
      meshConfig:
        enableTracing: true
        defaultConfig:
          tracing:
            custom_tags:
              my_tag_literal:
                literal:
                  value: <VALUE>
    
  2. 环境变量可用于从工作负载代理环境变量填充自定义标签的值。

    apiVersion: install.istio.io/v1alpha1
    kind: IstioOperator
    spec:
      meshConfig:
        enableTracing: true
        defaultConfig:
          tracing:
            custom_tags:
              my_tag_env:
                environment:
                  name: <ENV_VARIABLE_NAME>
                  defaultValue: <VALUE>      # optional
    
  3. 客户端请求头选项可用于从传入的客户端请求头填充标签值。

    apiVersion: install.istio.io/v1alpha1
    kind: IstioOperator
    spec:
      meshConfig:
        enableTracing: true
        defaultConfig:
          tracing:
            custom_tags:
              my_tag_header:
                header:
                  name: <CLIENT-HEADER>
                  defaultValue: <VALUE>      # optional
    

自定义跟踪标签长度

默认情况下,作为HttpUrl跨度标签的一部分包含的请求路径的最大长度为 256。要修改此最大长度,请将以下内容添加到您的tracing.yaml文件中。

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  meshConfig:
    enableTracing: true
    defaultConfig:
      tracing:
        max_path_tag_length: <VALUE>
这些信息是否有用?
您是否有任何改进建议?

感谢您的反馈!