Skip to content

Commit

Permalink
feat(wrangler): enable telemetry by default (#7291)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Dec 4, 2024
1 parent 4e571fd commit f5b9cd5
Show file tree
Hide file tree
Showing 43 changed files with 1,406 additions and 529 deletions.
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://[email protected]/583"
Expand Down
58 changes: 58 additions & 0 deletions packages/create-cloudflare/src/helpers/__tests__/command.test.ts
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";
}
}
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

0 comments on commit f5b9cd5

Please sign in to comment.