Skip to content

Commit 15341bc

Browse files
authored
Merge 5e35096 into 8c90f98
2 parents 8c90f98 + 5e35096 commit 15341bc

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Use `<meta-data android:name="io.sentry.force-init" android:value="true" />` to ensure Sentry Android auto init is not easily overwritten
2424
- Attach request body for `application/x-www-form-urlencoded` requests in Spring ([#3731](https://github.com/getsentry/sentry-java/pull/3731))
2525
- Previously request body was only attached for `application/json` requests
26+
- Set breadcrumb level based on http status ([#3771](https://github.com/getsentry/sentry-java/pull/3771))
2627
- Support `graphql-java` v22 via a new module `sentry-graphql-22` ([#3740](https://github.com/getsentry/sentry-java/pull/3740))
2728
- If you are using `graphql-java` v21 or earlier, you can use the `sentry-graphql` module
2829
- For `graphql-java` v22 and newer please use the `sentry-graphql-22` module

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6220,6 +6220,8 @@ public final class io/sentry/util/HttpUtils {
62206220
public static fun filterOutSecurityCookies (Ljava/lang/String;Ljava/util/List;)Ljava/lang/String;
62216221
public static fun filterOutSecurityCookiesFromHeader (Ljava/util/Enumeration;Ljava/lang/String;Ljava/util/List;)Ljava/util/List;
62226222
public static fun filterOutSecurityCookiesFromHeader (Ljava/util/List;Ljava/lang/String;Ljava/util/List;)Ljava/util/List;
6223+
public static fun isHttpClientError (I)Z
6224+
public static fun isHttpServerError (I)Z
62236225
public static fun isSecurityCookie (Ljava/lang/String;Ljava/util/List;)Z
62246226
}
62256227

sentry/src/main/java/io/sentry/Breadcrumb.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry;
22

33
import io.sentry.util.CollectionUtils;
4+
import io.sentry.util.HttpUtils;
45
import io.sentry.util.Objects;
56
import io.sentry.util.UrlUtils;
67
import io.sentry.vendor.gson.stream.JsonToken;
@@ -189,6 +190,7 @@ public static Breadcrumb fromMap(
189190
final Breadcrumb breadcrumb = http(url, method);
190191
if (code != null) {
191192
breadcrumb.setData("status_code", code);
193+
breadcrumb.setLevel(levelFromHttpStatusCode(code));
192194
}
193195
return breadcrumb;
194196
}
@@ -494,6 +496,16 @@ public static Breadcrumb fromMap(
494496
return userInteraction(subCategory, viewId, viewClass, null, additionalData);
495497
}
496498

499+
private static @Nullable SentryLevel levelFromHttpStatusCode(final @NotNull Integer code) {
500+
if (HttpUtils.isHttpClientError(code)) {
501+
return SentryLevel.WARNING;
502+
} else if (HttpUtils.isHttpServerError(code)) {
503+
return SentryLevel.ERROR;
504+
} else {
505+
return null;
506+
}
507+
}
508+
497509
/** Breadcrumb ctor */
498510
public Breadcrumb() {
499511
this(DateUtils.getCurrentDateTime());

sentry/src/main/java/io/sentry/util/HttpUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static io.sentry.util.UrlUtils.SENSITIVE_DATA_SUBSTITUTE;
44

5+
import io.sentry.HttpStatusCodeRange;
56
import java.util.ArrayList;
67
import java.util.Arrays;
78
import java.util.Collections;
@@ -42,6 +43,12 @@ public final class HttpUtils {
4243
"CSRFTOKEN",
4344
"XSRF-TOKEN");
4445

46+
private static final HttpStatusCodeRange CLIENT_ERROR_STATUS_CODES =
47+
new HttpStatusCodeRange(400, 499);
48+
49+
private static final HttpStatusCodeRange SEVER_ERROR_STATUS_CODES =
50+
new HttpStatusCodeRange(500, 599);
51+
4552
public static boolean containsSensitiveHeader(final @NotNull String header) {
4653
return SENSITIVE_HEADERS.contains(header.toUpperCase(Locale.ROOT));
4754
}
@@ -130,4 +137,12 @@ public static boolean isSecurityCookie(
130137

131138
return false;
132139
}
140+
141+
public static boolean isHttpClientError(final int statusCode) {
142+
return CLIENT_ERROR_STATUS_CODES.isInRange(statusCode);
143+
}
144+
145+
public static boolean isHttpServerError(final int statusCode) {
146+
return SEVER_ERROR_STATUS_CODES.isInRange(statusCode);
147+
}
133148
}

sentry/src/test/java/io/sentry/BreadcrumbTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,38 @@ class BreadcrumbTest {
131131
assertFalse(breadcrumb.data.containsKey("status_code"))
132132
}
133133

134+
@Test
135+
fun `creates HTTP breadcrumb with WARNING level if status code is 4xx`() {
136+
val breadcrumb = Breadcrumb.http("http://example.com", "POST", 417)
137+
assertEquals("http://example.com", breadcrumb.data["url"])
138+
assertEquals("POST", breadcrumb.data["method"])
139+
assertEquals("http", breadcrumb.type)
140+
assertEquals("http", breadcrumb.category)
141+
assertEquals(SentryLevel.WARNING, breadcrumb.level)
142+
}
143+
144+
@Test
145+
fun `creates HTTP breadcrumb with error level if status code is 5xx`() {
146+
val breadcrumb = Breadcrumb.http("http://example.com", "POST", 502)
147+
assertEquals("http://example.com", breadcrumb.data["url"])
148+
assertEquals("POST", breadcrumb.data["method"])
149+
assertEquals("http", breadcrumb.type)
150+
assertEquals("http", breadcrumb.category)
151+
assertEquals(502, breadcrumb.data["status_code"])
152+
assertEquals(SentryLevel.ERROR, breadcrumb.level)
153+
}
154+
155+
@Test
156+
fun `creates HTTP breadcrumb with null level if status code is not 5xx or 4xx`() {
157+
val breadcrumb = Breadcrumb.http("http://example.com", "POST", 200)
158+
assertEquals("http://example.com", breadcrumb.data["url"])
159+
assertEquals("POST", breadcrumb.data["method"])
160+
assertEquals("http", breadcrumb.type)
161+
assertEquals("http", breadcrumb.category)
162+
assertEquals(200, breadcrumb.data["status_code"])
163+
assertEquals(null, breadcrumb.level)
164+
}
165+
134166
@Test
135167
fun `creates navigation breadcrumb`() {
136168
val breadcrumb = Breadcrumb.navigation("from", "to")

0 commit comments

Comments
 (0)