在 Knative serving 中記錄及查看記錄

本頁面說明使用 Knative serving 時可用的記錄,以及如何查看和寫入記錄。

Knative serving 有兩種記錄:

  • 要求記錄:傳送至 Knative serving 服務的要求記錄。這類的記錄是自動建立的。
  • 容器記錄:容器執行個體發出的記錄,通常是來自您自己的程式碼,這類的記錄會寫入支援的位置,如寫入容器記錄所述。

啟用記錄

如果是 Google Cloud ,記錄會自動傳送至 Cloud Logging。如果是 Google Distributed Cloud,必須先啟用記錄功能。

查看記錄

有幾種方式可以查看您的服務記錄:

  • 使用 Google Cloud 控制台中的 Knative Serving 頁面
  • 在 Google Cloud 控制台使用 Cloud Logging Logs Explorer。

這兩種檢視方法都會檢查儲存在 Cloud Logging 中的相同記錄,但 Cloud Logging Logs Explorer 提供更多詳細資料和篩選功能。

在 Knative serving 查看記錄

如要在 Knative serving 頁面查看記錄,請按照下列步驟操作:

  1. 前往 Knative serving

  2. 在顯示的清單中按一下所要的服務。

  3. 按一下 [LOGS] (記錄) 分頁標籤,取得此服務所有修訂版本的要求和容器記錄。您可以依記錄的嚴重性等級進行篩選。

在 Cloud Logging 查看記錄

如要在 Cloud Logging Logs Explorer 中查看 Knative serving 記錄,請按照下列步驟操作:

  1. 前往Google Cloud 控制台的「Logs Explorer」頁面。

  2. 在頁面頂端選取現有的 Google Cloud 專案,或建立新專案。

  3. 使用下拉式選單選取資源:Kubernetes Container。

詳情請參閱「使用記錄檔探索工具」。

在 Cloud Code 查看記錄

如要在 Cloud Code 中查看記錄,請參閱 IntelliJ 和 Visual Studio Code 指南。

透過程式讀取記錄

如要透過程式輔助方式讀取記錄,可以使用下列任一方法:

寫入容器記錄

只要將服務中的記錄寫入下列任一位置,Cloud Logging 就會自動擷取這些記錄:

大多數開發人員應使用標準輸出內容和標準錯誤寫入記錄。

寫入這些支援位置的容器記錄會自動與 Knative serving 服務、修訂版本和位置建立關聯。

在記錄中使用簡單文字與結構化 JSON

當您寫入記錄時,您可以傳送簡易文字字串或傳送一行序列化 JSON (又稱為「結構化」資料)。Cloud Logging 會擷取並剖析這項資訊,然後放入 jsonPayload。相較之下,簡單的文字訊息會放在 textPayload 中。

寫入結構化記錄檔

下列程式碼片段說明如何寫入結構化記錄項目。此外,也會說明如何將記錄訊息與對應的要求記錄建立關聯。

Node.js


// Uncomment and populate this variable in your code:
// const project = 'The project ID of your function or Cloud Run service';

// Build structured log messages as an object.
const globalLogFields = {};

// Add log correlation to nest all log messages beneath request log in Log Viewer.
// (This only works for HTTP-based invocations where `req` is defined.)
if (typeof req !== 'undefined') {
  const traceHeader = req.header('X-Cloud-Trace-Context');
  if (traceHeader && project) {
    const [trace] = traceHeader.split('/');
    globalLogFields['logging.googleapis.com/trace'] =
      `projects/${project}/traces/${trace}`;
  }
}

// Complete a structured log entry.
const entry = Object.assign(
  {
    severity: 'NOTICE',
    message: 'This is the default display field.',
    // Log viewer accesses 'component' as 'jsonPayload.component'.
    component: 'arbitrary-property',
  },
  globalLogFields
);

// Serialize to a JSON string and output.
console.log(JSON.stringify(entry));

Python

