Open-Telemetry 跨度未显示在 GCP Cloud Trace 中

Open-Telemetry spans not showing up in GCP Cloud Trace

我正在 Google 云平台的云 运行 中检测 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();
};

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

部署云 运行 实例的服务帐户具有 roles/cloudtrace.agent 角色:

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

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

到目前为止我已经试过了

我真的很茫然

在 GCP documentation 中检查过,此服务仅支持 Google Compute Engine 和 GKE。

不过可以参考Cloud Run Support in Github, if you are running OpenTelemetry on Cloud Run and a Whosebug .