基于请求或响应对指标进行分类

根据网格中服务处理的请求和响应类型来可视化遥测数据非常有用。例如,一家书店跟踪书籍评论请求的次数。书籍评论请求具有以下结构

GET /reviews/{review_id}

计算评论请求的次数必须考虑无界元素 review_idGET /reviews/1 后面跟着 GET /reviews/2 应该算作两个获取评论的请求。

Istio 允许您使用 AttributeGen 插件创建分类规则,将请求分组到固定数量的逻辑操作中。例如,您可以创建一个名为 GetReviews 的操作,这是一种使用 Open API Spec operationId 识别操作的常见方法。此信息将作为 istio_operationId 属性注入到请求处理中,其值为 GetReviews。您可以将该属性用作 Istio 标准指标的维度。类似地,您可以根据其他操作(如 ListReviewsCreateReviews)跟踪指标。

按请求对指标进行分类

您可以根据请求类型对请求进行分类,例如 ListReviewGetReviewCreateReview

  1. 创建一个文件,例如 attribute_gen_service.yaml,并保存以下内容。这将添加 istio.attributegen 插件。它还会创建一个属性 istio_operationId,并使用要作为指标计数的类别的值填充它。

    此配置是特定于服务的,因为请求路径通常是特定于服务的。

    apiVersion: extensions.istio.io/v1alpha1
    kind: WasmPlugin
    metadata:
      name: istio-attributegen-filter
    spec:
      selector:
        matchLabels:
          app: reviews
      url: https://storage.googleapis.com/istio-build/proxy/attributegen-359dcd3a19f109c50e97517fe6b1e2676e870c4d.wasm
      imagePullPolicy: Always
      phase: AUTHN
      pluginConfig:
        attributes:
        - output_attribute: "istio_operationId"
          match:
            - value: "ListReviews"
              condition: "request.url_path == '/reviews' && request.method == 'GET'"
            - value: "GetReview"
              condition: "request.url_path.matches('^/reviews/[[:alnum:]]*$') && request.method == 'GET'"
            - value: "CreateReview"
              condition: "request.url_path == '/reviews/' && request.method == 'POST'"
    ---
    apiVersion: telemetry.istio.io/v1
    kind: Telemetry
    metadata:
      name: custom-tags
    spec:
      metrics:
        - overrides:
            - match:
                metric: REQUEST_COUNT
                mode: CLIENT_AND_SERVER
              tagOverrides:
                request_operation:
                  value: istio_operationId
          providers:
            - name: prometheus
    
  2. 使用以下命令应用更改

    $ kubectl -n istio-system apply -f attribute_gen_service.yaml
    
  3. 更改生效后,访问 Prometheus 并查找新的或更改的维度,例如 reviews pod 中的 istio_requests_total

按响应对指标进行分类

您可以使用类似于请求的过程对响应进行分类。请注意,response_code 维度默认情况下已存在。以下示例将改变其填充方式。

  1. 创建一个文件,例如 attribute_gen_service.yaml,并保存以下内容。这将添加 istio.attributegen 插件并生成 istio_responseClass 属性,该属性由统计插件使用。

    此示例对各种响应进行分类,例如将 200 范围内的所有响应代码分组为 2xx 维度。

    apiVersion: extensions.istio.io/v1alpha1
    kind: WasmPlugin
    metadata:
      name: istio-attributegen-filter
    spec:
      selector:
        matchLabels:
          app: productpage
      url: https://storage.googleapis.com/istio-build/proxy/attributegen-359dcd3a19f109c50e97517fe6b1e2676e870c4d.wasm
      imagePullPolicy: Always
      phase: AUTHN
      pluginConfig:
        attributes:
          - output_attribute: istio_responseClass
            match:
              - value: 2xx
                condition: response.code >= 200 && response.code <= 299
              - value: 3xx
                condition: response.code >= 300 && response.code <= 399
              - value: "404"
                condition: response.code == 404
              - value: "429"
                condition: response.code == 429
              - value: "503"
                condition: response.code == 503
              - value: 5xx
                condition: response.code >= 500 && response.code <= 599
              - value: 4xx
                condition: response.code >= 400 && response.code <= 499
    ---
    apiVersion: telemetry.istio.io/v1
    kind: Telemetry
    metadata:
      name: custom-tags
    spec:
      metrics:
        - overrides:
            - match:
                metric: REQUEST_COUNT
                mode: CLIENT_AND_SERVER
              tagOverrides:
                response_code:
                  value: istio_responseClass
          providers:
            - name: prometheus
    
  2. 使用以下命令应用更改

    $ kubectl -n istio-system apply -f attribute_gen_service.yaml
    

验证结果

  1. 通过向您的应用程序发送流量来生成指标。

  2. 访问 Prometheus 并查找新的或更改的维度,例如 2xx。或者,使用以下命令验证 Istio 是否为您新的维度生成数据

    $ kubectl exec pod-name -c istio-proxy -- curl -sS 'localhost:15000/stats/prometheus' | grep istio_
    

    在输出中,找到指标(例如 istio_requests_total)并验证新维度或更改的维度的存在。

疑难解答

如果分类没有按预期进行,请检查以下可能的起因和解决方案。

查看应用配置更改的服务所在的 pod 的 Envoy 代理日志。检查 Envoy 代理日志中该 pod(pod-name)的服务是否报告任何错误,您在其中使用以下命令配置分类

$ kubectl logs pod-name -c istio-proxy | grep -e "Config Error" -e "envoy wasm"

此外,确保没有 Envoy 代理崩溃,方法是查看以下命令输出中重启的迹象

$ kubectl get pods pod-name

清理

删除 yaml 配置文件。

$ kubectl -n istio-system delete -f attribute_gen_service.yaml
这些信息对您有帮助吗?
您有什么改进建议吗?

感谢您的反馈!