回報執行階段函式錯誤 (第 1 代)

您應處理並回報 Cloud Run 函式發生的執行階段錯誤。未偵測到的例外狀況或導致程序當機的執行作業,可能會導致冷啟動,因此您應盡量減少這類情況。

函式發出錯誤訊號的建議方式取決於函式類型:

  • HTTP 函式應傳回適當的 HTTP 狀態碼,表示錯誤。詳情請參閱「HTTP 函式」。

  • 事件驅動函式應記錄並傳回錯誤訊息。詳情請參閱「編寫事件導向函式」。

如果錯誤處理得當,遇到錯誤的函式執行個體就能保持有效,並可用於服務要求。

將錯誤傳送至 Error Reporting

您可以從 Cloud Run 函式傳送錯誤至錯誤回報,如下所示:

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)

Go


package tips

import (
	"fmt"
	"net/http"
	"os"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

func init() {
	functions.HTTP("HTTPError", HTTPError)
}

// HTTPError describes how errors are handled in an HTTP function.
func HTTPError(w http.ResponseWriter, r *http.Request) {
	// An error response code is NOT reported to Error Reporting.
	// http.Error(w, "An error occurred", http.StatusInternalServerError)

	// Printing to stdout and stderr is NOT reported to Error Reporting.
	fmt.Println("An error occurred (stdout)")
	fmt.Fprintln(os.Stderr, "An error occurred (stderr)")

	// Calling log.Fatal sets a non-zero exit code and is NOT reported to Error
	// Reporting.
	// log.Fatal("An error occurred (log.Fatal)")

	// Panics are reported to Error Reporting.
	panic("An error occurred (panic)")
}

Java


import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.IOException;
import java.util.logging.Logger;

public class HelloError implements HttpFunction {

  private static final Logger logger = Logger.getLogger(HelloError.class.getName());

  @Override
  public void service(HttpRequest request, HttpResponse response)
      throws IOException {
    // These will NOT be reported to Error Reporting
    System.err.println("I failed you");
    logger.severe("I failed you");

    // This WILL be reported to Error Reporting
    throw new RuntimeException("I failed you");
  }
}

如果您想要更精細的錯誤回報,可以使用錯誤回報用戶端程式庫

您可以在 Google Cloud 主控台的 Error Reporting 中查看已回報的錯誤。您也可以在 Google Cloud 控制台的函式清單中選取特定函式,查看該函式回報的錯誤。

函式產生的未偵測例外狀況會顯示在 Error Reporting 中。請注意,某些類型的未偵測到例外狀況 (例如非同步擲回的例外狀況) 會在日後函式叫用時導致冷啟動。這會增加函式執行所需的時間。