# Uncomment and populate this variable in your code:
# PROJECT = 'The project ID of your Cloud Run service';

# Build structured log messages as an object.
global_log_fields = {}

# Add log correlation to nest all log messages.
# This is only relevant in HTTP-based contexts, and is ignored elsewhere.
# (In particular, non-HTTP-based Cloud Functions.)
request_is_defined = "request" in globals() or "request" in locals()
if request_is_defined and request:
    trace_header = request.headers.get("X-Cloud-Trace-Context")

    if trace_header and PROJECT:
        trace = trace_header.split("/")
        global_log_fields[
            "logging.googleapis.com/trace"
        ] = f"projects/{PROJECT}/traces/{trace[0]}"

# Complete a structured log entry.
entry = dict(
    severity="NOTICE",
    message="This is the default display field.",
    # Log viewer accesses 'component' as jsonPayload.component'.
    component="arbitrary-property",
    **global_log_fields,
)

print(json.dumps(entry))

Go

每個記錄項目的結構都由 Entry 類型提供:


// Entry defines a log entry.
type Entry struct {
	Message  string `json:"message"`
	Severity string `json:"severity,omitempty"`
	Trace    string `json:"logging.googleapis.com/trace,omitempty"`

	// Logs Explorer allows filtering and display of this as `jsonPayload.component`.
	Component string `json:"component,omitempty"`
}

// String renders an entry structure to the JSON format expected by Cloud Logging.
func (e Entry) String() string {
	if e.Severity == "" {
		e.Severity = "INFO"
	}
	out, err := json.Marshal(e)
	if err != nil {
		log.Printf("json.Marshal: %v", err)
	}
	return string(out)
}

記錄 Entry struct 時,系統會呼叫 String 方法,將其封送至 Cloud Logging 預期的 JSON 格式:


func init() {
	// Disable log prefixes such as the default timestamp.
	// Prefix text prevents the message from being parsed as JSON.
	// A timestamp is added when shipping logs to Cloud Logging.
	log.SetFlags(0)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	// Uncomment and populate this variable in your code:
	// projectID = "The project ID of your Cloud Run service"

	// Derive the traceID associated with the current request.
	var trace string
	if projectID != "" {
		traceHeader := r.Header.Get("X-Cloud-Trace-Context")
		traceParts := strings.Split(traceHeader, "/")
		if len(traceParts) > 0 && len(traceParts[0]) > 0 {
			trace = fmt.Sprintf("projects/%s/traces/%s", projectID, traceParts[0])
		}
	}

	log.Println(Entry{
		Severity:  "NOTICE",
		Message:   "This is the default display field.",
		Component: "arbitrary-property",
		Trace:     trace,
	})

	fmt.Fprintln(w, "Hello Logger!")
}

Java

若要啟用 JSON 記錄功能,請在 logback.xml 設定中啟用 Logstash JSON Encoder,並搭配 Logback 與 SLF4J 使用。

// Add log correlation to nest all log messages beneath request log in Log Viewer.
// TODO(developer): delete this code if you're creating a Cloud
//                  Function and it is *NOT* triggered by HTTP.
String traceHeader = req.headers("x-cloud-trace-context");
if (traceHeader != null && project != null) {
  String trace = traceHeader.split("/")[0];
  MDC.put(
      "logging.googleapis.com/trace",
      String.format("projects/%s/traces/%s", project, trace));
}
// -- End log correlation code --

// Create a structured log entry using MDC (Mapped Diagnostic Context) keys.
// For instantiating the "logger" variable, see
// https://cloud.google.com/run/docs/logging#run_manual_logging-java
MDC.put("component", "arbitrary-property");

logger.info("This is the default display field.");

