Skip to content

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

fix(otel): avoid native task dispatch for small media-free inputs (#18786)

Valery Meleshkin4b3cedd4363d
diff --git a/packages/native/index.d.ts b/packages/native/index.d.ts
index c577b405f4..3a1e476d7d 100644
--- a/packages/native/index.d.ts
+++ b/packages/native/index.d.ts
@@ -86,6 +86,12 @@ export interface ExtractedOtelMedia {
*/
export declare function extractOtelMedia(bytes: Buffer): Promise<EarlyOtelBatch | null>
+/**
+ * Synchronously check raw bytes for possible media without copying or validating JSON.
+ * Callers must bound input size to limit JS-thread work; a match still needs extraction.
+ */
+export declare function hasOtelMediaCandidate(bytes: Buffer): boolean
+
/**
* Hello-world entry point. It proves that the worker can load and call the
* addon and that native code reports through the same telemetry as the rest
diff --git a/packages/native/index.js b/packages/native/index.js
index 546a99d340..61fdb9ca10 100644
--- a/packages/native/index.js
+++ b/packages/native/index.js
@@ -706,6 +706,7 @@ module.exports.ValidatedOtelJson = nativeBinding.ValidatedOtelJson
module.exports.clusterTopicEmbeddings = nativeBinding.clusterTopicEmbeddings
module.exports.encodeClickhouseEvents = nativeBinding.encodeClickhouseEvents
module.exports.extractOtelMedia = nativeBinding.extractOtelMedia
+module.exports.hasOtelMediaCandidate = nativeBinding.hasOtelMediaCandidate
module.exports.hello = nativeBinding.hello
module.exports.initTelemetry = nativeBinding.initTelemetry
module.exports.validateOtelJson = nativeBinding.validateOtelJson
diff --git a/packages/native/src/lib.rs b/packages/native/src/lib.rs
index 9115d42267..38f125d99d 100644
--- a/packages/native/src/lib.rs
+++ b/packages/native/src/lib.rs
@@ -19,7 +19,10 @@ mod topics;
pub use topics::{cluster_topic_embeddings, TopicClusteringResult, TopicClusteringSettings};
-pub use otel_input::{extract_otel_media, validate_otel_json, EarlyOtelBatch, ValidatedOtelJson};
+pub use otel_input::{
+ extract_otel_media, has_otel_media_candidate, validate_otel_json, EarlyOtelBatch,
+ ValidatedOtelJson,
+};
use std::sync::Arc;
use std::time::Duration;
diff --git a/packages/native/src/otel_input.rs b/packages/native/src/otel_input.rs
index 5b1e4fc45d..10c7078b70 100644
--- a/packages/native/src/otel_input.rs
+++ b/packages/native/src/otel_input.rs
@@ -335,6 +335,13 @@ pub fn validate_otel_json(
}))
}
+/// Synchronously check raw bytes for possible media without copying or validating JSON.
+/// Callers must bound input size to limit JS-thread work; a match still needs extraction.
+#[napi]
+pub fn has_otel_media_candidate(bytes: Buffer) -> bool {
+ otel_media::may_contain_media_candidate(&bytes)
+}
+
/// Extract media from unmasked input. Null means the caller should parse its original bytes;
/// marker-free input has NOT been validated. Masking callers must use validateOtelJson first.
#[napi(ts_return_type = "Promise<EarlyOtelBatch | null>")]
diff --git a/worker/src/features/otel-ingestion/prepareOtelBatch.ts b/worker/src/features/otel-ingestion/prepareOtelBatch.ts
index fdc6cb957c..4b5796163e 100644
--- a/worker/src/features/otel-ingestion/prepareOtelBatch.ts
+++ b/worker/src/features/otel-ingestion/prepareOtelBatch.ts
@@ -1,5 +1,6 @@
import {
extractOtelMedia,
+ hasOtelMediaCandidate,
validateOtelJson,
type EarlyOtelBatch,
type ValidatedOtelJson,
@@ -14,6 +15,9 @@ import {
isIngestionMaskingEnabled,
} from "@langfuse/shared/src/server/ee/ingestionMasking";
+/** Limits synchronous marker scanning so small marker-free inputs avoid a native task and copy. */
+const MAX_SYNC_MEDIA_CHECK_BYTES = 64 * 1024;
+
type PreparedInput =
| {
/** Absent when preprocessing is unnecessary; the caller must still parse JSON. */
@@ -43,7 +47,11 @@ export async function prepareOtelBatch(params: {
let maskedInput: ValidatedOtelJson | undefined;
try {
if (!isIngestionMaskingEnabled()) {
- const batch = extractMedia ? await extractOtelMedia(bytes) : null;
+ const needsExtraction =
+ extractMedia &&
+ (bytes.length > MAX_SYNC_MEDIA_CHECK_BYTES ||
+ hasOtelMediaCandidate(bytes));
+ const batch = needsExtraction ? await extractOtelMedia(bytes) : null;
outcome = batch ? "native" : "unchanged";
return { batch: batch ?? undefined, sourceUnchanged: !batch };
}
diff --git a/worker/src/features/otel-ingestion/prepareOtelBatch.unit.test.ts b/worker/src/features/otel-ingestion/prepareOtelBatch.unit.test.ts
index 09c7f34802..e1823c9067 100644
--- a/worker/src/features/otel-ingestion/prepareOtelBatch.unit.test.ts
+++ b/worker/src/features/otel-ingestion/prepareOtelBatch.unit.test.ts
@@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const nativeMocks = vi.hoisted(() => ({
validateOtelJson: vi.fn(),
extractOtelMedia: vi.fn(),
+ hasOtelMediaCandidate: vi.fn(),
}));
const maskingMocks = vi.hoisted(() => ({
applyIngestionMasking: vi.fn(),
@@ -13,10 +14,14 @@ vi.mock("@langfuse/native", async (importOriginal) => {
const actual = await importOriginal<typeof import("@langfuse/native")>();
nativeMocks.validateOtelJson.mockImplementation(actual.validateOtelJson);
nativeMocks.extractOtelMedia.mockImplementation(actual.extractOtelMedia);
+ nativeMocks.hasOtelMediaCandidate.mockImplementation(
+ actual.hasOtelMediaCandidate,
+ );
return {
...actual,
validateOtelJson: nativeMocks.validateOtelJson,
extractOtelMedia: nativeMocks.extractOtelMedia,
+ hasOtelMediaCandidate: nativeMocks.hasOtelMediaCandidate,
};
});
@@ -37,6 +42,7 @@ describe("prepareOtelBatch native input boundary", { retry: 0 }, () => {
beforeEach(() => {
nativeMocks.validateOtelJson.mockClear();
nativeMocks.extractOtelMedia.mockClear();
+ nativeMocks.hasOtelMediaCandidate.mockClear();
maskingMocks.isIngestionMaskingEnabled.mockReturnValue(true);
maskingMocks.applyIngestionMasking.mockImplementation(
async (params: { data: unknown }) => ({
@@ -84,6 +90,7 @@ describe("prepareOtelBatch native input boundary", { retry: 0 }, () => {
if ("error" in prepared) throw new Error("unexpected masking failure");
expect(prepared.batch?.mediaCount()).toBe(disabled ? undefined : 0);
expect(nativeMocks.extractOtelMedia).not.toHaveBeenCalled();
+ expect(nativeMocks.hasOtelMediaCandidate).not.toHaveBeenCalled();
expect(prepared.sourceUnchanged).toBe(sourceUnchanged);
const parsed = await parsePreparedOtelBatch(prepared, bytes);
expect(parsed.batch).toBe(sourceUnchanged ? undefined : prepared.batch);
@@ -101,6 +108,37 @@ describe("prepareOtelBatch native input boundary", { retry: 0 }, () => {
},
);
+ it.each([
+ [64 * 1024, false, 0],
+ [64 * 1024 + 1, false, 1],
+ [64 * 1024, true, 1],
+ ] as const)(
+ "prepares %i bytes with media=%s using %i extraction tasks",
+ async (size, withMedia, tasks) => {
+ maskingMocks.isIngestionMaskingEnabled.mockReturnValue(false);
+ const input = withMedia
+ ? `data:image/png;base64,${Buffer.alloc(1024, "a").toString("base64")}`
+ : "ordinary";
+ const bytes = Buffer.from(JSON.stringify({ input }).padStart(size));
+ const prepared = await prepareOtelBatch({
+ bytes,
+ projectId: "project-id",
+ extractMedia: true,
+ });
+ try {
+ expect(nativeMocks.hasOtelMediaCandidate).toHaveBeenCalledTimes(
+ size <= 64 * 1024 ? 1 : 0,
+ );
+ expect(nativeMocks.extractOtelMedia).toHaveBeenCalledTimes(tasks);
+ expect(nativeMocks.validateOtelJson).not.toHaveBeenCalled();
+ expect(prepared.batch?.mediaCount() ?? 0).toBe(withMedia ? 1 : 0);
+ expect(prepared.sourceUnchanged).toBe(!withMedia);
+ } finally {
+ await prepared.batch?.dispose();
+ }
+ },
+ );
+
it("rejects malformed JSON after marker-free preparation", async () => {
maskingMocks.isIngestionMaskingEnabled.mockReturnValue(false);
const bytes = Buffer.from('{"note":"ordinary"} trailing');