fuglede
fuglede

Reputation: 18221

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?

Upvotes: 1

Views: 462

Answers (2)

petrovski
petrovski

Reputation: 181

Here's a solution where you can still supply the custom_dimensions in logger.error() at execution time:

import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
    
def remove_items(envelope):
    for k in ['process', 'module', 'fileName', 'lineNumber', 'level']: 
        envelope.data.baseData.properties.pop(k)
    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', custom_dimensions={'foo': 'bar'})

Upvotes: 0

fuglede
fuglede

Reputation: 18221

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.

Upvotes: 3

Related Questions