diff --git a/README.md b/README.md index 75370d2..c686bdb 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,30 @@ Run `npx e2e run` with `DEVICE_CLOUD_API_KEY` set. The provider uploads `appPath With e2e's `video` option on (any setting but `'off'`), each test's video in e2e's report is a console link to that test's part of the session's recording. The session is recorded as one video, so the link plays the part from the test's start to its end; it works once the session has ended and its video has uploaded, for as long as the session's files are kept. +## Telemetry + +The CLI sends a small amount of usage and error data to the DeviceCloud API. We use it only for debugging and development: to find and fix bugs, and to see which commands and options are used. It is not used for marketing, and it is not sold. + +**What is sent**, for each command run: + +- the command name and its arguments, with secrets removed (API keys, `--env` values, text typed into a device, and the query part of any URL, which is where signed links keep their credential) +- whether it succeeded, how long it took, and the error message if it failed (URLs redacted the same way) +- the CLI version, how it was installed, the Node.js version, the OS and CPU architecture +- how you signed in (API key or `dcd login`), your organization ID, and a random ID for this run +- whether the app, flows and env vars were encrypted + +Nothing is sent until a command has signed in, so `--help`, `--version` and `dcd login` itself send nothing. Telemetry goes to the same API the command uses (`--api-url`, or the default). A failure to send it never affects the command. + +**Turning it off.** Set either of these in your environment: + +```sh-session +$ export DO_NOT_TRACK=1 # the cross-tool convention, see consoledonottrack.com +$ export DCD_TELEMETRY_DISABLED=1 +``` + +`0`, `false` or an empty value leaves telemetry on. For the MCP server, add the variable to the `env` block of your MCP client's config. + + ## Development Requires Node 22.8+ and [pnpm](https://pnpm.io). `pnpm install` builds the CLI and installs the git hooks automatically. diff --git a/src/services/telemetry.service.ts b/src/services/telemetry.service.ts index d330413..aa6d6c0 100644 --- a/src/services/telemetry.service.ts +++ b/src/services/telemetry.service.ts @@ -10,7 +10,8 @@ * - `flushSync()` — sync via `curl`, used inside `logger.error` right before * `process.exit` (no other way to send HTTP after exit is called) * - * Opt out: `DCD_TELEMETRY_DISABLED=1` in the environment. + * Opt out: `DCD_TELEMETRY_DISABLED=1` or `DO_NOT_TRACK=1` in the environment + * (see telemetryDisabled). */ import { execFileSync } from 'node:child_process'; import { randomUUID } from 'node:crypto'; @@ -82,6 +83,21 @@ interface TelemetryConfig { const DEFAULT_API_URL = 'https://api.devicecloud.dev'; +/** Set to something other than empty, `0` or `false` (in any case). */ +function isOn(value: string | undefined): boolean { + const v = value?.trim().toLowerCase(); + return v !== undefined && v !== '' && v !== '0' && v !== 'false'; +} + +/** + * Whether the user has turned telemetry off: `DCD_TELEMETRY_DISABLED`, or + * `DO_NOT_TRACK`, the cross-tool convention (https://consoledonottrack.com). + * `0` or `false` leaves telemetry on, so `DO_NOT_TRACK=0` means what it says. + */ +export function telemetryDisabled(env: NodeJS.ProcessEnv = process.env): boolean { + return isOn(env.DCD_TELEMETRY_DISABLED) || isOn(env.DO_NOT_TRACK); +} + class Telemetry { private buffer: TelemetryEvent[] = []; private config: TelemetryConfig | null = null; @@ -90,7 +106,7 @@ class Telemetry { private submitAttempts: number | undefined; private sessionId: string = randomUUID(); private startedAt: number = Date.now(); - private readonly disabled: boolean = !!process.env.DCD_TELEMETRY_DISABLED; + private readonly disabled: boolean = telemetryDisabled(); private readonly release: string = getCliVersion(); /** @@ -348,7 +364,6 @@ class Telemetry { nodeVersion: process.versions.node, platform: process.platform, arch: process.arch, - userEmail: this.config.auth.userEmail, orgId: this.config.auth.orgId, }; } diff --git a/test/unit/telemetry-api-url.test.ts b/test/unit/telemetry-api-url.test.ts index d4707dd..d3787de 100644 --- a/test/unit/telemetry-api-url.test.ts +++ b/test/unit/telemetry-api-url.test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { telemetry } from '../../src/services/telemetry.service.js'; +import { telemetry, telemetryDisabled } from '../../src/services/telemetry.service.js'; /** * Events go to the API the command itself talks to. A login's stored API used @@ -14,7 +14,7 @@ describe('telemetry API URL', () => { beforeEach(function () { // The singleton reads this once, at import. - if (process.env.DCD_TELEMETRY_DISABLED) this.skip(); + if (telemetryDisabled()) this.skip(); urls = []; globalThis.fetch = (async (input: unknown) => { diff --git a/test/unit/telemetry-disabled.test.ts b/test/unit/telemetry-disabled.test.ts new file mode 100644 index 0000000..5e780a8 --- /dev/null +++ b/test/unit/telemetry-disabled.test.ts @@ -0,0 +1,29 @@ +import { expect } from 'chai'; + +import { telemetryDisabled } from '../../src/services/telemetry.service.js'; + +/** The two env vars that turn telemetry off, and the values that don't. */ +describe('telemetryDisabled', () => { + it('is off (telemetry on) with neither var set', () => { + expect(telemetryDisabled({})).to.equal(false); + }); + + for (const name of ['DCD_TELEMETRY_DISABLED', 'DO_NOT_TRACK']) { + for (const value of ['1', 'true', 'TRUE', 'yes', ' 1 ']) { + it(`turns telemetry off for ${name}=${JSON.stringify(value)}`, () => { + expect(telemetryDisabled({ [name]: value })).to.equal(true); + }); + } + + for (const value of ['', '0', 'false', 'False', ' 0 ']) { + it(`leaves telemetry on for ${name}=${JSON.stringify(value)}`, () => { + expect(telemetryDisabled({ [name]: value })).to.equal(false); + }); + } + } + + it('turns telemetry off when either var says so', () => { + expect(telemetryDisabled({ DCD_TELEMETRY_DISABLED: '0', DO_NOT_TRACK: '1' })).to.equal(true); + expect(telemetryDisabled({ DCD_TELEMETRY_DISABLED: '1', DO_NOT_TRACK: '0' })).to.equal(true); + }); +}); diff --git a/test/unit/telemetry-encryption.test.ts b/test/unit/telemetry-encryption.test.ts index f1477a5..4340673 100644 --- a/test/unit/telemetry-encryption.test.ts +++ b/test/unit/telemetry-encryption.test.ts @@ -4,6 +4,7 @@ import { jsonResult, runTool } from '../../src/mcp/helpers.js'; import { type EncryptionTelemetry, telemetry, + telemetryDisabled, } from '../../src/services/telemetry.service.js'; /** @@ -22,7 +23,7 @@ describe('encryption telemetry (#1138)', () => { beforeEach(function () { // The singleton reads this once, at import. - if (process.env.DCD_TELEMETRY_DISABLED) this.skip(); + if (telemetryDisabled()) this.skip(); events = []; globalThis.fetch = (async (_input: unknown, init?: RequestInit) => { diff --git a/test/unit/telemetry-submit-attempts.test.ts b/test/unit/telemetry-submit-attempts.test.ts index 414aca1..9f5fe19 100644 --- a/test/unit/telemetry-submit-attempts.test.ts +++ b/test/unit/telemetry-submit-attempts.test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { telemetry } from '../../src/services/telemetry.service.js'; +import { telemetry, telemetryDisabled } from '../../src/services/telemetry.service.js'; /** * How many attempts the flow submit took (dcd#1617 follow-up) rides on the @@ -19,7 +19,7 @@ describe('submit attempts telemetry', () => { beforeEach(function () { // The singleton reads this once, at import. - if (process.env.DCD_TELEMETRY_DISABLED) this.skip(); + if (telemetryDisabled()) this.skip(); events = []; globalThis.fetch = (async (_input: unknown, init?: RequestInit) => {