1

Azure APIM と Application Insight で要求を関連付けたいと考えています。API については、インバウンドおよびアウトバウンド セクションで send-request を使用するポリシーがあります。W3C 分散トレース Azure仕様を使用しています

クライアントが traceparent ヘッダーを送信しない場合、インバウンドの send-request はアプリケーション インサイトで関連付けられません。

インバウンド ポリシーで 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

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 に答える