4
votes

I have created a Service bus triggered Azure function and want to log custom events in application insights.

 private static string key = TelemetryConfiguration.Active.InstrumentationKey =
            System.Environment.GetEnvironmentVariable(
                "APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);

        private static TelemetryClient telemetryClient =
            new TelemetryClient() { InstrumentationKey = key };

        [FunctionName("Function1")]
        public static void Run([ServiceBusTrigger("xxxxx", "xxxxx", AccessRights.Manage, Connection = "SBConnectionKey")]string mySbMsg, ILogger logger, TraceWriter log)
        {
            log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");

            telemetryClient.Context.Cloud.RoleName = "AIFunction";
            logger.LogMetric("test", 123);

            telemetryClient.TrackEvent("Ack123 Recieved");
            telemetryClient.TrackMetric("Test Metric", DateTime.Now.Millisecond);
       }

I can see only log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");this logs in the trace. But custom events and metrics are not logging to application insights. Any ideas what could be going on?

1
first thing I would verify is that you're getting the instrumentation key you expect. that it is both (1) set and (2) sending telemetry to the resource you think it is.John Gardner

1 Answers

3
votes

Answering your explicit question:

What is wrong with the telemetry that I send or where to find it in Application Insights Portal?

I created the function with almost the same code and tested. You can analyse the repo. I deployed the function and got the following results:

Event found through query

Metric found through query

Answering your implicit question:

How to use Application Insights?

It is tricky at the beginning to use App Insights query language, I found this succinct file helpful. Other elements to consider in working with this monitoring tool:

  1. there is a lagging between the telemetry is sent and you see it in application insights portal. Real time monitoring would be an expensive tool.
  2. In the past I faced the same issue and the problem was that event/metric name was not found in the name of the telemetry but somewhere in details. This issue, might be referring to it. So what we decided to do is put more details and use of this method and MetricTelemetry class.
  3. Although Application Insights might seem confusing, that is a powerful tool, it is worth investing time to learn better.