// Clear MDC at the end of the request to avoid resource leaks
MDC.clear();
<configuration>
  <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
    <!-- Optional : filter logs at or above a level -->
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFO</level>
    </filter>
    <log>application.log</log> <!-- Optional : default java.log -->
    <resourceType>gae_app</resourceType> <!-- Optional : default: auto-detected, fallback: global -->
    <enhancer>com.example.logging.logback.enhancers.ExampleEnhancer</enhancer> <!-- Optional -->
    <flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->
    <!-- Redirect logs to stdout in JSON format for Cloud Run to capture -->
    <redirectToStdout>true</redirectToStdout>
  </appender>
  <root level="INFO">
    <appender-ref ref="CLOUD"/>
  </root>
</configuration>

訊息中的特殊 JSON 欄位

以 JSON 字典形式提供結構化記錄時,某些特殊欄位會從 jsonPayload 中移除,並如特殊欄位說明文件所述,寫入系統所產生 LogEntry 的對應欄位。

例如,若 JSON 含有 severity 屬性,系統會從 jsonPayload 將其移除,並改以記錄項目的 severity 形式顯示。如果存在 message 屬性,則會用作記錄項目的主要顯示文字。如要進一步瞭解特殊屬性,請參閱下方的「記錄資源」一節。

建立容器記錄與要求記錄間的關聯

在 Logs Explorer 中,由相同 trace 關聯的記錄檔會以「父項-子項」格式顯示:點選要求記錄項目左側的三角形圖示時,與該要求相關的容器記錄檔會顯示在要求記錄檔下方。

除非您使用 Cloud Logging 用戶端程式庫,否則容器記錄不會自動與要求記錄建立關聯。如要在不使用用戶端程式庫的情況下,將容器記錄與要求記錄建立關聯,您可以使用結構化 JSON 記錄行,其中包含 logging.googleapis.com/trace 欄位,以及從 X-Cloud-Trace-Context 標頭擷取的追蹤 ID,如上方的結構化記錄範例所示。

控管要求記錄的資源使用量

要求記錄會自動建立。雖然您無法直接從 Knative serving 控制要求記錄的數量,但可以使用 Cloud Logging 的記錄排除功能。

Logging 代理程式注意事項

如果您曾搭配特定 Google Cloud 產品 (例如 Compute Engine) 使用 Cloud Logging,可能也用過 Cloud Logging Logging 代理程式。Knative serving 內建記錄檔收集功能,因此不使用 Logging 代理程式。

記錄資源

在 Logs Explorer 中點選記錄項目,即可開啟 JSON 格式的記錄項目,進一步查看所需詳細資料。

記錄項目中的所有欄位 (例如時間戳記、嚴重性和 httpRequest) 均為標準欄位,詳情請參閱記錄項目說明文件。

不過,有些標籤或資源標籤是 Knative serving 專屬。這裡列出使用範例內容的標籤:

{
 httpRequest: {…}
 insertId:  "5c82b3d1000ece0000000000"
 labels: {
  instanceId:  "00bf4bf00000fb59c906a00000c9e29c2c4e06dce91500000000056008d2b6460f163c0057b97b2345f2725fb2423ee5f0bafd36df887fdb1122371563cf1ff453717282afe000001"
 }
 logName:  "projects/my-project/logs/kubernetes-engine/enterprise/knative-serving/.googleapis.com%2Frequests"
 receiveTimestamp:  "2019-03-08T18:26:25.981686167Z"
 resource: {
  labels: {
   configuration_name:  "myservice"
   location:  "us-central1"
   project_id:  "my-project"
   revision_name:  "myservice-00002"
   service_name:  "myservice"
  }
  type:  "cloud_run_revision"
 }
 severity:  "INFO"
 timestamp:  "2019-03-08T18:26:25.970397Z"
}
欄位 值和備註
instanceId 處理要求的容器執行個體。
logName 識別記錄,例如要求記錄、標準錯誤、標準輸出內容等。
configuration_name 建立處理要求之修訂版本的「設定」資源。
location 識別服務的 GCP 位置。
project_id 部署服務的專案。
revision_name 處理要求的修訂版本。
service_name 處理要求的服務。
type cloud_run_revision:Knative serving 資源類型。