Skip to content

Commit 8fb6f39

Browse files
committed
feat(telemetry): report show state as JSON
Give the reporting command a machine-readable form and classify the group and mutations against the CLI JSON-output policy.
1 parent c7b7e83 commit 8fb6f39

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎docs/gateway/telemetry.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Run this command before or after changing your preference:
2020
openclaw telemetry show
2121
```
2222

23+
Add `--json` to get the same state and payload as one machine-readable
24+
document.
25+
2326
The output shows whether feature statistics are enabled, why they are enabled
2427
or disabled, the request endpoint, and the last successful check. When feature
2528
statistics are enabled, it prints the exact JSON payload the next request would

‎src/cli/program/root-command-descriptions.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const JSON_NOT_APPLICABLE = {
5050
"system",
5151
"system heartbeat",
5252
"promos",
53+
"telemetry",
5354
"infer",
5455
"infer model",
5556
"infer model auth",
@@ -146,6 +147,8 @@ const JSON_NOT_APPLICABLE = {
146147
"uninstall",
147148
"backup enable",
148149
"backup disable",
150+
"telemetry on",
151+
"telemetry off",
149152
"config set",
150153
"mcp add",
151154
"mcp set",

‎src/cli/telemetry-cli.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,40 @@ describe("telemetry cli", () => {
7979
);
8080
});
8181

82+
it("reports the same state and canonical payload as one JSON document", async () => {
83+
await runTelemetryCli(["show", "--json"]);
84+
85+
expect(mocks.runtimeLogs).toHaveLength(1);
86+
expect(JSON.parse(mocks.runtimeLogs[0] ?? "")).toEqual({
87+
featureStatsEnabled: true,
88+
reason: "enabled",
89+
endpoint: "https://telemetry.openclaw.ai/api/latest-version",
90+
lastPingAt: "2026-08-22T12:00:00.000Z",
91+
request: {
92+
method: "POST",
93+
userAgent: "openclaw/2026.8.2 (darwin; node/26.0.1; arm64; gateway)",
94+
payload,
95+
},
96+
});
97+
});
98+
99+
it("reports a null JSON request when update checks are disabled", async () => {
100+
mocks.resolveTelemetryStatus.mockReturnValue({
101+
enabled: false,
102+
reason: "update-disabled",
103+
endpoint: "https://telemetry.openclaw.ai/api/latest-version",
104+
});
105+
106+
await runTelemetryCli(["show", "--json"]);
107+
108+
expect(JSON.parse(mocks.runtimeLogs[0] ?? "")).toMatchObject({
109+
featureStatsEnabled: false,
110+
reason: "update-disabled",
111+
lastPingAt: null,
112+
request: null,
113+
});
114+
});
115+
82116
it("shows only the update request headers when feature statistics are disabled", async () => {
83117
mocks.resolveTelemetryStatus.mockReturnValue({
84118
enabled: false,

‎src/cli/telemetry-cli.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,34 @@ const TELEMETRY_REASON_LABELS = {
1616
"update-disabled": "update checks are disabled",
1717
} satisfies Record<ReturnType<typeof resolveTelemetryStatus>["reason"], string>;
1818

19-
async function showTelemetry(): Promise<void> {
19+
async function showTelemetry(options: { json?: boolean }): Promise<void> {
2020
const config = getRuntimeConfig({ skipPluginValidation: true });
2121
const telemetry = resolveTelemetryStatus(config);
2222
const userAgent = buildTelemetryUserAgent("gateway");
23+
const requestSent = telemetry.reason !== "update-disabled";
24+
const payload = telemetry.enabled
25+
? buildTelemetryPayload(config, { surface: "gateway" })
26+
: undefined;
27+
28+
if (options.json) {
29+
defaultRuntime.log(
30+
JSON.stringify({
31+
featureStatsEnabled: telemetry.enabled,
32+
reason: telemetry.reason,
33+
endpoint: telemetry.endpoint,
34+
lastPingAt: telemetry.lastPingAt ? new Date(telemetry.lastPingAt).toISOString() : null,
35+
request: requestSent
36+
? {
37+
method: telemetry.enabled ? "POST" : "GET",
38+
userAgent,
39+
...(payload ? { payload } : {}),
40+
}
41+
: null,
42+
}),
43+
);
44+
return;
45+
}
46+
2347
defaultRuntime.log(`Feature stats: ${telemetry.enabled ? "enabled" : "disabled"}`);
2448
defaultRuntime.log(`Reason: ${TELEMETRY_REASON_LABELS[telemetry.reason]}`);
2549
defaultRuntime.log(`Endpoint: ${telemetry.endpoint}`);
@@ -32,9 +56,9 @@ async function showTelemetry(): Promise<void> {
3256
}
3357
defaultRuntime.log(`Request: ${telemetry.enabled ? "POST" : "GET"} ${telemetry.endpoint}`);
3458
defaultRuntime.log(`User-Agent: ${userAgent}`);
35-
if (telemetry.enabled) {
59+
if (payload) {
3660
defaultRuntime.log("Payload:");
37-
defaultRuntime.log(JSON.stringify(buildTelemetryPayload(config, { surface: "gateway" })));
61+
defaultRuntime.log(JSON.stringify(payload));
3862
}
3963
}
4064

@@ -62,7 +86,10 @@ export function registerTelemetryCli(program: Command): void {
6286
telemetry
6387
.command("show")
6488
.description("Show exactly what the daily update request sends")
65-
.action(async () => runCommandWithRuntime(defaultRuntime, showTelemetry));
89+
.option("--json", "Print the request and payload as JSON")
90+
.action(async (options: { json?: boolean }) =>
91+
runCommandWithRuntime(defaultRuntime, () => showTelemetry(options)),
92+
);
6693

6794
telemetry
6895
.command("on")

0 commit comments

Comments
 (0)