GCP StackDriver:如何將資訊日誌與追蹤日誌關聯(和巢狀)?

GCP StackDriver:如何將資訊日誌與追蹤日誌關聯(和巢狀)?

請幫忙,我已經三天沒成功了:/

我正在將一些服務部署到 Google Cloud Run,並且希望在請求通過服務時追蹤請求並將日誌與這些請求相關聯。 StackDriver 在「StackDriver Trace」中為此提供了一個很棒的平台。我在我的 asp.net core web api 中使用 Google.Cloud.Diagnostics 函式庫。我能夠成功追蹤請求、啟動跨度,並在 StackDriver 追蹤時間軸中查看嵌套在追蹤內的跨度。然而,我對如何讓我的日誌與追蹤和跨度相關聯感到困惑。

StackDriver 文件指出,向LogEntry物件寫入「特殊欄位」將被 Logging API 識別。

它特別提到了這個trace領域:

The value of this field should be formatted as projects/[PROJECT-ID]/traces/[TRACE-ID], so it can be used by the Logs Viewer and the Trace Viewer to group log entries and display them in line with traces. 

來源:https://cloud.google.com/logging/docs/agent/configuration#special-fields

我對結構化 JSON 嘗試了許多不同的方法,但 Stackdriver 追蹤視圖無法識別我的日誌並將它們嵌套在請求追蹤中。目前我將trace 和spanId 都包含在JSON 中。

這是我的日誌之一,StackDriver Trace 不會嵌套在追蹤中:

{
 insertId: "5da6c3a200j0923bx23x2"  
 jsonPayload: {
  ConnectionId: "0HLQI121N94JM"   
  CorrelationId: null   
  RequestId: "0HLQI121N94JM:00000001"   
  RequestPath: "/graphql"   
  message: "Getting Collection of type: FakeItem in GetCollectionOfItemsAsync"   
  messageTemplate: "Getting Collection of type: FakeItem in GetCollectionOfItemsAsync"   
  spanId: "4560986706170855936"   
  timestamp: "2019-10-16T07:15:46.8713740Z"   
  trace: "projects/myProject/traces/04b4a840df0289bb9fddcd62235d3ee4"   
 }
 labels: {
  instanceId: "00bf4bf02d34e072dc1c49x8dj943x4b5609mubm0409u566ad08acf6283d2b5135651fd8f2633e7b06e7dde4b96cfddbf5373a642da0b65fb21cf87a5aad"   
 }
 logName: "projects/myProject/logs/run.googleapis.com%2Fstdout"  
 receiveTimestamp: "2019-10-16T07:15:47.113845061Z"  
 resource: {
  labels: {
   configuration_name: "baseproject-graphql"    
   location: "us-central1"    
   project_id: "myProject"    
   revision_name: "baseproject-graphql-vhglp"    
   service_name: "baseproject-graphql"    
  }
  type: "cloud_run_revision"   
 }
 timestamp: "2019-10-16T07:15:46.871489Z"  
}

這是由 gcp 產生的並能被辨識的日誌:

{
 httpRequest: {
  latency: "0.026068056s"   
  protocol: "HTTP/1.1"   
  remoteIp: "73.158.189.48"   
  requestMethod: "POST"   
  requestSize: "1950"   
  requestUrl: "https://baseproject-api.myUrl.com/graphql"   
  responseSize: "2768"   
  serverIp: "152.289.4.125"   
  status: 200   
  userAgent: "PostmanRuntime/7.18.0"   
 }
 insertId: "5da6c3a8j90kjo9db8346"  
 labels: {
  instanceId: "00bf4bf02d34e072dc1cfda1073f2f5ec6888d75e1d75f26259006ad08acf6283d2b5135651fd8f26398n9hu0h9h09gm08g76f67f567fb21cf87a5aad"   
 }
 logName: "projects/myProject/logs/run.googleapis.com%2Frequests"  
 receiveTimestamp: "2019-10-16T07:15:47.207098181Z"  
 resource: {
  labels: {
   configuration_name: "baseproject-graphql"    
   location: "us-central1"    
   project_id: "myProject"    
   revision_name: "baseproject-graphql-vhglp"    
   service_name: "baseproject-graphql"    
  }
  type: "cloud_run_revision"   
 }
 severity: "INFO"  
 timestamp: "2019-10-16T07:15:46.877387Z"  
 trace: "projects/myProject/traces/04b4a840df0289bb9fddcd62235d3ee4"  
}

有什麼想法嗎?

相關內容