Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Dec 2, 2024
1 parent f2b0b40 commit e345fff
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 71 deletions.
1 change: 0 additions & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"selfsigned": "^2.0.1",
"source-map": "^0.6.1",
"unenv": "npm:unenv-nightly@2.0.0-20241121-161142-806b5c0",
"which-pm-runs": "^1.1.0",
"workerd": "1.20241106.1",
"xxhash-wasm": "^1.0.1"
},
Expand Down
20 changes: 4 additions & 16 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("metrics", () => {
vi.mocked(getOS).mockReturnValue("foo:bar");
vi.mocked(getWranglerVersion).mockReturnValue("1.2.3");
vi.mocked(getOSVersion).mockReturnValue("mock os version");
vi.mocked(getNodeVersion).mockReturnValue("1.1.1");
vi.mocked(getNodeVersion).mockReturnValue(1);
vi.mocked(getPlatform).mockReturnValue("mock platform");
vi.mocked(getConfigFileType).mockReturnValue("toml");
vi.useFakeTimers({
Expand All @@ -83,19 +83,7 @@ describe("metrics", () => {
describe("sendEvent()", () => {
it("should send a request to the default URL", async () => {
const requests = mockMetricRequest();
// {
// event: "some-event",
// properties: {
// category: "Workers",
// wranglerVersion: "1.2.3",
// os: "foo:bar",
// a: 1,
// b: 2,
// },
// },
// {
// "Sparrow-Source-Key": "MOCK_KEY",
// }

const dispatcher = await getMetricsDispatcher({
sendMetrics: true,
});
Expand Down Expand Up @@ -192,7 +180,7 @@ describe("metrics", () => {
wranglerVersion: "1.2.3",
osPlatform: "mock platform",
osVersion: "mock os version",
nodeVersion: "1.1.1",
nodeVersion: 1,
isFirstUsage: false,
configFileType: "toml",
isCI: false,
Expand Down Expand Up @@ -293,7 +281,7 @@ describe("metrics", () => {
msw.use(
http.post<Record<string, never>, { params: string | undefined }>(
`*/1/indexes/developers-cloudflare2/query`,
async ({}) => {
async () => {
vi.advanceTimersByTime(6000);
return HttpResponse.error();
},
Expand Down
9 changes: 3 additions & 6 deletions packages/wrangler/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type {
RawEnvironment,
} from "./environment";

function configFormat(
export function configFormat(
configPath: string | undefined
): "jsonc" | "toml" | "none" {
if (configPath?.endsWith("toml")) {
Expand Down Expand Up @@ -91,9 +91,8 @@ export function readConfig(
requirePagesConfig?: boolean,
hideWarnings: boolean = false
): Config {
const { rawConfig, configPath: updateConfigPath } = parseConfig(
const { rawConfig, configPath: updateConfigPath } = readRawConfig(
configPath,
args,
requirePagesConfig
);

Expand Down Expand Up @@ -153,10 +152,8 @@ export function readConfig(
return config;
}

export const parseConfig = (
export const readRawConfig = (
configPath: string | undefined,
// Include command specific args as well as the wrangler global flags
args: ReadConfigCommandArgs,
requirePagesConfig?: boolean
) => {
let rawConfig: RawConfig = {};
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
configFileName,
formatConfigSnippet,
loadDotEnv,
parseConfig,
readRawConfig,
} from "./config";
import { demandSingleValue } from "./core";
import { CommandRegistry } from "./core/CommandRegistry";
Expand Down Expand Up @@ -1064,7 +1064,7 @@ export async function main(argv: string[]): Promise<void> {
// key to fetch) or flags

try {
const { rawConfig, configPath } = parseConfig(args.config, args, false);
const { rawConfig, configPath } = readRawConfig(args.config, false);
dispatcher = getMetricsDispatcher({
sendMetrics: rawConfig.send_metrics,
configPath,
Expand Down
29 changes: 11 additions & 18 deletions packages/wrangler/src/metrics/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import os from "node:os";
import whichPmRuns from "which-pm-runs";
import { version as wranglerVersion } from "../../package.json";
import { configFormat } from "../config";
import {
getPackageManager as _getPackageManager,
getPackageManagerName,
} from "../package-manager";

export function getWranglerVersion() {
return wranglerVersion;
}

export function getPackageManager() {
return whichPmRuns()?.name;
export async function getPackageManager() {
const packageManager = await _getPackageManager(process.cwd());
return getPackageManagerName(packageManager);
}

// used by "new" metrics
Expand Down Expand Up @@ -36,22 +41,10 @@ export function getOSVersion() {
}

export function getNodeVersion() {
return process.version;
const nodeVersion = process.versions.node;
return parseInt(nodeVersion.split(".")[0]);
}

export function getConfigFileType(configPath: string | undefined) {
if (configPath === undefined) {
return "none";
}
if (configPath.endsWith(".toml")) {
return "toml";
}
if (configPath.endsWith(".json")) {
return "json";
}
if (configPath.endsWith(".jsonc")) {
return "jsonc";
}
/** shouldn't ever get here */
return "invalid";
return configFormat(configPath);
}
39 changes: 16 additions & 23 deletions packages/wrangler/src/metrics/metrics-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from "chalk";
import { fetch } from "undici";
import _isInteractive from "../is-interactive";
import isInteractive from "../is-interactive";
import { logger } from "../logger";
import { CI } from "./../is-ci";
import {
Expand Down Expand Up @@ -28,16 +28,9 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
const SPARROW_SOURCE_KEY = process.env.SPARROW_SOURCE_KEY ?? "";
const requests: Array<Promise<void>> = [];
const wranglerVersion = getWranglerVersion();
const osPlatform = getPlatform();
const osVersion = getOSVersion();
const nodeVersion = getNodeVersion();
const packageManager = getPackageManager();
const isFirstUsage = readMetricsConfig().permission === undefined;
const isCI = CI.isCI();
const isInteractive = _isInteractive();
const amplitude_session_id = Date.now();
const configFileType = getConfigFileType(options.configPath);
let amplitude_event_id = 0;

/** We redact strings in arg values, unless they are named here */
const allowList = {
// applies to all commands
Expand Down Expand Up @@ -95,20 +88,20 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
amplitude_session_id,
amplitude_event_id: amplitude_event_id++,
wranglerVersion,
osPlatform,
osVersion,
nodeVersion,
packageManager,
isFirstUsage,
configFileType,
isCI,
isInteractive,
osPlatform: getPlatform(),
osVersion: getOSVersion(),
nodeVersion: getNodeVersion(),
packageManager: await getPackageManager(),
isFirstUsage: readMetricsConfig().permission === undefined,
configFileType: getConfigFileType(options.configPath),
isCI: CI.isCI(),
isInteractive: isInteractive(),
argsUsed,
argsCombination,
};
// we redact all args unless they are in the allowList
const allowedKeys = getAllowedKeys(allowList, properties.command ?? "");
properties.args = redactArgValues(properties.args ?? {}, allowedKeys);
// get the args where we don't want to redact their values
const allowedArgs = getAllowedArgs(allowList, properties.command ?? "");
properties.args = redactArgValues(properties.args ?? {}, allowedArgs);
await dispatch({
name,
properties: {
Expand Down Expand Up @@ -154,8 +147,8 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {

logger.debug(`Metrics dispatcher: Posting data ${JSON.stringify(body)}`);

// Do not await this fetch call.
// Just fire-and-forget, otherwise we might slow down the rest of Wrangler.
// Don't await fetch but make sure requests are resolved (with a timeout)
// before exiting Wrangler
const request = fetch(`${SPARROW_URL}/api/v1/event`, {
method: "POST",
headers: {
Expand Down Expand Up @@ -235,7 +228,7 @@ const sanitiseUserInput = (argsWithValues: Record<string, unknown>) => {
return result;
};

const getAllowedKeys = (
const getAllowedArgs = (
allowList: Record<string, string[]> & { "*": string[] },
key: string
) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export type CommonEventProperties = {
*/
packageManager: string | undefined;
/**
* The version of node that the Wrangler client is running on.
* The major version of node that the Wrangler client is running on.
*/
nodeVersion: string;
nodeVersion: number;
/**
* Whether this is the first time the user has used the wrangler client.
*/
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e345fff

Please sign in to comment.