Skip to content

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

perf(dual-write): clamp min start time to past day and optimize trace sorting (#13172)

Steffen Schmitz864055f59351
diff --git a/worker/src/env.ts b/worker/src/env.ts
index e99b80f199..4c3dd138e8 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -277,6 +277,12 @@ const EnvSchema = z.object({
.optional()
.transform((s) => (s ? s.split(",").map((id) => id.trim()) : [])),
+ // Comma-separated list of project IDs to exclude from event propagation dual-write
+ LANGFUSE_EVENT_PROPAGATION_EXCLUDE_PROJECT_IDS: z
+ .string()
+ .optional()
+ .transform((s) => (s ? s.split(",").map((id) => id.trim()) : [])),
+
// Core data S3 upload - Langfuse Cloud
LANGFUSE_S3_CORE_DATA_EXPORT_IS_ENABLED: z
.enum(["true", "false"])
diff --git a/worker/src/features/eventPropagation/handleEventPropagationJob.ts b/worker/src/features/eventPropagation/handleEventPropagationJob.ts
index 93a04ecedf..aff61ed6ec 100644
--- a/worker/src/features/eventPropagation/handleEventPropagationJob.ts
+++ b/worker/src/features/eventPropagation/handleEventPropagationJob.ts
@@ -131,16 +131,23 @@ export const handleEventPropagationJob = async (
// for the same span, this may create duplicates in the new events table. Deduplicating in this query
// will significantly affect run-time. This may be an accepted degradation and we test the outcome
// to check the likelihood of this happening in practice.
+ const excludeProjectIds =
+ env.LANGFUSE_EVENT_PROPAGATION_EXCLUDE_PROJECT_IDS;
+ const excludeProjectIdsInClause =
+ excludeProjectIds.length > 0
+ ? `NOT IN (${excludeProjectIds.map((id) => `'${id}'`).join(",")})`
+ : "";
await commandClickhouse({
query: `
with batch_stats as (
select
groupUniqArray(project_id) as project_ids,
groupUniqArray(trace_id) as trace_ids,
- min(start_time) as min_start_time,
+ minIf(start_time, start_time > now() - interval 1 day) as min_start_time,
max(start_time) as max_start_time
from observations_batch_staging
where _partition_value = tuple('${partitionToProcess}')
+ ${excludeProjectIdsInClause ? `AND project_id ${excludeProjectIdsInClause}` : ""}
), experiment_traces_to_exclude as (
select distinct
project_id,
@@ -171,7 +178,7 @@ export const handleEventPropagationJob = async (
t.timestamp >= greatest((select min(min_start_time) - interval 1 day from batch_stats), now() - interval 7 day)
)
and t.timestamp <= (select max(max_start_time) + interval 1 day from batch_stats)
- order by t.event_ts desc
+ order by t.project_id, t.id, t.event_ts desc
limit 1 by t.project_id, t.id
)
@@ -291,6 +298,7 @@ export const handleEventPropagationJob = async (
excl.trace_id = obs.trace_id
)
WHERE obs._partition_value = tuple('${partitionToProcess}')
+ ${excludeProjectIdsInClause ? `AND obs.project_id ${excludeProjectIdsInClause}` : ""}
`,
tags: {
feature: "ingestion",