Set gzip compression level to 3 out of 9
This is the default value beats uses
@@ -5,6 +5,7 @@ import co.elastic.apm.impl.TransactionPayload;
import co.elastic.apm.report.serialize.PayloadSerializer;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
+import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
@@ -17,28 +18,50 @@ import java.io.IOException;
public class ApmServerHttpPayloadSender implements PayloadSender {
private static final Logger logger = LoggerFactory.getLogger(ApmServerHttpPayloadSender.class);
+ private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json");
+ private static final int GZIP_COMPRESSION_LEVEL = 3;
private final OkHttpClient httpClient;
private final ReporterConfiguration reporterConfiguration;
- private final PayloadRequestBody body;
+ private final PayloadSerializer payloadSerializer;
private long droppedTransactions = 0;
public ApmServerHttpPayloadSender(OkHttpClient httpClient, PayloadSerializer payloadSerializer,
ReporterConfiguration reporterConfiguration) {
this.httpClient = httpClient;
this.reporterConfiguration = reporterConfiguration;
- this.body = new PayloadRequestBody(payloadSerializer);
+ this.payloadSerializer = payloadSerializer;
}
@Override
public void sendPayload(final TransactionPayload payload) {
// this is ok as its only executed single threaded
- body.payload = payload;
- okhttp3.Request request = new okhttp3.Request.Builder()
+ okhttp3.Request request = new Request.Builder()
.url(reporterConfiguration.getServerUrl() + "/v1/transactions")
.header("Content-Encoding", "gzip")
.header("User-Agent", "apm-agent-java " + payload.getService().getAgent().getVersion())
- .post(body)
+ .post(new RequestBody() {
+
+
+ @Override
+ public MediaType contentType() {
+ return MEDIA_TYPE_JSON;
+ }
+
+ @Override
+ public void writeTo(BufferedSink sink) throws IOException {
+ if (useGzip(payload)) {
+ GzipSink gzipSink = new GzipSink(sink);
+ gzipSink.deflater().setLevel(GZIP_COMPRESSION_LEVEL);
+ sink = Okio.buffer(gzipSink);
+ }
+ payloadSerializer.serializePayload(sink, payload);
+ sink.close();
+ for (Transaction transaction : payload.getTransactions()) {
+ transaction.recycle();
+ }
+ }
+ })
.build();
try {
@@ -60,33 +83,13 @@ public class ApmServerHttpPayloadSender implements PayloadSender {
}
}
- public long getDroppedTransactions() {
- return droppedTransactions;
+ private boolean useGzip(TransactionPayload payload) {
+ // TODO determine if turning on gzip is woth it based on the payload size
+ return true;
}
- private static class PayloadRequestBody extends RequestBody {
- private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json");
-
- private final PayloadSerializer payloadSerializer;
-
- TransactionPayload payload;
-
- PayloadRequestBody(PayloadSerializer payloadSerializer) {
- this.payloadSerializer = payloadSerializer;
- }
-
- @Override
- public MediaType contentType() {
- return MEDIA_TYPE_JSON;
+ public long getDroppedTransactions() {
+ return droppedTransactions;
}
- @Override
- public void writeTo(BufferedSink sink) throws IOException {
- payloadSerializer.serializePayload(Okio.buffer(new GzipSink(sink)), payload);
- sink.close();
- for (Transaction transaction : payload.getTransactions()) {
- transaction.recycle();
- }
- }
- }
}
