首页 > 解决方案 > Google.Cloud.Diagnostics.AspNetCore 3.0.0-beta13 不适用于 GKE

问题描述

目的

在 .net core 2.2 REST API 上使用 Google Cloud Diagnostics,在两种可能的情况下进行日志记录、跟踪和错误报告:

  1. Visual Studio 2017 上的本地执行
  2. 部署在 Docker 容器上并在 GCP Kubernetes Engine 上运行

环境细节

描述

为了配置 Google Cloud Diagnostics,使用了两个文档源:

  1. Google.Cloud.Diagnostics.AspNetCore
  2. 启用对 Google.Cloud.Diagnostics.AspNetCore #2435 的配置支持

基于上述文档,使用了 IWebHostBuilder 上的 UseGoogleDiagnostics 扩展方法,因为它配置了日志记录、跟踪和错误报告中间件。

根据 2) 链接,下表显示了使用 UseGoogleDiagnostics 方法时所需的信息: 配置表

  1. 对于本地执行 => 需要 project_id、module_id 和 version_id,
  2. 对于 GKE => module_id 和 version_id

.net 核心配置文件用于为每个部署提供上述信息:

应用设置.json

{
  "GCP": {
    "ServiceID": "my-service",
    "VersionID": "v1"
  } 
}

appsettings.Development.json

{
  "GCP": {
    "ID": "my-id"
  } 
}

基本上,上面将呈现以下配置:

  1. 本地执行
return WebHost.CreateDefaultBuilder(args)
    .UseGoogleDiagnostics("my-id", "my-service", "v1")
    .UseStartup<Startup>();
  1. 在 GKE 上
return WebHost.CreateDefaultBuilder(args)
    .UseGoogleDiagnostics(null, "my-service", "v1")
    .UseStartup<Startup>();

为了保证我使用的是正确的信息,我在 GCP UI 上使用了两个地方来验证:

  1. 在端点列表中,检查了服务详细信息:
Service name: my-service Active version: v1 
  1. 检查端点日志,以获取特定的 API POST 端点
{
insertId: "e6a63a28-1451-4132-ad44-a4447c33a4ac@a1"  

jsonPayload: {…}  
 logName: "projects/xxx%2Fendpoints_log"  
 receiveTimestamp: "2019-07-11T21:03:34.851569606Z"  

resource: {

labels: {
   location: "us-central1-a"    
   method: "v1.xxx.ApiOCRPost"    
   project_id: "my-id"    
   service: "my-service"    
   version: "v1"    
  }
  type: "api"   
 }
 severity: "INFO"  
 timestamp: "2019-07-11T21:03:27.397632588Z"  
} 

我做错了什么还是 Google.Cloud.Diagnostics.AspNetCore 3.0.0-beta13 上的错误?

在执行服务端点时,对于每个特定部署,Google Cloud 诊断的行为都不同:

  1. 在本地执行 (VS2017) => 记录、跟踪和错误报告按预期工作,所有内容都显示在 GCP UI 中
  2. 在 GKE 部署中 => 日志记录、跟踪和错误报告不起作用,GCP UI 中没有显示任何内容

我尝试了几种变体,直接在代码中硬编码值等,但无论我做什么,谷歌云诊断在 GKE 中部署时都不起作用:

  1. 直接硬编码值
return WebHost.CreateDefaultBuilder(args)
    .UseGoogleDiagnostics(null, "my-service", "v1")
    .UseStartup<Startup>();
  1. 版本中没有 v
return WebHost.CreateDefaultBuilder(args)
    .UseGoogleDiagnostics(null, "my-service", "1")
    .UseStartup<Startup>();

标签: google-cloud-platform

解决方案


推荐阅读