Skip to content

Commit f5b9cd5

Browse files
edmundhungemily-shenlrapoport-cf
authored
feat(wrangler): enable telemetry by default (#7291)
* feat: add telemetry commands (#7236) * add telemetry commands * changeset * fix and test dates * update changeset * add global/project status * default true * remove changeset * update wrangler telemetry status feat: add `wrangler metrics` as an alias for `wrangler telemetry` (#7284) * add metrics alias * tests * use each to test alias feat: send metrics for command start/complete/error (#7267) * stop collecting userId in telemetry Co-authored-by: emily-shen <[email protected]> * implement telemetry collection * infer errorType based on the constructor name * implement common event properties * log common event properties Co-authored-by: Edmund Hung <[email protected]> * respect metric enabled/disabled * remove dispatcher.identify * include SPARROW_SOURCE_KEY in PR pre-release build * fix tests * ensure debug log covers the request failed message * replace SPARROW_SOURCE_KEY regardless whethe env exists --------- Co-authored-by: Edmund Hung <[email protected]> Co-authored-by: emily-shen <[email protected]> Co-authored-by: Edmund Hung <[email protected]> fix nested properties (#7300) feat: add banner to indicate when telemetry is on (#7302) * add banner * abort if telemetry disable * basic sendNewEvent tests * banner tests * settle promises before exiting * remove unnecessary banner logic * just check if version is different feat: add some more properties to telemetry events (#7320) * isCI and isNonInteractive * add argsUsed and argsCombination * don't include args if value is false * redact arg values if string * lint * isNonInteractive -> isInteractive cleanup defineCommand test duration log metrics request failure add draft telemetry.md add node and os versions don't send wrangler metrics from c3 if disabled don't send c3 metrics from wrangler init add config type add more comments to send-event move types out of send-event.ts add comment about applyBeforeValidation normalize into camelcase refactor telemetry command update tests and some comments normalise all args pr feedback update telemetry.md use useragent to get package manager make sendEvent/sendNewEvent sync rename sendEvent and sendNewEvent fix lock file remove flushPromises changeset fail silently move telemetry.md out of src tiody up readRawConfig using the github browser merge is always a bad idea fix e2e * telemetry.md typos * Update packages/wrangler/telemetry.md --------- Co-authored-by: emily-shen <[email protected]> Co-authored-by: lrapoport-cf <[email protected]>
1 parent 4e571fd commit f5b9cd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1406
-529
lines changed

.changeset/perfect-cougars-lie.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Add anonymous telemetry to Wrangler commands
6+
7+
For new users, Cloudflare will collect anonymous usage telemetry to guide and improve Wrangler's development. If you have already opted out of Wrangler's existing telemetry, this setting will still be respected.
8+
9+
See our [data policy](https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler/telemetry.md) for more details on what we collect and how to opt out if you wish.

.github/workflows/create-pullrequest-prerelease.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ jobs:
5757
run: node .github/prereleases/2-build-pack-upload.mjs
5858
env:
5959
NODE_ENV: "production"
60+
# this is the "test/staging" key for sparrow analytics
61+
SPARROW_SOURCE_KEY: "5adf183f94b3436ba78d67f506965998"
6062
ALGOLIA_APP_ID: ${{ secrets.ALGOLIA_APP_ID }}
6163
ALGOLIA_PUBLIC_KEY: ${{ secrets.ALGOLIA_PUBLIC_KEY }}
6264
SENTRY_DSN: "https://[email protected]/583"

packages/create-cloudflare/src/helpers/__tests__/command.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { existsSync } from "fs";
22
import { spawn } from "cross-spawn";
3+
import { readMetricsConfig } from "helpers/metrics-config";
34
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
45
import whichPMRuns from "which-pm-runs";
56
import { quoteShellArgs, runCommand } from "../command";
@@ -13,6 +14,7 @@ let spawnStderr: string | undefined = undefined;
1314
vi.mock("cross-spawn");
1415
vi.mock("fs");
1516
vi.mock("which-pm-runs");
17+
vi.mock("helpers/metrics-config");
1618

1719
describe("Command Helpers", () => {
1820
afterEach(() => {
@@ -55,6 +57,62 @@ describe("Command Helpers", () => {
5557
});
5658
});
5759

60+
describe("respect telemetry permissions when running wrangler", () => {
61+
test("runCommand has WRANGLER_SEND_METRICS=false if its a wrangler command and c3 telemetry is disabled", async () => {
62+
vi.mocked(readMetricsConfig).mockReturnValue({
63+
c3permission: {
64+
enabled: false,
65+
date: new Date(2000),
66+
},
67+
});
68+
await runCommand(["npx", "wrangler"]);
69+
70+
expect(spawn).toHaveBeenCalledWith(
71+
"npx",
72+
["wrangler"],
73+
expect.objectContaining({
74+
env: expect.objectContaining({ WRANGLER_SEND_METRICS: "false" }),
75+
}),
76+
);
77+
});
78+
79+
test("runCommand doesn't have WRANGLER_SEND_METRICS=false if its a wrangler command and c3 telemetry is enabled", async () => {
80+
vi.mocked(readMetricsConfig).mockReturnValue({
81+
c3permission: {
82+
enabled: true,
83+
date: new Date(2000),
84+
},
85+
});
86+
await runCommand(["npx", "wrangler"]);
87+
88+
expect(spawn).toHaveBeenCalledWith(
89+
"npx",
90+
["wrangler"],
91+
expect.objectContaining({
92+
env: expect.not.objectContaining({ WRANGLER_SEND_METRICS: "false" }),
93+
}),
94+
);
95+
});
96+
97+
test("runCommand doesn't have WRANGLER_SEND_METRICS=false if not a wrangler command", async () => {
98+
vi.mocked(readMetricsConfig).mockReturnValue({
99+
c3permission: {
100+
enabled: false,
101+
date: new Date(2000),
102+
},
103+
});
104+
await runCommand(["ls", "-l"]);
105+
106+
expect(spawn).toHaveBeenCalledWith(
107+
"ls",
108+
["-l"],
109+
expect.objectContaining({
110+
env: expect.not.objectContaining({ WRANGLER_SEND_METRICS: "false" }),
111+
}),
112+
);
113+
});
114+
});
115+
58116
describe("quoteShellArgs", () => {
59117
test.runIf(process.platform !== "win32")("mac", async () => {
60118
expect(quoteShellArgs([`pages:dev`])).toEqual("pages:dev");

packages/create-cloudflare/src/helpers/command.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { stripAnsi } from "@cloudflare/cli";
22
import { CancelError } from "@cloudflare/cli/error";
33
import { isInteractive, spinner } from "@cloudflare/cli/interactive";
44
import { spawn } from "cross-spawn";
5+
import { readMetricsConfig } from "./metrics-config";
56

67
/**
78
* Command is a string array, like ['git', 'commit', '-m', '"Initial commit"']
@@ -52,6 +53,15 @@ export const runCommand = async (
5253
doneText: opts.doneText,
5354
promise() {
5455
const [executable, ...args] = command;
56+
// Don't send metrics data on any wrangler commands if telemetry is disabled in C3
57+
// If telemetry is disabled separately for wrangler, wrangler will handle that
58+
if (args[0] === "wrangler") {
59+
const metrics = readMetricsConfig();
60+
if (metrics.c3permission?.enabled === false) {
61+
opts.env ??= {};
62+
opts.env["WRANGLER_SEND_METRICS"] = "false";
63+
}
64+
}
5565
const abortController = new AbortController();
5666
const cmd = spawn(executable, [...args], {
5767
// TODO: ideally inherit stderr, but npm install uses this for warnings

packages/wrangler/scripts/bundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ async function buildMain(flags: BuildFlags = {}) {
4444
__RELATIVE_PACKAGE_PATH__,
4545
"import.meta.url": "import_meta_url",
4646
"process.env.NODE_ENV": `'${process.env.NODE_ENV || "production"}'`,
47-
...(process.env.SPARROW_SOURCE_KEY
48-
? { SPARROW_SOURCE_KEY: `"${process.env.SPARROW_SOURCE_KEY}"` }
49-
: {}),
47+
"process.env.SPARROW_SOURCE_KEY": JSON.stringify(
48+
process.env.SPARROW_SOURCE_KEY ?? ""
49+
),
5050
...(process.env.ALGOLIA_APP_ID
5151
? { ALGOLIA_APP_ID: `"${process.env.ALGOLIA_APP_ID}"` }
5252
: {}),

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { File, FormData } from "undici";
99
import { vi } from "vitest";
1010
import { version as wranglerVersion } from "../../package.json";
1111
import { downloadWorker } from "../init";
12+
import { writeMetricsConfig } from "../metrics/metrics-config";
1213
import { getPackageManager } from "../package-manager";
1314
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
1415
import { mockConsoleMethods } from "./helpers/mock-console";
@@ -149,6 +150,27 @@ describe("init", () => {
149150
);
150151
});
151152
});
153+
154+
test("if telemetry is disabled in wrangler, then disable for c3 too", async () => {
155+
writeMetricsConfig({
156+
permission: {
157+
enabled: false,
158+
date: new Date(2024, 11, 11),
159+
},
160+
});
161+
await runWrangler("init");
162+
163+
expect(execa).toHaveBeenCalledWith(
164+
"mockpm",
165+
["create", "cloudflare@^2.5.0"],
166+
{
167+
env: {
168+
CREATE_CLOUDFLARE_TELEMETRY_DISABLED: "1",
169+
},
170+
stdio: "inherit",
171+
}
172+
);
173+
});
152174
});
153175

154176
describe("deprecated behavior is retained with --no-delegate-c3", () => {

0 commit comments

Comments
 (0)