【问题标题】:Remove customDimensions items from Application Insights when using opencensus-python使用 opencensus-python 时从 Application Insights 中删除 customDimensions 项
【发布时间】:2024-01-22 20:45:01
【问题描述】:

the documentation 关于如何使用 opencensus-python 向 Azure Application Insights 提交跟踪中,详细说明了如何向 customDimensions 字段添加其他信息。也就是说,

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}})

变成

在 Application Insights UI 中。

这一切都很好,但是从customDimensions 中删除默认包含的项目的预期方法是什么?即 fileNameprocess 之类的东西?

【问题讨论】:

    标签: python azure azure-application-insights azure-monitoring opencensus


    【解决方案1】:

    inspection of the source code 看来,这些属性似乎很难避免创建,但可以通过对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')
    

    这是在opencensus-ext-azure 1.0.5 版中测试和工作的。

    另请注意,使用此方法,登录时不再需要指定extra

    【讨论】: