azure - 如果客户端未发送,如何在 APIM 入站策略中获取/设置 Traceparent header ?

标签 azure http-headers azure-application-insights azure-api-management distributed-tracing

我们希望将 Azure APIM 和 Application Insight 中的请求关联起来。对于 API,我们有一个在入站和出站部分使用发送请求的策略。我们正在使用 W3C distributed tracing Azure , specification

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

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

政策示例

<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>

Trace

最佳答案

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

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

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

更新

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

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

通过 context.RequestId.ToString("N"),您可以获取请求的内部相关 ID,并对其进行格式化(不带破折号)。另外值得一提的是:

您是否已在“设置”选项卡中将关联格式设置为 W3C? enter image description here

关于azure - 如果客户端未发送,如何在 APIM 入站策略中获取/设置 Traceparent header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63868924/

相关文章:

javascript - 使用 JavaScript 在客户端修改 HTTP header

javascript - 如何格式化来自 getJSON() jQuery 调用的 HTTP 请求的 HTTP 响应

azure - Blob 触发器影响 Azure 函数中的应用程序洞察日志记录

azure-web-app-service - 即使在删除可用性测试后,Azure 网站的 Application Insights 也会每五分钟显示一次 GET/请求

azure - Application Insights 未显示所有错误

iis - 我可以在实时 Azure 云上使用自签名证书吗?

php - 从 PHP 创建、显示图像

c# - 我在 Application Insights 中看不到我的轨迹跟踪消息

azure - 如何通过 Azure ARM 模板部署引用不在 VM/应用程序网关资源组中的另一个 Vnet 的 VM/应用程序网关

Azure AD token 颁发端点不返回 "scope"参数