Skip to content

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

chore: avoid infinitely nested spans on scheduled queues (#9924)

Steffen Schmitze853eee30e2e
diff --git a/worker/src/ee/dataRetention/handleDataRetentionProcessingJob.ts b/worker/src/ee/dataRetention/handleDataRetentionProcessingJob.ts
index 1338bf665e..f6c6e31ea4 100644
--- a/worker/src/ee/dataRetention/handleDataRetentionProcessingJob.ts
+++ b/worker/src/ee/dataRetention/handleDataRetentionProcessingJob.ts
@@ -5,6 +5,7 @@ import {
logger,
getS3MediaStorageClient,
removeIngestionEventsFromS3AndDeleteClickhouseRefsForProject,
+ getCurrentSpan,
} from "@langfuse/shared/src/server";
import { Job } from "bullmq";
import { prisma } from "@langfuse/shared/src/db";
@@ -13,6 +14,13 @@ import { env } from "../../env";
export const handleDataRetentionProcessingJob = async (job: Job) => {
const { projectId, retention } = job.data.payload;
+ const span = getCurrentSpan();
+ if (span) {
+ span.setAttribute("messaging.bullmq.job.input.jobId", job.data.id);
+ span.setAttribute("messaging.bullmq.job.input.projectId", projectId);
+ span.setAttribute("messaging.bullmq.job.input.retentionId", retention);
+ }
+
const cutoffDate = new Date(Date.now() - retention * 24 * 60 * 60 * 1000);
// Delete media files if bucket is configured
diff --git a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
index 3e806f7966..c6d911fd4d 100644
--- a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
+++ b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
@@ -11,6 +11,7 @@ import {
getObservationsForBlobStorageExport,
getTracesForBlobStorageExport,
getScoresForBlobStorageExport,
+ getCurrentSpan,
} from "@langfuse/shared/src/server";
import {
BlobStorageIntegrationType,
@@ -181,6 +182,12 @@ export const handleBlobStorageIntegrationProjectJob = async (
) => {
const { projectId } = job.data.payload;
+ const span = getCurrentSpan();
+ if (span) {
+ span.setAttribute("messaging.bullmq.job.input.jobId", job.data.id);
+ span.setAttribute("messaging.bullmq.job.input.projectId", projectId);
+ }
+
logger.info(`Processing blob storage integration for project ${projectId}`);
const blobStorageIntegration = await prisma.blobStorageIntegration.findUnique(
diff --git a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts
index 3a43220ac8..05cf50acc0 100644
--- a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts
+++ b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts
@@ -7,6 +7,7 @@ import {
getTracesForPostHog,
getGenerationsForPostHog,
getScoresForPostHog,
+ getCurrentSpan,
} from "@langfuse/shared/src/server";
import { v5 } from "uuid";
import { decrypt } from "@langfuse/shared/encryption";
@@ -191,6 +192,13 @@ export const handlePostHogIntegrationProjectJob = async (
job: Job<TQueueJobTypes[QueueName.PostHogIntegrationProcessingQueue]>,
) => {
const projectId = job.data.payload.projectId;
+
+ const span = getCurrentSpan();
+ if (span) {
+ span.setAttribute("messaging.bullmq.job.input.jobId", job.data.id);
+ span.setAttribute("messaging.bullmq.job.input.projectId", projectId);
+ }
+
logger.info(`Processing PostHog integration for project ${projectId}`);
// Fetch PostHog integration information for project
diff --git a/worker/src/queues/blobStorageIntegrationQueue.ts b/worker/src/queues/blobStorageIntegrationQueue.ts
index 081ee74b33..59c805ea22 100644
--- a/worker/src/queues/blobStorageIntegrationQueue.ts
+++ b/worker/src/queues/blobStorageIntegrationQueue.ts
@@ -1,7 +1,12 @@
import { Processor } from "bullmq";
-import { QueueJobs, logger } from "@langfuse/shared/src/server";
+import {
+ QueueJobs,
+ logger,
+ instrumentAsync,
+} from "@langfuse/shared/src/server";
import { handleBlobStorageIntegrationSchedule } from "../features/blobstorage/handleBlobStorageIntegrationSchedule";
import { handleBlobStorageIntegrationProjectJob } from "../features/blobstorage/handleBlobStorageIntegrationProjectJob";
+import { SpanKind } from "@opentelemetry/api";
export const blobStorageIntegrationProcessor: Processor = async (job) => {
if (job.name === QueueJobs.BlobStorageIntegrationJob) {
@@ -19,14 +24,23 @@ export const blobStorageIntegrationProcessingProcessor: Processor = async (
job,
) => {
if (job.name === QueueJobs.BlobStorageIntegrationProcessingJob) {
- try {
- return await handleBlobStorageIntegrationProjectJob(job);
- } catch (error) {
- logger.error(
- "Error executing BlobStorageIntegrationProcessingJob",
- error,
- );
- throw error;
- }
+ return await instrumentAsync(
+ {
+ name: "process blob-storage-project",
+ startNewTrace: true,
+ spanKind: SpanKind.CONSUMER,
+ },
+ async () => {
+ try {
+ return await handleBlobStorageIntegrationProjectJob(job);
+ } catch (error) {
+ logger.error(
+ "Error executing BlobStorageIntegrationProcessingJob",
+ error,
+ );
+ throw error;
+ }
+ },
+ );
}
};
diff --git a/worker/src/queues/dataRetentionQueue.ts b/worker/src/queues/dataRetentionQueue.ts
index 8fa786d2fe..6d7627f343 100644
--- a/worker/src/queues/dataRetentionQueue.ts
+++ b/worker/src/queues/dataRetentionQueue.ts
@@ -1,7 +1,12 @@
import { Processor } from "bullmq";
-import { logger, QueueJobs } from "@langfuse/shared/src/server";
+import {
+ instrumentAsync,
+ logger,
+ QueueJobs,
+} from "@langfuse/shared/src/server";
import { handleDataRetentionSchedule } from "../ee/dataRetention/handleDataRetentionSchedule";
import { handleDataRetentionProcessingJob } from "../ee/dataRetention/handleDataRetentionProcessingJob";
+import { SpanKind } from "@opentelemetry/api";
export const dataRetentionProcessor: Processor = async (job) => {
if (job.name === QueueJobs.DataRetentionJob) {
@@ -17,11 +22,20 @@ export const dataRetentionProcessor: Processor = async (job) => {
export const dataRetentionProcessingProcessor: Processor = async (job) => {
if (job.name === QueueJobs.DataRetentionProcessingJob) {
- try {
- return await handleDataRetentionProcessingJob(job);
- } catch (error) {
- logger.error("Error executing DataRetentionProcessingJob", error);
- throw error;
- }
+ return await instrumentAsync(
+ {
+ name: "process data-retention-project",
+ startNewTrace: true,
+ spanKind: SpanKind.CONSUMER,
+ },
+ async () => {
+ try {
+ return await handleDataRetentionProcessingJob(job);
+ } catch (error) {
+ logger.error("Error executing DataRetentionProcessingJob", error);
+ throw error;
+ }
+ },
+ );
}
};
diff --git a/worker/src/queues/eventPropagationQueue.ts b/worker/src/queues/eventPropagationQueue.ts
index 483cb4c773..d8a553240f 100644
--- a/worker/src/queues/eventPropagationQueue.ts
+++ b/worker/src/queues/eventPropagationQueue.ts
@@ -5,6 +5,7 @@ import {
instrumentAsync,
} from "@langfuse/shared/src/server";
import { handleEventPropagationJob } from "../features/eventPropagation/handleEventPropagationJob";
+import { SpanKind } from "@opentelemetry/api";
export const eventPropagationProcessor: Processor = async (job) => {
if (job.name === QueueJobs.EventPropagationJob) {
@@ -12,6 +13,7 @@ export const eventPropagationProcessor: Processor = async (job) => {
{
name: "process event-propagation",
startNewTrace: true,
+ spanKind: SpanKind.CONSUMER,
},
async () => {
logger.info("Executing Event Propagation Job");
diff --git a/worker/src/queues/postHogIntegrationQueue.ts b/worker/src/queues/postHogIntegrationQueue.ts
index 4330315ffe..b7540009e7 100644
--- a/worker/src/queues/postHogIntegrationQueue.ts
+++ b/worker/src/queues/postHogIntegrationQueue.ts
@@ -1,7 +1,12 @@
import { Processor } from "bullmq";
-import { logger, QueueJobs } from "@langfuse/shared/src/server";
+import {
+ instrumentAsync,
+ logger,
+ QueueJobs,
+} from "@langfuse/shared/src/server";
import { handlePostHogIntegrationSchedule } from "../features/posthog/handlePostHogIntegrationSchedule";
import { handlePostHogIntegrationProjectJob } from "../features/posthog/handlePostHogIntegrationProjectJob";
+import { SpanKind } from "@opentelemetry/api";
export const postHogIntegrationProcessor: Processor = async (job) => {
if (job.name === QueueJobs.PostHogIntegrationJob) {
@@ -17,11 +22,23 @@ export const postHogIntegrationProcessor: Processor = async (job) => {
export const postHogIntegrationProcessingProcessor: Processor = async (job) => {
if (job.name === QueueJobs.PostHogIntegrationProcessingJob) {
- try {
- return await handlePostHogIntegrationProjectJob(job);
- } catch (error) {
- logger.error("Error executing PostHogIntegrationProcessingJob", error);
- throw error;
- }
+ return await instrumentAsync(
+ {
+ name: "process posthog-integration-project",
+ startNewTrace: true,
+ spanKind: SpanKind.CONSUMER,
+ },
+ async () => {
+ try {
+ return await handlePostHogIntegrationProjectJob(job);
+ } catch (error) {
+ logger.error(
+ "Error executing PostHogIntegrationProcessingJob",
+ error,
+ );
+ throw error;
+ }
+ },
+ );
}
};
diff --git a/worker/src/services/ClickhouseWriter/index.ts b/worker/src/services/ClickhouseWriter/index.ts
index a15e32a1b5..c9452d6672 100644
--- a/worker/src/services/ClickhouseWriter/index.ts
+++ b/worker/src/services/ClickhouseWriter/index.ts
@@ -18,7 +18,6 @@ import {
import { env } from "../../env";
import { logger } from "@langfuse/shared/src/server";
import { instrumentAsync } from "@langfuse/shared/src/server";
-import { SpanKind } from "@opentelemetry/api";
import { backOff } from "exponential-backoff";
export class ClickhouseWriter {
@@ -104,7 +103,6 @@ export class ClickhouseWriter {
return instrumentAsync(
{
name: "write-to-clickhouse",
- spanKind: SpanKind.CONSUMER,
},
async () => {
recordIncrement("langfuse.queue.clickhouse_writer.request");