CodexBloom - Programming Q&A Platform

Azure Application Insights not capturing custom metrics from Azure Functions with Python

👀 Views: 1 💬 Answers: 1 📅 Created: 2025-06-09
azure-functions application-insights python Python

I'm working on a personal project and I'm optimizing some code but I've been researching this but I've been struggling with this for a few days now and could really use some help. I'm currently using Azure Functions with a Python 3.8 runtime, and I've implemented custom telemetry tracking using Azure Application Insights. However, I'm working with issues where my custom metrics are not being captured in Application Insights even though I can see the default metrics and logs. Here’s the code snippet I’m using to log my custom metric: ```python import logging from applicationinsights import TelemetryClient # Initialize the telemetry client with your Instrumentation Key instrumentation_key = 'YOUR_INSTRUMENTATION_KEY' tc = TelemetryClient(instrumentation_key) def main(req): # Log a custom event tc.track_event('CustomEvent', {'param1': 'value1'}) tc.track_metric('CustomMetric', 42) tc.flush() # Make sure to flush the telemetry return 'Hello World!' ``` I've made sure to replace `YOUR_INSTRUMENTATION_KEY` with my actual key. I also checked the Application Insights resource in the Azure portal, but I don't see any sign of my custom metrics appearing. I've tried adding `tc.flush()` after logging the metrics, but it doesn’t seem to help. Additionally, I verified that Application Insights is correctly configured in my Azure Function’s settings. I’m aware that the telemetry might take some time to appear, but it’s been more than 24 hours. I couldn't find any relevant errors in the Azure logs. Are there any specific configurations I might be missing, or is there a different way to ensure that my metrics are being sent correctly? Any help would be greatly appreciated! This is part of a larger CLI tool I'm building. I'm working with Python in a Docker container on CentOS. Am I approaching this the right way? This issue appeared after updating to Python LTS. I'm open to any suggestions.