Skip to content

Commit a339bd5

Browse files
authored
Use content type utils where possible (#112)
* Use content type utils where possible Signed-off-by: Pavol Loffay <[email protected]> * Add test Signed-off-by: Pavol Loffay <[email protected]>
1 parent 75e35e6 commit a339bd5

File tree

11 files changed

+70
-110
lines changed

11 files changed

+70
-110
lines changed

instrumentation/okhttp/okhttp-3.0/src/test/java/io/opentelemetry/instrumentation/hypertrace/okhttp/v3_0/OkHttpTracingInterceptorTest.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
3030
import org.hypertrace.agent.testing.TestHttpServer;
3131
import org.hypertrace.agent.testing.TestHttpServer.GetJsonHandler;
32-
import org.hypertrace.agent.testing.TestHttpServer.GetPlainTextHandler;
3332
import org.junit.jupiter.api.AfterAll;
3433
import org.junit.jupiter.api.Assertions;
3534
import org.junit.jupiter.api.BeforeAll;
@@ -112,34 +111,6 @@ public void getJson() throws Exception {
112111
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
113112
}
114113

115-
@Test
116-
public void getPlainText() throws Exception {
117-
Request request =
118-
new Builder()
119-
.url(String.format("http://localhost:%d/get_plain_text", testHttpServer.port()))
120-
.get()
121-
.build();
122-
123-
Response response = client.newCall(request).execute();
124-
response.close();
125-
126-
TEST_WRITER.waitForTraces(1);
127-
List<List<SpanData>> traces = TEST_WRITER.getTraces();
128-
Assertions.assertEquals(1, traces.size());
129-
Assertions.assertEquals(1, traces.get(0).size());
130-
SpanData clientSpan = traces.get(0).get(0);
131-
Assertions.assertEquals(
132-
"test-value",
133-
clientSpan
134-
.getAttributes()
135-
.get(HypertraceSemanticAttributes.httpResponseHeader("test-response-header")));
136-
Assertions.assertNull(
137-
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY));
138-
Assertions.assertEquals(
139-
GetPlainTextHandler.RESPONSE_BODY,
140-
clientSpan.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY));
141-
}
142-
143114
@Test
144115
public void postUrlEncoded() throws Exception {
145116
FormBody formBody = new FormBody.Builder().add("foo", "bar").build();

instrumentation/servlet/servlet-2.3/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v2_3/BufferingHttpServletRequest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.servlet.http.HttpServletRequest;
3333
import javax.servlet.http.HttpServletRequestWrapper;
3434
import javax.servlet.http.HttpServletResponse;
35+
import org.hypertrace.agent.core.ContentTypeUtils;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738

@@ -192,12 +193,7 @@ private boolean shouldReadContent() {
192193
if (contentType == null || contentType.isEmpty()) {
193194
return false;
194195
}
195-
if (contentType.contains("json")
196-
|| contentType.contains("x-www-form-urlencoded")
197-
|| contentType.contains("text/plain")) {
198-
return true;
199-
}
200-
return false;
196+
return ContentTypeUtils.shouldCapture(contentType);
201197
}
202198

203199
public Map<String, List<String>> getBufferedParams() {

instrumentation/servlet/servlet-2.3/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v2_3/BufferingHttpServletResponse.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.servlet.http.Cookie;
3333
import javax.servlet.http.HttpServletResponse;
3434
import javax.servlet.http.HttpServletResponseWrapper;
35+
import org.hypertrace.agent.core.ContentTypeUtils;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738

@@ -195,12 +196,7 @@ private boolean shouldReadContent() {
195196
if (contentType == null || contentType.isEmpty()) {
196197
return false;
197198
}
198-
if (contentType.contains("json")
199-
|| contentType.contains("x-www-form-urlencoded")
200-
|| contentType.contains("text/plain")) {
201-
return true;
202-
}
203-
return false;
199+
return ContentTypeUtils.shouldCapture(contentType);
204200
}
205201

206202
public static class BufferingServletOutputStream extends ServletOutputStream {

instrumentation/servlet/servlet-3.0/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v3_0/BufferingHttpServletRequest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.servlet.http.HttpServletRequest;
3434
import javax.servlet.http.HttpServletRequestWrapper;
3535
import javax.servlet.http.HttpServletResponse;
36+
import org.hypertrace.agent.core.ContentTypeUtils;
3637
import org.slf4j.Logger;
3738
import org.slf4j.LoggerFactory;
3839

@@ -198,12 +199,7 @@ private boolean shouldReadContent() {
198199
if (contentType == null || contentType.isEmpty()) {
199200
return false;
200201
}
201-
if (contentType.contains("json")
202-
|| contentType.contains("x-www-form-urlencoded")
203-
|| contentType.contains("text/plain")) {
204-
return true;
205-
}
206-
return false;
202+
return ContentTypeUtils.shouldCapture(contentType);
207203
}
208204

209205
public Map<String, List<String>> getBufferedParams() {

instrumentation/servlet/servlet-3.0/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v3_0/BufferingHttpServletResponse.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.servlet.ServletOutputStream;
2727
import javax.servlet.http.HttpServletResponse;
2828
import javax.servlet.http.HttpServletResponseWrapper;
29+
import org.hypertrace.agent.core.ContentTypeUtils;
2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
3132

@@ -108,12 +109,7 @@ private boolean shouldReadContent() {
108109
if (contentType == null || contentType.isEmpty()) {
109110
return false;
110111
}
111-
if (contentType.contains("json")
112-
|| contentType.contains("x-www-form-urlencoded")
113-
|| contentType.contains("text/plain")) {
114-
return true;
115-
}
116-
return false;
112+
return ContentTypeUtils.shouldCapture(contentType);
117113
}
118114

119115
public static class BufferingServletOutputStream extends ServletOutputStream {

instrumentation/servlet/servlet-3.1/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v3_1/BufferingHttpServletRequest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.servlet.http.HttpServletRequest;
3535
import javax.servlet.http.HttpServletRequestWrapper;
3636
import javax.servlet.http.HttpServletResponse;
37+
import org.hypertrace.agent.core.ContentTypeUtils;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -199,12 +200,7 @@ private boolean shouldReadContent() {
199200
if (contentType == null || contentType.isEmpty()) {
200201
return false;
201202
}
202-
if (contentType.contains("json")
203-
|| contentType.contains("x-www-form-urlencoded")
204-
|| contentType.contains("text/plain")) {
205-
return true;
206-
}
207-
return false;
203+
return ContentTypeUtils.shouldCapture(contentType);
208204
}
209205

210206
public Map<String, List<String>> getBufferedParams() {

instrumentation/servlet/servlet-3.1/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v3_1/BufferingHttpServletResponse.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.servlet.WriteListener;
2828
import javax.servlet.http.HttpServletResponse;
2929
import javax.servlet.http.HttpServletResponseWrapper;
30+
import org.hypertrace.agent.core.ContentTypeUtils;
3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
3233

@@ -109,12 +110,7 @@ private boolean shouldReadContent() {
109110
if (contentType == null || contentType.isEmpty()) {
110111
return false;
111112
}
112-
if (contentType.contains("json")
113-
|| contentType.contains("x-www-form-urlencoded")
114-
|| contentType.contains("text/plain")) {
115-
return true;
116-
}
117-
return false;
113+
return ContentTypeUtils.shouldCapture(contentType);
118114
}
119115

120116
public static class BufferingServletOutputStream extends ServletOutputStream {

javaagent-core/src/main/java/org/hypertrace/agent/core/ContentTypeUtils.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ public static boolean shouldCapture(String contentType) {
3030
if (contentType == null) {
3131
return false;
3232
}
33-
3433
contentType = contentType.toLowerCase();
35-
if (contentType.contains("json")
36-
|| contentType.contains("x-www-form-urlencoded")
37-
|| contentType.contains("text/plain")) {
38-
return true;
39-
}
40-
41-
return false;
34+
return contentType.contains("json")
35+
|| contentType.contains("graphql")
36+
|| contentType.contains("x-www-form-urlencoded");
4237
}
4338
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.hypertrace.agent.core;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
class ContentTypeUtilsTest {
23+
24+
@Test
25+
public void shouldCapture() {
26+
String[] valid = {"jSoN", "graphQL", "x-www-forM-urlencoded"};
27+
for (String contentType : valid) {
28+
Assertions.assertTrue(ContentTypeUtils.shouldCapture(contentType), contentType);
29+
}
30+
}
31+
32+
@Test
33+
public void shouldNotCapture() {
34+
String[] valid = {
35+
"text/plain",
36+
};
37+
for (String contentType : valid) {
38+
Assertions.assertFalse(ContentTypeUtils.shouldCapture(contentType), contentType);
39+
}
40+
}
41+
}

smoke-tests/src/test/java/org/hypertrace/agent/smoketest/SpringBootSmokeTest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Collection;
23-
import java.util.List;
2423
import java.util.jar.Attributes;
2524
import java.util.jar.JarFile;
26-
import java.util.stream.Collectors;
2725
import okhttp3.Request;
2826
import okhttp3.Response;
29-
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
3027
import org.junit.jupiter.api.AfterAll;
3128
import org.junit.jupiter.api.Assertions;
3229
import org.junit.jupiter.api.BeforeEach;
@@ -116,18 +113,20 @@ public void get() throws IOException {
116113
.map(attribute -> attribute.getValue().getStringValue())
117114
.count()
118115
> 0);
119-
List<String> responseBodyAttributes =
120-
getSpanStream(traces)
121-
.flatMap(span -> span.getAttributesList().stream())
122-
.filter(
123-
attribute ->
124-
attribute
125-
.getKey()
126-
.contains(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY.getKey()))
127-
.map(attribute -> attribute.getValue().getStringValue())
128-
.collect(Collectors.toList());
129-
Assertions.assertEquals(1, responseBodyAttributes.size());
130-
Assertions.assertEquals("Hi!", responseBodyAttributes.get(0));
116+
// OTEL BS smoke test app does not have an endpoint that uses content type what we capture
117+
// enable once we add smoke tests apps to our build.
118+
// List<String> responseBodyAttributes =
119+
// getSpanStream(traces)
120+
// .flatMap(span -> span.getAttributesList().stream())
121+
// .filter(
122+
// attribute ->
123+
// attribute
124+
// .getKey()
125+
// .contains(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY.getKey()))
126+
// .map(attribute -> attribute.getValue().getStringValue())
127+
// .collect(Collectors.toList());
128+
// Assertions.assertEquals(1, responseBodyAttributes.size());
129+
// Assertions.assertEquals("Hi!", responseBodyAttributes.get(0));
131130
}
132131

133132
@Test

0 commit comments

Comments
 (0)