Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect batch size limits when flushing queue #417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions langfuse-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,10 @@ abstract class LangfuseCoreStateless {
const MAX_MSG_SIZE = 1_000_000;
const BATCH_SIZE_LIMIT = 2_500_000;

this.processQueueItems(items, MAX_MSG_SIZE, BATCH_SIZE_LIMIT);
const { processedItems, remainingItems } = this.processQueueItems(items, MAX_MSG_SIZE, BATCH_SIZE_LIMIT);

// Add remaining items back to the start of the queue
queue.unshift(...remainingItems);

const promiseUUID = generateUUID();

Expand All @@ -708,9 +711,9 @@ abstract class LangfuseCoreStateless {
};

const payload = JSON.stringify({
batch: items,
batch: processedItems,
metadata: {
batch_size: items.length,
batch_size: processedItems.length,
sdk_integration: this.sdkIntegration,
sdk_version: this.getLibraryVersion(),
sdk_variant: this.getLibraryId(),
Expand Down
32 changes: 32 additions & 0 deletions langfuse-core/test/langfuse.flush.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,37 @@ describe("Langfuse Core", () => {
jest.advanceTimersByTime(300);
expect(mocks.fetch).toHaveBeenCalledTimes(1);
});

describe("when queue is completely full", () => {
const MAX_MSG_SIZE = 1_000_000;
const BATCH_SIZE_LIMIT = 2_500_000;
// Message is right under the message size limit
const MSG_SIZE = MAX_MSG_SIZE - 1000;
const BIG_STRING = "a".repeat(MSG_SIZE);

it("should flush remaining items on subsequent flush", () => {
const n = Math.floor(BATCH_SIZE_LIMIT / MSG_SIZE) + 1;

[langfuse, mocks] = createTestClient({
publicKey: "pk-lf-111",
secretKey: "sk-lf-111",
flushAt: n,
flushInterval: 200,
});

// Adds enough messages to exceed batch size limit
for (let i = 0; i < n; i++) {
langfuse.trace({ name: `test-trace-${i}`, input: { content: BIG_STRING } });
}

// First call flushes the messages that fit under the batch size limit
expect(mocks.fetch).toHaveBeenCalledTimes(1);

jest.advanceTimersByTime(300);

// Second call flushes the remaining messages
expect(mocks.fetch).toHaveBeenCalledTimes(2);
});
});
});
});