Google Cloud Run上的Stackdriver-trace失败,但在localhost上运行良好

Google Cloud Run上的Stackdriver-trace失败,但在localhost上运行良好

问题描述:

我有一个在Google Cloud Run上运行的节点服务器.现在,我想启用堆栈驱动程序跟踪.在本地运行服务时,我可以在GCP中获取跟踪.但是,当我将服务作为Google Cloud Run运行时,出现错误:

I have a node server running on Google Cloud Run. Now I want to enable stackdriver tracing. When I run the service locally, I am able to get the traces in the GCP. However, when I run the service as Google Cloud Run, I am getting an an error:

"@google-cloud/trace-agent ERROR TraceWriter#publish: Received error with status code 403 while publishing traces to cloudtrace.googleapis.com: Error: The request is missing a valid API key." 

我确保该服务帐户具有跟踪代理角色.

I made sure that the service account has tracing agent role.

我的app.js中的第一行

First line in my app.js

require('@google-cloud/trace-agent').start();

在本地运行,我正在使用包含.env文件

running locally I am using .env file containing

GOOGLE_APPLICATION_CREDENTIALS=<path to credentials.json>

根据 https://github.com/googleapis/cloud-trace-nodejs 如果应用程序在Google Cloud Platform上运行,则会自动检测这些值,因此,我在gcp映像上没有此凭据

According to https://github.com/googleapis/cloud-trace-nodejs These values are auto-detected if the application is running on Google Cloud Platform so, I don't have this credentials on the gcp image

在Cloud Run中使用此库有两个挑战:

There are two challenges to using this library with Cloud Run:

  1. 尽管有关于自动检测的说明,但Cloud Run是一个例外.尚未自动检测到.现在可以通过一些显式配置解决此问题.
  2. 由于Cloud Run服务只有在响应请求之前才具有资源,因此在提取CPU资源之前可能无法发送排队的跟踪数据.现在可以通过将跟踪代理配置为尽快刷新来解决此问题

const tracer = require('@google-cloud/trace-agent').start({
  serviceContext: {
    service: process.env.K_SERVICE || "unknown-service",
    version: process.env.K_REVISION || "unknown-revision"
  },
  flushDelaySeconds: 1,
});

快速浏览后,我看不到如何触发跟踪刷新,但是较短的超时应该有助于避免在跟踪跟踪数据出现在Stackdriver中时出现一些延迟.

On a quick review I couldn't see how to trigger the trace flush, but the shorter timeout should help avoid some delays in seeing the trace data appear in Stackdriver.

虽然从理论上讲不错,但实际上在退出CPU的情况下仍然存在明显的竞争条件.提起 https://github.com/googleapis/cloud-trace-nodejs/issues/1161 ,看看我们能否找到更一致的解决方案.

While nice in theory, in practice there's still significant race conditions with CPU withdrawal. Filed https://github.com/googleapis/cloud-trace-nodejs/issues/1161 to see if we can find a more consistent solution.