亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取當前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 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號