From cecfc3461bcbceb79b6a58b33d4d320a28fc7acf Mon Sep 17 00:00:00 2001 From: John Watson Date: Thu, 1 Jul 2021 06:16:17 -0700 Subject: [PATCH] Best effort at recording the content length on a http response. (#60) --- .../com/splunk/rum/OkHttpRumInterceptor.java | 19 ++++++++++++++++++- .../splunk/rum/OkHttpRumInterceptorTest.java | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/OkHttpRumInterceptor.java b/splunk-otel-android/src/main/java/com/splunk/rum/OkHttpRumInterceptor.java index e241a934..ffc7ba8b 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/OkHttpRumInterceptor.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/OkHttpRumInterceptor.java @@ -23,6 +23,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.trace.Span; +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import okhttp3.Call; import okhttp3.Connection; import okhttp3.Interceptor; @@ -78,13 +79,14 @@ public Response proceed(@NotNull Request request) throws IOException { Span span = Span.current(); span.setAttribute(SplunkRum.COMPONENT_KEY, "http"); - Response response = null; + Response response; try { response = chain.proceed(request); } catch (IOException e) { SplunkRum.addExceptionAttributes(span, e); throw e; } + recordContentLength(span, response); String serverTimingHeader = response.header("Server-Timing"); @@ -96,6 +98,21 @@ public Response proceed(@NotNull Request request) throws IOException { return response; } + private void recordContentLength(Span span, Response response) { + //make a best low-impact effort at getting the content length on the response. + String contentLengthHeader = response.header("Content-Length"); + if (contentLengthHeader != null) { + try { + long contentLength = Long.parseLong(contentLengthHeader); + if (contentLength > 0) { + span.setAttribute(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, contentLength); + } + } catch (NumberFormatException e) { + //who knows what we got back? It wasn't a number! + } + } + } + @Override public Connection connection() { return chain.connection(); diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/OkHttpRumInterceptorTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/OkHttpRumInterceptorTest.java index 32773b01..255bec42 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/OkHttpRumInterceptorTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/OkHttpRumInterceptorTest.java @@ -67,6 +67,7 @@ public void spanDecoration() throws IOException { .message("hello") .code(200) .addHeader("Server-Timing", "headerValue") + .addHeader("Content-Length", "101") .build()); OkHttpRumInterceptor interceptor = new OkHttpRumInterceptor(new TestTracingInterceptor(tracer), headerParser); @@ -78,6 +79,7 @@ public void spanDecoration() throws IOException { assertEquals("http", spanData.getAttributes().get(SplunkRum.COMPONENT_KEY)); assertEquals("9499195c502eb217c448a68bfe0f967c", spanData.getAttributes().get(OkHttpRumInterceptor.LINK_TRACE_ID_KEY)); assertEquals("fe16eca542cd5d86", spanData.getAttributes().get(OkHttpRumInterceptor.LINK_SPAN_ID_KEY)); + assertEquals(101L, (long) spanData.getAttributes().get(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH)); } @Test