Hello Jona,
Welcome to the Microsoft Q&A and thank you for posting your questions here. With kind of your detail explanations thank you once again.
Problem
I understand that you are having issue of duplicate log entries in Azure Application Insights where one entry includes the custom dimensions, and the other does not.
Solution
To address the issue of duplicate log entries in Azure Application Insights where one entry includes the custom dimensions and the other does not, I examined a few areas of your setup as posted. Below are some of the potential causes and solutions:
- Ensure that the
AzureLogHandler
is correctly configured and attached to the logger and the custom dimensions should be properly included in the log entries sent to Application Insights. This will help to affirm theAzureLogHandler
is only added once to prevent duplicate log entries and confirming that custom dimensions are consistently included in the log entries. - Your
host.json
configuration appears to be set up correctly for sampling settings, but it's worth verifying that there are no additional configurations that might be affecting the logging behavior. - Sometimes duplicate log entries can occur if the logger is added multiple times. Check your code to ensure that the logger is only being added once.
- Since OpenCensus is deprecated, there might be some underlying issues with how it handles custom dimensions. Upgrading to the newer OpenTelemetry SDK would be ideal, but if that's not an option, you can try the following workaround to ensure the custom dimensions are consistently included.
The below is an updated example of your function with some adjustments to ensure custom dimensions are properly logged:
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
import azure.functions as func
EXPERIMENT_NAME = 'CSV Compressed'
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Ensure the AzureLogHandler is only added once
if not any(isinstance(handler, AzureLogHandler) for handler in logger.handlers):
logger.addHandler(AzureLogHandler())
def checker_fn(blob: func.InputStream, context: func.Context):
blob_content = blob.read()
size_mb = round(len(blob_content) / (1024 * 1024), 2)
custom_dimensions = {
'experiment': EXPERIMENT_NAME,
'blob': blob.name,
'size_mb': size_mb,
}
# Log the message with custom dimensions
logger.info(f'File downloaded | {blob.name} | {size_mb:,} MB', extra={'custom_dimensions': custom_dimensions})
You can also go to your Application Insights, check the logs to see if there are still duplicate entries. Use Kusto Query Language (KQL) in Application Insights to filter and inspect the log entries. For example:
traces
| where message contains "File downloaded"
| order by timestamp desc
Accept Answer
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.
Best Regards,
Sina Salam