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

Add additional metrics #7549

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions .changeset/selfish-yaks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": patch
---

Expand metrics collection to:

- Detect Pages & Workers CI
- Filter out default args (e.g. `--x-versions`, `--x-dev-env`, and `--latest`) by only including args that were in `argv`
42 changes: 34 additions & 8 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,11 @@ describe("metrics", () => {
isFirstUsage: false,
configFileType: "toml",
isCI: false,
isPagesCI: false,
isWorkersCI: false,
isInteractive: true,
argsUsed: [
"j",
"search",
"xGradualRollouts",
"xJsonConfig",
"xVersions",
],
argsCombination: "j, search, xGradualRollouts, xJsonConfig, xVersions",
argsUsed: [],
argsCombination: "",
command: "wrangler docs",
args: {
xJsonConfig: true,
Expand Down Expand Up @@ -343,6 +339,36 @@ describe("metrics", () => {
expect(std.debug).toContain('isCI":true');
});

it("should mark isPagesCI as true if running in Pages CI", async () => {
vi.stubEnv("CF_PAGES", "1");
const requests = mockMetricRequest();

await runWrangler("docs arg");

expect(requests.count).toBe(2);
expect(std.debug).toContain('isPagesCI":true');
});

it("should mark isWorkersCI as true if running in Workers CI", async () => {
vi.stubEnv("WORKERS_CI", "1");
const requests = mockMetricRequest();

await runWrangler("docs arg");

expect(requests.count).toBe(2);
expect(std.debug).toContain('isWorkersCI":true');
});

it("should include args provided by the user", async () => {
const requests = mockMetricRequest();

await runWrangler("docs arg --search 'some search term'");

expect(requests.count).toBe(2);
// Notably, this _doesn't_ include default args (e.g. --x-versions)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: expect(std.debug).not.toContain(...) ?

expect(std.debug).toContain('argsCombination":"search"');
});

it("should mark as non-interactive if running in non-interactive environment", async () => {
setIsTTY(false);
const requests = mockMetricRequest();
Expand Down
52 changes: 32 additions & 20 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1153,10 +1153,14 @@ export async function main(argv: string[]): Promise<void> {
addBreadcrumb(command);
// NB despite 'applyBeforeValidation = true', this runs *after* yargs 'validates' options,
// e.g. if a required arg is missing, yargs will error out before we send any events :/
dispatcher?.sendCommandEvent("wrangler command started", {
command,
args,
});
dispatcher?.sendCommandEvent(
"wrangler command started",
{
command,
args,
},
argv
);
}, /* applyBeforeValidation */ true);

let cliHandlerThrew = false;
Expand All @@ -1165,13 +1169,17 @@ export async function main(argv: string[]): Promise<void> {

const durationMs = Date.now() - startTime;

dispatcher?.sendCommandEvent("wrangler command completed", {
command,
args: metricsArgs,
durationMs,
durationSeconds: durationMs / 1000,
durationMinutes: durationMs / 1000 / 60,
});
dispatcher?.sendCommandEvent(
"wrangler command completed",
{
command,
args: metricsArgs,
durationMs,
durationSeconds: durationMs / 1000,
durationMinutes: durationMs / 1000 / 60,
},
argv
);
} catch (e) {
cliHandlerThrew = true;
let mayReport = true;
Expand Down Expand Up @@ -1281,15 +1289,19 @@ export async function main(argv: string[]): Promise<void> {

const durationMs = Date.now() - startTime;

dispatcher?.sendCommandEvent("wrangler command errored", {
command,
args: metricsArgs,
durationMs,
durationSeconds: durationMs / 1000,
durationMinutes: durationMs / 1000 / 60,
errorType:
errorType ?? (e instanceof Error ? e.constructor.name : undefined),
});
dispatcher?.sendCommandEvent(
"wrangler command errored",
{
command,
args: metricsArgs,
durationMs,
durationSeconds: durationMs / 1000,
durationMinutes: durationMs / 1000 / 60,
errorType:
errorType ?? (e instanceof Error ? e.constructor.name : undefined),
},
argv
);

throw e;
} finally {
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/is-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import isCI from "is-ci";
export const CI = {
/** Is Wrangler currently running in a CI? */
isCI() {
return isCI;
return (
isCI || process.env.CF_PAGES === "1" || process.env.WORKERS_CI === "1"
penalosa marked this conversation as resolved.
Show resolved Hide resolved
);
},
};
20 changes: 16 additions & 4 deletions packages/wrangler/src/metrics/metrics-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
properties: Omit<
Extract<Events, { name: EventName }>["properties"],
keyof CommonEventProperties
>
>,
argv?: string[]
) {
try {
if (
Expand All @@ -91,7 +92,7 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
printMetricsBanner();
}

const argsUsed = sanitiseUserInput(properties.args ?? {});
const argsUsed = sanitiseUserInput(properties.args ?? {}, argv);
const argsCombination = argsUsed.sort().join(", ");
const commonEventProperties: CommonEventProperties = {
amplitude_session_id,
Expand All @@ -104,6 +105,8 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
isFirstUsage: readMetricsConfig().permission === undefined,
configFileType: configFormat(options.configPath),
isCI: CI.isCI(),
isPagesCI: process.env.CF_PAGES === "1",
penalosa marked this conversation as resolved.
Show resolved Hide resolved
isWorkersCI: process.env.WORKERS_CI === "1",
isInteractive: isInteractive(),
argsUsed,
argsCombination,
Expand Down Expand Up @@ -213,11 +216,20 @@ const normalise = (arg: string) => {
};

const exclude = new Set(["$0", "_"]);
/** just some pretty naive cleaning so we don't send "experimental-versions", "experimentalVersions", "x-versions" and "xVersions" etc. */
const sanitiseUserInput = (argsWithValues: Record<string, unknown>) => {
/**
* just some pretty naive cleaning so we don't send duplicates of "experimental-versions", "experimentalVersions", "x-versions" and "xVersions" etc.
* optionally, if an argv is provided remove all args that were not specified in argv (which means that default values will be filtered out)
*/
const sanitiseUserInput = (
argsWithValues: Record<string, unknown>,
argv?: string[]
) => {
const result: string[] = [];
const args = Object.keys(argsWithValues);
for (const arg of args) {
if (Array.isArray(argv) && !argv.some((a) => a.includes(arg))) {
continue;
}
if (exclude.has(arg)) {
continue;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export type CommonEventProperties = {
* Whether the Wrangler client is running in CI
*/
isCI: boolean;
/**
* Whether the Wrangler client is running in Pages CI
*/
isPagesCI: boolean;
/**
* Whether the Wrangler client is running in Workers CI
*/
isWorkersCI: boolean;
/**
* Whether the Wrangler client is running in an interactive instance
*/
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"EXPERIMENTAL_MIDDLEWARE",
"FORMAT_WRANGLER_ERRORS",
"CF_PAGES",
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish we could rename this to PAGES_CI

"WORKERS_CI",
"CI",
"CF_PAGES_UPLOAD_JWT",
"EXPERIMENTAL_MIDDLEWARE",
Expand Down
Loading