Skip to content

Commit e9b3bd1

Browse files
committed
feat: add wrangler metrics as an alias for wrangler telemetry (#7284)
* add metrics alias * tests * use each to test alias
1 parent 9391662 commit e9b3bd1

File tree

2 files changed

+35
-75
lines changed

2 files changed

+35
-75
lines changed

packages/wrangler/src/__tests__/metrics.test.ts

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -393,49 +393,33 @@ describe("metrics", () => {
393393
});
394394
});
395395

396-
describe("telemetry commands", () => {
396+
describe.each(["metrics", "telemetry"])("%s commands", (cmd) => {
397397
beforeEach(() => {
398398
vi.useFakeTimers();
399399
vi.setSystemTime(new Date(2024, 11, 12));
400400
});
401401
afterEach(() => {
402402
vi.useRealTimers();
403403
});
404-
describe("wrangler telemetry status", () => {
404+
describe(`${cmd} status`, () => {
405405
it("prints the current telemetry status based on the cached metrics config", async () => {
406406
writeMetricsConfig({
407407
permission: {
408408
enabled: true,
409409
date: new Date(2022, 6, 4),
410410
},
411411
});
412-
await runWrangler("telemetry status");
413-
expect(std.out).toMatchInlineSnapshot(`
414-
"Status: Enabled
415-
416-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
417-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
418-
"
419-
`);
412+
await runWrangler(`${cmd} status`);
413+
expect(std.out).toContain("Status: Enabled");
414+
expect(std.out).not.toContain("Status: Disabled");
420415
writeMetricsConfig({
421416
permission: {
422417
enabled: false,
423418
date: new Date(2022, 6, 4),
424419
},
425420
});
426421
await runWrangler("telemetry status");
427-
expect(std.out).toMatchInlineSnapshot(`
428-
"Status: Enabled
429-
430-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
431-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
432-
433-
Status: Disabled
434-
435-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
436-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
437-
"
438-
`);
422+
expect(std.out).toContain("Status: Disabled");
439423
});
440424

441425
it("shows wrangler.toml as the source with send_metrics is present", async () => {
@@ -446,14 +430,8 @@ describe("metrics", () => {
446430
},
447431
});
448432
writeWranglerToml({ send_metrics: false });
449-
await runWrangler("telemetry status");
450-
expect(std.out).toMatchInlineSnapshot(`
451-
"Status: Disabled (set by wrangler.toml)
452-
453-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
454-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
455-
"
456-
`);
433+
await runWrangler(`${cmd} status`);
434+
expect(std.out).toContain("Status: Disabled (set by wrangler.toml)");
457435
});
458436

459437
it("shows environment variable as the source if used", async () => {
@@ -464,26 +442,16 @@ describe("metrics", () => {
464442
},
465443
});
466444
vi.stubEnv("WRANGLER_SEND_METRICS", "false");
467-
await runWrangler("telemetry status");
468-
expect(std.out).toMatchInlineSnapshot(`
469-
"Status: Disabled (set by environment variable)
470-
471-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
472-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
473-
"
474-
`);
445+
await runWrangler(`${cmd} status`);
446+
expect(std.out).toContain(
447+
"Status: Disabled (set by environment variable)"
448+
);
475449
});
476450

477451
it("defaults to enabled if metrics config is not set", async () => {
478452
writeMetricsConfig({});
479-
await runWrangler("telemetry status");
480-
expect(std.out).toMatchInlineSnapshot(`
481-
"Status: Enabled
482-
483-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
484-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
485-
"
486-
`);
453+
await runWrangler(`${cmd} status`);
454+
expect(std.out).toContain("Status: Enabled");
487455
});
488456

489457
it("prioritises environment variable over send_metrics", async () => {
@@ -495,31 +463,24 @@ describe("metrics", () => {
495463
});
496464
writeWranglerToml({ send_metrics: true });
497465
vi.stubEnv("WRANGLER_SEND_METRICS", "false");
498-
await runWrangler("telemetry status");
499-
expect(std.out).toMatchInlineSnapshot(`
500-
"Status: Disabled (set by environment variable)
501-
502-
To configure telemetry globally on this machine, you can run \`wrangler telemetry disable / enable\`.
503-
You can override this for individual projects with the environment variable \`WRANGLER_SEND_METRICS=true/false\`.
504-
"
505-
`);
466+
await runWrangler(`${cmd} status`);
467+
expect(std.out).toContain(
468+
"Status: Disabled (set by environment variable)"
469+
);
506470
});
507471
});
508472

509-
it("disables telemetry when `wrangler telemetry disable` is run", async () => {
473+
it(`disables telemetry when "wrangler ${cmd} disable" is run`, async () => {
510474
writeMetricsConfig({
511475
permission: {
512476
enabled: true,
513477
date: new Date(2022, 6, 4),
514478
},
515479
});
516-
await runWrangler("telemetry disable");
517-
expect(std.out).toMatchInlineSnapshot(`
518-
"Status: Disabled
480+
await runWrangler(`${cmd} disable`);
481+
expect(std.out).toContain(`Status: Disabled
519482
520-
Wrangler is no longer collecting telemetry about your usage.
521-
"
522-
`);
483+
Wrangler is no longer collecting telemetry about your usage.`);
523484
expect(readMetricsConfig()).toMatchObject({
524485
permission: {
525486
enabled: false,
@@ -528,20 +489,17 @@ describe("metrics", () => {
528489
});
529490
});
530491

531-
it("enables telemetry when `wrangler telemetry enable` is run", async () => {
492+
it(`enables telemetry when "wrangler ${cmd} enable" is run`, async () => {
532493
writeMetricsConfig({
533494
permission: {
534495
enabled: false,
535496
date: new Date(2022, 6, 4),
536497
},
537498
});
538-
await runWrangler("telemetry enable");
539-
expect(std.out).toMatchInlineSnapshot(`
540-
"Status: Enabled
499+
await runWrangler(`${cmd} enable`);
500+
expect(std.out).toContain(`Status: Enabled
541501
542-
Wrangler is now collecting telemetry about your usage. Thank you for helping make Wrangler better 🧡
543-
"
544-
`);
502+
Wrangler is now collecting telemetry about your usage. Thank you for helping make Wrangler better 🧡`);
545503
expect(readMetricsConfig()).toMatchObject({
546504
permission: {
547505
enabled: true,
@@ -557,13 +515,10 @@ describe("metrics", () => {
557515
date: new Date(2022, 6, 4),
558516
},
559517
});
560-
await runWrangler("telemetry enable");
561-
expect(std.out).toMatchInlineSnapshot(`
562-
"Status: Enabled
518+
await runWrangler(`${cmd} enable`);
519+
expect(std.out).toContain(`Status: Enabled
563520
564-
Wrangler is now collecting telemetry about your usage. Thank you for helping make Wrangler better 🧡
565-
"
566-
`);
521+
Wrangler is now collecting telemetry about your usage. Thank you for helping make Wrangler better 🧡`);
567522
const config = readMetricsConfig();
568523
expect(config).toMatchObject({
569524
c3permission: {

packages/wrangler/src/metrics/commands.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import chalk from "chalk";
2-
import { defineCommand, defineNamespace } from "../core";
2+
import { defineAlias, defineCommand, defineNamespace } from "../core";
33
import { getWranglerSendMetricsFromEnv } from "../environment-variables/misc-variables";
44
import { logger } from "../logger";
55
import { readMetricsConfig, updateMetricsPermission } from "./metrics-config";
@@ -14,6 +14,11 @@ defineNamespace({
1414
},
1515
});
1616

17+
defineAlias({
18+
command: "wrangler metrics",
19+
aliasOf: "wrangler telemetry",
20+
});
21+
1722
defineCommand({
1823
command: "wrangler telemetry disable",
1924
metadata: {

0 commit comments

Comments
 (0)