Skip to content

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

fix(otel): persist post-media event bytes (#15287)

Hassieb Pakzad19ae60e00c2b
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 6e61d027dc..594559afb7 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -95,6 +95,22 @@ type MergeAndWriteParams = {
attribution: IngestionAttribution;
};
+/**
+ * Returns a new event record whose `event_bytes` value is the UTF-8 size of
+ * the final JSONEachRow payload, excluding the self-referential accounting
+ * field. The supplied record is not mutated.
+ */
+function withSerializedEventByteLength(
+ eventRecord: EventRecordInsertType,
+): EventRecordInsertType {
+ const { event_bytes: _eventBytes, ...eventWithoutSize } = eventRecord;
+
+ return {
+ ...eventWithoutSize,
+ event_bytes: Buffer.byteLength(JSON.stringify(eventWithoutSize), "utf8"),
+ };
+}
+
const immutableEntityKeys: {
[TableName.Traces]: (keyof TraceRecordInsertType)[];
[TableName.Scores]: (keyof ScoreRecordInsertType)[];
@@ -416,10 +432,17 @@ export class IngestionService {
* A materialized view auto-populates events_core from events_full.
* Use createEventRecord() first to get the record, then call this to write.
*
+ * Enqueues a new record whose `event_bytes` describes the final normalized
+ * event rather than the raw OTEL span measured before media processing. The
+ * supplied record is not mutated.
+ *
* @param eventRecord - The event record to write
*/
public writeEventRecord(eventRecord: EventRecordInsertType): void {
- this.clickHouseWriter.addToQueue(TableName.EventsFull, eventRecord);
+ this.clickHouseWriter.addToQueue(
+ TableName.EventsFull,
+ withSerializedEventByteLength(eventRecord),
+ );
}
private async processDatasetRunItemEventList(params: {
diff --git a/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts b/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts
index b04c893e8c..7a01d630a8 100644
--- a/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts
+++ b/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts
@@ -9,6 +9,51 @@ import {
import { TableName } from "../../ClickhouseWriter";
describe("IngestionService unit tests", () => {
+ it("writes the final serialized event size instead of the raw OTEL span size", async () => {
+ const addToQueue = vi.fn();
+ const ingestionService = new IngestionService(
+ {} as any,
+ {} as any,
+ { addToQueue } as any,
+ {} as any,
+ );
+ const rawOtelSpanBytes = 10_000_000;
+ const eventRecord = await ingestionService.createEventRecord(
+ {
+ projectId: "project-id",
+ traceId: "trace-id",
+ spanId: "observation-id",
+ parentSpanId: "",
+ name: "post-media-size",
+ type: "SPAN",
+ environment: "default",
+ startTimeISO: "2026-07-22T00:00:00.000Z",
+ endTimeISO: "2026-07-22T00:00:01.000Z",
+ input: "@@@langfuseMedia:type=image/png|id=media-id|source=bytes@@@",
+ output: "multibyte 🔥 output",
+ metadata: { nested: { value: "metadata" } },
+ source: "otel",
+ eventBytes: rawOtelSpanBytes,
+ },
+ "otel/project-id/raw-event.json",
+ );
+
+ expect(eventRecord.event_bytes).toBe(rawOtelSpanBytes);
+
+ ingestionService.writeEventRecord(eventRecord);
+
+ expect(addToQueue).toHaveBeenCalledOnce();
+ const queuedRecord = addToQueue.mock.calls[0]?.[1];
+ const { event_bytes: eventBytes, ...eventWithoutSize } = queuedRecord;
+
+ expect(queuedRecord).not.toBe(eventRecord);
+ expect(eventRecord.event_bytes).toBe(rawOtelSpanBytes);
+ expect(eventBytes).toBe(
+ Buffer.byteLength(JSON.stringify(eventWithoutSize), "utf8"),
+ );
+ expect(eventBytes).toBeLessThan(rawOtelSpanBytes);
+ });
+
it("correctly sorts events in ascending order by timestamp", async () => {
const firstTrace = { timestamp: 1, type: "observation-create" };
const secondTrace = { timestamp: 1, type: "observation-update" };