Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Insights and Azure Functions - tracking a distributed operation

I have a process which is distributed across several functions in the following manner:

Http binding function saves a message in a queue Queue binding function processes the message, then places another message in a queue Finally another function process the message and saves something in a database.

What I would like is for application insights to understand the context of the original request and be able to follow the flow of the request across all the distributed function calls, but there doesn't appear to be a way for me to set the operation id correctly (which I believe is ultimately the "correct" way to achieve my requirement).

I can set the parent id but this just prevents the data collected from showing up at all in application insights. I'm setting the parent id using the following code:

    private static void UpdateTelemetryContext(TelemetryContext context, ExecutionContext functionContext, string id)
    {
        context.Operation.Id = functionContext.InvocationId.ToString();
        context.Operation.ParentId = id;
        context.Operation.Name = functionContext.FunctionName;
    }

The only way I can sort of achieve what I want is to set the user id to be the context id I want (the thing that links all the operations together), and set the synthetic source to be "Functions" so at least I know it's not a real user.

This is the code I'm using to achieve this:

    private static void UpdateTelemetryContext(TelemetryContext context, ExecutionContext functionContext, string id)
    {
        context.Operation.Id = functionContext.InvocationId.ToString();
        context.Operation.ParentId = functionContext.InvocationId.ToString();
        context.Operation.Name = functionContext.FunctionName;
        context.User.Id = id;
        context.Operation.SyntheticSource = "Functions";
    }

Now inside application insights I can click "show timeline for user" and see all the executions of the function group together, but this feels so wrong there just has to be a better way to achieve this.

like image 763
Ross Dargan Avatar asked Oct 29 '22 00:10

Ross Dargan


1 Answers

For anyone looking for an answer for this the only way you can do this is to disable the automated logging from application insights, and instead do it all manually.

This blog post helped me a lot: https://azure-development.com/2017/12/15/end2end-monitoring-of-azure-functions-with-application-insights/

like image 183
Ross Dargan Avatar answered Nov 11 '22 14:11

Ross Dargan