Querying Python logs Azure Application Insight

You can send your Python logs to Azure Application Insights from anywhere and then leverage the Application Insights query and dashboard capabilities to do log analysis.  Getting access to the logs is trivial.

I wanted to plot basic internet performance information from data generated from two different machines in two different locations.  The source code is on GitHub here freemansoft/speedtest-app-insights. That project runs speedtest.net measurements and then posts them to Azure Application Insights.  It logs the raw data when the --verbose switch is set.  That verbose output is sent to Azure App Insights.


Execution pre-requisites

  1. You have an Azure login
  2. You have created an Azure Application Insights Application key https://docs.microsoft.com/en-us/azure/azure-monitor/app/create-new-resource
  3. You have pushed data to Application Insights.  I used https://github.com/freemansoft/speedtest-app-insights with the _--verbose__ switch

Video walkthrough

not yet available

Data Capture Notes

Our test program generates a single log message that is sent to Azure. We can see these messages in the Logs section as individual events. They appear as _traces_ which is the _OpenCensus_ naming convention and does not represent a true _Tracing event_. Metrics, Tracing, and Logs are all captured in the form of individual events in the Logs section of Application Insights. This is in addition to any other view on the same data.

Querying Metrics in the Application Insights Logs

  • Log in to the Azure portal 
  • Open up the Application Insights  resource that contains your data
  • Scroll down the left-hand side until you find Monitoring / Logs
  • You will get a New Query 1 panel with a pop-up panel on top of it that tries to give you useful web service queries.
  • Close the popup.  You should see something like the right side of this

Python log messages are called "traces" in the OpenCensus world which carries into here.
  1. Type traces in the query window.
  2. Delete any extra characters.  You can run more sophisticated queries later
  3. Click on the blue Run Query button
  4. You will see logging events in the results panel.

The results will show up in the lower right pane.  The entire message payload is put into the message column as a string.  The body of the log message of the first three events shown below were actually JSON strings and that is why they show up here as JSON.  You can see some text and a JSON string just below the red  square.


You can open up the full details of a message by clicking on the row.  The picture highlights
1. The logging message itself in a single field.
1. Logger metadata from the source logging framework that is stored here as custom dimensions.

References

Python Metrics and logs in Azure Applications Insights
  • https://joe.blog.freemansoft.com/2022/11/querying-python-transactions-and-spans.html
  • https://joe.blog.freemansoft.com/2021/03/querying-python-logs-azure-application.html
  • https://joe.blog.freemansoft.com/2021/03/querying-python-metrics-custom-tags-as.html
  • https://joe.blog.freemansoft.com/2021/03/displaying-python-metrics-in-azure.html
  • https://joe.blog.freemansoft.com/2021/03/monitor-internet-broadband-service-with.html
Microsoft Guides
  • https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python
  • https://docs.microsoft.com/en-us/azure/azure-monitor/app/create-new-resource
  • https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure
Python Data generator 
  • https://github.com/freemansoft/speedtest-app-insights
Created 3/2021

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs