Skip to content
Closed
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
2 changes: 1 addition & 1 deletion TELEMETRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ A telemetry request has the following structure:
|---|---|---|
| `client` | Always | Identifies the event as coming from the Chargebee CLI. |
| `visitor_id` | Always | Random install ID generated by the CLI. It is not derived from your account, user, or machine. |
| `site_name` | Always | Site name of the active Chargebee profile, or `unconfigured`. |
| `site_name` | Always | Site name of the active Chargebee profile, or `unconfigured`. For `auth add` and `auth switch`, the site that is active once the command succeeds. |
| `cli_version` | Always | Installed CLI version. |
| `name` | Always | Command path, such as `customer list`. Arguments and values are not included. |
| `timestamp` | Always | Time the event was recorded. |
Expand Down
2 changes: 2 additions & 0 deletions src/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { setCommandGroup } from "./help.js";
import { sectionTitle } from "../lib/help-style.js";
import { switchProfileAction } from "../lib/config/switch-profile.js";
import { EXIT_CODES } from "../lib/exit-codes.js";
import { noteActiveSite } from "../lib/telemetry/active-site.js";

const DEFAULT_PROFILE = "default";

Expand Down Expand Up @@ -192,6 +193,7 @@ export function registerAuthCommand(program: Command): void {
...(savedRegion === DEFAULT_REGION ? {} : { region: savedRegion }),
});
await writeConfig({ domain: site, activeProfile: profileName });
noteActiveSite(site);
jsonResult({ configured: true, profile: profileName, site, region: savedRegion,
host: persistableHost(apiHost), product_catalog_version: catalog.productCatalogVersion ?? null,
chargebee_response_schema_type: catalog.responseSchemaType ?? null, key_storage: saved.backend });
Expand Down
2 changes: 2 additions & 0 deletions src/lib/config/switch-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { humanLog, diagnostic, exitCommand, jsonResult } from "../output.js";
import { assertValidProfileName, listProfiles, loadProfile } from "./profiles.js";
import { readConfig, writeConfig } from "./store.js";
import { siteMode } from "../api/write-gate.js";
import { noteActiveSite } from "../telemetry/active-site.js";

/** `auth switch`. `domain` is still written as `CHARGEBEE_DOMAIN`. */
export async function switchProfileAction(name?: string): Promise<void> {
Expand Down Expand Up @@ -66,6 +67,7 @@ export async function switchProfileAction(name?: string): Promise<void> {
domain: p.site,
activeProfile: chosen,
});
noteActiveSite(p.site);

const mode = siteMode(p.site, p.api_key);
jsonResult({ active_profile: chosen, site: p.site, mode });
Expand Down
23 changes: 23 additions & 0 deletions src/lib/telemetry/active-site.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Shared, dependency-free slot for a site the current invocation made active
* (`auth add`, `auth switch`).
*
* Lives in its own module (importing nothing) so commands can set it without
* importing the recorder (`telemetry/index.ts`, which imports `sdk.ts`). The
* recorder reads the active site before the command runs, so without this the
* event would carry the previous site (or `unconfigured`). It reads this slot at
* finalize time and prefers it.
*/
let _site: string | undefined;

/** Record the site this invocation made active. Call only after it is saved. */
export function noteActiveSite(site: string): void {
_site = site;
}

/** Read and clear the noted site. */
export function takeActiveSite(): string | undefined {
const v = _site;
_site = undefined;
return v;
}
4 changes: 3 additions & 1 deletion src/lib/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./constants.js";
import { buildMetadata, normalizeGeneratedResource } from "./metadata.js";
import { recordTelemetryError, takeTelemetryError } from "./error.js";
import { takeActiveSite } from "./active-site.js";
import { wasInteractive } from "./interactive.js";
import { isTelemetryDisabled, maybeShowFirstRunNotice } from "./optout.js";
import { getVisitorId } from "./identity.js";
Expand Down Expand Up @@ -57,6 +58,7 @@ export function __resetTelemetryRecorderForTest(): void {
finalized = false;
listenLifecycleEmitted = false;
_version = "0.0.0";
takeActiveSite();
if (exitHandler) {
process.removeListener("exit", exitHandler);
exitHandler = null;
Expand Down Expand Up @@ -291,7 +293,7 @@ function finalize(exitCode: number): void {

const record: SpoolRecord = {
env: pending.env,
site_name: pending.siteName,
site_name: takeActiveSite() ?? pending.siteName,
visitor_id: pending.visitorId,
cli_version: _version,
event: { name: pending.command, timestamp: new Date().toISOString(), metadata },
Expand Down
11 changes: 9 additions & 2 deletions src/lib/test-support/_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Command } from "commander";

import { buildProgram } from "../../program.js";
import { __setClientFactory, __resetRuntimeState } from "../api/sdk.js";

Expand Down Expand Up @@ -130,9 +132,13 @@ export function createEnvPatcher(): {
/**
* Run the CLI in-process with the given args. Captures stdout/stderr (both
* `console.*` and direct `process.std*.write`) and the exit code, restoring all
* globals afterwards.
* globals afterwards. `configure` runs on the built program before parsing
* (e.g. to install the telemetry recorder, which `buildProgram` leaves out).
*/
export async function runCli(args: string[]): Promise<RunResult> {
export async function runCli(
args: string[],
opts: { configure?: (program: Command) => void } = {},
): Promise<RunResult> {
__resetRuntimeState();

let stdout = "";
Expand Down Expand Up @@ -173,6 +179,7 @@ export async function runCli(args: string[]): Promise<RunResult> {
let exitCode = 0;
try {
const program = buildProgram("0.0.0-test");
opts.configure?.(program);
await program.parseAsync(args, { from: "user" });
exitCode = Number(process.exitCode ?? 0);
} catch (err) {
Expand Down
130 changes: 130 additions & 0 deletions src/tests/integration/telemetry-site-label.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* The telemetry event for a command that changes the active site (`auth add`,
* `auth switch`) must carry the site that is active after it succeeds, not the
* one that was active before it ran.
*/
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { spoolPath } from "../../lib/telemetry/constants.js";
import {
__finalizeForTest,
__resetTelemetryRecorderForTest,
__setSpawnForTest,
installTelemetry,
} from "../../lib/telemetry/index.js";
import { writeState } from "../../lib/telemetry/state.js";
import type { SpoolRecord } from "../../lib/telemetry/types.js";
import {
clearCiEnv,
createEnvPatcher,
installFakeClient,
runCli,
setStdinIsTTY,
uninstallFakeClient,
type ClientConstruction,
} from "../../lib/test-support/_helpers.js";

const env = createEnvPatcher();
let configDir: string;
let restoreCiEnv: () => void = () => undefined;
let restoreStdin: (() => void) | undefined;

beforeEach(() => {
restoreCiEnv = clearCiEnv();
configDir = mkdtempSync(join(tmpdir(), "cb-telemetry-site-"));
env.set("CHARGEBEE_CONFIG_DIR", configDir);
env.set("CHARGEBEE_SITE", undefined);
env.set("CHARGEBEE_API_KEY", undefined);
env.set("DO_NOT_TRACK", undefined);
env.set("CHARGEBEE_CLI_TELEMETRY", undefined);
// A later run: the first-run notice has been shown, so events are recorded.
writeState({ notice_shown: true });
const constructions: ClientConstruction[] = [];
installFakeClient({ constructions });
restoreStdin = setStdinIsTTY(false);
__resetTelemetryRecorderForTest();
__setSpawnForTest(() => ({ unref() {} }));
});

afterEach(() => {
__resetTelemetryRecorderForTest();
__setSpawnForTest(null);
restoreStdin?.();
uninstallFakeClient();
env.restore();
restoreCiEnv();
rmSync(configDir, { recursive: true, force: true });
});

/** Run one command with the recorder installed and return its spooled event. */
async function runRecorded(args: string[]): Promise<{ exitCode: number; record: SpoolRecord }> {
const { exitCode } = await runCli(args, {
configure: (program) => installTelemetry(program, "0.0.0-test"),
});
__finalizeForTest(exitCode);
__resetTelemetryRecorderForTest();
const lines = readFileSync(spoolPath(), "utf-8").trim().split("\n");
return { exitCode, record: JSON.parse(lines.at(-1)!) as SpoolRecord };
}

/** Save a profile without recording an event. */
async function addProfile(profile: string, site: string, apiKey: string): Promise<void> {
const { exitCode } = await runCli(["auth", "add", "--profile", profile, "--site", site, "--api-key", apiKey]);
expect(exitCode).toBe(0);
}

describe("telemetry site label for commands that change the active site", () => {
it("labels the first auth add with the added site, not unconfigured", async () => {
const { exitCode, record } = await runRecorded([
"auth", "add", "--profile", "dev", "--site", "acme-test", "--api-key", "test_key",
]);
expect(exitCode).toBe(0);
expect(record.event.name).toBe("auth add");
expect(record.site_name).toBe("acme-test");
});

it("labels auth add with the new site when another profile was active", async () => {
await addProfile("dev", "acme-test", "test_key");
const { record } = await runRecorded([
"auth", "add", "--profile", "other", "--site", "globex-test", "--api-key", "test_key_2",
]);
expect(record.site_name).toBe("globex-test");
});

it("labels auth switch with the site switched to", async () => {
await addProfile("dev", "acme-test", "test_key");
await addProfile("other", "globex-test", "test_key_2");
const { exitCode, record } = await runRecorded(["auth", "switch", "dev"]);
expect(exitCode).toBe(0);
expect(record.event.name).toBe("auth switch");
expect(record.site_name).toBe("acme-test");
});

it("keeps the previous site when auth add fails", async () => {
await addProfile("dev", "acme-test", "test_key");
const { exitCode, record } = await runRecorded([
"auth", "add", "--profile", "bad", "--site", "not a site!", "--api-key", "test_key_2",
]);
expect(exitCode).toBe(1);
expect(record.site_name).toBe("acme-test");
});

it("labels the next command from the active profile as before", async () => {
await addProfile("dev", "acme-test", "test_key");
const { record } = await runRecorded(["customer", "list"]);
expect(record.site_name).toBe("acme-test");
});

it("records nothing when telemetry is disabled", async () => {
env.set("CHARGEBEE_CLI_TELEMETRY", "0");
const { exitCode } = await runCli(["auth", "add", "--profile", "dev", "--site", "acme-test", "--api-key", "test_key"], {
configure: (program) => installTelemetry(program, "0.0.0-test"),
});
__finalizeForTest(exitCode);
expect(exitCode).toBe(0);
expect(existsSync(spoolPath())).toBe(false);
});
});
25 changes: 25 additions & 0 deletions src/tests/unit/lib/telemetry/active-site.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { afterEach, describe, expect, it } from "bun:test";

import { noteActiveSite, takeActiveSite } from "../../../../lib/telemetry/active-site.js";

afterEach(() => {
takeActiveSite();
});

describe("active-site slot", () => {
it("is empty by default", () => {
expect(takeActiveSite()).toBeUndefined();
});

it("returns the noted site once, then clears", () => {
noteActiveSite("acme-test");
expect(takeActiveSite()).toBe("acme-test");
expect(takeActiveSite()).toBeUndefined();
});

it("keeps the latest noted site", () => {
noteActiveSite("acme-test");
noteActiveSite("globex-test");
expect(takeActiveSite()).toBe("globex-test");
});
});
19 changes: 19 additions & 0 deletions src/tests/unit/lib/telemetry/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { appendRecord } from "../../../../lib/telemetry/spool.js";
import { setTelemetryEnabled } from "../../../../lib/telemetry/optout.js";
import { readState, writeState } from "../../../../lib/telemetry/state.js";
import { recordTelemetryError, takeTelemetryError } from "../../../../lib/telemetry/error.js";
import { noteActiveSite, takeActiveSite } from "../../../../lib/telemetry/active-site.js";
import type { SpoolRecord } from "../../../../lib/telemetry/types.js";
import { __resetKeychainForTest, __setKeychainForTest, type KeychainStore } from "../../../../lib/config/keychain.js";
import { saveProfile } from "../../../../lib/config/profiles.js";
Expand Down Expand Up @@ -588,5 +589,23 @@ describe("installTelemetry recorder", () => {
const rec = await recordOneCommand();
expect(rec.site_name).toBe("env-test");
});

it("prefers a site the command noted as newly active", async () => {
const program = new Command();
program.exitOverride();
program.command("connect").action(() => noteActiveSite("globex-test"));
installTelemetry(program, "1.0.0-test");
await program.parseAsync(["connect"], { from: "user" });
__finalizeForTest(0);
const rec = JSON.parse(readFileSync(spoolPath(), "utf-8").trim()) as SpoolRecord;
expect(rec.site_name).toBe("globex-test");
expect(takeActiveSite()).toBeUndefined();
});

it("drops a noted site on recorder reset", () => {
noteActiveSite("globex-test");
__resetTelemetryRecorderForTest();
expect(takeActiveSite()).toBeUndefined();
});
});
});
Loading