Skip to content
Draft
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
22 changes: 11 additions & 11 deletions apps/cli-e2e/src/tests/telemetry.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Effect, FileSystem, Path } from "effect";
import { describe, expect } from "vitest";
import { runE2e, testBehaviour } from "./test-context.ts";

// The harness opts every run out by env var, and the caller's shell can too. `status` reports
// either ahead of the file's state.
const withoutEnvOptOut = { env: { SUPABASE_TELEMETRY_DISABLED: "", DO_NOT_TRACK: "" } };

describe("telemetry", () => {
describe("telemetry:enable", () => {
testBehaviour("enables telemetry", ({ run, signal }) =>
Expand Down Expand Up @@ -51,7 +55,7 @@ describe("telemetry", () => {
testBehaviour("shows current telemetry state", ({ run, signal }) =>
runE2e(
Effect.gen(function* () {
const result = yield* run(["telemetry", "status"]);
const result = yield* run(["telemetry", "status"], withoutEnvOptOut);
expect(result.exitCode).toBe(0);
expect(result.stdout).toMatch(/Telemetry is (enabled|disabled)\./);
}),
Expand All @@ -62,8 +66,8 @@ describe("telemetry", () => {
testBehaviour("round-trip: enable then status shows enabled", ({ run, signal }) =>
runE2e(
Effect.gen(function* () {
yield* run(["telemetry", "enable"]);
const result = yield* run(["telemetry", "status"]);
yield* run(["telemetry", "enable"], withoutEnvOptOut);
const result = yield* run(["telemetry", "status"], withoutEnvOptOut);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Telemetry is enabled.");
}),
Expand All @@ -74,8 +78,8 @@ describe("telemetry", () => {
testBehaviour("round-trip: disable then status shows disabled", ({ run, signal }) =>
runE2e(
Effect.gen(function* () {
yield* run(["telemetry", "disable"]);
const result = yield* run(["telemetry", "status"]);
yield* run(["telemetry", "disable"], withoutEnvOptOut);
const result = yield* run(["telemetry", "status"], withoutEnvOptOut);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Telemetry is disabled.");
}),
Expand All @@ -90,13 +94,9 @@ describe("telemetry", () => {
const path = yield* Path.Path;
const telemetryPath = path.join(workspace.path, "telemetry.json");
yield* fs.writeFileString(telemetryPath, "{{not valid json}}");
const result = yield* run(["telemetry", "status"]);
const result = yield* run(["telemetry", "status"], withoutEnvOptOut);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("Telemetry is enabled.");
const telemetry = new TextDecoder("utf-8", { ignoreBOM: true }).decode(
yield* fs.readFile(telemetryPath),
);
expect(() => JSON.parse(telemetry)).not.toThrow();
expect(result.stdout).toContain("Telemetry is disabled.");
}),
signal,
),
Expand Down
14 changes: 14 additions & 0 deletions apps/cli/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ stack-started after health succeeds, and upgrade-suggested billing-gate events w
envelope and payload semantics. Use the catalog and the telemetry tests for the complete current
event contract.

An opt-out (`supabase telemetry disable`, `SUPABASE_TELEMETRY_DISABLED=1`, `DO_NOT_TRACK=1`) holds
on every path:

- Every send goes through the `Analytics` layer. Nothing else constructs a PostHog client or calls
an analytics host.
- A command that opts the user out sends nothing.
- A `telemetry.json` that cannot be read, or whose content does not decode, is an opt-out. Only a
missing or empty file is a first run.
- Every reader and writer of `telemetry.json` resolves it from `TelemetryRuntime.configDir`.
- While the user is opted out, only `telemetry enable` and `telemetry disable` create
`telemetry.json`. An existing file may be updated with its opt-out written back unchanged.
- Event and group properties carry no user-chosen names. Project groups send the organization
slug only.

## Error classification

`src/shared/telemetry/error-actionability.ts` owns the closed actionability vocabulary. Export every
Expand Down
42 changes: 34 additions & 8 deletions apps/cli/docs/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ Identity is resolved from `SUPABASE_HOME/telemetry.json`:

At login time, if the auth response includes a `user_id`, the CLI can:

- `alias(device_id -> user_id)`
- `identify(user_id, ...)`
- `alias(device_id -> user_id)`, for the first identity a device sees
- persist `distinct_id`

The CLI does not call `identify()`.

That stitching is intentionally best-effort. The current token format is opaque, so the CLI does
not derive the user ID locally from the stored token.

Expand All @@ -179,14 +180,15 @@ When a project is linked, the CLI can also attach PostHog groups for:
- `organization`
- `project`

The linked project snapshot cached in repo-local `.supabase/project.json` includes organization and
project metadata used for this purpose, while the user-level telemetry state remains in
`SUPABASE_HOME/telemetry.json`.
The linked project snapshot cached in `supabase/.temp/linked-project.json` includes organization
and project metadata used for this purpose (stack-mode projects read `supabase/.link.json`
instead), while the user-level telemetry state remains in `SUPABASE_HOME/telemetry.json`.

During `supabase link`, the CLI:

- refreshes linked project metadata
- calls `groupIdentify()` for organization and project
- calls `groupIdentify()` for organization and project, with the organization slug as the only
group property. The project name is user-chosen and is never sent.
- emits `cli_project_linked`
- uses scoped analytics context so later captures in the same invocation can reuse those groups

Expand All @@ -203,15 +205,39 @@ Analytics follows the shared CLI telemetry consent model:
- environment overrides can still disable telemetry in CI or sandboxed runs, primarily
`SUPABASE_TELEMETRY_DISABLED=1`

The CLI also honors `DO_NOT_TRACK=1` as a broader system-level opt-out signal.
The CLI also honors `DO_NOT_TRACK=1` as a broader system-level opt-out signal. Both variables opt
out only when set to `1`; any other value leaves telemetry on.

The telemetry commands:

- `supabase telemetry enable`
- `supabase telemetry disable`
- `supabase telemetry status`

read and update that same `telemetry.json` file.
read and update that same `telemetry.json` file. `status` reports what the current run does, so
it names the environment variable when one opts the run out.

An opt-out holds on every path:

- with consent denied the analytics layer builds no PostHog client, so nothing is captured,
aliased, or group-identified
- `supabase telemetry disable` sends no event of its own
- a `telemetry.json` that cannot be read, or whose content does not decode, is read as an
opt-out. Undecodable content is rewritten as one. A missing or empty file is a first run.
- the telemetry commands write to the same `SUPABASE_HOME` the CLI reads consent from, including
one set in a project `.env`
- while opted out, the CLI updates an existing `telemetry.json` but does not create one
- an opt-out applies to commands started after it. State writes take no lock, so a command that
is already running can write its earlier state back.

On the first interactive run the CLI prints the telemetry notice and sends nothing in that
invocation. Sending starts with the next one, so `is_first_run` is only ever true on events from
non-interactive first runs. A first interactive `supabase login` persists `distinct_id` without
sending the alias: the device has no earlier events to merge. Non-interactive first runs print no
notice.

The release check after each command is not analytics and is not covered by the telemetry
opt-out. `SUPABASE_NO_UPDATE_NOTIFIER` turns it off; see [supabase-home.md](supabase-home.md).

## Comparison: Vercel CLI

Expand Down
1 change: 0 additions & 1 deletion apps/cli/src/commands/bootstrap/bootstrap.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ export const bootstrap = Effect.fn("bootstrap")(function* (
if (linkMode === "stack") {
yield* identifyProjectGroups(analytics, {
ref: projectRef,
name: created.name,
organization_id: created.organizationId,
organization_slug: created.organizationSlug,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe("bootstrap integration", () => {
{
groupType: "project",
groupKey: VALID_REF,
properties: { name: CREATED.name, organization_slug: CREATED.organization_slug },
properties: { organization_slug: CREATED.organization_slug },
},
]);
}).pipe(Effect.provide(BunServices.layer)),
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/commands/link/link.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ describe("link integration", () => {
{
groupType: "project",
groupKey: VALID_REF,
properties: { name: "My Project", organization_slug: "acme" },
properties: { organization_slug: "acme" },
},
]);
const capture = analytics.captured.find((c) => c.event === "cli_project_linked");
Expand Down Expand Up @@ -1565,7 +1565,7 @@ describe("link integration", () => {
{
groupType: "project",
groupKey: PARENT_REF,
properties: { name: "My Project", organization_slug: "acme" },
properties: { organization_slug: "acme" },
},
]);
expect(yield* jsonText(analytics.captured)).not.toContain('"main"');
Expand Down
10 changes: 6 additions & 4 deletions apps/cli/src/commands/telemetry/disable/disable.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export const telemetryDisableCommand = Command.make("disable", config).pipe(
Command.withShortDescription("Disable telemetry"),
withPermissions({ operations: [] }),
Command.withHandler((flags) =>
// `cli_command_executed` fires based on the consent value read at layer-construction
// time, before this handler mutates it — so `disable` still fires when telemetry was
// enabled going into the call, and stays silent when it was already disabled.
telemetryDisable(flags).pipe(withCommandTelemetry({ flags }), withJsonErrorHandling),
// Consent is read at layer construction, before this handler writes the opt-out, so the
// command event has to be switched off here or the opt-out itself would be reported.
telemetryDisable(flags).pipe(
withCommandTelemetry({ flags, analytics: false }),
withJsonErrorHandling,
),
),
Command.provide(commandRuntimeLayer(["telemetry", "disable"])),
withTargetPolicy({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Effect } from "effect";
import { Output } from "../../../shared/output/output.service.ts";
import { TelemetryRuntime } from "../../../shared/telemetry/runtime.service.ts";
import { setTelemetryEnabled } from "../../../telemetry/telemetry-state.layer.ts";
import type { TelemetryDisableFlags } from "./disable.command.ts";

export const telemetryDisable = Effect.fn("telemetry.disable")(function* (
_flags: TelemetryDisableFlags,
) {
const output = yield* Output;
yield* setTelemetryEnabled(false);
const runtime = yield* TelemetryRuntime;
yield* setTelemetryEnabled(runtime.configDir, false);
yield* output.raw("Telemetry is disabled.\n");
});
4 changes: 3 additions & 1 deletion apps/cli/src/commands/telemetry/enable/enable.handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Effect } from "effect";
import { Output } from "../../../shared/output/output.service.ts";
import { TelemetryRuntime } from "../../../shared/telemetry/runtime.service.ts";
import { setTelemetryEnabled } from "../../../telemetry/telemetry-state.layer.ts";
import type { TelemetryEnableFlags } from "./enable.command.ts";

export const telemetryEnable = Effect.fn("telemetry.enable")(function* (
_flags: TelemetryEnableFlags,
) {
const output = yield* Output;
yield* setTelemetryEnabled(true);
const runtime = yield* TelemetryRuntime;
yield* setTelemetryEnabled(runtime.configDir, true);
yield* output.raw("Telemetry is enabled.\n");
});
8 changes: 5 additions & 3 deletions apps/cli/src/commands/telemetry/status/status.handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Effect } from "effect";
import { Output } from "../../../shared/output/output.service.ts";
import { loadOrCreateTelemetryState } from "../../../telemetry/telemetry-state.layer.ts";
import { TelemetryRuntime } from "../../../shared/telemetry/runtime.service.ts";
import type { TelemetryStatusFlags } from "./status.command.ts";

export const telemetryStatus = Effect.fn("telemetry.status")(function* (
_flags: TelemetryStatusFlags,
) {
const output = yield* Output;
const state = yield* loadOrCreateTelemetryState();
yield* output.raw(`Telemetry is ${state.enabled ? "enabled" : "disabled"}.\n`);
const runtime = yield* TelemetryRuntime;
const state = runtime.consent === "granted" ? "enabled" : "disabled";
const reason = runtime.optOutVariable === undefined ? "" : ` (${runtime.optOutVariable}=1)`;
yield* output.raw(`Telemetry is ${state}${reason}.\n`);
});
Loading