收集Metrics和日志

本章展示如何配置Istio来自动收集mesh中服务的遥测数据。

在本章末尾,将为mesh中的服务调用启用新的metric和新的日志流。

BookInfo应用将作为介绍本章内容的示例应用。

开始之前

  • 在集群中安装Istio并部署一个应用程序。

    本章假设Mixer使用默认配置(--configDefaultNamespace=istio-system)。 如果使用不同的值,则更新这个任务中的配置和命令来匹配这个值。

  • 安装Prometheus插件。

    Prometheus用于验证任务是否成功。

    1. kubectl apply -f install/kubernetes/addons/prometheus.yaml

    详细信息请看Prometheus

收集新的遥测数据

  1. 创建YAML文件来保存新metric和日志流的配置,Istio将自动生成和收集。

    把以下内容保存成文件new_telemetry.yaml:

    1. # Configuration for metric instances
    2. apiVersion: "config.istio.io/v1alpha2"
    3. kind: metric
    4. metadata:
    5. name: doublerequestcount
    6. namespace: istio-system
    7. spec:
    8. value: "2" # count each request twice
    9. dimensions:
    10. source: source.service | "unknown"
    11. destination: destination.service | "unknown"
    12. message: '"twice the fun!"'
    13. monitored_resource_type: '"UNSPECIFIED"'
    14. ---
    15. # Configuration for a Prometheus handler
    16. apiVersion: "config.istio.io/v1alpha2"
    17. kind: prometheus
    18. metadata:
    19. name: doublehandler
    20. namespace: istio-system
    21. spec:
    22. metrics:
    23. - name: double_request_count # Prometheus metric name
    24. instance_name: doublerequestcount.metric.istio-system # Mixer instance name (fully-qualified)
    25. kind: COUNTER
    26. label_names:
    27. - source
    28. - destination
    29. - message
    30. ---
    31. # Rule to send metric instances to a Prometheus handler
    32. apiVersion: "config.istio.io/v1alpha2"
    33. kind: rule
    34. metadata:
    35. name: doubleprom
    36. namespace: istio-system
    37. spec:
    38. actions:
    39. - handler: doublehandler.prometheus
    40. instances:
    41. - doublerequestcount.metric
    42. ---
    43. # Configuration for logentry instances
    44. apiVersion: "config.istio.io/v1alpha2"
    45. kind: logentry
    46. metadata:
    47. name: newlog
    48. namespace: istio-system
    49. spec:
    50. severity: '"warning"'
    51. timestamp: request.time
    52. variables:
    53. source: source.labels["app"] | source.service | "unknown"
    54. user: source.user | "unknown"
    55. destination: destination.labels["app"] | destination.service | "unknown"
    56. responseCode: response.code | 0
    57. responseSize: response.size | 0
    58. latency: response.duration | "0ms"
    59. monitored_resource_type: '"UNSPECIFIED"'
    60. ---
    61. # Configuration for a stdio handler
    62. apiVersion: "config.istio.io/v1alpha2"
    63. kind: stdio
    64. metadata:
    65. name: newhandler
    66. namespace: istio-system
    67. spec:
    68. severity_levels:
    69. warning: 1 # Params.Level.WARNING
    70. outputAsJson: true
    71. ---
    72. # Rule to send logentry instances to a stdio handler
    73. apiVersion: "config.istio.io/v1alpha2"
    74. kind: rule
    75. metadata:
    76. name: newlogstdio
    77. namespace: istio-system
    78. spec:
    79. match: "true" # match for all requests
    80. actions:
    81. - handler: newhandler.stdio
    82. instances:
    83. - newlog.logentry
    84. ---
    85. ---
    86. # Rule to send metric instances to a Prometheus handler
    87. apiVersion: "config.istio.io/v1alpha2"
    88. kind: rule
    89. metadata:
    90. name: doubleprom
    91. namespace: istio-system
    92. spec:
    93. actions:
    94. - handler: doublehandler.prometheus
    95. instances:
    96. - doublerequestcount.metric
    97. ---
    98. # Configuration for logentry instances
    99. apiVersion: "config.istio.io/v1alpha2"
    100. kind: logentry
    101. metadata:
    102. name: newlog
    103. namespace: istio-system
    104. spec:
    105. severity: '"warning"'
    106. timestamp: request.time
    107. variables:
    108. source: source.labels["app"] | source.service | "unknown"
    109. user: source.user | "unknown"
    110. destination: destination.labels["app"] | destination.service | "unknown"
    111. responseCode: response.code | 0
    112. responseSize: response.size | 0
    113. latency: response.duration | "0ms"
    114. monitored_resource_type: '"UNSPECIFIED"'
    115. ---
    116. # Configuration for a stdio handler
    117. apiVersion: "config.istio.io/v1alpha2"
    118. kind: stdio
    119. metadata:
    120. name: newhandler
    121. namespace: istio-system
    122. spec:
    123. severity_levels:
    124. warning: 1 # Params.Level.WARNING
    125. outputAsJson: true
    126. ---
    127. # Rule to send logentry instances to a stdio handler
    128. apiVersion: "config.istio.io/v1alpha2"
    129. kind: rule
    130. metadata:
    131. name: newlogstdio
    132. namespace: istio-system
    133. spec:
    134. match: "true" # match for all requests
    135. actions:
    136. - handler: newhandler.stdio
    137. instances:
    138. - newlog.logentry
    139. ---
  2. 推送新配置。

    1. istioctl create -f new_telemetry.yaml

    期待输出类似内容:

    1. Created config metric/istio-system/doublerequestcount at revision 1973035
    2. Created config prometheus/istio-system/doublehandler at revision 1973036
    3. Created config rule/istio-system/doubleprom at revision 1973037
    4. Created config logentry/istio-system/newlog at revision 1973038
    5. Created config stdio/istio-system/newhandler at revision 1973039
    6. Created config rule/istio-system/newlogstdio at revision 1973041
  3. 发送流量到示例的应用程序。

    以Bookinfo的例子为例,在浏览器中访问http://$GATEWAY_URL/productpage或者使用以下命令:

    1. curl http://$GATEWAY_URL/productpage
  4. 验证已产生新的metric并且能被收集。

    在Kubernetes环境中,通过执行以下命令为Prometheus设置端口转发:

    1. kubectl -n istio-system port-forward $(kubectl -n istio-system get pod -l app=prometheus -o jsonpath='{.items[0].metadata.name}') 9090:9090 &

    通过Prometheus UI查看新metric信息。

    打开Prometheus UI,查询double_request_countConsole tab页中有类似如下信息:

    1. istio_double_request_count{destination="details.default.svc.cluster.local",instance="istio-mixer.istio-system:42422",job="istio-mesh",message="twice the fun!",source="productpage.default.svc.cluster.local"} 2
    2. istio_double_request_count{destination="ingress.istio-system.svc.cluster.local",instance="istio-mixer.istio-system:42422",job="istio-mesh",message="twice the fun!",source="unknown"} 2
    3. istio_double_request_count{destination="productpage.default.svc.cluster.local",instance="istio-mixer.istio-system:42422",job="istio-mesh",message="twice the fun!",source="ingress.istio-system.svc.cluster.local"} 2
    4. istio_double_request_count{destination="reviews.default.svc.cluster.local",instance="istio-mixer.istio-system:42422",job="istio-mesh",message="twice the fun!",source="productpage.default.svc.cluster.local"} 2

    有关查询Prometheus获取更多metric信息,请参阅查询Istio Metrics

  5. 验证日志流是否已创建并可使用。

    在Kubernetes环境中,使用以下方式搜索Mixer pod的日志:

    1. kubectl -n istio-system logs $(kubectl -n istio-system get pods -l istio=mixer -o jsonpath='{.items[0].metadata.name}') mixer | grep \"instance\":\"newlog.logentry.istio-system\"

    输出类似以下内容:

    1. {"level":"warn","ts":"2017-09-21T04:33:31.249Z","instance":"newlog.logentry.istio-system","destination":"details","latency":"6.848ms","responseCode":200,"responseSize":178,"source":"productpage","user":"unknown"}
    2. {"level":"warn","ts":"2017-09-21T04:33:31.291Z","instance":"newlog.logentry.istio-system","destination":"ratings","latency":"6.753ms","responseCode":200,"responseSize":48,"source":"reviews","user":"unknown"}
    3. {"level":"warn","ts":"2017-09-21T04:33:31.263Z","instance":"newlog.logentry.istio-system","destination":"reviews","latency":"39.848ms","responseCode":200,"responseSize":379,"source":"productpage","user":"unknown"}
    4. {"level":"warn","ts":"2017-09-21T04:33:31.239Z","instance":"newlog.logentry.istio-system","destination":"productpage","latency":"67.675ms","responseCode":200,"responseSize":5599,"source":"ingress.istio-system.svc.cluster.local","user":"unknown"}
    5. {"level":"warn","ts":"2017-09-21T04:33:31.233Z","instance":"newlog.logentry.istio-system","destination":"ingress.istio-system.svc.cluster.local","latency":"74.47ms","responseCode":200,"responseSize":5599,"source":"unknown","user":"unknown"}

