3
votes

I am monitoring a lot of applications in Azure Application Insights. In all of them I add some custom properties to events, traces etc so I can filter/group in the portal.

Is it possible to add the same custom properties to the built in application insight integration with Azure Functions?

Have read the documentation but can't find anything about it.

Edit:

I maintain a large number of applications hosted in various environments. About 15 of these are Azure Functions. From all my applications I send telemetry to the same application insights instance via a log handler. To filter/group the information I add "CustomerName" and "CustomerInstance" properties to all events automatically via my log handler.

When I get the standard events from an Azure Function it is difficult to present information in a useful way and correlate it with other events. With some clever naming of the function apps I can parse the request url in analytics but not in the portal.

1
I am having the same problem. Did you make any headway with this issue?Pratik Bhattacharya
@PratikBhattacharya, No, I disabled the built in AI-integration and create my own requests, performance etc using the normal nuget packages (dependencycollector etc) as I do in all my non-function applications. Unfortunately function v2 does not allow custom code to run before/after functions so I still use v1.adrianm

1 Answers

3
votes

You can add these custom properties explicitly using telemetry.Context.Properties.Add() method.

I did a demo with function v2 as below:

1.Create a function v2 in visual studio

2.Then in the visual studio, add Microsoft.ApplicationInsights 2.8.1(latest version) via nuget package manager

3.In your Function.cs, write the following code:

using Microsoft.ApplicationInsights;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;

namespace FunctionApp17
{
    public static class Function1
    {
        private static string key = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY",EnvironmentVariableTarget.Process);
        private static TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey= key };

        [FunctionName("Function1")]
        public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log)
        {
            if (!telemetry.Context.Properties.ContainsKey("Function_appName"))
            {
                telemetry.Context.Properties.Add("Function_appName", "myfuncapp111");
            }
            else
            {
                telemetry.Context.Properties["Function_appName"] = "myfuncapp111";
            }
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            telemetry.TrackEvent("event111");
            telemetry.TrackTrace("trace111");
        }
    }
}

4.Publish to azure, and in your function app -> Application settings, add the instrumentation key: enter image description here

5.After the function app is running, nav to your application insights -> search, you can add your filters which defined in your code.

Then you can see the filtered message: enter image description here