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): Capture Workers with static assets in the telemetry data #7604

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be a tad misleading, since if options.hasAssets is undefined, we'll default to false, which might not be entirely true. However, the only use case I can see this happening is for code paths that call the old metrics.sendMetricsEvent(). The new metricsDispatcher will handle this correctly

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
Loading