Azure API 管理和 Application Insights,与入站范围内使用发送请求策略完成的请求不相关的跟踪

问题描述 投票:0回答:1

问题: 我正在构建一个 composition API 资源,我在入站范围内定义了一些

send-request
策略。为了实现可观察性,我们使用 Application Insights,我已在 APIM 中激活了它。 APIM 中的日志记录是在全局级别上配置的,具有 100% 采样、详细和传统协议,不会遗漏任何内容。我想要实现的目标是通过 Application Insights 跟踪并显示我的所有服务,以便我可以利用“故障”和“性能”选项卡下的事务诊断。所有下游服务也利用自己的 Application Insights,并且基于 .Net C#。

我的预期结果是 APIM 将对我的

Request-Id
策略调用的所有依赖服务使用相同的
send-request
,这将在事务诊断视图中产生完整的跟踪。

实际结果是一个跟踪图,其中仅记录传入请求,而不记录调用的服务。 然而,对于在

backend
范围内发出的请求,依赖性是正确关联的。

我已经找到了解决方法,我所有的调试工作都表明 APIM 没有将

Request-Id
标头附加到入站部分中的
send-request
策略中。

解决方法: 互联网上的一些人建议手动添加一个

set-header
,最好是像这样的全局策略来解决这个问题:

<set-header name="Request-Id" exists-action="skip">
   <value>@(context.RequestId.ToString())</value>
</set-header>

上述内容与

mode="copy"
set-header
政策中的
send-request
政策相结合确实有效。 然而,只有当我们强制客户端在原始请求中发送
Request-Id
标头时,这才是正确的,这是非常脆弱的。我们不能让我们的内部跟踪依赖于 API 调用者正确使用它。 如果调用者不发送它,则上述策略将尝试解析应用程序洞察用于策略中的初始请求和后端请求的
Request-Id
值,但实际结果是该值将与显示的值不同作为 Application Insights 中用于关联的操作 ID,这会破坏跟踪并且不会显示任何依赖项。

需要明确的是,设置

Ocp-Apim-Trace: true
标头后,blob 存储中可用的内置跟踪不是我想要的,它已经可以工作了。

我觉得这是非常基本的功能,我一定做错了什么。非常感谢任何帮助!

下面是一个示例配置,它没有按应有的方式进行跟踪。 全局策略是默认的,只添加了 CORS:

<policies>
<inbound>
    <cors allow-credentials="true">
        <allowed-origins>
            <origin>https://api.website.com</origin>
        </allowed-origins>
        <allowed-methods preflight-result-max-age="300">
            <method>*</method>
        </allowed-methods>
        <allowed-headers>
            <header>*</header>
        </allowed-headers>
        <expose-headers>
            <header>*</header>
        </expose-headers>
    </cors>
</inbound>
<backend>
    <forward-request />
</backend>
<outbound />
<on-error />
API 范围策略是默认的:
<policies>
<inbound>
    <base />
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
</outbound>
<on-error>
    <base />
</on-error>
以及在入站范围内使用单个“发送请求”的简化端点策略:
<policies>
<inbound>
    <base />
    <!-- No requests done in this inbound scope get the `Request-Id` header and consequently no traces -->
    <send-request mode="copy" response-variable-name="service1" timeout="10" ignore-error="false">
        <set-url>myservice</set-url>
        <set-method>POST</set-method>
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-body>"hello": "world"</set-body>
    </send-request>
</inbound>
<backend>
    <base />
    <!-- the forwarded request in this backend block always get the correct `Request-Id` header, without any configuration at all. -->
</backend>
<outbound>
    <base />
</outbound>
<on-error>
    <base />
</on-error>

编辑1: 类似的问题但不同的相关协议: 如果客户端未发送,如何在 APIM 入站策略中获取/设置 Traceparent 标头?

编辑2: 更正示例中的错误

c# azure azure-application-insights azure-api-management distributed-tracing
1个回答
0
投票

您好,这个问题是怎么解决的,我也有同样的情况! 感谢您的反馈

© www.soinside.com 2019 - 2024. All rights reserved.