【问题标题】:Getting functionName and executionId inside running Firebase function在运行的 Firebase 函数中获取 functionName 和 executionId
【发布时间】:2021-01-13 15:07:00
【问题描述】:

我们的产品在很大程度上基于 Node.js v10 firebase 函数,到目前为止,我们一直在使用 firebase 函数 logger SDK 进行日志记录。然而,这对我们来说已经不够了,因为我们需要为每个日志发送一些额外的属性,以便在 GCP Logging Explorer 中更好地过滤。

很遗憾,非常有用的 functionNameexecutionId 标签未附加到 Cloud Logging SDK 触发的日志中。 Node.js Firebase SDK 是否以某种方式公开了这些标签,以便我手动将它们附加到元数据?

问候

【问题讨论】:

    标签: node.js firebase google-cloud-functions google-cloud-logging


    【解决方案1】:

    我们有完全相同的堆栈在工作,我们只是创建了一个位于 firebase 记录器之上的记录器并基本上进行手动记录。

    // Use the firebase functions logger to report all logs from out logger to
    // Can do logger.info, .error, .log, .warn
    // Logger is a custom function function that sits on top of console or firebase logger
    // This means initialise a logger singleton using the firebase functions logger as a base
    logger(functions.logger)
    

    例如典型的功能日志是:

    // Use the logger global singleton initialised above
    logger.log(`[endpoint-name.methodName] - Execution failed with error code ${error.code} and message ${error.message}`)
    

    然后我们使用函数日志搜索字段来查找特定错误的实例。我们还将所有内部错误报告给哨兵并使用它进行调试。

    希望这能给你一些想法?

    【讨论】:

    • 虽然这是一个解决方案,但它有点老套,我宁愿避免它。另外,如何获取端点和方法名?
    • 我们有一个自定义中间件,可以在请求对象到达端点之前将端点名称添加到请求对象中。 [${req.endpointName} - ManuallyAddFuncName] - blob。我也不认为它是一个很好的可行解决方案。
    • 这是一个很好的可行解决方案 :) 我只是更喜欢对 functionName 和 executionId 使用记录器机制,因为这些标签在资源管理器中非常有用。顺便说一句,我确实使用了您的想法从请求中获取数据,但我将它们传递到日志条目元数据中。这样标签就会显示在 GCP 日志记录资源管理器中。
    • 啊,这是个好主意 - 很高兴你想出了一些办法!
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多