Google StackDriver:如何将其他日志与跟踪日志关联(并嵌套)?

时间:2019-10-16 16:14:37

标签: logging trace stackdriver google-cloud-stackdriver google-cloud-run

请帮助,我已经3天没成功了:/

我正在为Google Cloud Run部署一些服务,并希望跟踪请求并在请求通过服务时将日志与这些请求相关联。 StackDriver在“ StackDriver跟踪”中为此提供了一个出色的平台。我在asp.net核心网络api中使用Google.Cloud.Diagnostics库。我能够成功跟踪请求,启动跨度,并在StackDriver跟踪时间轴中看到跟踪中嵌套的跨度。 但是我对如何使日志与轨迹和跨度相关感到困惑?

StackDriver文档指出,通过日志记录API可以将“特殊字段”写入LogEntry对象:

Special fields in structured payloads

When the Logging agent receives a structured log record, it treats the following fields specially, allowing you to set specific fields in the LogEntry object that get written to the Logging API.

All (special fields) are stripped from the payload if present.

它具体说明了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 View无法识别我的日志并将其嵌套在请求跟踪中。目前,我在JSON中同时包含了trace和spanId。

这是我的其中一个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"  
}

任何想法?

1 个答案:

答案 0 :(得分:2)

为了使您的日志与Cloud Run请求日志相关联,您需要填充属性logging.googleapis.com/trace,Cloud Run将其用作Stackdriver Logging的跟踪属性。

您可以在Cloud Run logging docs中阅读更多详细信息,并查看structured logging Node.js sample

样本中的相关代码片段:

const traceHeader = req.header('X-Cloud-Trace-Context');
if (traceHeader && project) {
  const [trace] = traceHeader.split('/');
  globalLogFields[
    'logging.googleapis.com/trace'
  ] = `projects/${project}/traces/${trace}`;
}

其中“ globalLogFields”对象与每个日志条目的对象合并在一起,作为代码的一部分。

相关问题