理解监控的配置

在本章中,您添加了Istio配置,委托Mixer自动生成并报告mesh内所有流量的新metric和新日志流。

新增配置控制Mixer功能的三块:

  1. instance生成(在本例中为metrcis值和日志),使用Istio属性。

  2. handler(配置好的Mixer适配器)的创建,能够处理已生成的实例

  3. 根据一套规则分发instancehandler

理解metrics配置

metrics配置指定Mixer把metrics指标值发送给Prometheus。它由三部分(或块)的配置组成:instance配置,handler配置和rule配置。

kind: metric 这部分配置定义了一个名为doublerequestcount的新metric模板去生成metric值(或实例)。这个实例的配置告诉Mixer如何根据Envoy返回的属性(同样由Mixer自身生成)为任何给定的请求生成metric。

doublerequestcount.metric中的配置指定Mixer为每个instance的值是2。由于Istio为每个请求生成一个instance,这意味着这个metric的值等于收到请求总数的两倍。

每个doublerequestcount.metric instance指定一个dimensions。Dimensions提供了根据不同的需求和查询方式来分割,聚合和分析度量数据的方法。例如,在对应用程序行为进行故障排除时,可能只需要关注对某个服务的请求。

根据属性值和实际的值配置Dimensions来约定Mixer的行为。例如,对于source dimensions,新的配置请求从source.service属性取值。如果该属性值未设置,则该规则中Mixer会使用默认值unknown。对于message,设置值为"twice the fun!"将用于所有实例。

