我正在检测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 我不确定是否需要添加其他权限(或者它们可能需要添加到哪个实体)。
到目前为止我已经试过了
OTLPTraceExporter导出GCP中的跨域(仍然没有显示任何内容)trace-agent (与webpack不兼容)OTLPTraceExporter和一个开放遥测收集器在本地运行所有这些(一切都像预期的那样工作--所有的跟踪都出现了)我真的很茫然。
发布于 2022-03-15 03:40:03
发布于 2022-08-03 05:38:01
对我来说,这是几件事的结合:
使用BatchSpanProcessor对我起了很大的作用,即使没有正确调用shutdown。
呃,就像这样:
provider.addSpanProcessor(new BatchSpanProcessor(new TraceExporter()));但是,并不是所有的跨跨都将在GCP上进行采样,因此我需要在AlwaysOnSampler中添加一个NodeTracerProvider。
export const provider = new NodeTracerProvider({
sampler: new AlwaysOnSampler(), //https://github.com/open-telemetry/opentelemetry-js/issues/3057
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: processName,
}),
});我目前不确定为什么在本地运行时所有的跟踪都被采样,但是在GCP上运行时只有一个子集被采样。
https://stackoverflow.com/questions/71416066
复制相似问题