Use content type utils where possible (#112) · hypertrace/javaagent@a339bd5 · GitHub
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 <p.loffay@gmail.com> * Add test Signed-off-by: Pavol Loffay <p.loffay@gmail.com>
1 parent 75e35e6 commit a339bd5

11 files changed

Lines changed: 70 additions & 110 deletions

File tree

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

Lines changed: 0 additions & 29 deletions

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

0 commit comments

Comments
 (0)