0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

feat: Add configuration option to disable CLI telemetry

This commit is contained in:
Matt Kane 2024-10-04 21:46:44 +01:00
parent 55e9cd8855
commit f690a58375
6 changed files with 60 additions and 5 deletions

View file

@ -0,0 +1,18 @@
---
'@astrojs/telemetry': minor
'astro': minor
---
Adds a configuration option to disable CLI telemetry for the project
Astro collects optional anonymous telemetry to help us understand how people use the CLI. This change adds a new way to disable telemetry on a per-project basis. Currently you can disable telemetry globally per machine by running `astro telemetry disable` or by setting the environment variable `ASTRO_TELEMETRY_DISABLE`. This change adds a new configuration option to disable telemetry for a specific project, which applies to all users of the project.
```js
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
disableTelemetry: true
});
```

View file

@ -148,6 +148,11 @@ export async function resolveConfig(
const userConfig = await loadConfig(root, inlineOnlyConfig.configFile, fsMod); const userConfig = await loadConfig(root, inlineOnlyConfig.configFile, fsMod);
const mergedConfig = mergeConfig(userConfig, inlineUserConfig); const mergedConfig = mergeConfig(userConfig, inlineUserConfig);
if (mergedConfig.disableTelemetry) {
telemetry.disableForSession();
}
// First-Pass Validation // First-Pass Validation
let astroConfig: AstroConfig; let astroConfig: AstroConfig;
try { try {

View file

@ -87,6 +87,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
security: { security: {
checkOrigin: true, checkOrigin: true,
}, },
disableTelemetry: false,
env: { env: {
schema: {}, schema: {},
validateSecrets: false, validateSecrets: false,
@ -509,6 +510,7 @@ export const AstroConfigSchema = z.object({
.strict() .strict()
.optional() .optional()
.default(ASTRO_CONFIG_DEFAULTS.env), .default(ASTRO_CONFIG_DEFAULTS.env),
disableTelemetry: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.disableTelemetry),
experimental: z experimental: z
.object({ .object({
clientPrerender: z clientPrerender: z
@ -526,10 +528,7 @@ export const AstroConfigSchema = z.object({
.default({}), .default({}),
legacy: z legacy: z
.object({ .object({
collections: z collections: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.legacy.collections),
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.legacy.collections),
}) })
.default({}), .default({}),
}); });

View file

@ -899,6 +899,22 @@ export interface AstroUserConfig {
defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'load'; defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'load';
}; };
/**
* @docs
* @name disableTelemetry
* @type {boolean}
* @version 5.0.0
* @default `false`
* @description
* Disable anonymous telemetry collection for this project.
*
* You can also disable telemetry globally by running `astro telemetry disable`.
*
* Visit https://astro.build/telemetry/ for more information about our approach to anonymous telemetry in Astro.
*/
disableTelemetry?: boolean;
/** /**
* @docs * @docs
* @kind heading * @kind heading

View file

@ -25,6 +25,7 @@ export class AstroTelemetry {
private debug = debug('astro:telemetry'); private debug = debug('astro:telemetry');
private isCI = isCI; private isCI = isCI;
private env = process.env; private env = process.env;
private disabledForSession = false;
private get astroVersion() { private get astroVersion() {
return this.opts.astroVersion; return this.opts.astroVersion;
@ -85,7 +86,7 @@ export class AstroTelemetry {
} }
private get isDisabled(): boolean { private get isDisabled(): boolean {
if (Boolean(this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED)) { if (this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED || this.disabledForSession) {
return true; return true;
} }
return this.enabled === false; return this.enabled === false;
@ -95,6 +96,11 @@ export class AstroTelemetry {
this.config.set(KEY.TELEMETRY_ENABLED, value); this.config.set(KEY.TELEMETRY_ENABLED, value);
} }
disableForSession() {
this.debug('[notify] telemetry has been disabled for this session');
this.disabledForSession = true;
}
clear() { clear() {
return this.config.clear(); return this.config.clear();
} }

View file

@ -81,4 +81,15 @@ describe('AstroTelemetry', () => {
assert.notEqual(log, undefined); assert.notEqual(log, undefined);
assert.match(logs.join(''), /enabled/); assert.match(logs.join(''), /enabled/);
}); });
it('respects disabling per session', async () => {
const { telemetry, logs } = setup();
telemetry.disableForSession();
assert.equal(telemetry.isDisabled, true);
const result = await telemetry.record(['TEST']);
assert.equal(result, undefined);
const [log] = logs;
assert.notEqual(log, undefined);
assert.match(logs.join(''), /disabled/);
});
}); });