Logging and Monitoring Azure Functions
Effective logging and monitoring are crucial for understanding the behavior of your Azure Functions, diagnosing issues, and ensuring their performance and reliability. Azure Functions integrates seamlessly with Azure Monitor and Application Insights to provide comprehensive insights into your function executions.
Key Concepts
1. Built-in Logging
Azure Functions automatically logs information about function executions, including invocation details, errors, and warnings. You can write custom logs within your function code to capture application-specific events.
Most language SDKs provide logging utilities. Here's an example in C#:
using Microsoft.Extensions.Logging;
public static class MyFunction
{
[FunctionName("MyLoggingFunction")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation("C# Timer trigger function executed at: {Now}", DateTime.Now);
// Your function logic here
log.LogWarning("This is a sample warning message.");
try
{
// Some operation that might fail
throw new InvalidOperationException("Simulating an error.");
}
catch (Exception ex)
{
log.LogError(ex, "An error occurred during function execution.");
}
}
}
And in JavaScript (Node.js):
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? "Hello, " + name + ". This HTTP triggered function executed successfully."
: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
context.log.warn('This is a sample warning log from Node.js.');
try {
throw new Error("Simulating a JavaScript error.");
} catch (error) {
context.log.error('An error occurred: ' + error.message);
}
context.res = {
body: responseMessage
};
};
2. Application Insights Integration
Application Insights is Azure's extensible application performance management (APM) service. It provides deep insights into your application's performance, availability, and usage. When integrated with Azure Functions, Application Insights captures:
- Function invocations
- Dependencies (e.g., calls to other Azure services)
- Exceptions
- Requests
- Performance metrics
- Live metrics
APPINSIGHTS_INSTRUMENTATIONKEY
application setting.
3. Azure Monitor
Azure Monitor is the foundational service for collecting, analyzing, and acting on telemetry from your Azure and on-premises environments. It unifies metrics and logs from various Azure services, including Azure Functions. You can use Azure Monitor to:
- Create dashboards for visualizing key metrics.
- Set up alerts based on specific conditions (e.g., high error rates, long execution times).
- Analyze logs using Kusto Query Language (KQL).
Accessing Logs and Telemetry
1. Logs in the Azure Portal
For quick inspection, you can view live logs or past executions directly in the Azure portal:
- Navigate to your Function App in the Azure portal.
- Under "Monitoring", select "Log stream" for live logs or "Application Insights" to access detailed telemetry.
2. Querying Logs with Kusto Query Language (KQL)
Application Insights stores your telemetry data in a dedicated log analytics workspace. You can use KQL to write powerful queries for detailed analysis.
Some common KQL queries for Azure Functions:
// All function executions in the last hour
requests
| where timestamp > ago(1h)
| where success == true
| project timestamp, name, url, resultCode, duration
// Function executions that failed
exceptions
| where timestamp > ago(1h)
| project timestamp, problemId, severityLevel, type, outerMessage
// Logs from custom telemetry (Trace logs)
traces
| where timestamp > ago(1h)
| where message contains "custom event"
| project timestamp, message, severityLevel
3. Setting Up Alerts
Proactive alerting helps you respond to issues before they impact your users. In Azure Monitor, you can configure alerts based on various metrics and log queries.
- Go to your Function App, then "Monitoring" > "Alerts".
- Click "+ Create alert rule".
- Define the condition (e.g., "Function Execution Count" > 1000 in 5 minutes, or a custom log query that detects a high error rate).
- Configure the action group to notify you via email, SMS, or other methods.
Advanced Logging Techniques
1. Correlation
When functions call other services or are triggered by events from other services, it's important to correlate the telemetry. Application Insights automatically handles much of this correlation, but ensure you pass relevant context (like operation IDs) when making custom calls.
2. Custom Metrics
Beyond standard logs, you can emit custom metrics to track business-specific KPIs directly within Application Insights. This allows you to monitor aspects like the number of items processed, revenue generated, or specific event frequencies.
3. Distributed Tracing
For complex microservices architectures involving multiple Azure Functions and other services, distributed tracing provides an end-to-end view of a request as it flows through your system. Application Insights supports distributed tracing out-of-the-box for many scenarios.
Conclusion
Mastering logging and monitoring with Azure Functions is essential for building robust, maintainable, and performant serverless applications. By leveraging Application Insights and Azure Monitor, you gain the visibility needed to troubleshoot effectively and ensure the health of your functions.