question

KnutWannheden-9556 avatar image
0 Votes"
KnutWannheden-9556 asked EdwinvandeBurgt-1999 commented

Access Span ID for Application Insights Traces created by API Management

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KnutWannheden-9556 Thank you for the question. We'll review and update at the earliest.

0 Votes 0 ·

1 Answer

JayaC-MSFT avatar image
0 Votes"
JayaC-MSFT answered EdwinvandeBurgt-1999 commented

Hello @KnutWannheden-9556 I believe you want to create the dependency with the legacy id : https://github.com/microsoft/ApplicationInsights-Announcements/issues/27
For correlation headers : https://docs.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://docs.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




image.png (564.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @JayaC-MSFT I don't think this answer really applies. We are already using W3C headers for all our tracing. When we enable the tracing in API Management it will add Request and Dependency spans to the trace as expected (with the span from the "traceparent" header as expected). But we would like a way to retrieve the span ID for the Dependency span (created by API Management) so that we can set that in the "traceparent" header we forward to the backend. I hope this clarifies our use case.

0 Votes 0 ·

And please see my related question https://docs.microsoft.com/en-us/answers/questions/553477/apim-expose-w3c-tracing-trace-id-parent-id-for-get.html



We need this to flow the traceparent to Azure services that don't support it OOB...

0 Votes 0 ·