Fix duplicate startSession logs and duplicate logging events over the wire (#1357)

This commit is contained in:
anj-s 2025-06-23 17:05:42 -07:00 committed by GitHub
parent 335802f4dd
commit 3012684469
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 13 deletions

View File

@ -85,12 +85,15 @@ export class ClearcutLogger {
if (this.config?.getDebugMode()) { if (this.config?.getDebugMode()) {
console.log('Flushing log events to Clearcut.'); console.log('Flushing log events to Clearcut.');
} }
const eventsToSend = [...this.events];
this.events.length = 0;
return new Promise<Buffer>((resolve, reject) => { return new Promise<Buffer>((resolve, reject) => {
const request = [ const request = [
{ {
log_source_name: 'CONCORD', log_source_name: 'CONCORD',
request_time_ms: Date.now(), request_time_ms: Date.now(),
log_event: this.events, log_event: eventsToSend,
}, },
]; ];
const body = JSON.stringify(request); const body = JSON.stringify(request);
@ -114,12 +117,13 @@ export class ClearcutLogger {
if (this.config?.getDebugMode()) { if (this.config?.getDebugMode()) {
console.log('Clearcut POST request error: ', e); console.log('Clearcut POST request error: ', e);
} }
// Add the events back to the front of the queue to be retried.
this.events.unshift(...eventsToSend);
reject(e); reject(e);
}); });
req.end(body); req.end(body);
}).then((buf: Buffer) => { }).then((buf: Buffer) => {
try { try {
this.events.length = 0;
this.last_flush_time = Date.now(); this.last_flush_time = Date.now();
return this.decodeLogResponse(buf) || {}; return this.decodeLogResponse(buf) || {};
} catch (error: unknown) { } catch (error: unknown) {

View File

@ -28,8 +28,6 @@ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { Config } from '../config/config.js'; import { Config } from '../config/config.js';
import { SERVICE_NAME } from './constants.js'; import { SERVICE_NAME } from './constants.js';
import { initializeMetrics } from './metrics.js'; import { initializeMetrics } from './metrics.js';
import { logCliConfiguration } from './loggers.js';
import { StartSessionEvent } from './types.js';
import { ClearcutLogger } from './clearcut-logger/clearcut-logger.js'; import { ClearcutLogger } from './clearcut-logger/clearcut-logger.js';
// For troubleshooting, set the log level to DiagLogLevel.DEBUG // For troubleshooting, set the log level to DiagLogLevel.DEBUG
@ -115,7 +113,6 @@ export function initializeTelemetry(config: Config): void {
console.log('OpenTelemetry SDK started successfully.'); console.log('OpenTelemetry SDK started successfully.');
telemetryInitialized = true; telemetryInitialized = true;
initializeMetrics(config); initializeMetrics(config);
logCliConfiguration(config, new StartSessionEvent(config));
} catch (error) { } catch (error) {
console.error('Error starting OpenTelemetry SDK:', error); console.error('Error starting OpenTelemetry SDK:', error);
} }

View File

@ -12,12 +12,9 @@ import {
} from './sdk.js'; } from './sdk.js';
import { Config } from '../config/config.js'; import { Config } from '../config/config.js';
import { NodeSDK } from '@opentelemetry/sdk-node'; import { NodeSDK } from '@opentelemetry/sdk-node';
import * as loggers from './loggers.js';
import { StartSessionEvent } from './types.js';
vi.mock('@opentelemetry/sdk-node'); vi.mock('@opentelemetry/sdk-node');
vi.mock('../config/config.js'); vi.mock('../config/config.js');
vi.mock('./loggers.js');
describe('telemetry', () => { describe('telemetry', () => {
let mockConfig: Config; let mockConfig: Config;
@ -38,8 +35,6 @@ describe('telemetry', () => {
'http://localhost:4317', 'http://localhost:4317',
); );
vi.spyOn(mockConfig, 'getSessionId').mockReturnValue('test-session-id'); vi.spyOn(mockConfig, 'getSessionId').mockReturnValue('test-session-id');
vi.spyOn(loggers, 'logCliConfiguration').mockImplementation(() => {});
mockNodeSdk = { mockNodeSdk = {
start: vi.fn(), start: vi.fn(),
shutdown: vi.fn().mockResolvedValue(undefined), shutdown: vi.fn().mockResolvedValue(undefined),
@ -56,11 +51,8 @@ describe('telemetry', () => {
it('should initialize the telemetry service', () => { it('should initialize the telemetry service', () => {
initializeTelemetry(mockConfig); initializeTelemetry(mockConfig);
const event = new StartSessionEvent(mockConfig);
expect(NodeSDK).toHaveBeenCalled(); expect(NodeSDK).toHaveBeenCalled();
expect(mockNodeSdk.start).toHaveBeenCalled(); expect(mockNodeSdk.start).toHaveBeenCalled();
expect(loggers.logCliConfiguration).toHaveBeenCalledWith(mockConfig, event);
}); });
it('should shutdown the telemetry service', async () => { it('should shutdown the telemetry service', async () => {