Access Span ID for Application Insights Traces created by API Management

Knut Wannheden 21 Reputation points
2021-01-12T15:26:26.56+00:00

We have enabled tracing to Application Insights for our APIs in API Management. Now, when a called backend does some of its own tracing and sends that data to Application Insights, the spans (W3C Tracing nomenclature) don't have the spans created by API Management as their parent. This is for obvious reasons: I found no API that would let me in a policy access the span ID for the span created by API Management and set the W3C Tracing header accordingly. Did I miss something or does this not make any sense?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,768 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JayaC-MSFT 5,526 Reputation points
    2021-01-18T15:26:48.077+00:00

    Hello @Knut Wannheden I believe you want to create the dependency with the legacy id : https://github.com/microsoft/ApplicationInsights-Announcements/issues/27
    For correlation headers : https://learn.microsoft.com/en-us/azure/azure-monitor/app/correlation#correlation-headers-using-w3c-tracecontext

    -----------------------------------------------------------------------------------------------------------

    UPDATE :

    The feature is automatic - developer should not need to do anything for http interactions as this is handled by the App Insights SDK.
    For example:
    (1) I deploy a http function with app insights resource
    (2) I deploy APIM and add App Insights via the ikey to APIM (this could be the same resource as the function, or i could create a new App Insights resource for APIM)
    (3) I add the function as a new API to APIM and configure the API (in settings) to point to my APIM App Insights resource
    (4) Trigger APIM API via the Azure Portal "test" page
    (5) observe in App Insights after a minute or so
    Note: It's important to send more than one request - ideally for at least 1 min to allow App Insights to correlate correctly.

    61837-image.png
    [Function-call correctly correlated without writing any extra code]

    The reason this works is because Function looks at the W3C context and in this case (as it's a dot net/c# function) sets the activity id for the thread running the function request. By the time the function code runs the telemetry has already been correlated and any further actions (for example a downstream depend to the function) would also correlate correct. The same should apply when using App Insights SDK with any app listening on http endpoint.

    More work is required wherein there's a storage queue, or event grid or any service between resources that doesn't "see app insights" (e.g. does not support flowing the correlation information).

    References:

    https://learn.microsoft.com/en-us/azure/azure-monitor/app/custom-operations-tracking#azure-storage-queue ( this is an example of the "exception" - where it is not possible to automatically propagate the necessary values and additional work is required. For http => http this should be automatic via the AI SDK)

    Another one which might help in this case : https://tsuyoshiushio.medium.com/correlation-with-activity-with-application-insights-3-w3c-tracecontext-d9fb143c0ce2