Elffuss BraidHub

kikocis/continue public

feat: isHeadless and isGitHubCI on cli OTel
Changeset736b2a5165faaaf920dd3e0ffb645c6d9347913df232a2c2197c236d7017f4db
AuthorNate <sestinj@gmail.com> · 2025-09-09 17:15 -07:00
CommitterNate <sestinj@gmail.com> · 2025-09-09 17:15 -07:00
Parentsdd35e28818ae

Browse the files at this changeset

5 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 +186 −0
@@ -0,0 +1,186 @@
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+ ci_environment: "none",
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+ ci_environment: "github_actions",
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+ ci_environment: "github_actions",
182+ }),
183+ );
184+ });
185+ });
186+});
modified extensions/cli/src/telemetry/telemetryService.ts +10 −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 { isGitHubActions } from "../util/git.js";
2224 import { logger } from "../util/logger.js";
2325 import { getVersion } from "../version.js";
2426
@@ -350,7 +352,14 @@
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 sessionAttributes = this.getStandardAttributes({
358+ is_headless: isHeadlessMode().toString(),
359+ is_github_actions: isGitHubActions().toString(),
360+ });
361+
362+ this.sessionCounter.add(1, sessionAttributes);
354363 this.recordStartupTime(Date.now() - this.startTime);
355364 }
356365
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+}