Elffuss BraidHub

kikocis/continue public

Merge pull request #7672 from continuedev/nate/filter-by-headless-or-ci

feat: isHeadless and isGitHubCI on cli OTel
Changeset5474b5e9e86c212c05ceee2a7af085105387aeacdc664b40030d31c6c73ee4e7
AuthorNate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-10 14:45 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-10 14:45 -07:00
Parentsd769512763cc, e69865bbb658

Browse the files at this changeset

6 files changed

modified extensions/cli/src/init.ts +3 −2
@@ -3,10 +3,11 @@
33 * This file intercepts console and stdout/stderr to prevent dependency logging
44 */
55
6+import { isHeadlessMode as checkIsHeadlessMode } from "./util/cli.js";
7+
68 // Check if we're in headless mode by looking at process arguments
79 // We need to do this before any imports to catch early logging
8-const args = process.argv.slice(2);
9-const isHeadlessMode = args.includes("-p") || args.includes("--print");
10+const isHeadlessMode = checkIsHeadlessMode();
1011
1112 // Store original methods before ANY dependencies can use them
1213 const originalStdoutWrite = process.stdout.write.bind(process.stdout);
modified extensions/cli/src/telemetry/posthogService.ts +2 −9
@@ -5,6 +5,7 @@
55 import type { PostHog as PostHogType } from "posthog-node";
66
77 import { isAuthenticatedConfig, loadAuthConfig } from "../auth/workos.js";
8+import { isHeadlessMode } from "../util/cli.js";
89 import { isGitHubActions } from "../util/git.js";
910 import { logger } from "../util/logger.js";
1011 import { getVersion } from "../version.js";
@@ -38,14 +39,6 @@
3839 return this._hasInternetConnection;
3940 }
4041
41- /**
42- * Check if running in headless mode (-p/--print flags)
43- */
44- private isHeadlessMode(): boolean {
45- const args = process.argv.slice(2);
46- return args.includes("-p") || args.includes("--print");
47- }
48-
4942 get isEnabled() {
5043 return process.env.CONTINUE_CLI_ENABLE_TELEMETRY !== "0";
5144 }
@@ -100,7 +93,7 @@
10093 extensionVersion: getVersion(),
10194 ideName: "cn",
10295 ideType: "cli",
103- isHeadless: this.isHeadlessMode(),
96+ isHeadless: isHeadlessMode(),
10497 isGitHubCI: isGitHubActions(),
10598 };
10699 const payload = {
added extensions/cli/src/telemetry/telemetryService.sessionMetadata.test.ts +238 −0
@@ -0,0 +1,238 @@
1+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
3+import { telemetryService } from "./telemetryService.js";
4+
5+describe("TelemetryService - Session Metadata", () => {
6+ let originalEnv: NodeJS.ProcessEnv;
7+ let originalArgv: string[];
8+
9+ beforeEach(() => {
10+ // Save original environment and argv
11+ originalEnv = { ...process.env };
12+ originalArgv = [...process.argv];
13+
14+ // Mock logger to prevent console output during tests
15+ vi.mock("../util/logger.js", () => ({
16+ logger: {
17+ debug: vi.fn(),
18+ info: vi.fn(),
19+ warn: vi.fn(),
20+ error: vi.fn(),
21+ },
22+ }));
23+
24+ // Enable telemetry for tests
25+ process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "1";
26+ process.env.OTEL_METRICS_EXPORTER = "console";
27+ });
28+
29+ afterEach(() => {
30+ // Restore original environment and argv
31+ process.env = originalEnv;
32+ process.argv = originalArgv;
33+ vi.clearAllMocks();
34+ });
35+
36+ describe("recordSessionStart", () => {
37+ it("should include is_headless=false when not in headless mode", () => {
38+ // Set up non-headless mode (no -p or --print flags)
39+ process.argv = ["node", "cli.js", "chat"];
40+
41+ const mockAdd = vi.fn();
42+ const mockRecord = vi.fn();
43+ const service = telemetryService as any;
44+
45+ // Mock the session counter and startup time histogram
46+ service.sessionCounter = { add: mockAdd };
47+ service.startupTimeHistogram = { record: mockRecord };
48+ service.config = { enabled: true };
49+ service.meter = {};
50+
51+ service.recordSessionStart();
52+
53+ expect(mockAdd).toHaveBeenCalledWith(
54+ 1,
55+ expect.objectContaining({
56+ is_headless: "false",
57+ }),
58+ );
59+ });
60+
61+ it("should include is_headless=true when -p flag is present", () => {
62+ // Set up headless mode with -p flag
63+ process.argv = ["node", "cli.js", "-p", "What is the weather?"];
64+
65+ const mockAdd = vi.fn();
66+ const mockRecord = vi.fn();
67+ const service = telemetryService as any;
68+
69+ // Mock the session counter and startup time histogram
70+ service.sessionCounter = { add: mockAdd };
71+ service.startupTimeHistogram = { record: mockRecord };
72+ service.config = { enabled: true };
73+ service.meter = {};
74+
75+ service.recordSessionStart();
76+
77+ expect(mockAdd).toHaveBeenCalledWith(
78+ 1,
79+ expect.objectContaining({
80+ is_headless: "true",
81+ }),
82+ );
83+ });
84+
85+ it("should include is_headless=true when --print flag is present", () => {
86+ // Set up headless mode with --print flag
87+ process.argv = ["node", "cli.js", "--print", "What is the weather?"];
88+
89+ const mockAdd = vi.fn();
90+ const mockRecord = vi.fn();
91+ const service = telemetryService as any;
92+
93+ // Mock the session counter and startup time histogram
94+ service.sessionCounter = { add: mockAdd };
95+ service.startupTimeHistogram = { record: mockRecord };
96+ service.config = { enabled: true };
97+ service.meter = {};
98+
99+ service.recordSessionStart();
100+
101+ expect(mockAdd).toHaveBeenCalledWith(
102+ 1,
103+ expect.objectContaining({
104+ is_headless: "true",
105+ }),
106+ );
107+ });
108+
109+ it("should include is_github_actions=false when not in GitHub Actions", () => {
110+ // Ensure GITHUB_ACTIONS is not set
111+ delete process.env.GITHUB_ACTIONS;
112+
113+ const mockAdd = vi.fn();
114+ const mockRecord = vi.fn();
115+ const service = telemetryService as any;
116+
117+ // Mock the session counter and startup time histogram
118+ service.sessionCounter = { add: mockAdd };
119+ service.startupTimeHistogram = { record: mockRecord };
120+ service.config = { enabled: true };
121+ service.meter = {};
122+
123+ service.recordSessionStart();
124+
125+ expect(mockAdd).toHaveBeenCalledWith(
126+ 1,
127+ expect.objectContaining({
128+ is_github_actions: "false",
129+ is_continue_remote_agent: "false",
130+ }),
131+ );
132+ });
133+
134+ it("should include is_github_actions=true when in GitHub Actions", () => {
135+ // Set GitHub Actions environment
136+ process.env.GITHUB_ACTIONS = "true";
137+
138+ const mockAdd = vi.fn();
139+ const mockRecord = vi.fn();
140+ const service = telemetryService as any;
141+
142+ // Mock the session counter and startup time histogram
143+ service.sessionCounter = { add: mockAdd };
144+ service.startupTimeHistogram = { record: mockRecord };
145+ service.config = { enabled: true };
146+ service.meter = {};
147+
148+ service.recordSessionStart();
149+
150+ expect(mockAdd).toHaveBeenCalledWith(
151+ 1,
152+ expect.objectContaining({
153+ is_github_actions: "true",
154+ is_continue_remote_agent: "false",
155+ }),
156+ );
157+ });
158+
159+ it("should include both headless and GitHub Actions metadata when both are true", () => {
160+ // Set both headless mode and GitHub Actions
161+ process.argv = ["node", "cli.js", "-p", "What is the weather?"];
162+ process.env.GITHUB_ACTIONS = "true";
163+
164+ const mockAdd = vi.fn();
165+ const mockRecord = vi.fn();
166+ const service = telemetryService as any;
167+
168+ // Mock the session counter and startup time histogram
169+ service.sessionCounter = { add: mockAdd };
170+ service.startupTimeHistogram = { record: mockRecord };
171+ service.config = { enabled: true };
172+ service.meter = {};
173+
174+ service.recordSessionStart();
175+
176+ expect(mockAdd).toHaveBeenCalledWith(
177+ 1,
178+ expect.objectContaining({
179+ is_headless: "true",
180+ is_github_actions: "true",
181+ is_continue_remote_agent: "false",
182+ }),
183+ );
184+ });
185+
186+ it("should include is_continue_remote_agent=true when in remote agent mode", () => {
187+ // Set Continue remote agent environment
188+ process.env.CONTINUE_REMOTE = "true";
189+
190+ const mockAdd = vi.fn();
191+ const mockRecord = vi.fn();
192+ const service = telemetryService as any;
193+
194+ // Mock the session counter and startup time histogram
195+ service.sessionCounter = { add: mockAdd };
196+ service.startupTimeHistogram = { record: mockRecord };
197+ service.config = { enabled: true };
198+ service.meter = {};
199+
200+ service.recordSessionStart();
201+
202+ expect(mockAdd).toHaveBeenCalledWith(
203+ 1,
204+ expect.objectContaining({
205+ is_continue_remote_agent: "true",
206+ }),
207+ );
208+ });
209+
210+ it("should include all environment flags when all are true", () => {
211+ // Set all environments
212+ process.argv = ["node", "cli.js", "-p", "What is the weather?"];
213+ process.env.GITHUB_ACTIONS = "true";
214+ process.env.CONTINUE_REMOTE = "true";
215+
216+ const mockAdd = vi.fn();
217+ const mockRecord = vi.fn();
218+ const service = telemetryService as any;
219+
220+ // Mock the session counter and startup time histogram
221+ service.sessionCounter = { add: mockAdd };
222+ service.startupTimeHistogram = { record: mockRecord };
223+ service.config = { enabled: true };
224+ service.meter = {};
225+
226+ service.recordSessionStart();
227+
228+ expect(mockAdd).toHaveBeenCalledWith(
229+ 1,
230+ expect.objectContaining({
231+ is_headless: "true",
232+ is_github_actions: "true",
233+ is_continue_remote_agent: "true",
234+ }),
235+ );
236+ });
237+ });
238+});
modified extensions/cli/src/telemetry/telemetryService.ts +12 −1
@@ -19,6 +19,8 @@
1919 } from "@opentelemetry/semantic-conventions";
2020 import { v4 as uuidv4 } from "uuid";
2121
22+import { isHeadlessMode } from "../util/cli.js";
23+import { isContinueRemoteAgent, isGitHubActions } from "../util/git.js";
2224 import { logger } from "../util/logger.js";
2325 import { getVersion } from "../version.js";
2426
@@ -350,7 +352,16 @@
350352 if (!this.isEnabled()) return;
351353
352354 logger.debug("Recording session start with telemetry");
353- this.sessionCounter.add(1, this.getStandardAttributes());
355+
356+ // Add extra metadata for session breakdown
357+ const isGitHubActionsEnv = isGitHubActions();
358+ const sessionAttributes = this.getStandardAttributes({
359+ is_headless: isHeadlessMode().toString(),
360+ is_github_actions: isGitHubActionsEnv.toString(),
361+ is_continue_remote_agent: isContinueRemoteAgent().toString(),
362+ });
363+
364+ this.sessionCounter.add(1, sessionAttributes);
354365 this.recordStartupTime(Date.now() - this.startTime);
355366 }
356367
added extensions/cli/src/util/cli.ts +11 −0
@@ -0,0 +1,11 @@
1+/**
2+ * Utility functions for CLI operations
3+ */
4+
5+/**
6+ * Check if running in headless mode (-p/--print flags)
7+ */
8+export function isHeadlessMode(): boolean {
9+ const args = process.argv.slice(2);
10+ return args.includes("-p") || args.includes("--print");
11+}
modified extensions/cli/src/util/git.ts +7 −0
@@ -57,6 +57,13 @@
5757 return process.env.GITHUB_ACTIONS === "true";
5858 }
5959
60+/**
61+ * Check if running in Continue remote agents
62+ */
63+export function isContinueRemoteAgent(): boolean {
64+ return process.env.CONTINUE_REMOTE === "true";
65+}
66+
6067 /**
6168 * Get repository URL from GitHub Actions environment variables
6269 * @returns GitHub repository URL or null if not available