Category: Data, Infrastructure

Working in DevOps or SRE occasionally involves reading or writing event logs and tracking events over time. In some cases, these events can trigger an alert on either a system’s state or anomalies within it.

For our use case, I’ll be writing this sample JSON payload into Azure Log Analytics every hour (or whenever I run the script): The following Python snippet can be used to generate that payload in Log Analytics Workspace: Run the script a few times after you replace url ,azure_log_customer_id and azure_log_shared_key with any url and your Workspace ID and Primary Key that you noted from Advanced Settings in your Workspace earlier.

Reading from Azure Log Analytics via API is slightly different than writing as you can see in this Python code: You will need to add in your own IDs and passwords and your Service Principal will need, at minimum, Monitor Reader RBAC on the Azure Log Analytics Workspace via IAM.

Now that we know how to read and write data to Azure Log Analytics via API, I hope that you will feel more comfortable incorporating Azure Log Analytics into your infrastructure, especially if you are using Python.

Related Articles