gpt4 book ai didi

node.js - 将自定义信息添加到 Firebase 函数中的 Stackdriver 错误日志

转载 作者:行者123 更新时间:2023-12-01 04:25:55 25 4
gpt4 key购买 nike

我将 Firebase 函数与 Stackdriver 一起使用。

Stackdriver 与 Firebase 功能很好地集成在一起,因此我可以使用 console.error 轻松记录错误。命令。
但是,我不仅要记录错误对象,还要记录查询参数。
如果我可以在同一日志行中记录错误对象和查询参数,则可以轻松地将它们分组和导出。

是否有一种简单的方法可以将信息添加到 Stackdriver 上的错误日志中,如下所示?

    console.error(new Error('Invalid query'), req.query)

谢谢。

- - 编辑

我尝试了以下代码。这可以向日志条目添加查询参数,但不幸的是 Stackdriver 将所有错误放在一组中,如下面的屏幕截图所示。即使每个错误的类型不同并且出现在不同的文件中,所有错误也被组合在一起。我希望 Stackdriver Error Reporting 像往常一样按错误类型或堆栈跟踪对错误进行分组。

索引.js
const functions = require('firebase-functions')
const raiseReferenceError = require('./raiseReferenceError')
const raiseSyntaxError = require('./raiseSyntaxError')
const raiseTypeError = require('./raiseTypeError')

exports.stackdriverErrorLogging = functions.https.onRequest((req, res) => {
try {
switch (Math.round(Math.random() * 2)) {
case 0:
raiseReferenceError()
break

case 1:
raiseSyntaxError()
break

default:
raiseTypeError()
break
}
} catch (error) {
console.error({
error: error,
method: req.method,
query: req.query
})
}

res.send('Hello from Firebase!')
})

raiseReferenceError.js
module.exports = () => {
console.log(foo)
}

raiseSyntaxError.js
module.exports = () => {
eval(',')
}

raiseTypeError.js
module.exports = () => {
const foo = null
foo()
}

10 次运行结果的屏幕截图:

Stackdriver 错误报告错误摘要页面
Stackdriver error summary page
Stackdriver 错误报告错误详情页面
Stackdriver error details page 1
Stackdriver error details page 2

最佳答案

自我回答:

我试图找到简单的方法,但没有。所以我决定摆脱我的懒惰,学习如何使用 Yuri Grinshteyn 提到的 @google-cloud/logging。

我找到了很多例子,但对于这种情况,没有一个是足够的。
最后,我构建了一个函数,可以轻松地报告带有附加信息的错误。

要点如下:

  • 要将自定义错误日志在 GCP 控制台上显示为普通错误日志,它需要包含严重性、区域、execution_id、trace ID。如果缺少它们,GCP 控制台不会在默认过滤器上显示它们。也不能按执行 ID 标签分组。
  • 应从 http 请求中提取 execution_id 和跟踪 ID。 (我不知道如何在另一个触发器类型函数上获取它们。)

  • 这需要时间,但最终,Stackdriver Error Reporting 识别并分组每种错误类型,并成功地将自定义信息包含在错误日志的 JSON 负载中。这就是我想要的。

    现在我终于可以回去工作了。

    Here is a gist I made.

    这是一个主要的代码。
    // https://firebase.google.com/docs/functions/reporting-errors#manually_reporting_errors
    const { Logging } = require('@google-cloud/logging')

    // Instantiates a client
    const logging = new Logging()

    module.exports = function reportError (err, context = {}, req = undefined) {
    // Use Stackdriver only on production environment.
    if (process.env.NODE_ENV !== 'production') {
    return new Promise((resolve, reject) => {
    console.error(err, context)
    return resolve()
    })
    }

    const log = logging.log('cloudfunctions.googleapis.com%2Fcloud-functions')

    // https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
    const metadata = {
    severity: 'ERROR',
    resource: {
    type: 'cloud_function',
    labels: {
    function_name: process.env.FUNCTION_NAME,
    project: process.env.GCLOUD_PROJECT,
    region: process.env.FUNCTION_REGION
    }
    }
    }

    // Extract execution_id, trace from http request
    // https://stackoverflow.com/a/55642248/7908771
    if (req) {
    const traceId = req.get('x-cloud-trace-context').split('/')[0]
    Object.assign(metadata, {
    trace: `projects/${process.env.GCLOUD_PROJECT}/traces/${traceId}`,
    labels: {
    execution_id: req.get('function-execution-id')
    }
    })
    }

    // https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent
    const errorEvent = {
    message: err.stack,
    serviceContext: {
    service: process.env.FUNCTION_NAME,
    resourceType: 'cloud_function'
    },
    context: context
    }

    // Write the error log entry
    return new Promise((resolve, reject) => {
    log.write(log.entry(metadata, errorEvent), (error) => {
    if (error) {
    return reject(error)
    }
    return resolve()
    })
    })
    }

    10 次运行结果的屏幕截图:

    Stackdriver 错误报告错误摘要页面。
    Stackdriver Error Reporting error summary page.

    Stackdriver Logging 显示自定义信息错误。
    Stackdriver Logging shows a error with custom information.

    关于node.js - 将自定义信息添加到 Firebase 函数中的 Stackdriver 错误日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671382/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com