Skip to content

Commit

Permalink
Merge 43ed5c9 into cb6c658
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Jun 6, 2023
2 parents cb6c658 + 43ed5c9 commit 6e2e00b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Add `lock` attribute to the `SentryStackFrame` protocol to better highlight offending frames in the UI ([#2761](https://github.com/getsentry/sentry-java/pull/2761))
- Enrich database spans with blocked main thread info ([#2760](https://github.com/getsentry/sentry-java/pull/2760))
- Add `api_target` to `Request` and `data` to `Response` Protocols ([#2760](https://github.com/getsentry/sentry-java/pull/2760))

## 6.21.0

Expand Down
34 changes: 30 additions & 4 deletions sentry/src/main/java/io/sentry/protocol/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public final class Request implements JsonUnknown, JsonSerializable {
/** The fragment (anchor) of the request URL. */
private @Nullable String fragment;

/**
* The API target/specification that made the request.
* <p>
* Values can be `graphql`, `rest`, etc.
* <p>
* The data field should contain the request and response bodies based on its target specification.
* */
private @Nullable String apiTarget;

@SuppressWarnings("unused")
private @Nullable Map<String, Object> unknown;

Expand All @@ -126,6 +135,7 @@ public Request(final @NotNull Request request) {
this.data = request.data;
this.fragment = request.fragment;
this.bodySize = request.bodySize;
this.apiTarget = request.apiTarget;
}

public @Nullable String getUrl() {
Expand Down Expand Up @@ -220,12 +230,13 @@ public boolean equals(Object o) {
&& Objects.equals(headers, request.headers)
&& Objects.equals(env, request.env)
&& Objects.equals(bodySize, request.bodySize)
&& Objects.equals(fragment, request.fragment);
&& Objects.equals(fragment, request.fragment)
&& Objects.equals(apiTarget, request.getApiTarget());
}

@Override
public int hashCode() {
return Objects.hash(url, method, queryString, cookies, headers, env, bodySize, fragment);
return Objects.hash(url, method, queryString, cookies, headers, env, bodySize, fragment, apiTarget);
}

// region json
Expand All @@ -241,6 +252,14 @@ public void setUnknown(@Nullable Map<String, Object> unknown) {
this.unknown = unknown;
}

public @Nullable String getApiTarget() {
return apiTarget;
}

public void setApiTarget(final @Nullable String apiTarget) {
this.apiTarget = apiTarget;
}

public static final class JsonKeys {
public static final String URL = "url";
public static final String METHOD = "method";
Expand All @@ -252,6 +271,7 @@ public static final class JsonKeys {
public static final String OTHER = "other";
public static final String FRAGMENT = "fragment";
public static final String BODY_SIZE = "body_size";
public static final String API_TARGET = "api_target";
}

@Override
Expand Down Expand Up @@ -286,7 +306,10 @@ public void serialize(@NotNull JsonObjectWriter writer, @NotNull ILogger logger)
writer.name(JsonKeys.FRAGMENT).value(logger, fragment);
}
if (bodySize != null) {
writer.name(Response.JsonKeys.BODY_SIZE).value(logger, bodySize);
writer.name(JsonKeys.BODY_SIZE).value(logger, bodySize);
}
if (apiTarget != null) {
writer.name(JsonKeys.API_TARGET).value(logger, apiTarget);
}
if (unknown != null) {
for (String key : unknown.keySet()) {
Expand Down Expand Up @@ -346,9 +369,12 @@ public static final class Deserializer implements JsonDeserializer<Request> {
case JsonKeys.FRAGMENT:
request.fragment = reader.nextStringOrNull();
break;
case Response.JsonKeys.BODY_SIZE:
case JsonKeys.BODY_SIZE:
request.bodySize = reader.nextLongOrNull();
break;
case JsonKeys.API_TARGET:
request.apiTarget = reader.nextStringOrNull();
break;
default:
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
Expand Down
25 changes: 24 additions & 1 deletion sentry/src/main/java/io/sentry/protocol/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public final class Response implements JsonUnknown, JsonSerializable {
/** The body size in bytes */
private @Nullable Long bodySize;

/**
* Response data in any format that makes sense.
* <p>
* SDKs should discard large and binary bodies by default. Can be given as a string or
* structural data of any format.
*/
private @Nullable Object data;

@SuppressWarnings("unused")
private @Nullable Map<String, Object> unknown;

Expand All @@ -48,6 +56,7 @@ public Response(final @NotNull Response response) {
this.unknown = CollectionUtils.newConcurrentHashMap(response.unknown);
this.statusCode = response.statusCode;
this.bodySize = response.bodySize;
this.data = response.data;
}

public @Nullable String getCookies() {
Expand Down Expand Up @@ -93,13 +102,22 @@ public void setBodySize(final @Nullable Long bodySize) {
this.bodySize = bodySize;
}

public @Nullable Object getData() {
return data;
}

public void setData(final @Nullable Object data) {
this.data = data;
}

// region json

public static final class JsonKeys {
public static final String COOKIES = "cookies";
public static final String HEADERS = "headers";
public static final String STATUS_CODE = "status_code";
public static final String BODY_SIZE = "body_size";
public static final String DATA = "data";
}

@Override
Expand All @@ -119,7 +137,9 @@ public void serialize(final @NotNull JsonObjectWriter writer, final @NotNull ILo
if (bodySize != null) {
writer.name(JsonKeys.BODY_SIZE).value(logger, bodySize);
}

if (data != null) {
writer.name(JsonKeys.DATA).value(logger, data);
}
if (unknown != null) {
for (final String key : unknown.keySet()) {
final Object value = unknown.get(key);
Expand Down Expand Up @@ -157,6 +177,9 @@ public static final class Deserializer implements JsonDeserializer<Response> {
case JsonKeys.BODY_SIZE:
response.bodySize = reader.nextLongOrNull();
break;
case JsonKeys.DATA:
response.data = reader.nextObjectOrNull();
break;
default:
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RequestSerializationTest {
)
bodySize = 1000
fragment = "fragment"
apiTarget = "graphql"
}
}
private val fixture = Fixture()
Expand Down
4 changes: 4 additions & 0 deletions sentry/src/test/java/io/sentry/protocol/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RequestTest {
assertEquals("unknown", clone.unknown!!["unknown"])
assertEquals(1000, clone.bodySize)
assertEquals("fragment", clone.fragment)
assertEquals("graphql", clone.apiTarget)
}

@Test
Expand All @@ -52,6 +53,7 @@ class RequestTest {
request.unknown = newUnknown
request.bodySize = 1001
request.fragment = "fragment2"
request.apiTarget = "graphql"

assertEquals("get", clone.method)
assertEquals("http://localhost:8080", clone.url)
Expand All @@ -64,6 +66,7 @@ class RequestTest {
assertEquals(1, clone.unknown!!.size)
assertEquals(1000, clone.bodySize)
assertEquals("fragment", clone.fragment)
assertEquals("graphql", clone.apiTarget)
}

@Test
Expand Down Expand Up @@ -119,6 +122,7 @@ class RequestTest {
setUnknown(unknown)
bodySize = 1000
fragment = "fragment"
apiTarget = "graphql"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ResponseSerializationTest {
headers = mapOf("content-type" to "text/html")
statusCode = 500
bodySize = 1000
data = mapOf(
"d9d709db-b666-40cc-bcbb-093bb12aad26" to "1631d0e6-96b7-4632-85f8-ef69e8bcfb16"
)
unknown = mapOf("arbitrary_field" to "arbitrary")
}
}
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/test/resources/json/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"669ff1c1-517b-46dc-a889-131555364a56": "89043294-f6e1-4e2e-b152-1fdf9b1102fc"
},
"fragment": "fragment",
"body_size": 1000
"body_size": 1000,
"api_target": "graphql"
}
4 changes: 4 additions & 0 deletions sentry/src/test/resources/json/response.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
},
"status_code": 500,
"body_size": 1000,
"data":
{
"d9d709db-b666-40cc-bcbb-093bb12aad26": "1631d0e6-96b7-4632-85f8-ef69e8bcfb16"
},
"arbitrary_field": "arbitrary"
}

0 comments on commit 6e2e00b

Please sign in to comment.