Skip to content

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

chore: only warn on upsert failures if traceSession exists (#4307)

Steffen Schmitz2ef9aad41c5b
diff --git a/packages/shared/src/server/ingestion/legacy/EventProcessor.ts b/packages/shared/src/server/ingestion/legacy/EventProcessor.ts
index 93ae5f5cff..a1e3d8f6fe 100644
--- a/packages/shared/src/server/ingestion/legacy/EventProcessor.ts
+++ b/packages/shared/src/server/ingestion/legacy/EventProcessor.ts
@@ -625,19 +625,32 @@ export class TraceProcessor implements EventProcessor {
: undefined;
if (body.sessionId) {
- await prisma.traceSession.upsert({
- where: {
- id_projectId: {
+ try {
+ await prisma.traceSession.upsert({
+ where: {
+ id_projectId: {
+ id: body.sessionId,
+ projectId: apiScope.projectId,
+ },
+ },
+ create: {
id: body.sessionId,
projectId: apiScope.projectId,
},
- },
- create: {
- id: body.sessionId,
- projectId: apiScope.projectId,
- },
- update: {},
- });
+ update: {},
+ });
+ } catch (e) {
+ if (
+ e instanceof Prisma.PrismaClientKnownRequestError &&
+ e.code === "P2002"
+ ) {
+ logger.warn(
+ `Failed to upsert session. Session ${body.sessionId} in project ${apiScope.projectId} already exists`,
+ );
+ } else {
+ throw e;
+ }
+ }
}
// Do not use nested upserts or multiple where conditions as this should be a single native database upsert
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 4e51f1dea5..8673a68036 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -241,19 +241,32 @@ export class IngestionService {
// If the trace has a sessionId, we upsert the corresponding session into Postgres.
if (finalTraceRecord.session_id) {
- await this.prisma.traceSession.upsert({
- where: {
- id_projectId: {
+ try {
+ await this.prisma.traceSession.upsert({
+ where: {
+ id_projectId: {
+ id: finalTraceRecord.session_id,
+ projectId,
+ },
+ },
+ create: {
id: finalTraceRecord.session_id,
projectId,
},
- },
- create: {
- id: finalTraceRecord.session_id,
- projectId,
- },
- update: {},
- });
+ update: {},
+ });
+ } catch (e) {
+ if (
+ e instanceof Prisma.PrismaClientKnownRequestError &&
+ e.code === "P2002"
+ ) {
+ logger.warn(
+ `Failed to upsert session. Session ${finalTraceRecord.session_id} in project ${projectId} already exists`,
+ );
+ } else {
+ throw e;
+ }
+ }
}
this.clickHouseWriter.addToQueue(TableName.Traces, finalTraceRecord);