Skip to content

Commit b8cf047

Browse files
committed
Read response before accessing trailers
1 parent 5336b6b commit b8cf047

File tree

1 file changed

+10
-11
lines changed
  • exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal

1 file changed

+10
-11
lines changed

exporters/sender/okhttp/src/main/java/io/opentelemetry/exporter/sender/okhttp/internal/OkHttpGrpcSender.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,19 @@ public void onFailure(Call call, IOException e) {
164164
@Override
165165
public void onResponse(Call call, Response response) {
166166
try (ResponseBody body = response.body()) {
167+
// Must consume body before accessing trailers
168+
byte[] bodyBytes = null;
169+
try {
170+
bodyBytes = body.bytes();
171+
} catch (IOException e) {
172+
bodyBytes = new byte[0];
173+
logger.log(Level.WARNING, "Failed to read response body", e);
174+
}
175+
byte[] resolvedBodyBytes = bodyBytes;
167176
GrpcStatusCode status = grpcStatus(response);
168177
String description = grpcMessage(response);
169178
onResponse.accept(
170179
new GrpcResponse() {
171-
@Nullable private byte[] bodyBytes;
172-
173180
@Override
174181
public GrpcStatusCode getStatusCode() {
175182
return status;
@@ -182,15 +189,7 @@ public String getStatusDescription() {
182189

183190
@Override
184191
public byte[] getResponseMessage() {
185-
if (bodyBytes == null) {
186-
try {
187-
bodyBytes = body.bytes();
188-
} catch (IOException e) {
189-
bodyBytes = new byte[0];
190-
logger.log(Level.WARNING, "Failed to read response body", e);
191-
}
192-
}
193-
return bodyBytes;
192+
return resolvedBodyBytes;
194193
}
195194
});
196195
}

0 commit comments

Comments
 (0)