























@@ -210,6 +210,12 @@ function startedSpanOptions(name: string) {
210210return call?.[1] as { attributes?: Record<string, unknown>; startTime?: unknown } | undefined;
211211}
212212213+function lastHistogramRecord(name: string) {
214+return telemetryState.histograms.get(name)?.record.mock.calls.at(-1) as
215+| [unknown, Record<string, unknown>?]
216+| undefined;
217+}
218+213219async function emitAndCaptureLog(
214220event: Omit<Extract<Parameters<typeof emitDiagnosticEvent>[0], { type: "log.record" }>, "type">,
215221options: { trusted?: boolean } = {},
@@ -453,11 +459,9 @@ describe("diagnostics-otel service", () => {
453459});
454460await flushDiagnosticEvents();
455461456-const runDurationRecordCall = telemetryState.histograms
457-.get("openclaw.run.duration_ms")
458-?.record.mock.calls.at(-1);
462+const runDurationRecordCall = lastHistogramRecord("openclaw.run.duration_ms");
459463expect(runDurationRecordCall?.[0]).toBe(100);
460-const runDurationAttributes = runDurationRecordCall?.[1] as Record<string, unknown> | undefined;
464+const runDurationAttributes = runDurationRecordCall?.[1];
461465expect(runDurationAttributes?.["openclaw.provider"]).toBe("openai");
462466expect(runDurationAttributes?.["openclaw.model"]).toBe("gpt-5.4");
463467const runSpanOptions = startedSpanOptions("openclaw.run");
@@ -537,16 +541,12 @@ describe("diagnostics-otel service", () => {
537541).toHaveBeenCalledWith(1.4, {
538542"openclaw.liveness.reason": "event_loop_delay:cpu",
539543});
540-const livenessSpan = telemetryState.tracer.startSpan.mock.calls.find(
541-(call) => call[0] === "openclaw.liveness.warning",
544+const livenessSpanOptions = startedSpanOptions("openclaw.liveness.warning");
545+expect(livenessSpanOptions?.attributes?.["openclaw.liveness.reason"]).toBe(
546+"event_loop_delay:cpu",
542547);
543-expect(livenessSpan?.[1]).toMatchObject({
544-attributes: {
545-"openclaw.liveness.reason": "event_loop_delay:cpu",
546-"openclaw.liveness.active": 2,
547-"openclaw.liveness.queued": 4,
548-},
549-});
548+expect(livenessSpanOptions?.attributes?.["openclaw.liveness.active"]).toBe(2);
549+expect(livenessSpanOptions?.attributes?.["openclaw.liveness.queued"]).toBe(4);
550550const span = telemetryState.spans.find((item) => item.name === "openclaw.liveness.warning");
551551expect(span?.setStatus).toHaveBeenCalledWith({
552552code: 2,
@@ -577,18 +577,14 @@ describe("diagnostics-otel service", () => {
577577});
578578await flushDiagnosticEvents();
579579580-expect(events).toEqual(
581-expect.arrayContaining([
582-expect.objectContaining({
583-type: "telemetry.exporter",
584-exporter: "diagnostics-otel",
585-signal: "logs",
586-status: "failure",
587-reason: "emit_failed",
588-errorCategory: "TypeError",
589-}),
590-]),
591-);
580+const exporterEvents = events.filter((event) => event.type === "telemetry.exporter");
581+const failureEvent = exporterEvents.find((event) => event.status === "failure");
582+expect(failureEvent?.type).toBe("telemetry.exporter");
583+expect(failureEvent?.exporter).toBe("diagnostics-otel");
584+expect(failureEvent?.signal).toBe("logs");
585+expect(failureEvent?.status).toBe("failure");
586+expect(failureEvent?.reason).toBe("emit_failed");
587+expect(failureEvent?.errorCategory).toBe("TypeError");
592588expect(
593589telemetryState.counters.get("openclaw.telemetry.exporter.events")?.add,
594590).toHaveBeenCalledWith(1, {
@@ -640,13 +636,10 @@ describe("diagnostics-otel service", () => {
640636});
641637await flushDiagnosticEvents();
642638643-expect(telemetryState.histograms.get("openclaw.run.duration_ms")?.record).toHaveBeenCalledWith(
644-100,
645-expect.objectContaining({
646-"openclaw.outcome": "blocked",
647-"openclaw.blocked_by": "policy-plugin",
648-}),
649-);
639+const runDurationRecordCall = lastHistogramRecord("openclaw.run.duration_ms");
640+expect(runDurationRecordCall?.[0]).toBe(100);
641+expect(runDurationRecordCall?.[1]?.["openclaw.outcome"]).toBe("blocked");
642+expect(runDurationRecordCall?.[1]?.["openclaw.blocked_by"]).toBe("policy-plugin");
650643expect(JSON.stringify(telemetryState)).not.toContain("matched secret prompt");
651644652645await service.stop?.(ctx);
@@ -669,12 +662,9 @@ describe("diagnostics-otel service", () => {
669662await flushDiagnosticEvents();
670663671664expect(sdkStart).not.toHaveBeenCalled();
672-expect(telemetryState.histograms.get("openclaw.run.duration_ms")?.record).toHaveBeenCalledWith(
673-100,
674-expect.objectContaining({
675-"openclaw.provider": "openai",
676-}),
677-);
665+const runDurationRecordCall = lastHistogramRecord("openclaw.run.duration_ms");
666+expect(runDurationRecordCall?.[0]).toBe(100);
667+expect(runDurationRecordCall?.[1]?.["openclaw.provider"]).toBe("openai");
678668expect(telemetryState.tracer.startSpan).not.toHaveBeenCalled();
679669680670await service.stop?.(ctx);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。