エラーを自動的に報告する
次のように、関数から Error Reporting にエラーを送信できます。
Node.js
// These WILL be reported to Error Reporting
throw new Error('I failed you'); // Will cause a cold start if not caught
Python
@functions_framework.http
def hello_error_1(request):
# This WILL be reported to Error Reporting,
# and WILL NOT show up in logs or
# terminate the function.
from google.cloud import error_reporting
client = error_reporting.Client()
try:
raise RuntimeError("I failed you")
except RuntimeError:
client.report_exception()
# This WILL be reported to Error Reporting,
# and WILL terminate the function
raise RuntimeError("I failed you")
@functions_framework.http
def hello_error_2(request):
# These errors WILL NOT be reported to Error
# Reporting, but will show up in logs.
import logging
import sys
print(RuntimeError("I failed you (print to stdout)"))
logging.warning(RuntimeError("I failed you (logging.warning)"))
logging.error(RuntimeError("I failed you (logging.error)"))
sys.stderr.write("I failed you (sys.stderr.write)\n")
# This is considered a successful execution and WILL NOT be reported
# to Error Reporting, but the status code (500) WILL be logged.
from flask import abort
return abort(500)
より詳細なエラー報告が必要な場合は、Error Reporting クライアント ライブラリを使用します。
報告されたエラーは、Google Cloud コンソールの Error Reporting で確認できます。また、Google Cloud コンソールで関数のリストから関数を選択して、その特定の関数から報告されたエラーを表示することもできます。
関数によって生成され、キャッチされていない例外は、Error Reporting に表示されます。キャッチされなかった例外(非同期でスローされた例外など)は、将来の関数呼び出しでコールド スタートの原因となる可能性があります。これにより、関数の実行時間が長くなります。
エラーを手動で報告する
依存関係をインポートする
関数から Error Reporting にエラーを報告するには、Cloud Functions のLogger SDK から error
関数をインポートします。
// All available logging functions
const {
log,
info,
debug,
warn,
error,
write,
} = require("firebase-functions/logger");
Cloud Logging に送信しています
Cloud Functions のロガー SDK の error
関数は、Cloud Logging と Error Reporting の両方にエラーを報告します。エラーのコンテキストを構造化データとして追加するには、エラー オブジェクトを 2 番目の引数として渡します。
} catch (err) {
// Attach an error object as the second argument
error("Unable to read quote from Firestore, sending default instead",
err);