为了账号安全,请及时绑定邮箱和手机立即绑定

获取当前InitationId或Operation_Id

获取当前InitationId或Operation_Id

红颜莎娜 2024-01-04 15:22:12
有没有办法获得一个完整的输出日志custom_dimensions?我在(Azure Functions 的)监视器选项卡中看到,仅显示带有operation_Id和 的消息。customDimensions['InvocationId']有没有办法将这两个参数添加到 opencensus 的所有日志消息中?我知道你可以使用第二个记录器。但这仅有助于调试。为了在生产中运行 Azure Functions,我需要查看这两个流。这是可能的,但效率低下且令人沮丧,因为信息是断开的并且无法总结。然而,另一种选择是加入 Azure Monitor 端的流。InvocationId但除非我知道当前或 ,否则我不能这样做operation_Id。因此我的问题是我是否可以将这两个 ID 中的任何一个添加到我当前的日志消息中我的最小示例__init__.py如下所示:from opencensus.ext.azure.log_exporter import AzureLogHandlerimport logging.configimport yamlimport azure.functions as func# Load logging configurationwith open("logging.yaml", 'rt') as f: # for local debugging add the console handler to the output    config_data = yaml.safe_load(f.read())    logging.config.dictConfig(config_data)def main(mytimer: func.TimerRequest) -> None:    try:         someID = 14        logging.debug('debug message',extra = {'custom_dimensions': {'ID': someID}})        logging.info('info message',extra = {'custom_dimensions': {'ID': someID}})        logging.warning('warn message',extra = {'custom_dimensions': {'ID': someID}})        logging.error('error message',extra = {'custom_dimensions': {'ID': someID}})        logging.critical('critical message',extra = {'custom_dimensions': {'ID': someID}})    except Exception as e:        logging.error("Main program failed with error: " + str(e))我更喜欢将日志配置保存在logging.yaml:version: 1formatters:  simple:    format: '%(asctime)s | %(levelname)-8s | %(module)s:%(funcName)s:%(lineno)d | %(message)s'handlers:  azure:    class: opencensus.ext.azure.log_exporter.AzureLogHandler    level: DEBUG    formatter: simple    instrumentation_key: 'your-key'loggers:  simpleExample:    level: DEBUG    handlers: [azure]    propagate: noroot:  level: INFO  handlers: [azure]
查看完整描述

2 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

在文档中找到正确的部分后,事情变得非常简单:

def main(mytimer: func.TimerRequest, context: func.Context) -> None:

    try: 

        invocation_id = context.invocation_id

        # Function continues here. 


    except Exception as e:

        logging.error("Main program failed with error: " + str(e))

请注意,此解决方案仅在func.Context分配给时才有效context。使用任何其他名称都会context给我带来错误 -.-


查看完整回答
反对 回复 2024-01-04
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

刚刚在此处添加了操作 ID,根据文档,Traceparent 的第二部分是 Trace_id,它将成为日志控制台中的操作 ID。


def main(mytimer: func.TimerRequest, context: func.Context) -> None:

    try: 

        invocation_id = context.invocation_id

        traceparent = context.trace_context.Traceparent

        try:

            operation_id = f"{traceparent}".split('-')[1]

        except IndexError as i_err:

            # as a backup option 

            logging.exception(i_err)

            operation_id = 'default_id'

        except Exception as final_err:

           logging.exception(final_err)

           # Function continues here. 

    except Exception as e:

        logging.error("Main program failed with error: " + str(e))


查看完整回答
反对 回复 2024-01-04
  • 2 回答
  • 0 关注
  • 47 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信