Skip to content
Open
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 @@ -308,7 +308,7 @@ Attributes: `gen_ai.provider.name`, `gen_ai.request.model`,
| ------------------------- | -------------------------------------------- | --------------------------------------- |
| `VITE_SENTRY_DSN` | Cloudflare Sentry telemetry | disabled when unset |
| `VITE_SENTRY_ENVIRONMENT` | Cloudflare Sentry environment | development |
| `SENTRY_CLI_NO_TELEMETRY` | stdio telemetry off when `1` | on, to the CLI's project |
| `SENTRY_CLI_NO_TELEMETRY` | stdio telemetry off when set, as in the CLI | on, to the CLI's project |
| `SENTRY_RELEASE` | stdio release tag | unset |
| `SENTRY_HOST` | upstream Sentry host and `app.upstream.host` | `sentry.io` |
| `NODE_ENV` | stdio environment | production fallback in packaged runtime |
Expand Down
3 changes: 2 additions & 1 deletion apps/cli-docs/src/content/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ curl https://cli.sentry.dev/install -fsS | SENTRY_INIT=1 bash
```

The installer reports its own errors to Sentry. Set `SENTRY_CLI_NO_TELEMETRY=1`
to turn this off; the CLI honors the same variable.
or `DO_NOT_TRACK=1` to turn this off; the CLI honors the same variables. Any
value except an empty or false-like one (`0`, `false`, `no`, `off`) counts.

### Verify the Install Script

Expand Down
4 changes: 3 additions & 1 deletion apps/cli-docs/src/content/docs/migrating-from-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,9 @@ Otherwise the command stops with exit code 13 before sending any request.

v4 sends usage data and error reports about the CLI itself to Sentry. To opt
out, set `SENTRY_CLI_NO_TELEMETRY=1` or `DO_NOT_TRACK=1`, or run
`sentry cli defaults telemetry off`.
`sentry cli defaults telemetry off`. Either variable opts out with any value
except an empty or false-like one (`0`, `false`, `no`, `off`), so
`DO_NOT_TRACK=true` works too.

## Getting help

Expand Down
2 changes: 1 addition & 1 deletion apps/cli-docs/src/fragments/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Hints print below the output only on a terminal. When stdout is piped or redirec

## Telemetry

The CLI sends usage data and error reports about itself to Sentry. To opt out, set `SENTRY_CLI_NO_TELEMETRY=1` or `DO_NOT_TRACK=1`, or run `sentry cli defaults telemetry off`.
The CLI sends usage data and error reports about itself to Sentry. To opt out, set `SENTRY_CLI_NO_TELEMETRY=1` or `DO_NOT_TRACK=1`, or run `sentry cli defaults telemetry off`. Either variable opts out with any value except an empty or false-like one (`0`, `false`, `no`, `off`), so `DO_NOT_TRACK=true` works too.

When telemetry can't be sent, for example while offline or when a firewall blocks Sentry, the CLI keeps it in its local database and sends up to 10 stored envelopes on each later run. The cache holds at most 100 envelopes and 4 MiB, compressed. When it's full, the oldest usage data goes first, then the oldest error reports. It discards error reports after 30 days and everything else after 4 days. Metrics, such as timings, aren't cached: when they can't be sent, they're dropped.

Expand Down
17 changes: 15 additions & 2 deletions packages/cli/install
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NC='\033[0m'

# Installer errors go to Sentry through the envelope API, using the CLI's
# public write-only DSN. No PII is collected. Opt out: SENTRY_CLI_NO_TELEMETRY=1
# or DO_NOT_TRACK=1.
SENTRY_DSN_KEY="1188a86f3f8168f089450587b00bca66"
SENTRY_INGEST="https://o1.ingest.us.sentry.io"
SENTRY_PROJECT_ID="4510776311808000"
Expand Down Expand Up @@ -153,6 +154,7 @@ Environment Variables:
SENTRY_VERSION Install a specific version (e.g., 0.42.2, nightly)
SENTRY_INIT Set to 1 to run \`sentry init\` after installing
SENTRY_CLI_NO_TELEMETRY Set to 1 to disable installer error reporting
DO_NOT_TRACK Same as SENTRY_CLI_NO_TELEMETRY
Examples:
curl -fsSL https://cli.sentry.dev/install | bash
Expand Down Expand Up @@ -234,11 +236,22 @@ matches() {
printf '%s\n' "$1" | grep -Eq "$2"
}

# Usage: env_flag_set <value>. Succeeds when a boolean environment variable is
# on: any value except an empty or false-like one (0, false, f, n, no, off, in
# any case). The CLI reads its telemetry opt-outs the same way.
env_flag_set() {
case "$1" in
"" | 0 | [Ff] | [Nn] | [Ff][Aa][Ll][Ss][Ee] | [Nn][Oo] | [Oo][Ff][Ff]) return 1 ;;
esac
}

# Usage: report_error <message> <step>. Sends an error event to Sentry in a
# background subshell, so it never slows down or fails the install.
report_error() {
# Respect the same opt-out as the CLI binary.
[ "${SENTRY_CLI_NO_TELEMETRY:-}" = "1" ] && return 0
# Respect the same opt-outs as the CLI binary.
if env_flag_set "${SENTRY_CLI_NO_TELEMETRY:-}" || env_flag_set "${DO_NOT_TRACK:-}"; then
return 0
fi

(
# Telemetry must never fail the script or report its own failures.
Expand Down
10 changes: 9 additions & 1 deletion packages/cli/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SENTRY_INIT Set to 1 to run `sentry init` after installing
SENTRY_NO_MODIFY_PATH Set to 1 to leave the user PATH unchanged
SENTRY_CLI_NO_TELEMETRY Set to 1 to disable installer error reporting
DO_NOT_TRACK Same as SENTRY_CLI_NO_TELEMETRY
For the nightly channel, install stable first and then run
`sentry cli upgrade nightly`.
Expand Down Expand Up @@ -309,10 +310,17 @@ function Install-SentryCli {
return 0
}

# Whether a boolean environment variable is on: any value except an empty or
# false-like one (0, false, f, n, no, off; -contains ignores case), as the
# CLI reads its telemetry opt-outs.
function Test-EnvFlag([string]$Value) {
return -not (@('', '0', 'false', 'f', 'n', 'no', 'off') -contains $Value)
}

# Fire-and-forget error report via the envelope API, mirroring the bash
# installer. Uses the CLI's public write-only DSN. No PII collected.
function Send-InstallError([string]$Message) {
if ($env:SENTRY_CLI_NO_TELEMETRY -eq '1') { return }
if ((Test-EnvFlag $env:SENTRY_CLI_NO_TELEMETRY) -or (Test-EnvFlag $env:DO_NOT_TRACK)) { return }
try {
$dsnKey = '1188a86f3f8168f089450587b00bca66'
$projectId = '4510776311808000'
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/env-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ export const ENV_VAR_REGISTRY: readonly EnvVarEntry[] = [
{
name: "SENTRY_CLI_NO_TELEMETRY",
description:
"Disable CLI telemetry (error tracking for the CLI itself). The CLI sends anonymized error reports to help improve reliability — set this to opt out.",
"Disable CLI telemetry (error tracking for the CLI itself). The CLI sends anonymized error reports to help improve reliability — set this to opt out.\n\nAny value turns telemetry off (`1`, `true`, `yes`, `on`) except an empty or false-like one (`0`, `false`, `no`, `off`). A false-like value doesn't turn telemetry on: `DO_NOT_TRACK` and `sentry cli defaults telemetry` still apply.",
example: "1",
devGuide: "Disable CLI telemetry (error tracking)",
},
{
name: "DO_NOT_TRACK",
description:
"Industry-standard opt-out for telemetry. When set to `1`, the CLI disables all telemetry, equivalent to `SENTRY_CLI_NO_TELEMETRY=1`.",
"Industry-standard opt-out for telemetry. When set, the CLI disables all telemetry, equivalent to `SENTRY_CLI_NO_TELEMETRY=1`. It reads values the same way as `SENTRY_CLI_NO_TELEMETRY`: `1`, `true`, or any other value opts out, but an empty or false-like value (`0`, `false`, `no`, `off`) doesn't.",
example: "1",
},
{
Expand Down
20 changes: 12 additions & 8 deletions packages/cli/src/lib/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import { createRequire } from "node:module";
import { API_BASE_PATH } from "@sentry/api";
import type { Metric, TransactionEvent } from "@sentry/core";
import { envToBool, type Metric, type TransactionEvent } from "@sentry/core";
// oxlint-disable-next-line sentry-cli/no-namespace-import -- Sentry SDK recommends namespace import
import * as Sentry from "@sentry/node";

Expand Down Expand Up @@ -114,7 +114,7 @@ export function markSessionCrashed(): void {
}
}

/** Env var that disables CLI telemetry when set to `"1"`. */
/** Env var that disables CLI telemetry (see {@link computeTelemetryEffective}). */
export const TELEMETRY_ENV_VAR = "SENTRY_CLI_NO_TELEMETRY";

/** Industry-standard env var for opting out of telemetry. */
Expand All @@ -138,20 +138,24 @@ export type TelemetryEffective = {
* Resolve the effective telemetry state with source attribution.
*
* Priority (highest to lowest):
* 1. `SENTRY_CLI_NO_TELEMETRY=1` — explicit CLI env var opt-out
* 2. `DO_NOT_TRACK=1` — industry-standard opt-out
* 1. `SENTRY_CLI_NO_TELEMETRY` — explicit CLI env var opt-out
* 2. `DO_NOT_TRACK` — industry-standard opt-out
* 3. SQLite persistent preference — `sentry cli defaults telemetry on/off`
* 4. Default: enabled
*
* An env var opts out when the SDK's `envToBool` reads it as true: any value
* but an empty or false-like one (`0`, `false`, `no`, `off`, `f`, `n`, any
* case). A false-like value does not opt in either: the next source decides.
*
* The DB read is wrapped in try/catch — if the database is uninitialized
* or corrupted, we fall through to the default (enabled).
*/
export function computeTelemetryEffective(): TelemetryEffective {
if (getEnv()[TELEMETRY_ENV_VAR] === "1") {
if (envToBool(getEnv()[TELEMETRY_ENV_VAR])) {
return { enabled: false, source: `env:${TELEMETRY_ENV_VAR}` };
}

if (getEnv()[DO_NOT_TRACK_ENV_VAR] === "1") {
if (envToBool(getEnv()[DO_NOT_TRACK_ENV_VAR])) {
return { enabled: false, source: `env:${DO_NOT_TRACK_ENV_VAR}` };
}

Expand Down Expand Up @@ -213,8 +217,8 @@ export function scrubAnonymousMetric(metric: Metric): Metric {
* paths that bypass this function's try/catch.
*
* Telemetry can be disabled via:
* - `SENTRY_CLI_NO_TELEMETRY=1` environment variable
* - `DO_NOT_TRACK=1` environment variable
* - `SENTRY_CLI_NO_TELEMETRY` environment variable (`1`, `true`, ...)
* - `DO_NOT_TRACK` environment variable (`1`, `true`, ...)
* - `sentry cli defaults telemetry off` (persistent preference)
*
* @param callback - The CLI execution function to wrap, receives the span for naming
Expand Down
83 changes: 83 additions & 0 deletions packages/cli/test/commands/cli/defaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,89 @@ describe("computeTelemetryEffective", () => {
teardown();
}
});

const optOutVars = ["SENTRY_CLI_NO_TELEMETRY", "DO_NOT_TRACK"];

test.each(
optOutVars.flatMap((name) =>
["1", "true", "TRUE", "yes", "on"].map((value) => [name, value]),
),
)("%s=%j opts out", (name, value) => {
setup();
try {
process.env[name] = value;
expect(computeTelemetryEffective()).toEqual({
enabled: false,
source: `env:${name}`,
});
} finally {
teardown();
}
});

test.each(
optOutVars.flatMap((name) =>
["0", "false", "FALSE", "no", "off", ""].map((value) => [name, value]),
),
)("%s=%j does not opt out, so the preference applies", (name, value) => {
setup();
try {
setOnOffPreference("telemetry", false);
process.env[name] = value;
expect(computeTelemetryEffective()).toEqual({
enabled: false,
source: "preference",
});
setOnOffPreference("telemetry", true);
expect(computeTelemetryEffective()).toEqual({
enabled: true,
source: "preference",
});
} finally {
teardown();
}
});

test("SENTRY_CLI_NO_TELEMETRY=0 falls through to DO_NOT_TRACK", () => {
setup();
try {
process.env["SENTRY_CLI_NO_TELEMETRY"] = "0";
process.env["DO_NOT_TRACK"] = "true";
expect(computeTelemetryEffective()).toEqual({
enabled: false,
source: "env:DO_NOT_TRACK",
});
} finally {
teardown();
}
});

test("SENTRY_CLI_NO_TELEMETRY wins over DO_NOT_TRACK as the source", () => {
setup();
try {
process.env["SENTRY_CLI_NO_TELEMETRY"] = "yes";
process.env["DO_NOT_TRACK"] = "true";
expect(computeTelemetryEffective().source).toBe(
"env:SENTRY_CLI_NO_TELEMETRY",
);
} finally {
teardown();
}
});

test("false-like env values with no preference leave the default on", () => {
setup();
try {
process.env["SENTRY_CLI_NO_TELEMETRY"] = "false";
process.env["DO_NOT_TRACK"] = "0";
expect(computeTelemetryEffective()).toEqual({
enabled: true,
source: "default",
});
} finally {
teardown();
}
});
});

// ---------------------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/test/lib/install-ps1-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ function Invoke-WebRequest {
Get-Content -Raw -LiteralPath $env:SENTRY_TEST_INSTALL_SCRIPT | Invoke-Expression
`;

