Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"jsonwebtoken": "^9.0.2",
"openapi-fetch": "^0.13.3",
"openapi-typescript-helpers": "^0.0.15",
"p-retry": "^4.6.2",
"set-cookie-parser": "^2.7.2",
"ts-case-convert": "^2.1.0"
},
Expand Down
83 changes: 67 additions & 16 deletions src/api-client/galileo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class GalileoApiClientParams {
public experimentId?: string = undefined;
public sessionId?: string = undefined;
public projectScoped: boolean = true;
public forceInit: boolean = true;
}

export class GalileoApiClient extends BaseClient {
Expand Down Expand Up @@ -174,9 +175,14 @@ export class GalileoApiClient extends BaseClient {
datasetId = defaultParams.datasetId,
experimentId = defaultParams.experimentId,
sessionId = defaultParams.sessionId,
projectScoped = defaultParams.projectScoped
projectScoped = defaultParams.projectScoped,
forceInit = defaultParams.forceInit
} = params;

if (this._isClientInitialized(params) && !forceInit) {
return;
}

this.projectType = projectType;

if (runId) {
Expand Down Expand Up @@ -331,6 +337,51 @@ export class GalileoApiClient extends BaseClient {
}
}

/**
* Utility function to ensure the Galileo client is initialized.
* Checks if the client is already initialized before calling init() to avoid redundant initialization.
* A client is considered initialized if it has:
* - A projectId (non-empty string) OR is not projectScoped
* - Either logStreamId or experimentId set (non-empty string)
*
* @param client - The GalileoApiClient instance to initialize
* @param params - Parameters for client initialization
* @returns Promise that resolves when client is initialized
*/
private _isClientInitialized(params: {
projectName?: string;
projectId?: string;
logStreamName?: string;
logStreamId?: string;
experimentId?: string;
sessionId?: string;
}): boolean {
// Check if client is already initialized
// Client is initialized if:
// 1. It has a projectId (non-empty string) OR is not projectScoped
// 2. It has either logStreamId or experimentId set (non-empty string)
// Note: We check client properties first, then fall back to params if needed
const hasProject =
(this.projectId && this.projectId.trim() !== '') ||
!this.projectScoped ||
(params.projectId && params.projectId.trim() !== '');

const hasLogStreamOrExperiment =
(this.logStreamId && this.logStreamId.trim() !== '') ||
(this.experimentId && this.experimentId.trim() !== '') ||
(params.logStreamId && params.logStreamId.trim() !== '') ||
(params.experimentId && params.experimentId.trim() !== '');

// If both conditions are met, client is likely initialized
// However, if params provide IDs that differ from client's current state, we should re-init
const needsReinit =
(params.projectId && params.projectId !== this.projectId) ||
(params.logStreamId && params.logStreamId !== this.logStreamId) ||
(params.experimentId && params.experimentId !== this.experimentId);

return Boolean(hasProject && hasLogStreamOrExperiment && !needsReinit);
}

static getTimestampRecord(): Date {
const dateNow = Date.now();
const timeDifference = dateNow - this.timestampRecord;
Expand Down Expand Up @@ -790,20 +841,20 @@ export class GalileoApiClient extends BaseClient {
limit: number = 100
): Promise<ListDatasetProjectsResponse> {
this.ensureService(this.datasetService);
return this.datasetService!.listDatasetProjects(datasetId, limit);
return this.datasetService.listDatasetProjects(datasetId, limit);
}

// Trace methods - delegate to TraceService
public async ingestTracesLegacy(traces: Trace[]) {
this.ensureService(this.traceService);
return this.traceService!.ingestTracesLegacy(traces);
return this.traceService.ingestTracesLegacy(traces);
}

public async ingestTraces(
options: LogTracesIngestRequest
): Promise<LogTracesIngestResponse> {
this.ensureService(this.traceService);
return this.traceService!.ingestTraces(options);
return this.traceService.ingestTraces(options);
}

public async createSessionLegacy({
Expand All @@ -816,7 +867,7 @@ export class GalileoApiClient extends BaseClient {
externalId?: string;
}): Promise<SessionCreateResponse> {
this.ensureService(this.traceService);
return this.traceService!.createSessionLegacy({
return this.traceService.createSessionLegacy({
name,
previousSessionId,
externalId
Expand All @@ -842,7 +893,7 @@ export class GalileoApiClient extends BaseClient {
request: LogRecordsQueryRequest
): Promise<LogRecordsQueryResponse> {
this.ensureService(this.traceService);
return this.traceService!.searchSessions(request);
return this.traceService.searchSessions(request);
}

/**
Expand All @@ -855,7 +906,7 @@ export class GalileoApiClient extends BaseClient {
sessionId: string
): Promise<ExtendedSessionRecordWithChildren> {
this.ensureService(this.traceService);
return this.traceService!.getSession(sessionId);
return this.traceService.getSession(sessionId);
}

/**
Expand All @@ -873,7 +924,7 @@ export class GalileoApiClient extends BaseClient {
options: LogRecordsDeleteRequest
): Promise<LogRecordsDeleteResponse> {
this.ensureService(this.traceService);
return this.traceService!.deleteSessions(options);
return this.traceService.deleteSessions(options);
}

/**
Expand All @@ -886,7 +937,7 @@ export class GalileoApiClient extends BaseClient {
traceId: string
): Promise<ExtendedTraceRecordWithChildren> {
this.ensureService(this.traceService);
return this.traceService!.getTrace(traceId);
return this.traceService.getTrace(traceId);
}

/**
Expand All @@ -910,7 +961,7 @@ export class GalileoApiClient extends BaseClient {
options: LogTraceUpdateRequest
): Promise<LogTraceUpdateResponse> {
this.ensureService(this.traceService);
return this.traceService!.updateTrace(options);
return this.traceService.updateTrace(options);
}

/**
Expand All @@ -928,7 +979,7 @@ export class GalileoApiClient extends BaseClient {
options: LogRecordsDeleteRequest
): Promise<LogRecordsDeleteResponse> {
this.ensureService(this.traceService);
return this.traceService!.deleteTraces(options);
return this.traceService.deleteTraces(options);
}

/**
Expand All @@ -950,7 +1001,7 @@ export class GalileoApiClient extends BaseClient {
options: LogSpansIngestRequest
): Promise<LogSpansIngestResponse> {
this.ensureService(this.traceService);
return this.traceService!.ingestSpans(options);
return this.traceService.ingestSpans(options);
}

/**
Expand All @@ -961,7 +1012,7 @@ export class GalileoApiClient extends BaseClient {
*/
public async getSpan(spanId: string): Promise<ExtendedSpanRecord> {
this.ensureService(this.traceService);
return this.traceService!.getSpan(spanId);
return this.traceService.getSpan(spanId);
}

/**
Expand All @@ -985,7 +1036,7 @@ export class GalileoApiClient extends BaseClient {
options: LogSpanUpdateRequest
): Promise<LogSpanUpdateResponse> {
this.ensureService(this.traceService);
return this.traceService!.updateSpan(options);
return this.traceService.updateSpan(options);
}

/**
Expand All @@ -1003,7 +1054,7 @@ export class GalileoApiClient extends BaseClient {
options: LogRecordsDeleteRequest
): Promise<LogRecordsDeleteResponse> {
this.ensureService(this.traceService);
return this.traceService!.deleteSpans(options);
return this.traceService.deleteSpans(options);
}

/**
Expand All @@ -1021,7 +1072,7 @@ export class GalileoApiClient extends BaseClient {
options: LogRecordsQueryCountRequest
): Promise<LogRecordsQueryCountResponse> {
this.ensureService(this.traceService);
return this.traceService!.countTraces(options);
return this.traceService.countTraces(options);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/api-client/services/trace-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class TraceService extends BaseClient {
if (!this.projectId) {
throw new Error('Project not initialized');
}
options.logStreamId ??= this.logStreamId;

this.validateLogstreamAndExperiment<typeof options>(options);
const request = this.convertToSnakeCase<
Expand Down Expand Up @@ -174,6 +175,8 @@ export class TraceService extends BaseClient {
}

this.validateLogstreamAndExperiment<typeof options>(options);
options.sessionId ??= this.sessionId;

const request = this.convertToSnakeCase<
typeof options,
LogTracesIngestRequestOpenAPI
Expand Down
Loading
Loading