【问题标题】:Open-Telemetry spans not showing up in GCP Cloud Trace开放式遥测跨度未显示在 GCP Cloud Trace 中
【发布时间】:2022-08-03 13:40:09
【问题描述】:

我正在 Google Cloud Platform 的 Cloud Run 中检测 node.js 服务。

我遇到了一个问题Trace 中未显示自定义跨度.

我知道跟踪是有效的,因为 HTTP/TCP 跨度(您可以在 GCP 中免费获得)正确显示嵌套 - 如果没有配置,它们不会自动嵌套,这表明下面的配置正在工作:

tracing.ts:

import { NodeTracerProvider } from \"@opentelemetry/sdk-trace-node\";
import {
  SimpleSpanProcessor,
} from \"@opentelemetry/sdk-trace-base\";
import { TraceExporter } from \"@google-cloud/opentelemetry-cloud-trace-exporter\";
import { getNodeAutoInstrumentations } from \"@opentelemetry/auto-instrumentations-node\";
import { registerInstrumentations } from \"@opentelemetry/instrumentation\";
import { ExpressInstrumentation } from \"@opentelemetry/instrumentation-express\";
import * as opentelemetry from \"@opentelemetry/api\";
import { AsyncHooksContextManager } from \"@opentelemetry/context-async-hooks\";
import { SemanticResourceAttributes } from \"@opentelemetry/semantic-conventions\";
import { Resource } from \"@opentelemetry/resources\"

export const provider = new NodeTracerProvider({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: \"my-service-name\",
  })
});

// this *should* work automatically in GCP??
provider.addSpanProcessor(new SimpleSpanProcessor(new TraceExporter({
  resourceFilter: /^service\\./
})));

provider.register();

opentelemetry.trace.setGlobalTracerProvider(provider);

const contextManager = new AsyncHooksContextManager();
contextManager.enable();
opentelemetry.context.setGlobalContextManager(contextManager);

export const tracer = opentelemetry.trace.getTracer(\"basic\");

// this works (spans are correctly associated with parents)
registerInstrumentations({
  instrumentations: [
    getNodeAutoInstrumentations({
      \"@opentelemetry/instrumentation-http\": {},
      \"@opentelemetry/instrumentation-express\": {},
    }),
  ],
});

跨度是不是显示的是那些在代码中发出的代码,例如以下经过编辑的生产代码:

import { tracer } from \"../tracing\";

// ...

export const doWork = async (
  req: Request,
  res: Response
) => {

  // ... but this does *NOT* work: these spans appear nowhere
  // start span
  const span = tracer.startSpan(\"doWork\");
  const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), span);
  opentelemetry.propagation.extract(ctx, req.headers);

  try {
    // ... do work here with ctx to emit child spans

    res.status(200).send(\"ok\");
  } catch (e) {
    res.status(500).send(\"error\");
  }

  span.end();
};

我不清楚为什么这些跨度没有出现在任何地方。

部署 Cloud Run 实例的服务帐号具有 roles/cloudtrace.agent 角色:

- members:
  - serviceAccount:<my service account name>@<project id>.iam.gserviceaccount.com
  role: roles/cloudtrace.agent    

我不确定是否需要添加其他权限(或者可能需要将它们添加到哪个实体)。

到目前为止我已经尝试过

  • 使用和不使用 Provider 配置进行部署(没有区别)
  • 使用 Open-Telemetry OTLPTraceExporter 在 GCP 中导出 span(仍然没有显示)
  • 改用 Stackdriver trace-agent(与 webpack 不兼容)
  • 使用带有 Open-Telemetry 收集器的 OTLPTraceExporter 在本地运行所有这些(一切都按预期工作——所有跟踪都显示出来了)
  • 在 GCP 中使用 ConsoleSpanExporter(跨度在日志中正确显示)

我真的很茫然。

  • 根据 GCP 的文档,Google Compute Engine 和 GKE 有 auto-instrumentation 模块,但 Cloud Run 没有。你可以查看这个 Github Cloud Run Support
  • 啊,谢谢你的链接!我已经在链接评论中尝试了建议的更改:\"@patryk-smc 我没有实际示例,但跟踪应该可以正常工作。使用常规 BatchSpanProcessor 并在程序结束之前调用 TracerProvider.shutdown()。对于 Cloud Run,您可以添加一个 SIGTERM 处理程序来调用关机。\" ...但它似乎在 Cloud Run 中不起作用。

标签: node.js google-cloud-run open-telemetry google-cloud-trace


【解决方案1】:

正如在 GCP documentation 中检查的那样,此服务仅支持 Google Compute Engine 和 GKE。

但是,如果您在 Cloud Run 上运行 OpenTelemetry 和 Stackoverflow discussion,则可以参考 Github 中的 Cloud Run Support

【讨论】:

    【解决方案2】:

    即使没有正确调用shutdown,使用BatchSpanProcessor 也对我有用。

    即,像这样:

     provider.addSpanProcessor(new BatchSpanProcessor(new TraceExporter()));
    

    我仍然遇到自动检测/手动跨度的父母未正确连接到 HTTP 跨度的问题。

    请注意,您可以在本地运行并将跟踪导出到 GCP,如下所述:https://cloud.google.com/trace/docs/setup/nodejs-ot#running_locally_and_elsewhere。但是,我注意到在本地运行和在 Cloud Run 上运行之间存在差异,并且还无法解决这些问题。我有类似的经验,跟踪在本地/与 Jaeger 或其他导出器一起工作正常,但在推送到 Cloud Run 实例时会中断。

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多