根据请求或响应对指标进行分类

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

  1. GET /reviews/{review_id}

计算审查请求的数量必须考虑到无界元素 review_idGET /reviews/1 紧随其后的 GET /reviews/2 应该算作两次获得评论的请求。

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

有关详细信息,请参阅参考内容

Istio 使用 Envoy 代理生成指标并在 EnvoyFiltermanifests/charts/istio-control/istio-discovery/templates/telemetryv2_1.15.yaml。 因此,编写分类规则涉及将属性添加到 EnvoyFilter

按请求分类指标

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

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

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

    1. apiVersion: networking.istio.io/v1alpha3
    2. kind: EnvoyFilter
    3. metadata:
    4. name: istio-attributegen-filter
    5. spec:
    6. workloadSelector:
    7. labels:
    8. app: reviews
    9. configPatches:
    10. - applyTo: HTTP_FILTER
    11. match:
    12. context: SIDECAR_INBOUND
    13. proxy:
    14. proxyVersion: '1\.9.*'
    15. listener:
    16. filterChain:
    17. filter:
    18. name: "envoy.http_connection_manager"
    19. subFilter:
    20. name: "istio.stats"
    21. patch:
    22. operation: INSERT_BEFORE
    23. value:
    24. name: istio.attributegen
    25. typed_config:
    26. "@type": type.googleapis.com/udpa.type.v1.TypedStruct
    27. type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
    28. value:
    29. config:
    30. configuration:
    31. "@type": type.googleapis.com/google.protobuf.StringValue
    32. value: |
    33. {
    34. "attributes": [
    35. {
    36. "output_attribute": "istio_operationId",
    37. "match": [
    38. {
    39. "value": "ListReviews",
    40. "condition": "request.url_path == '/reviews' && request.method == 'GET'"
    41. },
    42. {
    43. "value": "GetReview",
    44. "condition": "request.url_path.matches('^/reviews/[[:alnum:]]*$') && request.method == 'GET'"
    45. },
    46. {
    47. "value": "CreateReview",
    48. "condition": "request.url_path == '/reviews/' && request.method == 'POST'"
    49. }
    50. ]
    51. }
    52. ]
    53. }
    54. vm_config:
    55. runtime: envoy.wasm.runtime.null
    56. code:
    57. local: { inline_string: "envoy.wasm.attributegen" }
  2. 使用以下命令应用您的更改:

    1. $ kubectl -n istio-system apply -f attribute_gen_service.yaml
  3. 查找 stats-filter-1.15 EnvoyFilter 资源从 istio-system 命名空间中 istio-system,使用以下命令:

    1. $ kubectl -n istio-system get envoyfilter | grep ^stats-filter-1.15
    2. stats-filter-1.15 2d
  4. 创建 EnvoyFilter 配置的本地文件系统副本,使用以下命令:

    1. $ kubectl -n istio-system get envoyfilter stats-filter-1.15 -o yaml > stats-filter-1.15.yaml
  5. 使用文本编辑器打开 stats-filter-1.15.yaml 并找到 name: istio.stats 扩展配置。更新它以映射 request_operation requests_total 标准指标中的指标到 istio_operationId 属性。 更新后的配置文件部分应如下所示。

    1. name: istio.stats
    2. typed_config:
    3. '@type': type.googleapis.com/udpa.type.v1.TypedStruct
    4. type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
    5. value:
    6. config:
    7. configuration:
    8. "@type": type.googleapis.com/google.protobuf.StringValue
    9. value: |
    10. {
    11. "metrics": [
    12. {
    13. "name": "requests_total",
    14. "dimensions": {
    15. "request_operation": "istio_operationId"
    16. }
    17. }]
    18. }
  6. 保存 stats-filter-1.15.yaml,然后使用以下命令应用配置:

    1. $ kubectl -n istio-system apply -f stats-filter-1.15.yaml
  7. 将以下配置添加到网格配置中。这导致添加了 request_operation 作为 istio_requests_total 指标的新维度。 没有它,一个名为 envoy_request_operation___somevalue___istio_requests_total 的新指标 被建造。

    1. meshConfig:
    2. defaultConfig:
    3. extraStatTags:
    4. - request_operation
  8. 通过向您的应用程序发送流量来生成指标。

  9. 更改生效后,访问 Prometheus 并查找新的或更改的维度,例如: istio_requests_total

