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: add some more properties to telemetry events #7320

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
238 changes: 218 additions & 20 deletions packages/wrangler/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
readMetricsConfig,
writeMetricsConfig,
} from "../metrics/metrics-config";
import { getMetricsDispatcher } from "../metrics/metrics-dispatcher";
import {
getMetricsDispatcher,
redactArgValues,
} from "../metrics/metrics-dispatcher";
import { mockConsoleMethods } from "./helpers/mock-console";
import { useMockIsTTY } from "./helpers/mock-istty";
import { msw } from "./helpers/msw";
Expand All @@ -23,16 +26,21 @@ vi.mock("../metrics/helpers");
vi.unmock("../metrics/metrics-config");

describe("metrics", () => {
let isCISpy: MockInstance;
const std = mockConsoleMethods();
const { setIsTTY } = useMockIsTTY();
runInTempDir();

beforeEach(async () => {
isCISpy = vi.spyOn(CI, "isCI").mockReturnValue(false);
setIsTTY(true);
vi.stubEnv("SPARROW_SOURCE_KEY", "MOCK_KEY");
logger.loggerLevel = "debug";
});

afterEach(() => {
vi.unstubAllEnvs();
isCISpy.mockClear();
});

describe("getMetricsDispatcher()", () => {
Expand Down Expand Up @@ -192,6 +200,19 @@ describe("metrics", () => {
optional: {
type: "string",
},
default: {
type: "boolean",
default: false,
},
array: {
type: "string",
array: true,
default: ["beep", "boop"],
},
number: {
type: "number",
default: 42,
},
},
positionalArgs: ["positional"],
handler(args, ctx) {
Expand All @@ -209,13 +230,85 @@ describe("metrics", () => {

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,
wranglerVersion: "1.2.3",
isFirstUsage: false,
isCI: false,
isInteractive: true,
argsUsed: [
"array",
"number",
"positional",
"xgradualrollouts",
"xversions",
],
argsCombination:
"array, number, positional, xgradualrollouts, xversions",
command: "wrangler command subcommand",
args: {
"experimental-versions": true,
"x-versions": true,
"experimental-gradual-rollouts": true,
xVersions: true,
experimentalGradualRollouts: true,
experimentalVersions: true,
default: false,
array: ["<REDACTED>", "<REDACTED>"],
number: 42,
positional: "<REDACTED>",
},
},
};
expect(std.debug).toContain(
`Metrics dispatcher: Posting data {"deviceId":"f82b1f46-eb7b-4154-aa9f-ce95f23b2288","event":"wrangler command started","timestamp":1733961600000,"properties":{"amplitude_session_id":1733961600000,"amplitude_event_id":0,"wranglerVersion":"1.2.3","isFirstUsage":false,"command":"wrangler command subcommand","args":{"_":["command","subcommand"],"experimental-versions":true,"x-versions":true,"experimental-gradual-rollouts":true,"xVersions":true,"experimentalGradualRollouts":true,"experimentalVersions":true,"$0":"wrangler","positional":"positional"}}}`
`Posting data ${JSON.stringify(expectedStartReq)}`
);
const expectedCompleteReq = {
deviceId: "f82b1f46-eb7b-4154-aa9f-ce95f23b2288",
event: "wrangler command completed",
timestamp: 1733961600000,
properties: {
amplitude_session_id: 1733961600000,
amplitude_event_id: 1,
wranglerVersion: "1.2.3",
isFirstUsage: false,
isCI: false,
isInteractive: true,
argsUsed: [
"array",
"number",
"positional",
"xgradualrollouts",
"xversions",
],
argsCombination:
"array, number, positional, xgradualrollouts, xversions",
command: "wrangler command subcommand",
args: {
"experimental-versions": true,
"x-versions": true,
"experimental-gradual-rollouts": true,
xVersions: true,
experimentalGradualRollouts: true,
experimentalVersions: true,
default: false,
array: ["<REDACTED>", "<REDACTED>"],
number: 42,
positional: "<REDACTED>",
},
durationMs: 0,
durationSeconds: 0,
durationMinutes: 0,
},
};
// command completed
expect(std.debug).toContain(
`Metrics dispatcher: Posting data {"deviceId":"f82b1f46-eb7b-4154-aa9f-ce95f23b2288","event":"wrangler command completed","timestamp":1733961600000,"properties":{"amplitude_session_id":1733961600000,"amplitude_event_id":1,"wranglerVersion":"1.2.3","isFirstUsage":false,"command":"wrangler command subcommand","args":{"_":["command","subcommand"],"experimental-versions":true,"x-versions":true,"experimental-gradual-rollouts":true,"xVersions":true,"experimentalGradualRollouts":true,"experimentalVersions":true,"$0":"wrangler","positional":"positional"},"durationMs":0,"durationSeconds":0,"durationMinutes":0}}`
`Posting data ${JSON.stringify(expectedCompleteReq)}`
);
expect(std.out).toMatchInlineSnapshot(`
"
Expand All @@ -235,16 +328,110 @@ describe("metrics", () => {

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,
wranglerVersion: "1.2.3",
isFirstUsage: false,
isCI: false,
isInteractive: true,
argsUsed: [
"array",
"number",
"positional",
"xgradualrollouts",
"xversions",
],
argsCombination:
"array, number, positional, xgradualrollouts, xversions",
command: "wrangler command subcommand",
args: {
"experimental-versions": true,
"x-versions": true,
"experimental-gradual-rollouts": true,
xVersions: true,
experimentalGradualRollouts: true,
experimentalVersions: true,
default: false,
array: ["<REDACTED>", "<REDACTED>"],
number: 42,
positional: "<REDACTED>",
},
},
};
expect(std.debug).toContain(
`Metrics dispatcher: Posting data {"deviceId":"f82b1f46-eb7b-4154-aa9f-ce95f23b2288","event":"wrangler command started","timestamp":1733961600000,"properties":{"amplitude_session_id":1733961600000,"amplitude_event_id":0,"wranglerVersion":"1.2.3","isFirstUsage":false,"command":"wrangler command subcommand","args":{"_":["command","subcommand"],"experimental-versions":true,"x-versions":true,"experimental-gradual-rollouts":true,"xVersions":true,"experimentalGradualRollouts":true,"experimentalVersions":true,"$0":"wrangler","positional":"error"}}}`
`Posting data ${JSON.stringify(expectedStartReq)}`
);
// command completed

const expectedErrorReq = {
deviceId: "f82b1f46-eb7b-4154-aa9f-ce95f23b2288",
event: "wrangler command errored",
timestamp: 1733961600000,
properties: {
amplitude_session_id: 1733961600000,
amplitude_event_id: 1,
wranglerVersion: "1.2.3",
isFirstUsage: false,
isCI: false,
isInteractive: true,
argsUsed: [
"array",
"number",
"positional",
"xgradualrollouts",
"xversions",
],
argsCombination:
"array, number, positional, xgradualrollouts, xversions",
command: "wrangler command subcommand",
args: {
"experimental-versions": true,
"x-versions": true,
"experimental-gradual-rollouts": true,
xVersions: true,
experimentalGradualRollouts: true,
experimentalVersions: true,
default: false,
array: ["<REDACTED>", "<REDACTED>"],
number: 42,
positional: "<REDACTED>",
},
durationMs: 0,
durationSeconds: 0,
durationMinutes: 0,
errorType: "UserError",
},
};

expect(std.debug).toContain(
`Metrics dispatcher: Posting data {"deviceId":"f82b1f46-eb7b-4154-aa9f-ce95f23b2288","event":"wrangler command errored","timestamp":1733961600000,"properties":{"amplitude_session_id":1733961600000,"amplitude_event_id":1,"wranglerVersion":"1.2.3","isFirstUsage":false,"command":"wrangler command subcommand","args":{"_":["command","subcommand"],"experimental-versions":true,"x-versions":true,"experimental-gradual-rollouts":true,"xVersions":true,"experimentalGradualRollouts":true,"experimentalVersions":true,"$0":"wrangler","positional":"error"},"durationMs":0,"durationSeconds":0,"durationMinutes":0,"errorType":"UserError"}}`
`Posting data ${JSON.stringify(expectedErrorReq)}`
);
});

it("should mark isCI as true if running in CI", async () => {
isCISpy.mockReturnValue(true);
const requests = mockMetricRequest({}, {});

await runWrangler("command subcommand positional");

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

it("should mark as non-interactive if running in non-interactive environment", async () => {
setIsTTY(false);
const requests = mockMetricRequest({}, {});

await runWrangler("command subcommand positional");

expect(requests.count).toBe(2);
expect(std.debug).toContain('"isInteractive":false,');
});

describe("banner", () => {
beforeEach(() => {
vi.mocked(getWranglerVersion).mockReturnValue("1.2.3");
Expand Down Expand Up @@ -320,15 +507,33 @@ describe("metrics", () => {
});
});
});

describe("redactArgValues()", () => {
it("should redact sensitive values", () => {
const args = {
default: false,
array: ["beep", "boop"],
secretArray: ["beep", "boop"],
number: 42,
string: "secret",
secretString: "secret",
};

const redacted = redactArgValues(args, ["string", "array"]);
expect(redacted).toMatchObject({
default: false,
array: ["beep", "boop"],
secretArray: ["<REDACTED>", "<REDACTED>"],
number: 42,
string: "secret",
secretString: "<REDACTED>",
});
});
});
});

describe("getMetricsConfig()", () => {
let isCISpy: MockInstance;

const { setIsTTY } = useMockIsTTY();
beforeEach(() => {
// Default the mock TTY to interactive for all these tests.
setIsTTY(true);
isCISpy = vi.spyOn(CI, "isCI").mockReturnValue(false);
});

Expand All @@ -344,13 +549,6 @@ describe("metrics", () => {
});
});

it("should return false if running in a CI environment", async () => {
isCISpy.mockReturnValue(true);
expect(await getMetricsConfig({})).toMatchObject({
enabled: false,
});
});

it("should return the sendMetrics argument for enabled if it is defined", async () => {
expect(await getMetricsConfig({ sendMetrics: false })).toMatchObject({
enabled: false,
Expand Down
7 changes: 0 additions & 7 deletions packages/wrangler/src/metrics/metrics-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { getWranglerSendMetricsFromEnv } from "../environment-variables/misc-variables";
import { getGlobalWranglerConfigPath } from "../global-wrangler-config-path";
import { isNonInteractiveOrCI } from "../is-interactive";
import { logger } from "../logger";

/**
Expand Down Expand Up @@ -86,12 +85,6 @@ export function getMetricsConfig({
}
}

// We couldn't get the metrics permission from the project-level nor the user-level config.
// If we are not interactive or in a CI build then just bail out.
if (isNonInteractiveOrCI()) {
return { enabled: false, deviceId };
}

// Otherwise, default to true
writeMetricsConfig({
...config,
Expand Down
Loading
Loading