Skip to content

Add Request to the Scope. #1270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Fix: Make the ANR Atomic flags immutable
* Enchancement: Integration interface better compatibility with Kotlin null-safety
* Enchancement: Simplify Sentry configuration in Spring integration (#1259)
* Enchancement: Add Request to the Scope. #1270
* Fix: Fix SentryTransaction#getStatus NPE (#1273)
* Enchancement: Optimize SentryTracingFilter when hub is disabled.
* Fix: SentryTransaction#finish should not clear another transaction from the scope (#1278)
Expand Down
6 changes: 5 additions & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ public final class io/sentry/Scope : java/lang/Cloneable {
public synthetic fun clone ()Ljava/lang/Object;
public fun getContexts ()Lio/sentry/protocol/Contexts;
public fun getLevel ()Lio/sentry/SentryLevel;
public fun getRequest ()Lio/sentry/protocol/Request;
public fun getSpan ()Lio/sentry/ISpan;
public fun getTransaction ()Lio/sentry/ITransaction;
public fun getTransactionName ()Ljava/lang/String;
Expand All @@ -450,6 +451,7 @@ public final class io/sentry/Scope : java/lang/Cloneable {
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
public fun setFingerprint (Ljava/util/List;)V
public fun setLevel (Lio/sentry/SentryLevel;)V
public fun setRequest (Lio/sentry/protocol/Request;)V
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Lio/sentry/ITransaction;)V
public fun setTransaction (Ljava/lang/String;)V
Expand Down Expand Up @@ -1465,9 +1467,11 @@ public final class io/sentry/protocol/OperatingSystem : io/sentry/IUnknownProper
public fun setVersion (Ljava/lang/String;)V
}

public final class io/sentry/protocol/Request : io/sentry/IUnknownPropertiesConsumer {
public final class io/sentry/protocol/Request : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
public fun <init> ()V
public fun acceptUnknownProperties (Ljava/util/Map;)V
public fun clone ()Lio/sentry/protocol/Request;
public synthetic fun clone ()Ljava/lang/Object;
public fun getCookies ()Ljava/lang/String;
public fun getData ()Ljava/lang/Object;
public fun getEnvs ()Ljava/util/Map;
Expand Down
26 changes: 26 additions & 0 deletions sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry;

import io.sentry.protocol.Contexts;
import io.sentry.protocol.Request;
import io.sentry.protocol.User;
import io.sentry.util.Objects;
import java.util.ArrayList;
Expand Down Expand Up @@ -29,6 +30,9 @@ public final class Scope implements Cloneable {
/** Scope's user */
private @Nullable User user;

/** Scope's request */
private @Nullable Request request;

/** Scope's fingerprint */
private @NotNull List<String> fingerprint = new ArrayList<>();

Expand Down Expand Up @@ -163,6 +167,24 @@ public void setUser(final @Nullable User user) {
}
}

/**
* Returns the Scope's request
*
* @return the request
*/
public @Nullable Request getRequest() {
return request;
}

/**
* Sets the Scope's request
*
* @param request the request
*/
public void setRequest(final @Nullable Request request) {
this.request = request;
}

/**
* Returns the Scope's fingerprint list
*
Expand Down Expand Up @@ -287,6 +309,7 @@ public void clear() {
level = null;
transaction = null;
user = null;
request = null;
fingerprint.clear();
breadcrumbs.clear();
tags.clear();
Expand Down Expand Up @@ -489,6 +512,9 @@ public void addAttachment(final @NotNull Attachment attachment) {
final User userRef = user;
clone.user = userRef != null ? userRef.clone() : null;

final Request requestRef = request;
clone.request = requestRef != null ? requestRef.clone() : null;

clone.fingerprint = new ArrayList<>(fingerprint);
clone.eventProcessors = new CopyOnWriteArrayList<>(eventProcessors);

Expand Down
3 changes: 3 additions & 0 deletions sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ public void captureSession(final @NotNull Session session, final @Nullable Objec
if (event.getUser() == null) {
event.setUser(scope.getUser());
}
if (event.getRequest() == null) {
event.setRequest(scope.getRequest());
}
if (event.getFingerprints() == null) {
event.setFingerprints(scope.getFingerprint());
}
Expand Down
35 changes: 34 additions & 1 deletion sentry/src/main/java/io/sentry/protocol/Request.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.sentry.protocol;

import io.sentry.IUnknownPropertiesConsumer;
import io.sentry.util.CollectionUtils;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

/**
* Http request information.
Expand Down Expand Up @@ -40,7 +44,7 @@
* csrftoken=u32t4o3tb3gg43; _gat=1;", "headers": { "content-type": "text/html" }, "env": {
* "REMOTE_ADDR": "192.168.0.1" } } } ```
*/
public final class Request implements IUnknownPropertiesConsumer {
public final class Request implements Cloneable, IUnknownPropertiesConsumer {
/**
* The URL of the request if available.
*
Expand Down Expand Up @@ -159,9 +163,38 @@ public void setOthers(Map<String, String> other) {
this.other = other;
}

/**
* the Request's unknown fields
*
* @return the unknown map
*/
@TestOnly
@Nullable
Map<String, Object> getUnknown() {
return unknown;
}

@ApiStatus.Internal
@Override
public void acceptUnknownProperties(Map<String, Object> unknown) {
this.unknown = unknown;
}

/**
* Clones an User aka deep copy
*
* @return the cloned User
* @throws CloneNotSupportedException if the User is not cloneable
*/
@Override
public @NotNull Request clone() throws CloneNotSupportedException {
final Request clone = (Request) super.clone();

clone.headers = CollectionUtils.shallowCopy(headers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think clone.data could be a List or Map and it might require a shallow copy, I guess we've done this in another clone methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean to check if with instanceof? Since data can be anything for now it's just a reference copy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but if it's a list/map and items are added/removed, it would mutate both, right, that's what we could avoid.
we did something similar here https://github.com/getsentry/sentry-dart/blob/main/dart/lib/src/protocol/request.dart#L25-L38

clone.env = CollectionUtils.shallowCopy(env);
clone.other = CollectionUtils.shallowCopy(other);
clone.unknown = CollectionUtils.shallowCopy(unknown);

return clone;
}
}
32 changes: 30 additions & 2 deletions sentry/src/test/java/io/sentry/ScopeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.verify
import io.sentry.protocol.Request
import io.sentry.protocol.User
import io.sentry.test.callMethod
import java.util.concurrent.CopyOnWriteArrayList
Expand Down Expand Up @@ -33,6 +34,18 @@ class ScopeTest {

scope.user = user

val request = Request()
request.method = "post"
request.cookies = "cookies"
request.data = "cookies"
request.envs = mapOf("env" to "value")
request.headers = mapOf("header" to "value")
request.others = mapOf("other" to "value")
request.queryString = "?foo=bar"
request.url = "http://localhost:8080/url"

scope.request = request

val fingerprints = mutableListOf("abc", "def")
scope.fingerprint = fingerprints

Expand All @@ -56,6 +69,7 @@ class ScopeTest {
assertNotNull(clone)
assertNotSame(scope, clone)
assertNotSame(scope.user, clone.user)
assertNotSame(scope.request, clone.request)
assertNotSame(scope.contexts, clone.contexts)
assertNotSame(scope.fingerprint, clone.fingerprint)
assertNotSame(scope.breadcrumbs, clone.breadcrumbs)
Expand All @@ -73,9 +87,12 @@ class ScopeTest {

val user = User()
user.id = "123"

scope.user = user

val request = Request()
request.method = "get"
scope.request = request

val fingerprints = mutableListOf("abc")
scope.fingerprint = fingerprints

Expand All @@ -98,6 +115,8 @@ class ScopeTest {

assertEquals("123", clone.user?.id)

assertEquals("get", clone.request?.method)

assertEquals("abc", clone.fingerprint.first())

assertEquals("message", clone.breadcrumbs.first().message)
Expand All @@ -123,9 +142,12 @@ class ScopeTest {

val user = User()
user.id = "123"

scope.user = user

val request = Request()
request.method = "get"
scope.request = request

val fingerprints = mutableListOf("abc")
scope.fingerprint = fingerprints

Expand All @@ -146,6 +168,7 @@ class ScopeTest {

scope.level = SentryLevel.FATAL
user.id = "456"
request.method = "post"

scope.setTransaction(SentryTransaction("newTransaction", "op"))

Expand All @@ -166,6 +189,8 @@ class ScopeTest {

assertEquals("123", clone.user?.id)

assertEquals("get", clone.request?.method)

assertEquals("abc", clone.fingerprint.first())
assertEquals(1, clone.fingerprint.size)

Expand All @@ -191,6 +216,7 @@ class ScopeTest {
scope.level = SentryLevel.WARNING
scope.setTransaction(SentryTransaction("", "op"))
scope.user = User()
scope.request = Request()
scope.fingerprint = mutableListOf("finger")
scope.addBreadcrumb(Breadcrumb())
scope.setTag("some", "tag")
Expand All @@ -202,6 +228,8 @@ class ScopeTest {

assertNull(scope.level)
assertNull(scope.transaction)
assertNull(scope.user)
assertNull(scope.request)
assertEquals(0, scope.fingerprint.size)
assertEquals(0, scope.breadcrumbs.size)
assertEquals(0, scope.tags.size)
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/test/java/io/sentry/SentryClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ class SentryClientTest {
assertEquals("fp", event.fingerprints[0])
assertEquals("id", event.user.id)
assertEquals(SentryLevel.FATAL, event.level)
assertNotNull(event.request) {
assertEquals("post", it.method)
}
}

@Test
Expand Down Expand Up @@ -855,6 +858,9 @@ class SentryClientTest {
user = User().apply {
id = "id"
}
request = Request().apply {
method = "post"
}
}
}

Expand Down
81 changes: 81 additions & 0 deletions sentry/src/test/java/io/sentry/protocol/RequestTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.sentry.protocol

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame
import kotlin.test.assertNull

class RequestTest {
@Test
fun `cloning request wont have the same references`() {
val request = createRequest()
val clone = request.clone()

assertNotNull(clone)
assertNotSame(request, clone)

assertNotSame(request.others, clone.others)

assertNotSame(request.unknown, clone.unknown)
}

@Test
fun `cloning request will have the same values`() {
val request = createRequest()
val clone = request.clone()

assertEquals("get", clone.method)
assertEquals("http://localhost:8080", clone.url)
assertEquals("?foo=bar", clone.queryString)
assertEquals("envs", clone.envs!!["envs"])
assertEquals("others", clone.others!!["others"])
assertEquals("unknown", clone.unknown!!["unknown"])
}

@Test
fun `cloning request and changing the original values wont change the clone values`() {
val request = createRequest()
val clone = request.clone()

request.method = "post"
request.url = "http://another-host:8081/"
request.queryString = "?xxx=yyy"
request.envs!!["envs"] = "newEnvs"
request.others!!["others"] = "newOthers"
request.others!!["anotherOne"] = "anotherOne"
val newUnknown = mapOf(Pair("unknown", "newUnknown"), Pair("otherUnknown", "otherUnknown"))
request.acceptUnknownProperties(newUnknown)

assertEquals("get", clone.method)
assertEquals("http://localhost:8080", clone.url)
assertEquals("?foo=bar", clone.queryString)
assertEquals("envs", clone.envs!!["envs"])
assertEquals(1, clone.envs!!.size)
assertEquals("others", clone.others!!["others"])
assertEquals(1, clone.others!!.size)
assertEquals("unknown", clone.unknown!!["unknown"])
assertEquals(1, clone.unknown!!.size)
}

@Test
fun `setting null others do not crash`() {
val request = createRequest()
request.others = null

assertNull(request.others)
}

private fun createRequest(): Request {
return Request().apply {
method = "get"
url = "http://localhost:8080"
queryString = "?foo=bar"
envs = mutableMapOf(Pair("envs", "envs"))
val others = mutableMapOf(Pair("others", "others"))
setOthers(others)
val unknown = mapOf(Pair("unknown", "unknown"))
acceptUnknownProperties(unknown)
}
}
}