Monday, March 12, 2018

Log to Application Insights from Microsoft Flow or Logic Apps

If you haven’t checked out Azure Application Insights yet you might want to give it a look. It’s got a lot to offer in terms of  logging, monitoring, alerting, multi-colored charts and graphs, etc. Microsoft provides libraries for several languages to make logging things easier but ultimately you’re just making HTTP requests to a public endpoint.

There isn't a connector which lets you write to Application Insights but logging can be done fairly easily with a HTTP Action. I’d suggest referencing the telemetry API to figure out what options are available and how data needs to be structured as my examples won’t contain everything available.


All the requests will be:

Method: POST

In each request body:
  • Replace “time” with the Flow utcNow() function
  • Replace "00000000-0000-0000-0000-000000000000" with your Application Insights key
  • Replace “properties” with any custom key/value pairs you wish to track


Diagnostic log messages

  • Replace “message” with whatever you want to trace


User actions and other events

  • Replace “name” with the name of the event
  • Replace inside “measurements” with a measurement name (string) and value (numeric) or set to null if not using


Performance measurements such as queue lengths not related to specific events

  • Replace inside “metrics” with a metric name (string), kind (integer), value (double) – see API for additional details


Logging the duration and frequency of calls to external components that your app depends on


Recommended replacements, doesn’t need to follow this format exactly
  • Replace “id” with an id of some sort - Application Insights libraries have a way of generating an id if you care to track it down
  • Replace “name” with the HTTP verb and Url Absolute Path
  • Replace “resultCode” with the HTTP Status Code
  • Replace “duration” with the elapsed time
  • Replace “success” with true/false
  • Replace “data” with the HTTP verb and Url
  • Replace “target” with Url Host
  • Replace “type” with something describing the type of request
Tracking the time it took to execute a different HTTP request within a Flow was a little difficult. Catching the request Start and End times was fairly easy but I couldn’t seem to find a way to easily calculate the duration. I cheated a little and created an Azure Function to do it for me – not exactly ideal but whatever. Once I had that I could plug the “Calculate Duration” HTTP Action Body into the “duration”. The “resultCode” and be pulled directly from the HTTP Action. To populate “success” I used a simple formula which looked at the Status Code and set accordingly.

if(equals(outputs('Test_HTTP')['statusCode'], 200),true, false)


The Azure Function to calculate duration looks like this:

And that’s about it, you should start seeing results in your Application Insights instance.