[AppInsights Preview] Custom OperationId setter
I've been testing the built in App Insights and have run into a correlation issue when setting the OperationId across multiple invocations. We sometimes have functions that trigger other functions that we'd like to correlate as part of a single 'operation'.
Something very similar to this article https://dzimchuk.net/event-correlation-in-application-insights/ but instead of web and webjobs, just two or more follow up functions.
The current suggested way to correlate custom events is to use telemetry.Context.Operation.Id = context.InvocationId.ToString(); and my understanding is that the only way to override the OperationId to get all request data is by providing a custom initializer.
I would like some way to set the Operation.Id = <myEventId>. May be if the set operation Id was propogated back so all request and duration telemetry was still correlated would be fantastic. Or some other context way to globally set this Id for the executing function.
Great work to all the team. Fantastic product and experience!
n.b. may also be related to requested features in #225
CC @christopheranderson and @brettsam
Thanks @damienpontifex. We're looking at ways to make this happen automatically. Right now we're using OperationId to correlate everything about a single function invocation (related Traces from your Function, and we're adding Dependencies in the near future). Let me sync back up with the App Insights team about cross-invocation correlation and I'll update this with our plan so you (and others) can weigh in.
I want to voice my interest in this feature too. We are setting up a pipeline like system with azure functions and being able to correlate multiple requests together would be super useful.
Just a quick query around this;
Is this proposing to change the function's operationId value... or that we want to be able to group these operations as 1 thing? I think these are 2 very different points.
IMO, Application Insights is at a point where it can provide most of what I think @damienpontifex is after but it's about setting the ParentId - I'm just hunting to find the discussion that clearly spelled it out, but roughly;
OperationId = the running operation as in the logic unit of function (as @brettsam describes) ParentId = the function that caused this one to occur (provides the parent child path of 'triggers') RequestId = the top level request's operationId (i believe) .
You then would have (All HttpTriggers);
Func1
- OperationId = 123
- ParentId = 123
- RequestId = 123 (triggers Func2) Func2
- OperationId = 456
- ParentId = 123
- RequestId = 123 (triggers Func3) Func3
- OperationId = 789
- ParentId = 456
- RequestId = 123
etc.
It ultimately does not matter if you are calling functions, webApis, taking calls from javascript (as in Http Calls) because AI are injecting auto-correlation headers in their sdk Azure/azure-webjobs-sdk#1220.
If other triggers are being used.... maybe add on the binding a property that we can declare this value is the parentId.
Thanks @TheeJamesLee I wasn't aware of the ParentId property so will check that to see if it's sufficient.
@damienpontifex I have just found links to their official explanation of their data model. The above was from the top of my head looking at a request in app insights with logical assumptions.
For reference, this is the 'official' place for their correlation in application insights.
this is the place that neatly explains their data model.
The big things in the latter is that they describe syncing the 'operation' when you are processing a request - and that may be the point of A vs B...
For example, I put a message on a queue.
- Is the processing of the Queue explicitly fulfilling the original request or not. I would say no.. I have already finished responding on the earlier network call, so the request is fulfilled. In just an Azure Functions context, Func1 was a HttpTrigger adding a payload to a queue. That function isn't waiting to respond to the request. Therefore - the processing of the Queue is a new request (beauty of async background processes)... HOWEVER it is the cause of an earlier operation ... = my 'assumption' around the Operation.ParentId. I believe the Request may actually need to be initialized as a new Id for that background worked, but I think everyone is after the same thing..... correlation in application insights, which is starting to be solved with Azure/azure-webjobs-sdk#1220.
I think my walkthrough above is slightly wrong with the assumption of the same requestId... but the Operation.ParentId is the main thing I believe will give us the links we need... this is a discussion Microsoft/ApplicationInsights-dotnet#47 (just go to the end of it) that was at the start of the correlation story and how the data is conceptually linked.
I wanted to provide a use case (which I am running in to right now and trying to figure out the best way to do it). Having a HttpTrigger function that writes out to a ServiceBus, then another function(s) with a ServiceBusTrigger pulling from that queue and processing items.
The ServiceBus system already has a BrokeredMessage.CorrelationId that could be used to flow the id from one function to the next but there is no way currently to have the Request event that triggers the function that is sent to App Insights to have that correlation id set.
@damienpontifex, @leftler, @brettsam, please have a read over this, I've done a bit more digging around 'correlation', AI docs, some test calls to really get a feel what's going on.
The stuff I am working on currently is going to be heavily used in only a few months and I'm extremely keen to help get this correlation story sorted before then so we can see what's going on cleanly.
At the end I've tried to summarize steps to get dependency tracking and 'correlation' working within here, by not re inventing stuff and using the work the AI team have done as well as keeping within their RFC and just expanding for how we could adapt it for more than just http to give us correlation across multiple types of transport mechanisms (ServiceBus, Queues, EventHubs... etc) through the bindings.
N.B. writing a note around inter service correlation across Application Insights and just moving the discussion to the better advised thread across from Azure/azure-webjobs-sdk#1220 .
The example I meant to add;
https://docs.microsoft.com/en-us/azure/application-insights/application-insights-correlation#example
In the example table I've extended a little;
itemType | name | id | operation_ParentId | operation_Id
-- | -- | -- | -- | --
1- pageView | Stock page | | STYz | STYz
2- dependency | GET /Home/Stock | qJSXU | STYz | STYz
3- request | GET Home/Stock | KqKwlrSt9PA= | qJSXU | STYz
4- dependency | GET /api/stock/value | bBrf2L7mm2g= | KqKwlrSt9PA= | STYz
5- request | GET /api/stock/value | h7D9KleQ3h8= | bBrf2L7mm2g= | STYz
6- dependency | SQL {query} | Df8JkLe9Pdu= | h7D9KleQ3h8= | STYz
(for reference, between table above and AI logs) *id column in : Dependancy is "DependancyId" | Request is "RequestId"
I've just extended the table from the link into something a little more 'multi-tier' for 'correlation'. Each 'tier' may just be another top level service for it's domain, independently deployed and managed by multiple teams and we want a cross query for a high level business question, overall microservice analysis, etc...
1 and 2, are just a 'trigger for the action' that generate, (In essence it's loging the page view link to the request on the server for a page load.. for example... but I'm focusing a little lower 3-6), 3 a request onto an Api, 4 is an output of that api that it is waiting for a response from before continuing. 5 is the request on the 'other' api (because of 4) and 6 is a dependency call into Sql Azure (for example).
I'm on purpose not making a point of saying either are a function app trigger endpoint... but if they were it demonstrates that actually each function trigger is it's own request and dependency to a transport mechanism.
However... if it was a queue transport mechanism, the 'dependency' is only the writing to the queue as that is what dictates the request being fulfilled (You're all returning 202 Accepted right guys for long running processes? ;) ). The 'Operation' is what spans to demonstrate that the queue took an hour to start to process that specific message. The start of the 'Processing' of the queue message in the thread is the "Request" of processing that message and therefore that request 'of work' ends when the processing end. The operation is what spans the "action" that was taken the initiated the processes - which fits fanning out, etc. Requests and their fulfillment are wrapped around the time something takes to respond.
For example, a user uploads an image and I'm going to resize it. The upload "Request" is dealt with putting it into storage and writing a message on the queue. The user/client makes a "Request" to see if it can get to the image and if processing is complete - still not yet. These requests are milliseconds... the Operation (Upload and see the finished image) may be from the client side applying the same operation id value (in the case of this being a http call.. in a header value).
I've just run a test to pull out some data from AI and the headers; This is a webapi that internally is making a call to requestb.in to capture the later headers to prove what's being populated where...
Request AI Data
ParentId
5f276aab-466864e06ba7049f
OperationId
5f276aab-466864e06ba7049f
RequestId
|5f276aab-466864e06ba7049f.
Makes http call to requestb.in :
Dependancy AI Data
ParentId
|5f276aab-466864e06ba7049f.
OperationId
5f276aab-466864e06ba7049f
DependancyId
|5f276aab-466864e06ba7049f.1.
-- On HTTP outbound call into requestb.in --
x-ms-request-id : (DependancyId) |5f276aab-466864e06ba7049f.1.
x-ms-request-root-id : (Initial OperationId (carried through)) 5f276aab-466864e06ba7049f
*So if this was another AI app rather than requestb.in : (See quote below)
Request AI Data
ParentId
|5f276aab-466864e06ba7049f.1.
OperationId
5f276aab-466864e06ba7049f
RequestId
2fa6950b-75ff4b3e98752e61
Does stuff....
From : https://docs.microsoft.com/en-us/azure/application-insights/application-insights-correlation#telemetry-correlation-data-model
Every outgoing operation like http call to another component represented by dependency telemetry. Dependency telemetry also defines its own id that is globally unique. Request telemetry, initiated by this dependency call, uses it as operation_parentId.
So, I think the steps to get this working are;
-
Inputs to functions need some way to bring in upstream data. -> AppInsights have defined 2 headers (seem sensible and their correlation works well - because we're using their 'service' we shouldn't be changing their approach): ------ x-ms-request-id (OperationParentId) ------ x-ms-root-id (OperationId)
-
These inputs need to be applied to the WebJobs Sdk Application Insights TelemetryClient's: ----- Context.Operation.Id = x-ms-root-id ----- Context.Operation.ParentId = x-ms-request-Id
-
Outputs from functions need to provide these headers - enabling cross "web app" (including functions..) correlation. --- Each binding needs them applied in a non breaking way that the corresponding input can pick these up. ----- ServiceBus -------- Put them in the message headers. ----- QueueBindings -------- Maybe on the bindings have the ability to add these properties and on the inbound define which properties hold these values. etc...
The 'outputs' should be tracked as dependencies and the time for them should be recorded for how long it too for them to return (ie, is writing to storage queues taking longer than normal?). The DependancyId given to that dependency metric should be applied to the x-ms-request-id value and the x-ms-root-id should be passed through unchanged.
Within functions specifically, the invocationId ... maybe that should just really be a custom property in the request so we keep the traceability.
(Some earlier parts leading to this discussion were from Azure/azure-webjobs-sdk#1220 )
@brettsam @lindydonna
Hi, I'm now reaching a point using Azure Functions that I need a more solid correlation story - or have to make a decision to review our usage of Azure Functions. Azure Functions is great for what we need and our initial tests have proved it so - however without the correlation it is just going to be impossible to manage at scale - and I'm certain I am not alone in this position. If you see my last message on this thread I have tried to consolidate an understanding of how this can be very easily achieved with how Application Insights is running and questions around a possible approach to facilitate it around the bindings. I am more than happy to contribute, but I'm not able to because there is no agreed direction I can see...
- Could it be at all possible to have a skype call to discuss Azure Functions and Correlation?
- Upcoming approach.
- Is it on the back log. Eta?
- Is there anything I can develop locally and bundle (in Compiled Functions) to do a POC / implement ahead of Eta that then will not be a breaking change on the approach so I can have this in the time frame I need it?
I truly only have a month or two before having correlation become critical - and I am a little disappointed that the message previous on this thread consolidating a suggested correlation approach hasn't had any real comments in the last 2 weeks.
The key thing I need... A http request was the cause of the chain of functions that failed because of 1 action (ie.. http func triggered, out to queue, queue runs out to another queue, 2nd queue failed)... If there is right now a way to see that (by putting everything in 1 Application Insight container to keep it simple.. and AI's recommended approach) can you let me know?
Right now I believe not because every function creates data with no up stream links!
I fully support the proposals by @TheeJamesLee above. Use of "Request-Id" headers/properties on HTTP requests and ServiceBus BrokeredMessages (as per Hierarchical Request-Id) or the Application Insights headers/properties "x-ms-request-id" and "x-ms-root-id" would be ideal, but needs to be incorporated into the Functions framework (/trigger frameworks) to correctly set the Ids on Request, Exception, ILogger, and Dependency events (including the output trigger dependencies as @TheeJamesLee mentions).
Any updates on plans for supporting such features to enable AI Application Maps and customer's cross microservice call chains (not just customer function to MS Azure resource dependency)?
Closing as many of these features have now landed. @alexkarcher-msft to elaborate.
@alexkarcher-msft @ColbyTresness Can you please elaborate? The available documentation seems to be a stub (https://docs.microsoft.com/en-us/azure/application-insights/application-insights-correlation). I can't find any documentation on "EnableW3CHeadersExtraction"