Allow telemetry exporters to GCP to utilize user's login credentials, if requested (#13778)

This commit is contained in:
Marat Boshernitsan
2025-12-02 21:27:37 -08:00
committed by GitHub
parent 92e95ed806
commit b9b3b8050d
26 changed files with 994 additions and 428 deletions

View File

@@ -20,15 +20,15 @@ const USER_SETTINGS_DIR = join(
const USER_SETTINGS_PATH = join(USER_SETTINGS_DIR, 'settings.json');
const WORKSPACE_SETTINGS_PATH = join(projectRoot, GEMINI_DIR, 'settings.json');
let settingsTarget = undefined;
let telemetrySettings = undefined;
function loadSettingsValue(filePath) {
function loadSettings(filePath) {
try {
if (existsSync(filePath)) {
const content = readFileSync(filePath, 'utf-8');
const jsonContent = content.replace(/\/\/[^\n]*/g, '');
const settings = JSON.parse(jsonContent);
return settings.telemetry?.target;
return settings.telemetry;
}
} catch (e) {
console.warn(
@@ -38,13 +38,13 @@ function loadSettingsValue(filePath) {
return undefined;
}
settingsTarget = loadSettingsValue(WORKSPACE_SETTINGS_PATH);
telemetrySettings = loadSettings(WORKSPACE_SETTINGS_PATH);
if (!settingsTarget) {
settingsTarget = loadSettingsValue(USER_SETTINGS_PATH);
if (!telemetrySettings) {
telemetrySettings = loadSettings(USER_SETTINGS_PATH);
}
let target = settingsTarget || 'local';
let target = telemetrySettings?.target || 'local';
const allowedTargets = ['local', 'gcp', 'genkit'];
const targetArg = process.argv.find((arg) => arg.startsWith('--target='));
@@ -55,13 +55,15 @@ if (targetArg) {
console.log(`⚙️ Using command-line target: ${target}`);
} else {
console.error(
`🛑 Error: Invalid target '${potentialTarget}'. Allowed targets are: ${allowedTargets.join(', ')}.`,
`🛑 Error: Invalid target '${potentialTarget}'. Allowed targets are: ${allowedTargets.join(
', ',
)}.`,
);
process.exit(1);
}
} else if (settingsTarget) {
} else if (telemetrySettings?.target) {
console.log(
`⚙️ Using telemetry target from settings.json: ${settingsTarget}`,
`⚙️ Using telemetry target from settings.json: ${telemetrySettings.target}`,
);
}
@@ -75,7 +77,13 @@ const scriptPath = join(projectRoot, 'scripts', targetScripts[target]);
try {
console.log(`🚀 Running telemetry script for target: ${target}.`);
execSync(`node ${scriptPath}`, { stdio: 'inherit', cwd: projectRoot });
const env = { ...process.env };
execSync(`node ${scriptPath}`, {
stdio: 'inherit',
cwd: projectRoot,
env,
});
} catch (error) {
console.error(`🛑 Failed to run telemetry script for target: ${target}`);
console.error(error);

View File

@@ -7,7 +7,7 @@
*/
import path from 'node:path';
import fs from 'node:fs';
import * as fs from 'node:fs';
import { spawn, execSync } from 'node:child_process';
import {
OTEL_DIR,
@@ -132,11 +132,13 @@ async function main() {
fs.writeFileSync(OTEL_CONFIG_FILE, getOtelConfigContent(projectId));
console.log(`📄 Wrote OTEL collector config to ${OTEL_CONFIG_FILE}`);
const spawnEnv = { ...process.env };
console.log(`🚀 Starting OTEL collector for GCP... Logs: ${OTEL_LOG_FILE}`);
collectorLogFd = fs.openSync(OTEL_LOG_FILE, 'a');
collectorProcess = spawn(otelcolPath, ['--config', OTEL_CONFIG_FILE], {
stdio: ['ignore', collectorLogFd, collectorLogFd],
env: { ...process.env },
env: spawnEnv,
});
console.log(

View File

@@ -4,9 +4,17 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi } from 'vitest';
import { main as generateDocs } from '../generate-settings-doc.ts';
vi.mock('fs', () => ({
readFileSync: vi.fn().mockReturnValue(''),
createWriteStream: vi.fn(() => ({
write: vi.fn(),
on: vi.fn(),
})),
}));
describe('generate-settings-doc', () => {
it('keeps documentation in sync in check mode', async () => {
const previousExitCode = process.exitCode;

View File

@@ -4,12 +4,21 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { main as generateSchema } from '../generate-settings-schema.ts';
vi.mock('fs', () => ({
readFileSync: vi.fn().mockReturnValue(''),
writeFileSync: vi.fn(),
createWriteStream: vi.fn(() => ({
write: vi.fn(),
on: vi.fn(),
})),
}));
describe('generate-settings-schema', () => {
it('keeps schema in sync in check mode', async () => {
const previousExitCode = process.exitCode;

View File

@@ -0,0 +1,56 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
const mockSpawn = vi.fn(() => ({ on: vi.fn(), pid: 123 }));
vi.mock('node:child_process', () => ({
spawn: mockSpawn,
execSync: vi.fn(),
}));
vi.mock('node:fs', () => ({
readFileSync: vi.fn(),
writeFileSync: vi.fn(),
openSync: vi.fn(() => 1),
unlinkSync: vi.fn(),
mkdirSync: vi.fn(),
}));
vi.mock('../telemetry_utils.js', () => ({
ensureBinary: vi.fn(() => Promise.resolve('/fake/path/to/otelcol-contrib')),
waitForPort: vi.fn(() => Promise.resolve()),
manageTelemetrySettings: vi.fn(),
registerCleanup: vi.fn(),
fileExists: vi.fn(() => true), // Assume all files exist for simplicity
OTEL_DIR: '/tmp/otel',
BIN_DIR: '/tmp/bin',
}));
describe('telemetry_gcp.js', () => {
beforeEach(() => {
vi.resetModules(); // This is key to re-run the script
vi.clearAllMocks();
process.env.OTLP_GOOGLE_CLOUD_PROJECT = 'test-project';
// Clear the env var before each test
delete process.env.GEMINI_CLI_CREDENTIALS_PATH;
});
afterEach(() => {
delete process.env.OTLP_GOOGLE_CLOUD_PROJECT;
});
it('should not set GOOGLE_APPLICATION_CREDENTIALS when env var is not set', async () => {
await import('../telemetry_gcp.js');
expect(mockSpawn).toHaveBeenCalled();
const spawnOptions = mockSpawn.mock.calls[0][2];
expect(spawnOptions?.env).not.toHaveProperty(
'GOOGLE_APPLICATION_CREDENTIALS',
);
});
});