-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OPIK-75: Add client side and server side compression (#227)
- Loading branch information
1 parent
b10781e
commit 260865f
Showing
6 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,5 @@ authentication: | |
|
||
server: | ||
enableVirtualThreads: ${ENABLE_VIRTUAL_THREADS:-false} | ||
gzip: | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../opik-backend/src/test/java/com/comet/opik/api/resources/utils/ConditionalGZipFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.comet.opik.api.resources.utils; | ||
|
||
|
||
import com.comet.opik.utils.JsonUtils; | ||
import jakarta.ws.rs.client.ClientRequestContext; | ||
import jakarta.ws.rs.client.ClientRequestFilter; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.MediaType; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
public class ConditionalGZipFilter implements ClientRequestFilter { | ||
|
||
private static final int GZIP_THRESHOLD_500KB = 500 * 1024; | ||
|
||
@Override | ||
public void filter(ClientRequestContext requestContext) throws IOException { | ||
|
||
if (requestContext.hasEntity() && MediaType.APPLICATION_JSON_TYPE.equals(requestContext.getMediaType())) { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
JsonUtils.writeValueAsString(baos, requestContext.getEntity()); // Serialize the entity to byte array | ||
|
||
byte[] entityBytes = baos.toByteArray(); | ||
int entitySize = entityBytes.length; | ||
|
||
if (entitySize > GZIP_THRESHOLD_500KB) { | ||
ByteArrayOutputStream compressedBaos = new ByteArrayOutputStream(); | ||
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedBaos)) { | ||
gzipOutputStream.write(entityBytes); | ||
} | ||
|
||
byte[] compressedEntity = compressedBaos.toByteArray(); | ||
requestContext.setEntity(compressedEntity, null, MediaType.APPLICATION_JSON_TYPE); | ||
requestContext.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip"); | ||
} else { | ||
// Use the original entity if below the threshold | ||
requestContext.setEntity(entityBytes, null, MediaType.APPLICATION_JSON_TYPE); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,5 @@ authentication: | |
|
||
server: | ||
enableVirtualThreads: ${ENABLE_VIRTUAL_THREADS:-false} | ||
gzip: | ||
enabled: true |