> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/bu-shu-yun-wei-xu-ni-hua/kubernetes/12.6istio-he-xin-gong-neng-shi-jian-zhi-biao-shou-ji-he-cha-xun.md).

# 12.6Istio核心功能实践-指标收集和查询

使用Istio Mixer和Istio sidecar获取指标和日志，并在不同的服务间进行追踪

1.收集指标：配置Mixer，收集Bookinfo应用中所有服务的系列指标

从istio属性中生成instance（这里是指标值和日志条目）

```yaml
# Configuration for metric instances
apiVersion: config.istio.io/v1alpha2
kind: instance
metadata:
  name: doublerequestcount
  namespace: istio-system
spec:
  compiledTemplate: metric
  params:
    value: "2" # count each request twice
    dimensions:
      reporter: conditional((context.reporter.kind | "inbound") == "outbound", "client", "server")
      source: source.workload.name | "unknown"
      destination: destination.workload.name | "unknown"
      message: '"twice the fun!"'
    monitored_resource_type: '"UNSPECIFIED"'
```

> 提示：指标来自于Envoy汇报的属性

创建handler（配置Mixer适配器），用来处理生成的instance

```yaml
# Configuration for a Prometheus handler
apiVersion: config.istio.io/v1alpha2
kind: handler
metadata:
  name: doublehandler
  namespace: istio-system
spec:
  compiledAdapter: prometheus
  params:
    metrics:
    - name: double_request_count # Prometheus metric name
      instance_name: doublerequestcount.instance.istio-system # Mixer instance name (fully-qualified)
      kind: COUNTER
      label_names:
      - reporter
      - source
      - destination
      - message
```

> reporter、source、destination、message将转化为汇报到Prometheus中数据的key
>
> instance\_name必须为全限定性名称：名称.类型.命名空间，如oublerequestcount.instance.istio-system

根据一系列的rule，把instance传递给handler

```yaml
# Rule to send metric instances to a Prometheus handler
apiVersion: config.istio.io/v1alpha2
kind: rule
metadata:
  name: doubleprom
  namespace: istio-system
spec:
  actions:
  - handler: doublehandler
    instances: [ doublerequestcount ]
```

kubectl get ingress -n istio-system

```bash
kubectl get ingress -n istio-system
```

2.查询指标：安装Prometheus插件，用来收集指标，并在Prometheus服务中查询istio指标

3.分布式追踪：这个任务会使用Istio来对应用中请求的流动路径进行追踪

jaeger

zipkin

LightStep

4.使用Istio Dashboard：安装Grafana插件，这一插件中带有一个预配置DashBoard，可以用来对服务网格中的流量进行监控
