Skip to content

Commit

Permalink
feat(wrangler): Capture Workers with static assets in the telemetry d…
Browse files Browse the repository at this point in the history
…ata (#7604)

* feat(wrangler): Capture Workers with static assets in the telemetry data

* add changeset
  • Loading branch information
CarmenPopoviciu authored Dec 20, 2024
1 parent e4c79bb commit 6c2f173
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .changeset/long-houses-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": minor
---

feat: Capture Workers with static assets in the telemetry data

We want to measure accurately what this number of Workers + Assets projects running in remote mode is, as this number will be a very helpful data point down the road, when more decisions around remote mode will have to be taken.

These changes add this kind of insight to our telemetry data, by capturing whether the command running is in the context of a Workers + Assets project.

N.B. With these changes in place we will be capturing the Workers + Assets context for all commands, not just wrangler dev --remote.
54 changes: 54 additions & 0 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from "node:fs";
import { http, HttpResponse } from "msw";
import { vi } from "vitest";
import { CI } from "../is-ci";
Expand Down Expand Up @@ -189,6 +190,7 @@ describe("metrics", () => {
isPagesCI: false,
isWorkersCI: false,
isInteractive: true,
hasAssets: false,
argsUsed: [],
argsCombination: "",
command: "wrangler docs",
Expand Down Expand Up @@ -359,6 +361,58 @@ describe("metrics", () => {
expect(std.debug).toContain('isWorkersCI":true');
});

it("should capture Workers + Assets projects", async () => {
writeWranglerConfig({ assets: { directory: "./public" } });

// set up empty static assets directory
fs.mkdirSync("./public");

const requests = mockMetricRequest();

await runWrangler("docs arg");
expect(requests.count).toBe(2);

// command started
const expectedStartReq = {
deviceId: "f82b1f46-eb7b-4154-aa9f-ce95f23b2288",
event: "wrangler command started",
timestamp: 1733961600000,
properties: {
amplitude_session_id: 1733961600000,
amplitude_event_id: 0,
...{ ...reused, hasAssets: true },
},
};
expect(std.debug).toContain(
`Posting data ${JSON.stringify(expectedStartReq)}`
);

// command completed
const expectedCompleteReq = {
deviceId: "f82b1f46-eb7b-4154-aa9f-ce95f23b2288",
event: "wrangler command completed",
timestamp: 1733961606000,
properties: {
amplitude_session_id: 1733961600000,
amplitude_event_id: 1,
...{ ...reused, hasAssets: true },
durationMs: 6000,
durationSeconds: 6,
durationMinutes: 0.1,
},
};
expect(std.debug).toContain(
`Posting data ${JSON.stringify(expectedCompleteReq)}`
);
expect(std.out).toMatchInlineSnapshot(`
"
Cloudflare collects anonymous telemetry about your usage of Wrangler. Learn more at https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler/telemetry.md
Opening a link in your default browser: FAKE_DOCS_URL:{\\"params\\":\\"query=arg&hitsPerPage=1&getRankingInfo=0\\"}"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should not send arguments with wrangler login", async () => {
const requests = mockMetricRequest();

Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ export async function main(argv: string[]): Promise<void> {
const { rawConfig, configPath } = experimental_readRawConfig(args);
dispatcher = getMetricsDispatcher({
sendMetrics: rawConfig.send_metrics,
hasAssets: !!rawConfig.assets?.directory,
configPath,
});
} catch (e) {
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/metrics/metrics-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export interface MetricsConfigOptions {
* Otherwise, infer the enabled value from the user configuration.
*/
sendMetrics?: boolean;
/**
* Captures whether this is a Worker with static assets
*/
hasAssets?: boolean;
/**
* Path to wrangler configuration file, if it exists. Used for configFileType property
*/
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/metrics/metrics-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
isPagesCI: isPagesCI(),
isWorkersCI: isWorkersCI(),
isInteractive: isInteractive(),
hasAssets: options.hasAssets ?? false,
argsUsed,
argsCombination,
};
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export type CommonEventProperties = {
* Whether the Wrangler client is running in an interactive instance
*/
isInteractive: boolean;
/**
* Whether this is a Worker with static assets
*/
hasAssets: boolean;
/**
* A list of normalised argument names/flags that were passed in or are set by default.
* Excludes boolean flags set to false.
Expand Down

0 comments on commit 6c2f173

Please sign in to comment.