1

我们想要关联 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>

痕迹

4

3 回答 3

0

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

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

查看文档

于 2021-08-10T06:51:07.140 回答
0

我通过在入站中添加以下标头解决了我的问题。您可以查看文档

 <inbound> 
  <allowed-headers>
    .....
    <header>request-id</header>
    <header>request-context</header>
    <header>traceparent</header>
   </allowed-headers>
 </inbound>
于 2021-07-08T09:52:34.257 回答
0

您是否尝试过使用这样的set-header策略:

<set-header name="traceparent" exists-action="override">
     <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
</set-header>

注意覆盖 exists-action。在我的测试中,traceparent 已按预期设置并发送到后端。

更新

如果您只想在没有可用的情况下设置新的相关上下文标头,我建议您使用以下语句:

<set-header name="traceparent" exists-action="skip">
    <value>@($"00-{context.RequestId.ToString("N")}-0000000000000000-01")</value>
</set-header>

使用 ,context.RequestId.ToString("N")您可以获得请求的内部相关 id 并在没有破折号的情况下对其进行格式化。还有一点要提:

您是否将“设置”选项卡中的关联格式设置为 W3C? 在此处输入图像描述

于 2020-09-13T11:44:42.920 回答