按响应对指标进行分类

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

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

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

    1. apiVersion: networking.istio.io/v1alpha3
    2. kind: EnvoyFilter
    3. metadata:
    4. name: istio-attributegen-filter
    5. spec:
    6. workloadSelector:
    7. labels:
    8. app: productpage
    9. configPatches:
    10. - applyTo: HTTP_FILTER
    11. match:
    12. context: SIDECAR_INBOUND
    13. proxy:
    14. proxyVersion: '1\.9.*'
    15. listener:
    16. filterChain:
    17. filter:
    18. name: "envoy.http_connection_manager"
    19. subFilter:
    20. name: "istio.stats"
    21. patch:
    22. operation: INSERT_BEFORE
    23. value:
    24. name: istio.attributegen
    25. typed_config:
    26. "@type": type.googleapis.com/udpa.type.v1.TypedStruct
    27. type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
    28. value:
    29. config:
    30. configuration:
    31. "@type": type.googleapis.com/google.protobuf.StringValue
    32. value: |
    33. {
    34. "attributes": [
    35. {
    36. "output_attribute": "istio_responseClass",
    37. "match": [
    38. {
    39. "value": "2xx",
    40. "condition": "response.code >= 200 && response.code <= 299"
    41. },
    42. {
    43. "value": "3xx",
    44. "condition": "response.code >= 300 && response.code <= 399"
    45. },
    46. {
    47. "value": "404",
    48. "condition": "response.code == 404"
    49. },
    50. {
    51. "value": "429",
    52. "condition": "response.code == 429"
    53. },
    54. {
    55. "value": "503",
    56. "condition": "response.code == 503"
    57. },
    58. {
    59. "value": "5xx",
    60. "condition": "response.code >= 500 && response.code <= 599"
    61. },
    62. {
    63. "value": "4xx",
    64. "condition": "response.code >= 400 && response.code <= 499"
    65. }
    66. ]
    67. }
    68. ]
    69. }
    70. vm_config:
    71. runtime: envoy.wasm.runtime.null
    72. code:
    73. local: { inline_string: "envoy.wasm.attributegen" }
  2. 使用以下命令应用您的更改:

    1. $ kubectl -n istio-system apply -f attribute_gen_service.yaml
  3. istio-system 中找到 stats-filter-1.15 EnvoyFilter 资源 命名空间,使用以下命令:

    1. $ kubectl -n istio-system get envoyfilter | grep ^stats-filter-1.15
    2. stats-filter-1.15 2d
  4. 创建 EnvoyFilter 配置的本地文件系统副本,使用以下命令:

    1. $ kubectl -n istio-system get envoyfilter stats-filter-1.15 -o yaml > stats-filter-1.15.yaml
  5. 使用文本编辑器打开 stats-filter-1.15.yaml 并找到 name: istio.stats 扩展配置。 更新它以映射 response_coderequests_total 标准指标中的维度到 istio_responseClass 属性。 更新后的配置文件部分应如下所示。

    1. name: istio.stats
    2. typed_config:
    3. '@type': type.googleapis.com/udpa.type.v1.TypedStruct
    4. type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
    5. value:
    6. config:
    7. configuration:
    8. "@type": type.googleapis.com/google.protobuf.StringValue
    9. value: |
    10. {
    11. "metrics": [
    12. {
    13. "name": "requests_total",
    14. "dimensions": {
    15. "response_code": "istio_responseClass"
    16. }
    17. }]
    18. }
  6. 保存 stats-filter-1.15.yaml,然后使用以下命令应用配置:

    1. $ kubectl -n istio-system apply -f stats-filter-1.15.yaml

验证结果

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

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

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

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

故障排除

如果分类未按预期进行,请检查以下潜在原因和解决方法。

查看具有您应用了配置更改的服务的 Pod 的 Envoy 代理日志。 在您使用以下命令配置分类的 Pod (pod-name) 上的 Envoy 代理日志中检查服务是否没有报告错误:

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

此外,通过在以下命令的输出中查找重新启动的迹象,确保没有 Envoy 代理崩溃:

  1. $ kubectl get pods pod-name