Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: remove shadow optimization test and related code (#12066)

Valery Meleshkin4847a50f5897
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 8fd46871ce..743a236c5c 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -388,10 +388,6 @@ export const env = createEnv({
.enum(["true", "false"])
.default("false"),
- LANGFUSE_ENABLE_QUERY_OPTIMIZATION_SHADOW_TEST: z
- .enum(["true", "false"])
- .default("false"),
-
// Blocked users for chat completion API (userId:reason format)
LANGFUSE_BLOCKED_USERIDS_CHATCOMPLETION: z
.string()
@@ -746,8 +742,6 @@ export const env = createEnv({
process.env.LANGFUSE_ENABLE_EVENTS_TABLE_FLAGS,
LANGFUSE_ENABLE_EVENTS_TABLE_V2_APIS:
process.env.LANGFUSE_ENABLE_EVENTS_TABLE_V2_APIS,
- LANGFUSE_ENABLE_QUERY_OPTIMIZATION_SHADOW_TEST:
- process.env.LANGFUSE_ENABLE_QUERY_OPTIMIZATION_SHADOW_TEST,
LANGFUSE_BLOCKED_USERIDS_CHATCOMPLETION:
process.env.LANGFUSE_BLOCKED_USERIDS_CHATCOMPLETION,
},
diff --git a/web/src/features/query/server/queryExecutor.ts b/web/src/features/query/server/queryExecutor.ts
index 3a2708caab..7e5b9b36ed 100644
--- a/web/src/features/query/server/queryExecutor.ts
+++ b/web/src/features/query/server/queryExecutor.ts
@@ -1,8 +1,4 @@
-import {
- queryClickhouse,
- measureAndReturn,
- logger,
-} from "@langfuse/shared/src/server";
+import { queryClickhouse, measureAndReturn } from "@langfuse/shared/src/server";
import { QueryBuilder } from "@/src/features/query/server/queryBuilder";
import { type QueryType, type ViewVersion } from "@/src/features/query/types";
import { getViewDeclaration } from "@/src/features/query/dataModel";
@@ -132,60 +128,6 @@ export function validateQuery(
return { valid: true };
}
-/**
- * Compares two query results for equivalence.
- * Returns true if results match, false otherwise.
- * Optimized for performance - avoids expensive JSON.stringify on large result sets.
- */
-function compareQueryResults(
- result1: Array<Record<string, unknown>>,
- result2: Array<Record<string, unknown>>,
-): boolean {
- if (result1.length !== result2.length) {
- return false;
- }
-
- if (result1.length === 0) {
- return true;
- }
-
- // Get all keys from first row of each result
- const keys1 = Object.keys(result1[0]);
- const keys2 = Object.keys(result2[0]);
-
- // Quick check: same number of columns
- if (keys1.length !== keys2.length) {
- return false;
- }
-
- // Sort keys once for performance (avoid sorting on every row)
- const sortedKeys = keys1.sort();
-
- // Create a simple hash for each row to enable fast comparison
- const createRowHash = (row: Record<string, unknown>): string => {
- // Use a simple concatenation of key-value pairs (sorted by key for consistency)
- return sortedKeys.map((k) => `${k}:${row[k]}`).join("|");
- };
-
- // Build sets of row hashes
- const hashes1 = new Set(result1.map(createRowHash));
- const hashes2 = new Set(result2.map(createRowHash));
-
- // Compare sets
- if (hashes1.size !== hashes2.size) {
- return false;
- }
-
- // Check if all hashes from result1 exist in result2
- for (const hash of hashes1) {
- if (!hashes2.has(hash)) {
- return false;
- }
- }
-
- return true;
-}
-
/**
* Execute a query using the QueryBuilder.
*
@@ -208,158 +150,67 @@ export async function executeQuery(
query.chartConfig;
const queryBuilder = new QueryBuilder(chartConfig, version);
- // Build the primary query (with or without optimization based on flag)
+ // Build the query (with or without optimization based on flag)
const { query: compiledQuery, parameters } = await queryBuilder.build(
query,
projectId,
enableSingleLevelOptimization,
);
- // Shadow testing: Build list of queries to execute in parallel
- const queriesToExecute: Array<{
- type: "regular" | "shadow";
- query: string;
- params: Record<string, unknown>;
- }> = [
- {
- type: "regular",
- query: compiledQuery,
- params: parameters,
- },
- ];
-
- // Add shadow test query if optimization is OFF and shadow testing is enabled
- const shadowTestEnabled =
- env.LANGFUSE_ENABLE_QUERY_OPTIMIZATION_SHADOW_TEST === "true";
-
- if (!enableSingleLevelOptimization && shadowTestEnabled) {
- const { query: optimizedQuery, parameters: optimizedParams } =
- await queryBuilder.build(query, projectId, true);
-
- // Only run shadow test if optimization actually changed the query
- if (optimizedQuery !== compiledQuery) {
- queriesToExecute.push({
- type: "shadow",
- query: optimizedQuery,
- params: optimizedParams,
- });
- }
- }
-
// Check if the query contains trace table references
const usesTraceTable = compiledQuery.includes("traces");
- // Execute all queries in parallel
- const results = await Promise.all(
- queriesToExecute.map(
- async (
- queryToExecute,
- ): Promise<Array<Record<string, unknown>> | null> => {
- try {
- if (!usesTraceTable) {
- // No trace table placeholders, execute normally
- return await queryClickhouse<Record<string, unknown>>({
- query: queryToExecute.query,
- params: queryToExecute.params,
- clickhouseConfigs: {
- clickhouse_settings: {
- date_time_output_format: "iso",
- max_bytes_before_external_group_by: String(
- env.CLICKHOUSE_MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY,
- ),
- },
- },
- tags: {
- feature:
- queryToExecute.type === "shadow"
- ? "custom-queries-shadow-test"
- : "custom-queries",
- type: query.view,
- kind: "analytic",
- projectId,
- },
- });
- } else {
- // Use measureAndReturn for trace table queries
- return await measureAndReturn({
- operationName: "executeQuery",
- projectId,
- input: {
- query: queryToExecute.query,
- params: queryToExecute.params,
- fromTimestamp: query.fromTimestamp,
- tags: {
- feature:
- queryToExecute.type === "shadow"
- ? "custom-queries-shadow-test"
- : "custom-queries",
- type: query.view,
- kind: "analytic",
- projectId,
- operation_name: "executeQuery",
- },
- },
- fn: async (input) => {
- return queryClickhouse<Record<string, unknown>>({
- query: input.query,
- params: input.params,
- clickhouseConfigs: {
- clickhouse_settings: {
- date_time_output_format: "iso",
- max_bytes_before_external_group_by: String(
- env.CLICKHOUSE_MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY,
- ),
- },
- },
- tags: input.tags,
- });
- },
- });
- }
- } catch (error) {
- // Only log shadow test errors
- if (queryToExecute.type === "shadow") {
- logger.warn("Shadow test query failed", {
- view: query.view,
- error: error instanceof Error ? error.message : String(error),
- });
- return null;
- }
- // Re-throw errors from regular query
- throw error;
- }
- },
- ),
- );
-
- // Extract results - regularResult should never be null (would have thrown)
- const regularResult = results[0]!;
- const shadowResult = results[1] ?? null;
-
- // Compare shadow test results if available
- if (shadowResult !== null) {
- const resultsMatch = compareQueryResults(regularResult, shadowResult);
+ const tags = {
+ feature: "custom-queries",
+ type: query.view,
+ kind: "analytic",
+ projectId,
+ };
- if (resultsMatch) {
- // Log success (can be aggregated to track optimization coverage)
- logger.info("Shadow test: Optimization query matches", {
- view: query.view,
- version,
- });
- } else {
- // Log discrepancy with full details
- logger.error("Shadow test: Optimization query MISMATCH", {
- view: query.view,
- version,
- projectId,
- query: query,
- regularResultCount: regularResult.length,
- optimizedResultCount: shadowResult.length,
- regularResult: regularResult,
- optimizedResult: shadowResult,
- });
- }
+ if (!usesTraceTable) {
+ // No trace table placeholders, execute normally
+ return queryClickhouse<Record<string, unknown>>({
+ query: compiledQuery,
+ params: parameters,
+ clickhouseConfigs: {
+ clickhouse_settings: {
+ date_time_output_format: "iso",
+ max_bytes_before_external_group_by: String(
+ env.CLICKHOUSE_MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY,
+ ),
+ },
+ },
+ tags,
+ });
}
- return regularResult;
+ // Use measureAndReturn for trace table queries
+ return measureAndReturn({
+ operationName: "executeQuery",
+ projectId,
+ input: {
+ query: compiledQuery,
+ params: parameters,
+ fromTimestamp: query.fromTimestamp,
+ tags: {
+ ...tags,
+ operation_name: "executeQuery",
+ },
+ },
+ fn: async (input) => {
+ return queryClickhouse<Record<string, unknown>>({
+ query: input.query,
+ params: input.params,
+ clickhouseConfigs: {
+ clickhouse_settings: {
+ date_time_output_format: "iso",
+ max_bytes_before_external_group_by: String(
+ env.CLICKHOUSE_MAX_BYTES_BEFORE_EXTERNAL_GROUP_BY,
+ ),
+ },
+ },
+ tags: input.tags,
+ });
+ },
+ });
}