istio.io/content/zh/docs/tasks/observability/metrics/customize-metrics/index.md

4.6 KiB
Raw Blame History

title description weight keywords owner test
自定义 Istio 指标 此任务向您展示如何自定义 Istio 指标。 25
telemetry
metrics
customize
istio/wg-policies-and-telemetry-maintainers yes

此任务向您展示如何自定义 Istio 生成的指标。

Istio 可以生成各种仪表盘所使用的遥测数据,帮助您直观地显示您的网格信息。 例如,支持 Istio 的仪表盘包括:

默认情况下Istio 定义并生成一组标准指标(例如 requests_total),但您也可以使用 Telemetry API 自定义标准指标并创建新指标。

开始之前

在集群中安装 Istio 并部署应用程序。 或者,您可以设置自定义统计作为 Istio 安装的一部分。

Bookinfo 示例应用程序在此任务中用作示例应用程序。 关于安装说明,请参阅部署 Bookinfo 示例

启用自定义指标

例如要自定义遥测指标,可以使用以下命令,沿着入站和出站方向,将 request_hostdestination_port 维度添加到同由 Gateway 和 Sidecar 发出的 requests_total

{{< text bash >}} $ cat < ./custom_metrics.yaml apiVersion: telemetry.istio.io/v1 kind: Telemetry metadata: name: namespace-metrics spec: metrics:

  • providers:
    • name: prometheus overrides:
    • match: metric: REQUEST_COUNT tagOverrides: destination_port: value: "string(destination.port)" request_host: value: "request.host" EOF $ kubectl apply -f custom_metrics.yaml {{< /text >}}

验证结果

将流量发送到网格。对于 Bookinfo 示例,请在您的网络浏览器中访问 http://$GATEWAY_URL/productpage 或运行以下命令:

{{< text bash >}} $ curl "http://$GATEWAY_URL/productpage" {{< /text >}}

{{< tip >}} $GATEWAY_URLBookinfo 示例中设置的值。 {{< /tip >}}

使用以下命令验证 Istio 是否为您的新维度或修改后的维度生成数据:

{{< text bash >}} kubectl exec "(kubectl get pod -l app=productpage -o jsonpath='{.items[0].metadata.name}')" -c istio-proxy -- curl -sS 'localhost:15000/stats/prometheus' | grep istio_requests_total {{< /text >}}

例如,在输出中,找到指标 istio_requests_total 并验证它是否包含您的新维度。

{{< tip >}} 代理开始应用配置可能需要很短的时间。如果未收到该指标,您可以在稍等片刻后重试发送请求, 然后再次查找该指标。 {{< /tip >}}

对值使用表达式

指标配置中的值是常用表达式,这意味着 JSON 中的字符必须双引号(例如:"'string value'")。 与 Mixer 表达式语言不同,不支持 pipe|)运算符, 但您可以使用 hasin 操作符来模拟它,例如:

{{< text plain >}} has(request.host) ? request.host : "unknown" {{< /text >}}

有关详细信息,请参阅通用表达式语言

Istio 公开了所有标准 Envoy 属性。 对等元数据可用作出站属性 upstream_peer 和入站属性 downstream_peer,具有以下字段:

字段 类型
app string Application 名称。
version string Application 版本。
service string 服务实例。
revision string 服务版本。
name string Pod 名称。
namespace string Pod 所处命名空间。
type string 工作负载类型。
workload string 工作负载名称。
cluster string 此工作负载所属集群的标识符。

例如,要在出站配置中使用的对等 app 标签的表达式是 filter_state.downstream_peer.appfilter_state.upstream_peer.app

清理

要删除 Bookinfo 示例应用及其配置,请参阅 Bookinfo 清理