Skip to content

Commit

Permalink
Disable telemetry with env var (#8159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Calder committed Dec 7, 2022
1 parent b390c40 commit bdbe1ad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-emus-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Adds the ability to disable telemetry using environment variable `KEYSTONE_TELEMETRY_DISABLED`
5 changes: 5 additions & 0 deletions docs/pages/docs/reference/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ Keystone stores your telemetry preferences in a location defined by [env-paths](
| Linux | ~/.config/keystonejs (or $XDG_CONFIG_HOME/keystonejs) |
| Windows | %APPDATA%\keystonejs\Config (for example C:\Users\YOUR_USERNAME\AppData\Roaming\keystonejs\Config) |

{% if $nextRelease %}
**Environment Variable**
You can opt-out of all telemetry by setting the `KEYSTONE_TELEMETRY_DISABLED` environment variable to `'1'`

{% /if %}
**Network-wide opt-out**

If you have a network-wide firewall, you can opt-out of Keystone telemetry by not resolving the following domain: [telemetry.keystonejs.com](https://telemetry.keystonejs.com)
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/lib/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ export function runTelemetry(
dbProviderName: DatabaseProvider
) {
try {
const { userConfig, telemetry } = getTelemetryConfig();
if (
ci.isCI || // Don't run in CI
process.env.NODE_ENV === 'production' || // Don't run in production
telemetry === false // Don't run if the user has opted out
process.env.KEYSTONE_TELEMETRY_DISABLED === '1' // Don't run if the user has disabled it
) {
return;
}
const { userConfig, telemetry } = getTelemetryConfig();
if (telemetry === false) return; // Don't run if the user has opted out
if (telemetry === undefined) {
const newTelemetry: Configuration['telemetry'] = {
device: { informedAt: new Date().toISOString() },
Expand Down
26 changes: 26 additions & 0 deletions packages/core/tests/telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import fetch, { Response } from 'node-fetch';
import { InitialisedList } from '../src/lib/core/types-for-lists';
import { runTelemetry } from '../src/lib/telemetry';
import { telemetry } from '../src/scripts/telemetry';

var mockProjectRoot = path.resolve(__dirname, '..', '..', '..');
var mockProjectDir = path.join(mockProjectRoot, './tests/test-projects/basic');
Expand Down Expand Up @@ -149,6 +150,7 @@ describe('Inital Telemetry tests', () => {
afterEach(() => {
// Reset env variables
delete process.env.KEYSTONE_TELEMETRY_ENDPOINT;
delete process.env.KEYSTONE_TELEMETRY_DISABLED;
delete process.env.NOW_BUILDER;
// clear mocks (fetch specifically)
jest.clearAllMocks();
Expand Down Expand Up @@ -199,6 +201,30 @@ describe('Inital Telemetry tests', () => {
expect(mockTelemetryConfig?.device).toBeUndefined();
expect(mockTelemetryConfig?.projects).toBeUndefined();
});

test('Telemetry Does not send when the user has opted out using keystone telemetry disable', async () => {
defaultFetchMock();

telemetry(mockProjectDir, 'disable');
expect(mockTelemetryConfig).toBe(false);
runTelemetry(mockProjectDir, lists, 'sqlite');
runTelemetry(mockProjectDir, lists, 'sqlite');
expect(mockFetch).toHaveBeenCalledTimes(0);
expect(mockTelemetryConfig?.device).toBeUndefined();
expect(mockTelemetryConfig?.projects).toBeUndefined();
});

test('Telemetry Does not send when env KEYSTONE_TELEMETRY_DISABLED is set to 1', async () => {
defaultFetchMock();
// @ts-ignore
process.env.KEYSTONE_TELEMETRY_DISABLED = '1';

runTelemetry(mockProjectDir, lists, 'sqlite');
runTelemetry(mockProjectDir, lists, 'sqlite');
expect(mockFetch).toHaveBeenCalledTimes(0);
expect(mockTelemetryConfig?.device).toBeUndefined();
expect(mockTelemetryConfig?.projects).toBeUndefined();
});
});

describe('Test isCI being set', () => {
Expand Down

1 comment on commit bdbe1ad

@vercel
Copy link

@vercel vercel bot commented on bdbe1ad Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.