const OPT_OUT_CASES = ["SENTRY_CLI_NO_TELEMETRY", "DO_NOT_TRACK"].flatMap(
(name) => [
...["1", "true", "TRUE", "yes", "on"].map((value) => ({
name,
value,
reports: false,
})),
...["0", "false", "FALSE", "no", "off", ""].map((value) => ({
name,
value,
reports: true,
})),
],
);

const ENVELOPE_URL =
"https://o1.ingest.us.sentry.io/api/4510776311808000/envelope/";

const asset = "sentry-windows-x64.exe";
const served: ServedAsset = {
name: asset,
Expand Down Expand Up @@ -257,6 +275,22 @@ describe.skipIf(!hasPwsh)("PowerShell install script", () => {
expect(recorded("setup-args").at(-1)).toBe("--no-modify-path");
});

test.each(OPT_OUT_CASES)(
"$name=$value sends the error report: $reports",
({ name, value, reports }) => {
env["SENTRY_TEST_SETUP_EXIT"] = "3";
delete env["SENTRY_CLI_NO_TELEMETRY"];
env[name] = value;

const result = run();

expect(result.status).not.toBe(0);
expect(recorded("urls").some((url) => url === ENVELOPE_URL)).toBe(
reports,
);
},
);

test.each([
{
name: "non-Windows hosts",
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/test/lib/install-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ printf '%s\\n' "$0" >> "$SENTRY_TEST_DIR/post-binary"
exit "\${SENTRY_TEST_POST_EXIT:-0}"
`;

const OPT_OUT_CASES = ["SENTRY_CLI_NO_TELEMETRY", "DO_NOT_TRACK"].flatMap(
(name) => [
...["1", "true", "TRUE", "yes", "on"].map((value) => ({
name,
value,
reports: false,
})),
...["0", "false", "FALSE", "no", "off", ""].map((value) => ({
name,
value,
reports: true,
})),
],
);

const hostAsset = `sentry-${process.platform === "darwin" ? "darwin" : "linux"}-${process.arch === "arm64" ? "arm64" : "x64"}`;
const served: ServedAsset = {
name: hostAsset,
Expand Down Expand Up @@ -733,6 +748,25 @@ exit 127
expect(report.tags["registry.curl_exit"]).toBe("0");
});

test.each(OPT_OUT_CASES)(
"$name=$value sends the error report: $reports",
({ name, value, reports }) => {
applyScenario(
{ ...at(INSTALL_SCENARIOS, 0), registry: { status: 503 } },
served,
testDir,
env,
);
delete env["SENTRY_CLI_NO_TELEMETRY"];
env[name] = value;

const result = install();

expect(result.status).not.toBe(0);
expect(existsSync(join(testDir, "error-report"))).toBe(reports);
},
);

const shIsBash =
spawnSync("sh", ["-c", 'echo "${BASH_VERSION:-}"'], {
encoding: "utf8",
Expand Down
Loading