Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wrangler): enable telemetry by default #7291

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/perfect-cougars-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": minor
---

Add anonymous telemetry to Wrangler commands

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.

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.
2 changes: 2 additions & 0 deletions .github/workflows/create-pullrequest-prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ jobs:
run: node .github/prereleases/2-build-pack-upload.mjs
env:
NODE_ENV: "production"
# this is the "test/staging" key for sparrow analytics
SPARROW_SOURCE_KEY: "5adf183f94b3436ba78d67f506965998"
ALGOLIA_APP_ID: ${{ secrets.ALGOLIA_APP_ID }}
ALGOLIA_PUBLIC_KEY: ${{ secrets.ALGOLIA_PUBLIC_KEY }}
SENTRY_DSN: "https://9edbb8417b284aa2bbead9b4c318918b@sentry10.cfdata.org/583"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync } from "fs";
import { spawn } from "cross-spawn";
import { readMetricsConfig } from "helpers/metrics-config";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import whichPMRuns from "which-pm-runs";
import { quoteShellArgs, runCommand } from "../command";
Expand All @@ -13,6 +14,7 @@ let spawnStderr: string | undefined = undefined;
vi.mock("cross-spawn");
vi.mock("fs");
vi.mock("which-pm-runs");
vi.mock("helpers/metrics-config");

describe("Command Helpers", () => {
afterEach(() => {
Expand Down Expand Up @@ -55,6 +57,62 @@ describe("Command Helpers", () => {
});
});

describe("respect telemetry permissions when running wrangler", () => {
test("runCommand has WRANGLER_SEND_METRICS=false if its a wrangler command and c3 telemetry is disabled", async () => {
vi.mocked(readMetricsConfig).mockReturnValue({
c3permission: {
enabled: false,
date: new Date(2000),
},
});
await runCommand(["npx", "wrangler"]);

expect(spawn).toHaveBeenCalledWith(
"npx",
["wrangler"],
expect.objectContaining({
env: expect.objectContaining({ WRANGLER_SEND_METRICS: "false" }),
}),
);
});

test("runCommand doesn't have WRANGLER_SEND_METRICS=false if its a wrangler command and c3 telemetry is enabled", async () => {
vi.mocked(readMetricsConfig).mockReturnValue({
c3permission: {
enabled: true,
date: new Date(2000),
},
});
await runCommand(["npx", "wrangler"]);

expect(spawn).toHaveBeenCalledWith(
"npx",
["wrangler"],
expect.objectContaining({
env: expect.not.objectContaining({ WRANGLER_SEND_METRICS: "false" }),
}),
);
});

test("runCommand doesn't have WRANGLER_SEND_METRICS=false if not a wrangler command", async () => {
vi.mocked(readMetricsConfig).mockReturnValue({
c3permission: {
enabled: false,
date: new Date(2000),
},
});
await runCommand(["ls", "-l"]);

expect(spawn).toHaveBeenCalledWith(
"ls",
["-l"],
expect.objectContaining({
env: expect.not.objectContaining({ WRANGLER_SEND_METRICS: "false" }),
}),
);
});
});

describe("quoteShellArgs", () => {
test.runIf(process.platform !== "win32")("mac", async () => {
expect(quoteShellArgs([`pages:dev`])).toEqual("pages:dev");
Expand Down
10 changes: 10 additions & 0 deletions packages/create-cloudflare/src/helpers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { stripAnsi } from "@cloudflare/cli";
import { CancelError } from "@cloudflare/cli/error";
import { isInteractive, spinner } from "@cloudflare/cli/interactive";
import { spawn } from "cross-spawn";
import { readMetricsConfig } from "./metrics-config";

/**
* Command is a string array, like ['git', 'commit', '-m', '"Initial commit"']
Expand Down Expand Up @@ -52,6 +53,15 @@ export const runCommand = async (
doneText: opts.doneText,
promise() {
const [executable, ...args] = command;
// Don't send metrics data on any wrangler commands if telemetry is disabled in C3
// If telemetry is disabled separately for wrangler, wrangler will handle that
if (args[0] === "wrangler") {
const metrics = readMetricsConfig();
if (metrics.c3permission?.enabled === false) {
opts.env ??= {};
opts.env["WRANGLER_SEND_METRICS"] = "false";
}
}
emily-shen marked this conversation as resolved.
Show resolved Hide resolved
const abortController = new AbortController();
const cmd = spawn(executable, [...args], {
// TODO: ideally inherit stderr, but npm install uses this for warnings
Expand Down
6 changes: 3 additions & 3 deletions packages/wrangler/scripts/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ async function buildMain(flags: BuildFlags = {}) {
__RELATIVE_PACKAGE_PATH__,
"import.meta.url": "import_meta_url",
"process.env.NODE_ENV": `'${process.env.NODE_ENV || "production"}'`,
...(process.env.SPARROW_SOURCE_KEY
? { SPARROW_SOURCE_KEY: `"${process.env.SPARROW_SOURCE_KEY}"` }
: {}),
"process.env.SPARROW_SOURCE_KEY": JSON.stringify(
process.env.SPARROW_SOURCE_KEY ?? ""
),
...(process.env.ALGOLIA_APP_ID
? { ALGOLIA_APP_ID: `"${process.env.ALGOLIA_APP_ID}"` }
: {}),
Expand Down
22 changes: 22 additions & 0 deletions packages/wrangler/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { File, FormData } from "undici";
import { vi } from "vitest";
import { version as wranglerVersion } from "../../package.json";
import { downloadWorker } from "../init";
import { writeMetricsConfig } from "../metrics/metrics-config";
import { getPackageManager } from "../package-manager";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
Expand Down Expand Up @@ -149,6 +150,27 @@ describe("init", () => {
);
});
});

test("if telemetry is disabled in wrangler, then disable for c3 too", async () => {
writeMetricsConfig({
permission: {
enabled: false,
date: new Date(2024, 11, 11),
},
});
await runWrangler("init");

expect(execa).toHaveBeenCalledWith(
"mockpm",
["create", "cloudflare@^2.5.0"],
{
env: {
CREATE_CLOUDFLARE_TELEMETRY_DISABLED: "1",
},
stdio: "inherit",
}
);
});
});

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