-
Notifications
You must be signed in to change notification settings - Fork 44
feat: add execution context to telemetry events #1074
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f5f616c
feat: add execution context to telemetry events
patrikbraborec 2211399
test: add unit tests for telemetry environment detection
patrikbraborec eb1de06
refactor: use ci-info package for CI provider detection
patrikbraborec 8436012
fix: update lockfile for ci-info dependency
patrikbraborec 876bc8e
fix: format import statements in telemetry test
patrikbraborec b5c6109
fix: skip telemetry notice when APIFY_CLI_DISABLE_TELEMETRY is set
patrikbraborec 91f904c
fix: format telemetry state condition
patrikbraborec 6cbeaa2
fix: skip telemetry data collection when telemetry is disabled
patrikbraborec c3dcb6b
test: add tests for checkAndUpdateLastCommand telemetry helper
patrikbraborec 8826f92
fix: resolve lint errors in checkAndUpdateLastCommand test
patrikbraborec 69df104
fix: format checkAndUpdateLastCommand test to pass CI
patrikbraborec 61747af
Merge branch 'master' into feat/telemetry-execution-context
patrikbraborec 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import ciInfo from 'ci-info'; | ||
|
|
||
| const AI_AGENT_ENV_VARS: [string, string][] = [ | ||
| ['CLAUDECODE', 'claude_code'], | ||
| ['CLAUDE_CODE_ENTRYPOINT', 'claude_code'], | ||
| ['CURSOR_AGENT', 'cursor'], | ||
| ['CLINE_ACTIVE', 'cline'], | ||
| ['CODEX_SANDBOX', 'codex_cli'], | ||
| ['CODEX_THREAD_ID', 'codex_cli'], | ||
| ['GEMINI_CLI', 'gemini_cli'], | ||
| ['OPENCODE', 'open_code'], | ||
| ['OPENCLAW_SHELL', 'openclaw'], | ||
| ]; | ||
|
|
||
| export function detectAiAgent(): string | undefined { | ||
| for (const [envVar, agent] of AI_AGENT_ENV_VARS) { | ||
| if (process.env[envVar]) { | ||
| return agent; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| export function detectCi(): { isCi: boolean; ciProvider: string | undefined } { | ||
| if (!ciInfo.isCI) { | ||
| return { isCi: false, ciProvider: undefined }; | ||
| } | ||
|
|
||
| return { isCi: true, ciProvider: ciInfo.id?.toLowerCase() ?? 'unknown' }; | ||
| } | ||
|
|
||
| export function detectIsInteractive(): boolean { | ||
| return !!process.stdin.isTTY && !!process.stdout.isTTY; | ||
| } | ||
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
222 changes: 222 additions & 0 deletions
222
test/lib/hooks/telemetry/checkAndUpdateLastCommand.test.ts
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,222 @@ | ||
| import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { dirname, join } from 'node:path'; | ||
|
|
||
| let telemetryFilePath: string; | ||
|
|
||
| vi.mock('../../../../src/lib/consts.js', async (importOriginal) => { | ||
| const original = await importOriginal<typeof import('../../../../src/lib/consts.js')>(); | ||
|
|
||
| return { | ||
| ...original, | ||
| TELEMETRY_FILE_PATH: () => telemetryFilePath, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('../../../../src/lib/utils.js', () => ({ | ||
| getLocalUserInfo: async () => ({}), | ||
| })); | ||
|
|
||
| vi.mock('../../../../src/lib/outputs.js', () => ({ | ||
| info: () => { | ||
| /* noop */ | ||
| }, | ||
| })); | ||
|
|
||
| function writeTelemetryState(state: Record<string, unknown>) { | ||
| const dir = dirname(telemetryFilePath); | ||
| if (!existsSync(dir)) { | ||
| mkdirSync(dir, { recursive: true }); | ||
| } | ||
| writeFileSync(telemetryFilePath, JSON.stringify(state, null, '\t')); | ||
| } | ||
|
|
||
| function readTelemetryState() { | ||
| return JSON.parse(readFileSync(telemetryFilePath, 'utf-8')); | ||
| } | ||
|
|
||
| describe('checkAndUpdateLastCommand', () => { | ||
| let testDir: string; | ||
| let counter = 0; | ||
|
|
||
| beforeEach(() => { | ||
| counter++; | ||
| testDir = join(tmpdir(), `apify-cli-test-telemetry-${process.pid}-${counter}-${Date.now()}`); | ||
| telemetryFilePath = join(testDir, 'telemetry.json'); | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| // Clean up temp files | ||
| if (existsSync(testDir)) { | ||
| rmSync(testDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| test('returns false on first invocation (no prior command)', async () => { | ||
| vi.setSystemTime(1000); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('stores the command and timestamp in telemetry state', async () => { | ||
| vi.setSystemTime(50_000); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| await checkAndUpdateLastCommand('apify push'); | ||
|
|
||
| const state = readTelemetryState(); | ||
| expect(state.lastCommand).toBe('apify push'); | ||
| expect(state.lastCommandTimestamp).toBe(50_000); | ||
| }); | ||
|
|
||
| test('returns true when the same command is repeated within the retry window', async () => { | ||
| vi.setSystemTime(100_000); | ||
|
|
||
| // Seed state with a recent identical command | ||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| lastCommandTimestamp: 95_000, // 5 seconds ago — within the 10s window | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false when the same command is repeated outside the retry window', async () => { | ||
| vi.setSystemTime(100_000); | ||
|
|
||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| lastCommandTimestamp: 80_000, // 20 seconds ago — outside the 10s window | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false when a different command is run within the retry window', async () => { | ||
| vi.setSystemTime(100_000); | ||
|
|
||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| lastCommandTimestamp: 95_000, | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify push'); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('updates state after checking so the next call sees the new command', async () => { | ||
| vi.setSystemTime(100_000); | ||
|
|
||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| lastCommandTimestamp: 90_000, | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| await checkAndUpdateLastCommand('apify push'); | ||
|
|
||
| const state = readTelemetryState(); | ||
| expect(state.lastCommand).toBe('apify push'); | ||
| expect(state.lastCommandTimestamp).toBe(100_000); | ||
| }); | ||
|
|
||
| test('returns false when lastCommandTimestamp is missing', async () => { | ||
| vi.setSystemTime(100_000); | ||
|
|
||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| // no lastCommandTimestamp | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns false when telemetry state file is corrupted', async () => { | ||
| // Write invalid JSON | ||
| const dir = dirname(telemetryFilePath); | ||
| mkdirSync(dir, { recursive: true }); | ||
| writeFileSync(telemetryFilePath, '{{{invalid json'); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| test('returns true at exactly the retry window boundary', async () => { | ||
| // Command was run exactly 9999ms ago (just inside the 10_000ms window) | ||
| vi.setSystemTime(109_999); | ||
|
|
||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| lastCommandTimestamp: 100_000, | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false at exactly the retry window boundary (equal to window)', async () => { | ||
| // Command was run exactly 10_000ms ago (at the boundary, not strictly less than) | ||
| vi.setSystemTime(110_000); | ||
|
|
||
| writeTelemetryState({ | ||
| version: 1, | ||
| enabled: true, | ||
| anonymousId: 'CLI:test', | ||
| lastCommand: 'apify run', | ||
| lastCommandTimestamp: 100_000, | ||
| }); | ||
|
|
||
| const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); | ||
|
|
||
| const result = await checkAndUpdateLastCommand('apify run'); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
| }); |
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.