2

我一直在使用Stackdriver記錄很長一段時間,現在我想利用錯誤報告。如果可能的話(出於各種原因)而不是使用error_reporting庫,我寧願使用Python的日誌記錄機制並從日誌文件中刪除異常。話雖如此,文件很混亂。例如,文件說: https://cloud.google.com/error-reporting/docs/setup/compute-engine#log_exceptions如何記錄錯誤到Stackdriver錯誤報告通過Stackdriver登錄Python

首先,安裝流利記錄器 - Python庫:

須藤PIP安裝谷歌雲 - 錯誤報告--upgrade

導致我相信谷歌雲錯誤報告是fluent-logger-python的一個分支,但是當我初始化google-cloud-error-reporting時,它直接調用GCE元數據服務器而不是連接到本地fluentd。這兩個不相關的軟件包或文檔是錯誤還是誤導?如果我將JSON格式的異常發送到fluentd或通過fluentd監視的日誌文件,錯誤報告會理解它們嗎?

感謝您的任何澄清

回答

1

該文檔是錯誤的。
TL; DR你要輸出的東西,看起來像https://cloud.google.com/error-reporting/docs/formatting-error-messages

這裏是我的解決方案:

#Parse raw log entries to expose severity field so that 
#StackDriver log viewer can properly categorize (and so we can filter) 
<source> 
    @type tail 
    path /var/log/conductor 
    pos_file /var/log/td-agent/conductor.pos 
    format multiline 
    format_firstline /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/ 
    format1 /^(?<message>(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),\d*\s*[a-zA-Z_]*\s*(?<severity>[A-Z]*).*)/ 
    read_from_head true 
    multiline_flush_interval 3s 
    tag conductor.app 
</source> 

#Add hostname field 
<filter conductor.app> 
    @type record_transformer 
    <record> 
    hostname ${hostname} 
    </record> 
</filter> 

#Filter and tag log entries of severity ERROR or CRITICAL 
<match conductor.app> 
    @type rewrite_tag_filter 
    rewriterule1 severity ERROR|CRITICAL conductor.err 
    rewriterule2 severity .+ conductor.info 
</match> 

#Process entries with tracebacks differently than those without 
<match conductor.err> 
    @type rewrite_tag_filter 
    rewriterule1 message .*Traceback conductor.err.traceback 
    rewriterule2 message .+ conductor.err.message 
</match> 

#Parse out the traceback 
<match conductor.err.traceback> 
    @type parser 
    key_name message 
    format multiline 
    format1 /^(?<message>(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),\d*\s*(?<log>[a-zA-Z_]*)\s*(?<severity>[A-Z]*).*(? 
<traceback>Traceback .*))/ 
    tag conductor.err.traceback.report 
</match> 

#Format traceback reports 
<filter conductor.err.traceback.report> 
    @type record_transformer 
    <record> 
     serviceContext { 
     "service": "${record[\"log\"]}" 
     } 
     message ${record["traceback"]} 
    </record> 
    remove_keys traceback 
</filter> 

#Process errors that don't have tracebacks 
<match conductor.err.message> 
    @type parser 
    key_name message 
    format multiline 
    format1 /^(?<message>(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),\d*\s*(?<log>[a-zA-Z_]*)\s*(?<severity>[A-Z]*):\s*(?<report>.*))/ 
    tag conductor.err.message.report 
</match> 

#For errors without tracebacks we have to stub out some fields that 
#error reporting requires, but we don't have 
<filter conductor.err.message.report> 
    @type record_transformer 
    <record> 
     serviceContext { 
     "service": "${record[\"log\"]}" 
     } 
     message ${record["report"]} 
     reportLocation { 
     "filePath": "None", 
     "lineNumber": 0, 
     "functionName": "None" 
     } 
    </record> 
</filter> 

#Send to StackDriver logging! 
<match conductor.**> 
    @type google_cloud 
    buffer_chunk_limit 2M 
    flush_interval 5s 
    max_retry_wait 300 
    disable_retry_limit 
    num_threads 8 
</match>