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

Posted

技术标签:

【中文标题】Google StackDriver:如何将其他日志与跟踪日志关联(和嵌套)?【英文标题】:Google StackDriver: How do I correlate (and nest) other logs with trace logs? 【发布时间】:2020-02-13 11:46:48 【问题描述】:

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

我正在向 Google Cloud Run 部署一些服务,并希望在请求通过服务跳跃时跟踪请求并将日志与这些请求相关联。 StackDriver 在“StackDriver Trace”中有一个很好的平台。我在我的 asp.net 核心 web api 中使用 Google.Cloud.Diagnostics 库。我能够成功跟踪请求并启动跨度,并在 StackDriver 跟踪时间轴中查看嵌套在跟踪内的跨度。 但是我不知道如何让我的日志与跟踪和跨度相关联?

StackDriver 文档指出,将“特殊字段”写入LogEntry 对象将被 Logging API 识别:

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"  

有什么想法吗???

【问题讨论】:

这类似于我的一个问题。 ***.com/questions/55208085/…我为 Go 编写了一个 Logrus 插件,它将处理线程日志 github.com/andyfusniak/stackdriver-gae-logrus-plugin 【参考方案1】:

为了将您的日志与 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”对象作为代码的一部分与每个单独的日志条目的对象合并。

【讨论】:

废话!就是这样。非常感谢。我阅读了该文档页面大约 10 次,但因为我没有在节点中编写,所以我没有过多关注代码示例。代码示例是我唯一能找到字段名称为logging.googleapis.com/trace 的地方,我读到的所有其他地方都被简单地列为trace,所以这就是我使用的。 :/ 谢谢!

以上是关于Google StackDriver:如何将其他日志与跟踪日志关联(和嵌套)?的主要内容,如果未能解决你的问题,请参考以下文章

如何清除 Google Cloud Platform 中的 Stackdriver 日志?

用于错误报告的Stackdriver监视指标?

如何在 Google Stackdriver 日志中制作“不包含”过滤器

Stackdriver Trace PHP:如何在后台发送跨度?

Stackdriver Trace 与 Google Cloud Run

用于 Google Cloud 外部服务器的 agentPath 上的 Stackdriver GCP 多个代理