kind: prometheus:定义了一个名为doublehandlerhandlerspec配置prometheus适配器代码如何把接收的metric值转换为Prometheus后端可处理的格式。此配置指定了一个名为double_request_count的新Prometheus指标,有三个标签(与doublerequestcount.metric实例配置的相匹配)。

对于kind:prometheus这个handler,Mixer实例通过instance_name参数与Prometheus的metrics相匹配。 instance_name值必须和Mixer实例的名称完全一致(例如:doublerequestcount.metric.istio-system)。

kind: rule配置定义了一个名为doubleprom的新规则。在该规则下Mixer将所有doublerequestcount.metric实例发送到doublehandler.prometheus处理。由于规则中没有match子句,并且由于该规则位于已配置默认命名空间(istio-system),因此mesh中的所有请求都会执行该规则。

理解日志的配置

日志配置指定Mixer发送日志到stdout(标准输出)。它使用三部分(或块)配置:instance配置,handler配置和rule配置。

kind: logentry配置定义了一个名为newlog的日志对象(或instance)的模型。这个配置告诉Mixer如何根据Envoy返回的属性生成日志对象。

severity参数用于指定任何要生成logentry的日志级别。在这个例子中,使用了"warning"。该值将通过logentry项填充生成的日志中。

timestamp参数为所有日志提供时间信息。在本例中,用Envoy提供request.time属性来设置时间。

variables参数允许操作员配置每个logentry应包含哪些值。用表达式控制从Istio属性和设定值的映射关系来构成一个logentry。在此示例中,每个logentry实例都有一个名为latency的字段,对应了属性response.duration的值。如果response.duration没有值,则latency默认设置为0ms。

kind: stdio定义了一个名为newhandlerhandlerspec配置stdio适配器代码如何处理收到logentry实例。 severity_levels参数控制severity字段和logentry值如何映射到已支持的日志级别。这里,"warning"的值被映射到WARNING级别的日志。 outputAsJson参数指定适配器生成JSON格式的日志。

kind: rule定义了一个名为newlogstdio的新规则。该规则指定Mixer将newlog.logentry的所有实例发送到newhandler.stdio的handler。由于match参数设置为true,所以对mesh中的所有请求都执行该规则。

为所有请求都执行该规则是不需要明确配置match: truespec省略match参数项配置相当于设置match:true。这里是为了说明如何使用match表达式来配置控制规则。

清除

  • 删除监控配置:

    1. istioctl delete -f new_telemetry.yaml
  • 如果您不打算继续后面的章节,请参阅BookInfo cleanup的说明关闭应用程序。

进一步阅读