pythonazureazure-application-insightsazure-monitoringopencensus

Remove customDimensions items from Application Insights when using opencensus-python


In the documentation on how to use opencensus-python to submit traces to Azure Application Insights, it's spelled out how to add additional information to the customDimensions field. That is,

import logging

from opencensus.ext.azure.log_exporter import AzureLogHandler

logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler(
    connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
)

logger.error('blooh')
logger.error('blooh2', extra={'custom_dimensions': {'woot': 42}})

becomes

enter image description here

in the Application Insights UI.

That's all well and good, but what is the intended way to remove the items from customDimensions that are included by default; i.e. things like fileName and process?


Solution

  • By inspection of the source code, the properties do seem rather hard to avoid to create, but it is possible to remove them by post-processing the envelope:

    import logging
    
    from opencensus.ext.azure.log_exporter import AzureLogHandler
    
    custom_dimensions = {'foo': 'bar'}
    
    def remove_items(envelope):
        envelope.data.baseData.properties = custom_dimensions
        return True
    
    logger = logging.getLogger(__name__)
    handler = AzureLogHandler(connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
    handler.add_telemetry_processor(remove_items)
    logger.addHandler(handler)
    logger.error('blooh')
    

    This is tested and works in opencensus-ext-azure version 1.0.5.

    enter image description here

    Notice also that with this approarch, it is no longer necessary to specify the extra when logging.