🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
Revert "perf: race byId queries for traces and observations (#6555)" (#6612)
Steffen Schmitza2cbcbc58580
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.tsindex b90e4496f3..38cecd0d0c 100644--- a/packages/shared/src/server/repositories/observations.ts+++ b/packages/shared/src/server/repositories/observations.ts@@ -34,7 +34,6 @@ import {} from "./constants";import { env } from "../../env";import { ClickHouseClientConfigOptions } from "@clickhouse/client";-import { recordIncrement } from "../instrumentation";/*** Checks if observation exists in clickhouse.@@ -349,132 +348,65 @@ export const getObservationsById = async (return records.map(convertObservation);};-/**- * Retrieves an observation record by its ID and associated project ID, with optional filtering by startTime.- * If no startTime filters are provided, runs two queries in parallel:- * 1. One with a 7-day fromStartTime filter (typically faster)- * 2. One without any startTime filters (complete but slower)- * Returns the first non-empty result.- */const getObservationByIdInternal = async (id: string,projectId: string,fetchWithInputOutput: boolean = false,startTime?: Date,- fromStartTime?: Date,) => {- const getQuery = (startTime?: Date, fromStartTime?: Date) => {- return `- SELECT- id,- trace_id,- project_id,- environment,- type,- parent_observation_id,- start_time,- end_time,- name,- metadata,- level,- status_message,- version,- ${fetchWithInputOutput ? "input, output," : ""}- provided_model_name,- internal_model_id,- model_parameters,- provided_usage_details,- usage_details,- provided_cost_details,- cost_details,- total_cost,- completion_start_time,- prompt_id,- prompt_name,- prompt_version,- created_at,- updated_at,- event_ts- FROM observations- WHERE id = {id: String}- AND project_id = {projectId: String}- ${startTime ? `AND toDate(start_time) = toDate({startTime: DateTime64(3)})` : ""}- ${fromStartTime ? `AND start_time >= {fromStartTime: DateTime64(3)}` : ""}- ORDER BY event_ts desc- LIMIT 1 by id, project_id`;- };-- const hasStartTimeFilter = Boolean(startTime) || Boolean(fromStartTime);-- const tags = {- feature: "tracing",- type: "observation",- kind: "byId",- projectId,- };-- // If no fromStartTime or startTime is provided, use a lookback for a faster query- const queryFromStartTime = !hasStartTimeFilter- ? new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 1)- : fromStartTime;-- const queryWithStartTimePromise = queryClickhouse<ObservationRecordReadType>({- query: getQuery(startTime, queryFromStartTime),+ const query = `+ SELECT+ id,+ trace_id,+ project_id,+ environment,+ type,+ parent_observation_id,+ start_time,+ end_time,+ name,+ metadata,+ level,+ status_message,+ version,+ ${fetchWithInputOutput ? "input, output," : ""}+ provided_model_name,+ internal_model_id,+ model_parameters,+ provided_usage_details,+ usage_details,+ provided_cost_details,+ cost_details,+ total_cost,+ completion_start_time,+ prompt_id,+ prompt_name,+ prompt_version,+ created_at,+ updated_at,+ event_ts+ FROM observations+ WHERE id = {id: String}+ AND project_id = {projectId: String}+ ${startTime ? `AND toDate(start_time) = toDate({startTime: DateTime64(3)})` : ""}+ ORDER BY event_ts desc+ LIMIT 1 by id, project_id`;+ return await queryClickhouse<ObservationRecordReadType>({+ query,params: {id,projectId,...(startTime? { startTime: convertDateToClickhouseDateTime(startTime) }: {}),- ...(queryFromStartTime- ? { fromStartTime: convertDateToClickhouseDateTime(queryFromStartTime) }- : {}),},- tags,- });-- const queryWithoutStartTimePromise = !hasStartTimeFilter- ? queryClickhouse<ObservationRecordReadType>({- query: getQuery(undefined, undefined),- params: {- id,- projectId,- },- tags,- })- : null;-- const promises = [- queryWithStartTimePromise,- queryWithoutStartTimePromise,- ].filter((elem) => elem !== null);-- // Get the first result- const result = await Promise.race(promises);-- // Check if faster result has a value and if yes, return it- if (result?.length && result.length > 0) {- recordIncrement("langfuse.by_id.hit", 1, {- kind: "race",+ tags: {+ feature: "tracing",type: "observation",- has_time_filter: `${hasStartTimeFilter}`,- });- return result;- }-- // If not, check all results for a value and return the first non-null one- const allResults = await Promise.all(promises);- for (const result of allResults) {- if (result && result.length > 0) {- recordIncrement("langfuse.by_id.hit", 1, {- kind: "all_settled",- type: "observation",- has_time_filter: `${hasStartTimeFilter}`,- });- return result;- }- }- return [];+ kind: "byId",+ projectId,+ },+ });};export type ObservationTableQuery = {diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.tsindex 1c70811430..0b1496a7a6 100644--- a/packages/shared/src/server/repositories/traces.ts+++ b/packages/shared/src/server/repositories/traces.ts@@ -27,7 +27,6 @@ import {} from "./constants";import { env } from "../../env";import { ClickHouseClientConfigOptions } from "@clickhouse/client";-import { recordIncrement } from "../instrumentation";/*** Checks if trace exists in clickhouse.@@ -321,90 +320,40 @@ export const getTraceById = async ({timestamp?: Date;fromTimestamp?: Date;}) => {- const getQuery = (timestamp?: Date, fromTimestamp?: Date) => {- return `- SELECT *- FROM traces- WHERE id = {traceId: String}- AND project_id = {projectId: String}- ${timestamp ? `AND toDate(timestamp) = toDate({timestamp: DateTime64(3)})` : ""}- ${fromTimestamp ? `AND timestamp >= {fromTimestamp: DateTime64(3)}` : ""}- ORDER BY event_ts DESC- LIMIT 1- `;- };-- const hasTimestampFilter = Boolean(timestamp) || Boolean(fromTimestamp);-- const tags = {- feature: "tracing",- type: "trace",- kind: "byId",- projectId,- };-- // If no fromTimestamp or timestamp is provided, use a lookback for a faster query- const queryFromTimestamp = !hasTimestampFilter- ? new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 1)- : fromTimestamp;+ const query = `+ SELECT *+ FROM traces+ WHERE id = {traceId: String}+ AND project_id = {projectId: String}+ ${timestamp ? `AND toDate(timestamp) = toDate({timestamp: DateTime64(3)})` : ""}+ ${fromTimestamp ? `AND timestamp >= {fromTimestamp: DateTime64(3)}` : ""}+ ORDER BY event_ts DESC+ LIMIT 1+ `;- const queryWithTimestampPromise = queryClickhouse<TraceRecordReadType>({- query: getQuery(timestamp, queryFromTimestamp),+ const records = await queryClickhouse<TraceRecordReadType>({+ query,params: {traceId,projectId,...(timestamp? { timestamp: convertDateToClickhouseDateTime(timestamp) }: {}),- ...(queryFromTimestamp- ? { fromTimestamp: convertDateToClickhouseDateTime(queryFromTimestamp) }+ ...(fromTimestamp+ ? { fromTimestamp: convertDateToClickhouseDateTime(fromTimestamp) }: {}),},- tags,+ tags: {+ feature: "tracing",+ type: "trace",+ kind: "byId",+ projectId,+ },});- const queryWithoutTimestampPromise = !hasTimestampFilter- ? queryClickhouse<TraceRecordReadType>({- query: getQuery(undefined, undefined),- params: {- traceId,- projectId,- },- tags,- })- : null;-- const promises = [- queryWithTimestampPromise,- queryWithoutTimestampPromise,- ].filter((elem) => elem !== null);-- // Get the first result- const result = await Promise.race(promises);-- // Check if faster result has a value and if yes, return it- if (result?.length && result.length > 0) {- recordIncrement("langfuse.by_id.hit", 1, {- kind: "race",- type: "trace",- has_time_filter: `${hasTimestampFilter}`,- });- return convertClickhouseToDomain(result[0]);- }+ const res = records.map(convertClickhouseToDomain);- // If not, check all results for a value and return the first non-null one- const allResults = await Promise.all(promises);- for (const result of allResults) {- if (result && result.length > 0) {- recordIncrement("langfuse.by_id.hit", 1, {- kind: "all_settled",- type: "trace",- has_time_filter: `${hasTimestampFilter}`,- });- return convertClickhouseToDomain(result[0]);- }- }- return undefined;+ return res.shift();};export const getTracesGroupedByName = async (