




















@@ -137,22 +137,36 @@ function traceFlagsToOtel(traceFlags: string | undefined): TraceFlags {
137137return (parsed & TraceFlags.SAMPLED) !== 0 ? TraceFlags.SAMPLED : TraceFlags.NONE;
138138}
139139140+function contextForTraceContext(traceContext: DiagnosticTraceContext | undefined) {
141+const normalized = normalizeTraceContext(traceContext);
142+if (!normalized?.spanId) {
143+return undefined;
144+}
145+return trace.setSpanContext(otelContextApi.active(), {
146+traceId: normalized.traceId,
147+spanId: normalized.spanId,
148+traceFlags: traceFlagsToOtel(normalized.traceFlags),
149+isRemote: true,
150+});
151+}
152+140153function addTraceAttributes(
141154attributes: Record<string, string | number | boolean>,
142155traceContext: DiagnosticTraceContext | undefined,
143156): void {
144-if (!traceContext) {
157+const normalized = normalizeTraceContext(traceContext);
158+if (!normalized) {
145159return;
146160}
147-attributes["openclaw.traceId"] = traceContext.traceId;
148-if (traceContext.spanId) {
149-attributes["openclaw.spanId"] = traceContext.spanId;
161+attributes["openclaw.traceId"] = normalized.traceId;
162+if (normalized.spanId) {
163+attributes["openclaw.spanId"] = normalized.spanId;
150164}
151-if (traceContext.parentSpanId) {
152-attributes["openclaw.parentSpanId"] = traceContext.parentSpanId;
165+if (normalized.parentSpanId) {
166+attributes["openclaw.parentSpanId"] = normalized.parentSpanId;
153167}
154-if (traceContext.traceFlags) {
155-attributes["openclaw.traceFlags"] = traceContext.traceFlags;
168+if (normalized.traceFlags) {
169+attributes["openclaw.traceFlags"] = normalized.traceFlags;
156170}
157171}
158172@@ -448,13 +462,9 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
448462attributes: redactOtelAttributes(attributes),
449463timestamp: meta?.date ?? new Date(),
450464};
451-if (traceContext?.spanId) {
452-logRecord.context = trace.setSpanContext(otelContextApi.active(), {
453-traceId: traceContext.traceId,
454-spanId: traceContext.spanId,
455-traceFlags: traceFlagsToOtel(traceContext.traceFlags),
456-isRemote: true,
457-});
465+const logContext = contextForTraceContext(traceContext);
466+if (logContext) {
467+logRecord.context = logContext;
458468}
459469otelLogger.emit(logRecord);
460470} catch (err) {
@@ -467,13 +477,19 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
467477name: string,
468478attributes: Record<string, string | number>,
469479durationMs?: number,
480+traceContext?: DiagnosticTraceContext,
470481) => {
471482const startTime =
472483typeof durationMs === "number" ? Date.now() - Math.max(0, durationMs) : undefined;
473-const span = tracer.startSpan(name, {
474- attributes,
475- ...(startTime ? { startTime } : {}),
476-});
484+const parentContext = contextForTraceContext(traceContext);
485+const span = tracer.startSpan(
486+name,
487+{
488+ attributes,
489+ ...(startTime ? { startTime } : {}),
490+},
491+parentContext,
492+);
477493return span;
478494};
479495@@ -537,7 +553,7 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
537553"openclaw.tokens.total": usage.total ?? 0,
538554};
539555540-const span = spanWithDuration("openclaw.model.usage", spanAttrs, evt.durationMs);
556+const span = spanWithDuration("openclaw.model.usage", spanAttrs, evt.durationMs, evt.trace);
541557span.end();
542558};
543559@@ -568,7 +584,12 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
568584if (evt.chatId !== undefined) {
569585spanAttrs["openclaw.chatId"] = String(evt.chatId);
570586}
571-const span = spanWithDuration("openclaw.webhook.processed", spanAttrs, evt.durationMs);
587+const span = spanWithDuration(
588+"openclaw.webhook.processed",
589+spanAttrs,
590+evt.durationMs,
591+evt.trace,
592+);
572593span.end();
573594};
574595@@ -591,9 +612,13 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
591612if (evt.chatId !== undefined) {
592613spanAttrs["openclaw.chatId"] = String(evt.chatId);
593614}
594-const span = tracer.startSpan("openclaw.webhook.error", {
595-attributes: spanAttrs,
596-});
615+const span = tracer.startSpan(
616+"openclaw.webhook.error",
617+{
618+attributes: spanAttrs,
619+},
620+contextForTraceContext(evt.trace),
621+);
597622span.setStatus({ code: SpanStatusCode.ERROR, message: redactedError });
598623span.end();
599624};
@@ -648,7 +673,12 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
648673if (evt.reason) {
649674spanAttrs["openclaw.reason"] = redactSensitiveText(evt.reason);
650675}
651-const span = spanWithDuration("openclaw.message.processed", spanAttrs, evt.durationMs);
676+const span = spanWithDuration(
677+"openclaw.message.processed",
678+spanAttrs,
679+evt.durationMs,
680+evt.trace,
681+);
652682if (evt.outcome === "error" && evt.error) {
653683span.setStatus({ code: SpanStatusCode.ERROR, message: redactSensitiveText(evt.error) });
654684}
@@ -699,7 +729,11 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
699729addSessionIdentityAttrs(spanAttrs, evt);
700730spanAttrs["openclaw.queueDepth"] = evt.queueDepth ?? 0;
701731spanAttrs["openclaw.ageMs"] = evt.ageMs;
702-const span = tracer.startSpan("openclaw.session.stuck", { attributes: spanAttrs });
732+const span = tracer.startSpan(
733+"openclaw.session.stuck",
734+{ attributes: spanAttrs },
735+contextForTraceContext(evt.trace),
736+);
703737span.setStatus({ code: SpanStatusCode.ERROR, message: "session stuck" });
704738span.end();
705739};
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。