Repository navigation
feat(server,web,desktop): turn telemetry off in Settings or with t3 telemetry
#27077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inayayousfi
wants to merge
15
commits into
pingdotgg:main
Choose a base branch
from
inayayousfi:feat/telemetry-setting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+987
−213
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5488ba1
feat(server,web,desktop): turn telemetry off in Settings or with `t3 …
inayayousfi 8a11a10
fix(server,web): stop a running flush on opt-out and keep the locked …
inayayousfi 88b59e4
fix(server,shared): treat telemetry as off when settings.json cannot …
inayayousfi 8fee6fe
test(server): drive the telemetry CLI test through Effect FileSystem
inayayousfi ac153e8
fix(desktop): treat an unreadable settings.json as telemetry off
inayayousfi 87fe31d
test(server): the telemetry setting decides after desktop shell disco…
inayayousfi e37f196
fix(server): a late shell telemetry value can only opt out, and Setti…
inayayousfi 74c3d7c
fix(server): discard queued usage events as soon as telemetry is turn…
inayayousfi da8581b
fix(server): an opt-out during a flush stops it even if telemetry is …
inayayousfi 807a011
fix(web): lock the usage data switch off where a managed policy turns…
inayayousfi 69859e5
fix(server): a null relay tracing config keeps tracing off
inayayousfi 74bb558
fix(shared): read the telemetry setting through the narrowed settings…
inayayousfi e83dc62
fix(web): settings search hides product usage data on servers without…
inayayousfi 3303419
fix(server): t3 telemetry status reports a blank settings file as off
inayayousfi 0ce269e
docs: a login shell value can only turn desktop telemetry off
inayayousfi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite"; | ||
| import { fromJsonStringPretty, fromLenientJson } from "@t3tools/shared/schemaJson"; | ||
| import * as Config from "effect/Config"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Schema from "effect/Schema"; | ||
|
|
||
| import * as ServerConfig from "../config.ts"; | ||
| import { resolveBaseDir } from "../os-jank.ts"; | ||
| import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; | ||
|
|
||
| const SparseSettings = Schema.Record(Schema.String, Schema.Unknown); | ||
| const decodeSettingsJson = Schema.decodeUnknownEffect(fromLenientJson(SparseSettings)); | ||
| const encodeSettingsJson = Schema.encodeEffect(fromJsonStringPretty(SparseSettings)); | ||
|
|
||
| /** The environment's settings.json as written: only the keys that differ from defaults. */ | ||
| export type SparseSettings = typeof SparseSettings.Type; | ||
|
|
||
| export class SettingsUnreadableError extends Schema.TaggedError<SettingsUnreadableError>()( | ||
| "SettingsUnreadableError", | ||
| { settingsPath: Schema.String, cause: Schema.Defect() }, | ||
| ) { | ||
| override get message(): string { | ||
| return `Could not read ${this.settingsPath}. Fix its permissions, then run this again.`; | ||
| } | ||
| } | ||
|
|
||
| export class SettingsMalformedError extends Schema.TaggedError<SettingsMalformedError>()( | ||
| "SettingsMalformedError", | ||
| { settingsPath: Schema.String, cause: Schema.Defect() }, | ||
| ) { | ||
| override get message(): string { | ||
| return `${this.settingsPath} is not a JSON object. Fix or remove it, then run this again.`; | ||
| } | ||
| } | ||
|
|
||
| export class SettingsBusyError extends Schema.TaggedError<SettingsBusyError>()( | ||
| "SettingsBusyError", | ||
| { settingsPath: Schema.String, attempts: Schema.Number }, | ||
| ) { | ||
| override get message(): string { | ||
| return `${this.settingsPath} kept changing while writing (gave up after ${this.attempts} attempts). Try again.`; | ||
| } | ||
| } | ||
|
|
||
| export class SettingsWriteError extends Schema.TaggedError<SettingsWriteError>()( | ||
| "SettingsWriteError", | ||
| { settingsPath: Schema.String, cause: Schema.Defect() }, | ||
| ) { | ||
| override get message(): string { | ||
| return `Could not write ${this.settingsPath}.`; | ||
| } | ||
| } | ||
|
|
||
| const envT3Home = Config.String("T3CODE_HOME").pipe(Config.option); | ||
| const DEV_STATE_PLACEHOLDER_URL = new URL("http://localhost"); | ||
|
|
||
| const hasLiveServer = (statePath: string) => | ||
| readPersistedServerRuntimeState(statePath).pipe( | ||
| Effect.map((state) => Option.isSome(state) && isProcessAlive(state.value.pid)), | ||
| Effect.orElseSucceed(() => false), | ||
| ); | ||
|
|
||
| /** | ||
| * The paths of the environment a settings command targets: `--base-dir`, else `T3CODE_HOME`. | ||
| * With neither, a dev server's `dev` state wins unless a `userdata` server is running too, | ||
| * the same order `t3 pair` checks. | ||
| */ | ||
| export const resolveCliServerPaths = Effect.fn(function* (explicitBaseDir: Option.Option<string>) { | ||
| const envHome = Option.filter(yield* envT3Home, (value) => value.trim().length > 0); | ||
| const configuredBaseDir = Option.orElse(explicitBaseDir, () => envHome); | ||
| const baseDir = yield* resolveBaseDir(Option.getOrUndefined(configuredBaseDir)); | ||
| const userdataPaths = yield* ServerConfig.deriveServerPaths(baseDir, undefined, { | ||
| baseDirIsExplicit: Option.isSome(configuredBaseDir), | ||
| }); | ||
| if ( | ||
| Option.isSome(configuredBaseDir) || | ||
| (yield* hasLiveServer(userdataPaths.serverRuntimeStatePath)) | ||
| ) { | ||
| return userdataPaths; | ||
| } | ||
| const devPaths = yield* ServerConfig.deriveServerPaths(baseDir, DEV_STATE_PLACEHOLDER_URL); | ||
| return (yield* hasLiveServer(devPaths.serverRuntimeStatePath)) ? devPaths : userdataPaths; | ||
| }); | ||
|
|
||
| const NO_SETTINGS: SparseSettings = {}; | ||
|
|
||
| /** Propagate read errors to avoid overwriting unread settings with a sparse file. */ | ||
| export const readSettingsObject = Effect.fn(function* (settingsPath: string) { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const exists = yield* fs | ||
| .exists(settingsPath) | ||
| .pipe(Effect.mapError((cause) => new SettingsUnreadableError({ settingsPath, cause }))); | ||
| if (!exists) return { exists, raw: "", settings: NO_SETTINGS }; | ||
|
|
||
| const raw = yield* fs | ||
| .readFileString(settingsPath) | ||
| .pipe(Effect.mapError((cause) => new SettingsUnreadableError({ settingsPath, cause }))); | ||
| if (raw.trim().length === 0) return { exists, raw, settings: NO_SETTINGS }; | ||
|
|
||
| const settings = yield* decodeSettingsJson(raw).pipe( | ||
| Effect.mapError((cause) => new SettingsMalformedError({ settingsPath, cause })), | ||
| ); | ||
| return { exists, raw, settings }; | ||
| }); | ||
|
|
||
| /** The server's in-process semaphore cannot serialize writes from this process. */ | ||
| const CONCURRENT_WRITE_ATTEMPTS = 5; | ||
|
|
||
| /** | ||
| * Rewrites settings.json with `update`, retrying when another writer changed it meanwhile. | ||
| * A running server picks the change up from its file watcher. | ||
| */ | ||
| export const updateSettingsObject = Effect.fn(function* ( | ||
| settingsPath: string, | ||
| update: (settings: SparseSettings) => Effect.Effect<SparseSettings>, | ||
| ) { | ||
| const fs = yield* FileSystem.FileSystem; | ||
|
|
||
| for (let attempt = 1; ; attempt++) { | ||
| const { raw, settings } = yield* readSettingsObject(settingsPath); | ||
| const contents = yield* encodeSettingsJson(yield* update(settings)); | ||
| const current = yield* fs.readFileString(settingsPath).pipe(Effect.orElseSucceed(() => "")); | ||
| if (current !== raw) { | ||
| if (attempt >= CONCURRENT_WRITE_ATTEMPTS) { | ||
| return yield* new SettingsBusyError({ settingsPath, attempts: CONCURRENT_WRITE_ATTEMPTS }); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| yield* writeFileStringAtomically({ | ||
| filePath: settingsPath, | ||
| contents: `${contents}\n`, | ||
| }).pipe(Effect.mapError((cause) => new SettingsWriteError({ settingsPath, cause }))); | ||
| return; | ||
| } | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.