# 链路追踪

## 链路追踪

分布式跟踪使用户可以通过跨多个服务分布的网格跟踪请求。这样可以通过可视化更深入地了解请求延迟,序列化和并行性。

Istio利用Envoy的分布式跟踪功能提供开箱即用的跟踪集成。具体来说,Istio提供了用于安装各种跟踪后端并配置代理以自动向其发送跟踪范围的选项。请参阅Zipkin,Jaeger和Lightstep任务文档,以了解Istio如何与这些跟踪系统一起工作。

## 跟踪上下文传播

尽管Istio代理能够自动发送跨度,但是它们需要一些提示才能将整个轨迹绑定在一起。应用程序需要传播适当的HTTP标头,以便当代理发送跨度信息时,可以将跨度正确关联到单个跟踪中。

为此,应用程序需要收集以下标头并将其从传入请求传播到任何传出请求:

* x-request-id
* x-b3-traceid
* x-b3-spanid
* x-b3-parentspanid
* x-b3-sampled
* x-b3-flags
* x-ot-span-context

此外,基于OpenCensus的跟踪集成(例如Stackdriver)会传播以下标头:

* x-cloud-trace-context
* traceparent
* grpc-trace-bin

productpage例如,如果查看示例Python服务,则会发现该应用程序使用OpenTracing库从HTTP请求中提取了所需的标头:

```
def getForwardHeaders(request):
    headers = {}

    # x-b3-*** headers can be populated using the opentracing span
    span = get_current_span()
    carrier = {}
    tracer.inject(
        span_context=span.context,
        format=Format.HTTP_HEADERS,
        carrier=carrier)

    headers.update(carrier)

    # ...

    incoming_headers = ['x-request-id', 'x-datadog-trace-id', 'x-datadog-parent-id', 'x-datadog-sampled']

    # ...

    for ihdr in incoming_headers:
        val = request.headers.get(ihdr)
        if val is not None:
            headers[ihdr] = val

    return headers
```

reviews应用程序(Java)使用requestHeaders以下命令执行类似操作:

```
@GET
@Path("/reviews/{productId}")
public Response bookReviewsById(@PathParam("productId") int productId, @Context HttpHeaders requestHeaders) {

  // ...

  if (ratings_enabled) {
    JsonObject ratingsResponse = getRatings(Integer.toString(productId), requestHeaders);
```

在应用程序中进行下游调用时,请确保包括这些标头。

## jaeger集成

### 先决条件

1. 部署jaeger

```
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.8/samples/addons/jaeger.yaml
```

1. 启用跟踪时,可以设置Istio用于跟踪的采样率。meshConfig.defaultConfig.tracing.sampling在安装过程中使用该选项 设置采样率。默认采样率为1％。
2. 部署Bookinfo示例应用程序。

### 访问仪表板

远程访问遥测插件详细说明了如何配置通过网关对Istio插件的访问

对于测试(和临时访问),您还可以使用端口转发。假设已将Jaeger部署到istio-system名称空间,请使用以下内容. istioctl dashboard jaeger

### 使用Bookinfo示例生成跟踪

1. 当Bookinfo应用程序启动并运行时,访问<http://$GATEWAY_URL/productpage一次或多次以生成跟踪信息。>

要查看跟踪数据,必须将请求发送到服务。请求数量取决于Istio的采样率。您在安装Istio时设置此速率。默认采样率为1％。在显示第一条跟踪之前,您至少需要发送100个请求。要将100个请求发送到productpage服务,请使用以下命令:

$ for i in $(seq 1 100); do curl -s -o /dev/null "<http://$GATEWAY_URL/productpage>"; done

1. 在信息中心的左侧窗格中,productpage.default从"服务"下拉列表中选择,然后单击" 查找跟踪":

![trace仪表板](https://istio.io/latest/docs/tasks/observability/distributed-tracing/jaeger/istio-tracing-list.png)

1. 单击顶部的最新跟踪,以查看与对以下请求的最新请求相对应的详细信息/productpage:

![详细的trace视图](https://istio.io/latest/docs/tasks/observability/distributed-tracing/jaeger/istio-tracing-details.png)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rocdu.gitbook.io/deep-understanding-of-istio/4/7/7-3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
