1. Sending Azure Function Execution Logs to Azure Log Analytics

    Python

    To send Azure Function execution logs to Azure Log Analytics, you need to set up a couple of Azure resources:

    1. Azure Log Analytics Workspace: This is the first resource you need. It is a unique environment within Azure Monitor for your Azure and non-Azure resources where logs and metrics are collected, analyzed, and stored.

    2. Azure Function: This is the compute resource running your code. You must configure it to send diagnostic logs to your Log Analytics workspace.

    For this setup, the essential part is configuring the diagnostic settings for your Azure Function to send logs to your Log Analytics workspace. Azure provides several ways to do this, including the Azure portal, Azure CLI, PowerShell, and ARM templates. Here, we will show how to accomplish this using Pulumi, which enables you to declare infrastructure as code in popular programming languages.

    Here's how we can define and deploy these resources using Pulumi with Python:

    import pulumi import pulumi_azure_native as azure_native # Create an Azure Resource Group resource_group = azure_native.resources.ResourceGroup("my-resource-group") # Create an Azure Log Analytics Workspace log_analytics_workspace = azure_native.operationalinsights.Workspace( "my-log-analytics-workspace", resource_group_name=resource_group.name, location=resource_group.location, sku=azure_native.operationalinsights.SkuArgs( name="PerGB2018" ), retention_in_days=30 ) # Assume an existing Azure Function for which you want to send logs to Log Analytics # Here's a placeholder for it: function_app = azure_native.web.FunctionApp( "my-function-app", resource_group_name=resource_group.name, location=resource_group.location, # Other config ... ) # Now we setup the Diagnostic Settings to send logs to the Log Analytics Workspace diagnostic_setting = azure_native.insights.DiagnosticSetting( "my-diagnostic-setting", target_resource_id=function_app.id, logs=[azure_native.insights.LogSettingsArgs( category="FunctionExecutionLogs", enabled=True, retention_policy=azure_native.insights.RetentionPolicyArgs( enabled=True, days=30, ), )], workspace_id=log_analytics_workspace.id, resource_uri=function_app.id ) # Output the Workspace ID where logs will be sent pulumi.export('log_analytics_workspace_id', log_analytics_workspace.id)

    In this program:

    • We create an Azure Resource Group to keep our resources together.
    • Next, we set up the Log Analytics Workspace where we specify the retention period for our logs.
    • We then define an existing Function App assuming it's already created. To actually manage the Azure Function with Pulumi, you would include the configuration details.
    • The Diagnostic Setting resource is where we tell Azure to send the function's logs to the specified workspace. We enable FunctionExecutionLogs log category and set the retention policy.
    • Finally, we export the ID of the Log Analytics Workspace so you can reference it elsewhere, for example, in your monitoring tools or other Pulumi stacks.

    This setup will now ensure that your Azure Function's execution logs are sent to the Azure Log Analytics workspace, from where you can query and monitor the performance and status of your Azure Functions.

    To deploy this infrastructure, you simply run pulumi up after setting up Pulumi with Azure. The logs can then be queried and monitored using Azure Monitor or Azure Log Analytics queries.