首页 > 解决方案 > 如果未发送客户端,如何在 APIM 入站策略中获取/设置 Traceparent 标头?

问题描述

我们想要关联 Azure APIM 和 Application Insight 中的请求。对于 API,我们有一个在入站和出站部分使用发送请求的策略。我们正在使用W3C 分布式跟踪 Azure规范

现在,如果客户端不发送 traceparent 标头,则入站中的发送请求在应用程序洞察力中没有关联。

如果我们尝试在入站策略中设置 traceparent 标头,它将在策略的后端部分被覆盖。看起来 APIM 检查传入的请求,如果没有设置 traceparent,它将生成它。但是我们不能在策略中的传入请求中添加标头(只读)。

示例政策

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="inboundresponse" timeout="10" ignore-error="true">
            <set-url>https://someUrl.com</set-url>
            <set-method>GET</set-method>
            <set-header name="traceparent" exists-action="skip">
                <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
            </set-header>
            <set-body></set-body>
        </send-request>
        <!-- for test set fixed value, but this value is overwritten by Azure APIM in backend
         and all 3 requests are not coorrelated -->
        <set-header name="traceparent" exists-action="skip">
            <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <!-- traceparent value we get here is not the same that we set -->
        <send-request mode="new" response-variable-name="outboundresponse" timeout="10" ignore-error="true">
            <set-url>https://someUrl.com</set-url>
            <set-method>GET</set-method>
            <set-header name="traceparent" exists-action="skip">
                <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
            </set-header>
            <set-body></set-body>
        </send-request>
        
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

痕迹

标签: azurehttp-headersazure-application-insightsazure-api-managementdistributed-tracing

解决方案


可能发生的情况是它正在生成具有非法格式的 traceID,当这种情况发生时,它会生成一个新格式。

HEXDIGLC = DIGIT / "a" / "b" / "c" / "d" / "e" / "f" ; lowercase hex character
value           = version "-" version-format

查看文档


推荐阅读