Search code examples
azureazure-functionsazure-application-insights

Azure Function is not logging LogMetric telemetry to Application Insights


I have a 3-week-old Function on the v2 SDK and uses the out-box App Insights support, i.e. just set the environment variable and ...magic.

I did read that by default it doesn't send Information level logs to AppInsights and this looks correct because I can't see any of my information tracing but I do see occasional errors I've logged out with my custom message.

But for metrics, its broken. I can see some semblance of my telemetry in the logs, sort of. It's strange.

Look.

VS Code screen shot


Now look at these curious logs.

Log Analytics screen shot of some log lines

...see those n/a that match the LogMetric lines in my code?


And when I search explicitly for one of them, nothing is found.

Log Analytics screen shot with no results


And if I drill down on those n/a logs.

Log Analytics screen shot of strange log entries with not much data

...they're pretty empty.


Is it me or is this all rather fishy?

How do I log metrics from a Function in a way that works?


Solution

  • First, for the metric, do you mean custom metrics? If yes, then you should find it in the customMetrics table in your app insights.

    enter image description here

    Second, what's the method are you using to send metrics? From your code, what's the implementation of this.Logger?.LogMetric method?

    For sending metrics, you can use TrackMetric(just tested with this method, and works) or GetMetric method.

    A sample code use TrackMetric(deprecated, but still can use) / GetMetric(recommended) method:

                TelemetryConfiguration.Active.InstrumentationKey = "your key";
                TelemetryClient client = new TelemetryClient();            
                client.TrackMetric("my metric", 60);
                client.GetMetric("my metric").TrackValue(89);
                client.Flush();