오류 보고

오류 자동 보고

아래와 같이 Cloud 함수에서 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 클라이언트 라이브러리를 사용하면 보다 세밀하게 오류를 보고할 수 있습니다.

GCP 콘솔의 Error Reporting에서 보고된 오류를 확인할 수 있습니다. 또한 GCP 콘솔의 함수 목록에서 특정 함수를 선택하면 해당 함수에서 보고된 오류를 확인할 수 있습니다.

함수로 생성되었지만 발견되지 않은 예외는 Error Reporting에 표시됩니다. 일부 유형의 포착되지 않은 예외(예: 비동기식으로 발생)는 향후 함수 호출 시 콜드 스타트를 유발할 수 있습니다. 이로 인해 함수를 실행하는 데 걸리는 시간이 늘어납니다.

오류 수동 보고

Cloud Logging으로 전송

Cloud Functions 로거 SDK의 error 함수는 Cloud Logging 및 Error Reporting 모두에 오류를 보고합니다. 오류에서 더 많은 컨텍스트를 구조화된 데이터로 포함하려면 오류 객체를 두 번째 인수로 전달합니다.

} catch (err) {
  // Attach an error object as the second argument
  functions.logger.error(
    "Unable to read quote from Firestore, sending default instead",
    err
  );
}