Skip to content

Commit 3ef80c6

Browse files
committed
add NPE check for request body
1 parent 9369e85 commit 3ef80c6

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/telemetry/HttpTracingRequestInitializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ private static void addCommonResponseAttributesToSpan(
121121

122122
static void addRequestBodySizeToSpan(HttpRequest request, Span span) {
123123
try {
124-
span.setAttribute(HTTP_REQUEST_BODY_SIZE, request.getContent().getLength());
125-
} catch (Exception e) {
124+
if (request.getContent() != null) {
125+
span.setAttribute(HTTP_REQUEST_BODY_SIZE, request.getContent().getLength());
126+
}
127+
} catch (IOException e) {
126128
// Ignore - body size not available
127129
}
128130
}

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/telemetry/HttpTracingRequestInitializerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ public void testAddRequestBodySizeToSpan_ExceptionHandled() throws IOException {
218218
assertNull(span.getAttributes().get(HttpTracingRequestInitializer.HTTP_REQUEST_BODY_SIZE));
219219
}
220220

221+
@Test
222+
public void testAddRequestBodySizeToSpan_NullBody() throws IOException {
223+
HttpTransport transport = createTransport();
224+
HttpRequest request = buildPostRequest(transport, null, BASE_URL, null);
225+
226+
HttpTracingRequestInitializer.addRequestBodySizeToSpan(request, parentSpan);
227+
228+
spanScope.close();
229+
parentSpan.end();
230+
231+
List<SpanData> spans = spanExporter.getFinishedSpanItems();
232+
assertEquals(1, spans.size());
233+
SpanData span = spans.get(0);
234+
assertNull(span.getAttributes().get(HttpTracingRequestInitializer.HTTP_REQUEST_BODY_SIZE));
235+
}
236+
221237
@Test
222238
public void testAddResponseBodySizeToSpan_NullLength() throws IOException {
223239
HttpTransport transport =

0 commit comments

Comments
 (0)