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

feat: Cut off request/response body if its size exceeds limit #612 #613

Merged
merged 3 commits into from
Dec 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
public class GfLogStore implements LogStore {

private static final Log LOGGER = LogFactory.getLog("aidial.log");
// Max allowed size is 4 mb for request/response body
private static final int MAX_BODY_SIZE_BYTES = 4 * 1024 * 1024;

private final Vertx vertx;

public GfLogStore(Vertx vertx) {
Expand All @@ -62,7 +65,7 @@ private Void doSave(ProxyContext context) {
// prepare items to be written by the prompt logger
Buffer responseBody = context.getResponseBody();
String assembledStreamingResponse = null;
if (isStreamingResponse(responseBody)) {
if (isStreamingResponse(responseBody) && !exceedLimit(responseBody)) {
assembledStreamingResponse = assembleStreamingResponse(responseBody);
}
// end
Expand Down Expand Up @@ -193,10 +196,19 @@ private void append(ProxyContext context, LogEntry entry, String assembledStream
}

private static void append(LogEntry entry, Buffer buffer) {
if (buffer != null) {
byte[] bytes = buffer.getBytes();
String chars = new String(bytes, StandardCharsets.UTF_8); // not efficient, but ok for now
append(entry, chars, true);
if (buffer == null) {
return;
}
boolean largeBuffer = exceedLimit(buffer);
if (largeBuffer) {
buffer = buffer.slice(0, MAX_BODY_SIZE_BYTES);
artsiomkorzun marked this conversation as resolved.
Show resolved Hide resolved
}
byte[] bytes = buffer.getBytes();
String chars = new String(bytes, StandardCharsets.UTF_8); // not efficient, but ok for now
append(entry, chars, true);
if (largeBuffer) {
// append a special marker that entry is cut off due to its large size
append(entry, ">>", false);
}
}

Expand Down Expand Up @@ -245,6 +257,10 @@ private static String formatTimestamp(long timestamp) {
.format(DateTimeFormatter.ISO_DATE_TIME);
}

private static boolean exceedLimit(Buffer body) {
return body.length() > MAX_BODY_SIZE_BYTES;
}

/**
* Assembles streaming response into a single one.
* The assembling process merges chunks of the streaming response one by one using separator: <code>\n*data: *</code>
Expand Down
Loading