refactor(telemetry): Improve previous PR that allows telemetry to use the CLI auth and add testing (#14589)

This commit is contained in:
Marat Boshernitsan
2025-12-08 11:20:13 -08:00
committed by GitHub
parent 8651c53923
commit 4322137cee
6 changed files with 164 additions and 12 deletions
+34 -3
View File
@@ -19,7 +19,7 @@ import { OTLPLogExporter as OTLPLogExporterHttp } from '@opentelemetry/exporter-
import { OTLPMetricExporter as OTLPMetricExporterHttp } from '@opentelemetry/exporter-metrics-otlp-http';
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-node';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { GoogleAuth } from 'google-auth-library';
import { GoogleAuth, type JWTInput } from 'google-auth-library';
import {
GcpTraceExporter,
GcpLogExporter,
@@ -327,8 +327,7 @@ describe('Telemetry SDK', () => {
await vi.waitFor(() => {
// Check if debugLogger was called, which indicates the listener ran
expect(debugLogger.log).toHaveBeenCalledWith(
'Telemetry reinit with credentials: ',
mockCredentials,
'Telemetry reinit with credentials.',
);
// Should use GCP exporters now with the project ID
@@ -370,4 +369,36 @@ describe('Telemetry SDK', () => {
);
expect(NodeSDK.prototype.start).not.toHaveBeenCalled();
});
it('should log error when re-initializing with different credentials', async () => {
const creds1 = { client_email: 'user1@example.com' };
const creds2 = { client_email: 'user2@example.com' };
// 1. Initialize with first account
await initializeTelemetry(mockConfig, creds1 as JWTInput);
// 2. Attempt to initialize with second account
await initializeTelemetry(mockConfig, creds2 as JWTInput);
// 3. Verify error log
expect(debugLogger.error).toHaveBeenCalledWith(
expect.stringContaining(
'Telemetry credentials have changed (from user1@example.com to user2@example.com)',
),
);
});
it('should NOT log error when re-initializing with SAME credentials', async () => {
const creds1 = { client_email: 'user1@example.com' };
// 1. Initialize with first account
await initializeTelemetry(mockConfig, creds1 as JWTInput);
// 2. Attempt to initialize with same account
await initializeTelemetry(mockConfig, creds1 as JWTInput);
// 3. Verify NO error log
expect(debugLogger.error).not.toHaveBeenCalledWith(
expect.stringContaining('Telemetry credentials have changed'),
);
});
});
+18 -5
View File
@@ -87,6 +87,7 @@ let callbackRegistered = false;
let authListener: ((newCredentials: JWTInput) => Promise<void>) | undefined =
undefined;
const telemetryBuffer: Array<() => void | Promise<void>> = [];
let activeTelemetryEmail: string | undefined;
export function isTelemetrySdkInitialized(): boolean {
return telemetryInitialized;
@@ -144,7 +145,20 @@ export async function initializeTelemetry(
config: Config,
credentials?: JWTInput,
): Promise<void> {
if (telemetryInitialized || !config.getTelemetryEnabled()) {
if (!config.getTelemetryEnabled()) {
return;
}
if (telemetryInitialized) {
if (
credentials?.client_email &&
activeTelemetryEmail &&
credentials.client_email !== activeTelemetryEmail
) {
const message = `Telemetry credentials have changed (from ${activeTelemetryEmail} to ${credentials.client_email}), but telemetry cannot be re-initialized in this process. Please restart the CLI to use the new account for telemetry.`;
debugLogger.error(message);
console.error(message);
}
return;
}
@@ -165,10 +179,7 @@ export async function initializeTelemetry(
callbackRegistered = true;
authListener = async (newCredentials: JWTInput) => {
if (config.getTelemetryEnabled() && config.getTelemetryUseCliAuth()) {
debugLogger.log(
'Telemetry reinit with credentials: ',
newCredentials,
);
debugLogger.log('Telemetry reinit with credentials.');
await initializeTelemetry(config, newCredentials);
}
};
@@ -294,6 +305,7 @@ export async function initializeTelemetry(
debugLogger.log('OpenTelemetry SDK started successfully.');
}
telemetryInitialized = true;
activeTelemetryEmail = credentials?.client_email;
initializeMetrics(config);
void flushTelemetryBuffer();
} catch (error) {
@@ -366,5 +378,6 @@ export async function shutdownTelemetry(
authListener = undefined;
}
callbackRegistered = false;
activeTelemetryEmail = undefined;
}
}