Skip to content

Commit be9ce48

Browse files
committed
Add marketplace feed telemetry
1 parent 9bb0043 commit be9ce48

3 files changed

Lines changed: 278 additions & 2 deletions

File tree

src/cli/plugins-cli.marketplace-entries.test.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Covers the hosted OpenClaw marketplace feed entries command.
2-
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { mkdtemp, readFile } from "node:fs/promises";
3+
import { tmpdir } from "node:os";
4+
import path from "node:path";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
36

47
const mocks = vi.hoisted(() => {
58
const defaultRuntime = {
@@ -38,6 +41,19 @@ vi.mock("../plugins/official-external-plugin-catalog.js", async (importOriginal)
3841
};
3942
});
4043

44+
async function createTimelinePath(): Promise<string> {
45+
const dir = await mkdtemp(path.join(tmpdir(), "openclaw-marketplace-entries-"));
46+
return path.join(dir, "timeline.jsonl");
47+
}
48+
49+
async function readTimeline(pathname: string): Promise<Record<string, unknown>[]> {
50+
const content = await readFile(pathname, "utf8");
51+
return content
52+
.trim()
53+
.split("\n")
54+
.map((line) => JSON.parse(line) as Record<string, unknown>);
55+
}
56+
4157
describe("plugins marketplace entries", () => {
4258
beforeEach(() => {
4359
mocks.defaultRuntime.error.mockClear();
@@ -46,6 +62,11 @@ describe("plugins marketplace entries", () => {
4662
mocks.defaultRuntime.writeJson.mockClear();
4763
mocks.getRuntimeConfig.mockReset();
4864
mocks.loadConfiguredHostedOfficialExternalPluginCatalogEntries.mockReset();
65+
vi.unstubAllEnvs();
66+
});
67+
68+
afterEach(() => {
69+
vi.unstubAllEnvs();
4970
});
5071

5172
it("lists entries from the configured marketplace feed as JSON", async () => {
@@ -191,4 +212,66 @@ describe("plugins marketplace entries", () => {
191212
expect(output).toContain("hosted catalog feed offline mode");
192213
expect(mocks.defaultRuntime.exit).not.toHaveBeenCalled();
193214
});
215+
216+
it("emits bounded diagnostics for feed entry listing", async () => {
217+
const timelinePath = await createTimelinePath();
218+
vi.stubEnv("OPENCLAW_DIAGNOSTICS", "1");
219+
vi.stubEnv("OPENCLAW_DIAGNOSTICS_TIMELINE_PATH", timelinePath);
220+
mocks.getRuntimeConfig.mockReturnValue({});
221+
mocks.loadConfiguredHostedOfficialExternalPluginCatalogEntries.mockResolvedValue({
222+
source: "hosted-snapshot",
223+
entries: [
224+
{
225+
name: "@acme/calendar",
226+
openclaw: { plugin: { id: "acme-calendar", label: "Acme Calendar" } },
227+
},
228+
],
229+
feed: {
230+
schemaVersion: 1,
231+
id: "acme-marketplace",
232+
generatedAt: "2026-06-23T00:00:00.000Z",
233+
sequence: 7,
234+
entries: [],
235+
},
236+
metadata: {
237+
url: "https://user:[email protected]/openclaw/feed?token=leak#frag",
238+
status: 200,
239+
checksum: "feed-sha",
240+
},
241+
snapshot: {
242+
body: "{}",
243+
metadata: {
244+
url: "https://user:[email protected]/openclaw/feed?token=leak#frag",
245+
status: 200,
246+
checksum: "feed-sha",
247+
},
248+
savedAt: "2026-06-23T01:02:03.000Z",
249+
},
250+
error: "hosted catalog feed offline mode",
251+
});
252+
253+
const { runPluginMarketplaceEntriesCommand } = await import("./plugins-cli.runtime.js");
254+
await runPluginMarketplaceEntriesCommand({ feedProfile: "acme", offline: true });
255+
256+
const [event] = await readTimeline(timelinePath);
257+
expect(event?.name).toBe("plugins.marketplace.feed.entries");
258+
expect(event?.phase).toBe("plugin-marketplace");
259+
expect(event?.attributes).toMatchObject({
260+
command: "entries",
261+
entries: 1,
262+
fallbackCategory: "offline",
263+
feedIdPresent: true,
264+
feedProfileProvided: true,
265+
feedSequence: 7,
266+
offline: true,
267+
payloadChecksumPresent: true,
268+
snapshotUsed: true,
269+
source: "hosted-snapshot",
270+
});
271+
expect(JSON.stringify(event)).not.toContain("packages.acme.example");
272+
expect(JSON.stringify(event)).not.toContain("acme-marketplace");
273+
expect(JSON.stringify(event)).not.toContain("feed-sha");
274+
expect(JSON.stringify(event)).not.toContain("secret");
275+
expect(JSON.stringify(event)).not.toContain("token=leak");
276+
});
194277
});

src/cli/plugins-cli.marketplace-refresh.test.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Covers the hosted OpenClaw marketplace feed refresh command.
2-
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { mkdtemp, readFile } from "node:fs/promises";
3+
import { tmpdir } from "node:os";
4+
import path from "node:path";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
36

47
const mocks = vi.hoisted(() => {
58
const defaultRuntime = {
@@ -33,6 +36,19 @@ vi.mock("../plugins/official-external-plugin-catalog.js", () => ({
3336
mocks.loadConfiguredHostedOfficialExternalPluginCatalogEntries,
3437
}));
3538

39+
async function createTimelinePath(): Promise<string> {
40+
const dir = await mkdtemp(path.join(tmpdir(), "openclaw-marketplace-refresh-"));
41+
return path.join(dir, "timeline.jsonl");
42+
}
43+
44+
async function readTimeline(pathname: string): Promise<Record<string, unknown>[]> {
45+
const content = await readFile(pathname, "utf8");
46+
return content
47+
.trim()
48+
.split("\n")
49+
.map((line) => JSON.parse(line) as Record<string, unknown>);
50+
}
51+
3652
describe("plugins marketplace refresh", () => {
3753
beforeEach(() => {
3854
mocks.defaultRuntime.error.mockClear();
@@ -41,6 +57,11 @@ describe("plugins marketplace refresh", () => {
4157
mocks.defaultRuntime.writeJson.mockClear();
4258
mocks.getRuntimeConfig.mockReset();
4359
mocks.loadConfiguredHostedOfficialExternalPluginCatalogEntries.mockReset();
60+
vi.unstubAllEnvs();
61+
});
62+
63+
afterEach(() => {
64+
vi.unstubAllEnvs();
4465
});
4566

4667
it("refreshes the configured marketplace feed and prints JSON", async () => {
@@ -240,4 +261,68 @@ describe("plugins marketplace refresh", () => {
240261
);
241262
expect(mocks.defaultRuntime.exit).toHaveBeenCalledWith(1);
242263
});
264+
265+
it("emits bounded diagnostics for refresh without raw feed URLs", async () => {
266+
const timelinePath = await createTimelinePath();
267+
vi.stubEnv("OPENCLAW_DIAGNOSTICS_TIMELINE_PATH", timelinePath);
268+
const config = {
269+
diagnostics: { flags: ["timeline"] },
270+
marketplaces: {
271+
feeds: { acme: { url: "https://packages.acme.example/openclaw/feed" } },
272+
},
273+
};
274+
mocks.getRuntimeConfig.mockReturnValue(config);
275+
mocks.loadConfiguredHostedOfficialExternalPluginCatalogEntries.mockResolvedValue({
276+
source: "hosted",
277+
entries: [{ name: "@acme/calendar" }, { name: "@acme/docs" }],
278+
feed: {
279+
schemaVersion: 1,
280+
id: "acme-marketplace",
281+
generatedAt: "2026-06-23T00:00:00.000Z",
282+
sequence: 7,
283+
entries: [],
284+
},
285+
metadata: {
286+
url: "https://user:[email protected]/openclaw/feed?token=leak#frag",
287+
status: 200,
288+
checksum: "feed-sha",
289+
etag: '"abc"',
290+
},
291+
});
292+
293+
const { runPluginMarketplaceRefreshCommand } = await import("./plugins-cli.runtime.js");
294+
await runPluginMarketplaceRefreshCommand({
295+
expectedSha256: "feed-sha",
296+
feedProfile: "acme",
297+
feedUrl: "https://override.example/openclaw/feed?token=override-leak",
298+
});
299+
300+
const [event] = await readTimeline(timelinePath);
301+
expect(mocks.loadConfiguredHostedOfficialExternalPluginCatalogEntries).toHaveBeenCalledWith(
302+
config,
303+
expect.objectContaining({
304+
feedUrl: "https://override.example/openclaw/feed?token=override-leak",
305+
}),
306+
);
307+
expect(event?.name).toBe("plugins.marketplace.feed.refresh");
308+
expect(event?.phase).toBe("plugin-marketplace");
309+
expect(event?.attributes).toMatchObject({
310+
command: "refresh",
311+
entries: 2,
312+
expectedSha256Provided: true,
313+
feedIdPresent: true,
314+
feedProfileProvided: true,
315+
feedSequence: 7,
316+
feedUrlOverride: true,
317+
hasEtag: true,
318+
payloadChecksumPresent: true,
319+
source: "hosted",
320+
});
321+
expect(JSON.stringify(event)).not.toContain("packages.acme.example");
322+
expect(JSON.stringify(event)).not.toContain("acme-marketplace");
323+
expect(JSON.stringify(event)).not.toContain("feed-sha");
324+
expect(JSON.stringify(event)).not.toContain("secret");
325+
expect(JSON.stringify(event)).not.toContain("token=leak");
326+
expect(JSON.stringify(event)).not.toContain("override-leak");
327+
});
243328
});

src/cli/plugins-cli.runtime.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
replaceConfigFile,
1414
} from "../config/config.js";
1515
import type { OpenClawConfig } from "../config/types.openclaw.js";
16+
import { emitDiagnosticsTimelineEvent } from "../infra/diagnostics-timeline.js";
1617
import { tracePluginLifecyclePhaseAsync } from "../plugins/plugin-lifecycle-trace.js";
1718
import { defaultRuntime } from "../runtime.js";
1819
import { shortenHomeInString } from "../utils.js";
@@ -485,6 +486,99 @@ type MarketplaceEntryPayload = {
485486
};
486487
};
487488

489+
type MarketplaceFeedTelemetryOptions = {
490+
expectedSha256?: string;
491+
feedProfile?: string;
492+
feedUrl?: string;
493+
offline?: boolean;
494+
};
495+
496+
function classifyMarketplaceFeedFallback(error: string | undefined): string | undefined {
497+
const text = error?.toLowerCase();
498+
if (!text) {
499+
return undefined;
500+
}
501+
if (text.includes("offline mode")) {
502+
return "offline";
503+
}
504+
if (text.includes("checksum mismatch")) {
505+
return "checksum_mismatch";
506+
}
507+
if (text.includes("schema")) {
508+
return "schema";
509+
}
510+
if (/http\s+304/u.test(text)) {
511+
return "not_modified";
512+
}
513+
if (/http\s+\d{3}/u.test(text)) {
514+
return "http_error";
515+
}
516+
if (text.includes("timed out") || text.includes("timeout")) {
517+
return "timeout";
518+
}
519+
return "error";
520+
}
521+
522+
function emitMarketplaceFeedTelemetry(params: {
523+
command: "entries" | "refresh";
524+
entryCount?: number;
525+
failedPinnedRefresh?: boolean;
526+
opts: MarketplaceFeedTelemetryOptions;
527+
config?: OpenClawConfig;
528+
payload: MarketplaceRefreshPayload;
529+
}): void {
530+
const attributes: Record<string, string | number | boolean | null> = {
531+
command: params.command,
532+
entries: params.entryCount ?? params.payload.entries,
533+
source: params.payload.source,
534+
};
535+
if (params.opts.feedProfile?.trim()) {
536+
attributes.feedProfileProvided = true;
537+
}
538+
if (params.opts.feedUrl?.trim()) {
539+
attributes.feedUrlOverride = true;
540+
}
541+
if (params.opts.offline === true) {
542+
attributes.offline = true;
543+
}
544+
if (params.opts.expectedSha256?.trim()) {
545+
attributes.expectedSha256Provided = true;
546+
}
547+
if (params.payload.feed) {
548+
attributes.feedIdPresent = true;
549+
attributes.feedSequence = params.payload.feed.sequence;
550+
}
551+
if (params.payload.metadata) {
552+
attributes.httpStatus = params.payload.metadata.status;
553+
if (params.payload.metadata.checksum) {
554+
attributes.payloadChecksumPresent = true;
555+
}
556+
attributes.hasEtag = Boolean(params.payload.metadata.etag);
557+
attributes.hasLastModified = Boolean(params.payload.metadata.lastModified);
558+
}
559+
if (params.payload.snapshot) {
560+
attributes.snapshotUsed = true;
561+
}
562+
const fallbackCategory = classifyMarketplaceFeedFallback(params.payload.error);
563+
if (fallbackCategory) {
564+
attributes.fallbackCategory = fallbackCategory;
565+
}
566+
if (params.failedPinnedRefresh === true) {
567+
attributes.pinnedRefreshFailed = true;
568+
}
569+
emitDiagnosticsTimelineEvent(
570+
{
571+
type: "mark",
572+
name: `plugins.marketplace.feed.${params.command}`,
573+
phase: "plugin-marketplace",
574+
attributes,
575+
},
576+
{
577+
config: params.config,
578+
},
579+
);
580+
}
581+
488582
function buildMarketplaceRefreshPayload(
489583
result: Awaited<
490584
ReturnType<
@@ -653,6 +747,13 @@ export async function runPluginMarketplaceEntriesCommand(
653747
return payload;
654748
});
655749

750+
emitMarketplaceFeedTelemetry({
751+
command: "entries",
752+
entryCount: entries.length,
753+
opts,
754+
config: cfg,
755+
payload: summary,
756+
});
656757
if (opts.json) {
657758
defaultRuntime.writeJson({ ...summary, entries, entryCount: entries.length });
658759
return;
@@ -709,6 +810,13 @@ export async function runPluginMarketplaceRefreshCommand(
709810
expectedSha256,
710811
source: payload.source,
711812
});
813+
emitMarketplaceFeedTelemetry({
814+
command: "refresh",
815+
failedPinnedRefresh,
816+
opts,
817+
config: cfg,
818+
payload,
819+
});
712820

713821
if (opts.json) {
714822
defaultRuntime.writeJson(payload);

0 commit comments

Comments
 (0)