Skip to content

Commit

Permalink
Merge pull request #35183 from geoand/spans-alloc
Browse files Browse the repository at this point in the history
Reduce allocation cost reporting spans
  • Loading branch information
geoand authored Aug 4, 2023
2 parents 693b5d5 + d603488 commit 0d3cb35
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.sdk.internal.ThrottlingLogger;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.quarkus.vertx.core.runtime.BufferOutputStream;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
Expand Down Expand Up @@ -194,10 +195,9 @@ public void handle(GrpcClientRequest<Buffer, Buffer> request) {

try {
int messageSize = marshaler.getBinarySerializedSize();
var baos = new NonCopyingByteArrayOutputStream(messageSize); // TODO: we can probably use Vert.x / Netty buffering here
marshaler.writeBinaryTo(baos);
Buffer buffer = Buffer.buffer(messageSize);
buffer.appendBytes(baos.toByteArray());
var os = new BufferOutputStream(buffer);
marshaler.writeBinaryTo(os);
request.send(buffer).onSuccess(new Handler<>() {
@Override
public void handle(GrpcClientResponse<Buffer, Buffer> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static io.quarkus.opentelemetry.runtime.exporter.otlp.OtlpExporterUtil.getPort;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
Expand All @@ -18,6 +17,7 @@
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.quarkus.vertx.core.runtime.BufferOutputStream;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
Expand Down Expand Up @@ -129,17 +129,16 @@ public byte[] responseBody() {
})
.putHeader("Content-Type", contentType);

ByteArrayOutputStream os; // TODO: we can probably use Vert.x / Netty buffering here
Buffer buffer = Buffer.buffer(contentLength);
OutputStream os = new BufferOutputStream(buffer);
if (compressionEnabled) {
os = new ByteArrayOutputStream(contentLength);
clientRequest.putHeader("Content-Encoding", "gzip");
try (var gzos = new GZIPOutputStream(os)) {
marshaler.accept(gzos);
} catch (IOException e) {
throw new IllegalStateException(e);
}
} else {
os = new NonCopyingByteArrayOutputStream(contentLength);
marshaler.accept(os);
}

Expand All @@ -149,7 +148,7 @@ public byte[] responseBody() {
}
}

clientRequest.send(Buffer.buffer(os.toByteArray()));
clientRequest.send(buffer);

}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.quarkus.smallrye.health.runtime;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;
import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.vertx.core.runtime.BufferOutputStream;
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser;
import io.smallrye.health.SmallRyeHealth;
import io.smallrye.health.SmallRyeHealthReporter;
Expand Down Expand Up @@ -56,22 +56,4 @@ private void doHandle(RoutingContext ctx) {
}
}

private static class BufferOutputStream extends OutputStream {

private final Buffer buffer;

private BufferOutputStream(Buffer buffer) {
this.buffer = buffer;
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
buffer.appendBytes(b, off, len);
}

@Override
public void write(int b) throws IOException {
buffer.appendInt(b);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.vertx.core.runtime;

import java.io.IOException;
import java.io.OutputStream;

import io.vertx.core.buffer.Buffer;

/**
* Simple {@link OutputStream} implementation that appends content
* written in given {@link Buffer} instance.
*/
public class BufferOutputStream extends OutputStream {

private final Buffer buffer;

public BufferOutputStream(Buffer buffer) {
this.buffer = buffer;
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
buffer.appendBytes(b, off, len);
}

@Override
public void write(int b) throws IOException {
buffer.appendInt(b);
}
}

0 comments on commit 0d3cb35

Please sign in to comment.