Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/wet-starfishes-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

Include default values for wrangler types --path and --x-include-runtime in telemetry

User provided strings are still left redacted as always.
15 changes: 12 additions & 3 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,22 +582,31 @@ describe("metrics", () => {
const args = {
default: false,
array: ["beep", "boop"],
secretArray: ["beep", "boop"],
// Note how
// Note how this is normalised
"secret-array": ["beep", "boop"],
number: 42,
string: "secret",
secretString: "secret",
flagOne: "default",
// Note how this is normalised
experimentalIncludeRuntime: "",
};

const redacted = redactArgValues(args, ["string", "array"]);
const redacted = redactArgValues(args, {
string: "*",
array: "*",
flagOne: ["default"],
xIncludeRuntime: [".wrangler/types/runtime.d.ts"],
});
expect(redacted).toEqual({
default: false,
array: ["beep", "boop"],
secretArray: ["<REDACTED>", "<REDACTED>"],
number: 42,
string: "secret",
secretString: "<REDACTED>",
flagOne: "default",
xIncludeRuntime: ".wrangler/types/runtime.d.ts",
});
});
});
Expand Down
39 changes: 27 additions & 12 deletions packages/wrangler/src/metrics/metrics-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
let amplitude_event_id = 0;

/** We redact strings in arg values, unless they are named here */
const allowList = {
const allowList: Record<string, AllowedValues> & { "*": AllowedValues } = {
// applies to all commands
// use camelCase version
"*": ["format", "logLevel"],
// specific commands
tail: ["status"],
"*": { format: "*", logLevel: "*" },
"wrangler tail": { status: "*" },
"wrangler types": {
xIncludeRuntime: [".wrangler/types/runtime.d.ts"],
path: ["worker-configuration.d.ts"],
},
};

return {
Expand Down Expand Up @@ -253,35 +256,47 @@ const sanitiseUserInput = (
return result;
};

type AllowedValues = Record<string, string[] | "*">;
const getAllowedArgs = (
allowList: Record<string, string[]> & { "*": string[] },
allowList: Record<string, AllowedValues> & { "*": AllowedValues },
key: string
) => {
const commandSpecific = allowList[key] ?? [];
return [...commandSpecific, ...allowList["*"]];
return { ...commandSpecific, ...allowList["*"] };
};
export const redactArgValues = (
args: Record<string, unknown>,
allowedKeys: string[]
allowedValues: AllowedValues
) => {
const result: Record<string, unknown> = {};

for (const [k, value] of Object.entries(args)) {
const key = normalise(k);
for (let [key, value] of Object.entries(args)) {
key = normalise(key);
// the default is not set by yargs :/
if (key === "xIncludeRuntime" && value === "") {
value = ".wrangler/types/runtime.d.ts";
}
const allowedValuesForArg = allowedValues[key] ?? [];
if (exclude.has(key)) {
continue;
}
if (
typeof value === "number" ||
typeof value === "boolean" ||
allowedKeys.includes(normalise(key))
allowedValuesForArg.includes(normalise(key))
) {
result[key] = value;
} else if (typeof value === "string") {
} else if (
typeof value === "string" &&
!(allowedValuesForArg === "*" || allowedValuesForArg.includes(value))
) {
result[key] = "<REDACTED>";
} else if (Array.isArray(value)) {
result[key] = value.map((v) =>
typeof v === "string" ? "<REDACTED>" : v
typeof v === "string" &&
!(allowedValuesForArg === "*" || allowedValuesForArg.includes(v))
? "<REDACTED>"
: v
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add some comments explaining this logic? It's gotten pretty hairy—but be nice if there was a way to simplify

Copy link
Contributor Author

@emily-shen emily-shen Feb 13, 2025

Choose a reason for hiding this comment

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

yeah i'd like to tidy all of this up, because the sanitise + redact + normalise functions have gotten very redundant and messy. Would you be okay with leaving it for now so I can start collecting metrics, and doing that in a fast follow this afternooon?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

depends how long until the release really 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good--let's get this in as-is for the current release

);
} else {
result[key] = value;
Expand Down
Loading