Skip to content

Commit 45b2728

Browse files
authored
Merge pull request #5800 from getsentry/feat/data-collection-graphql
feat(graphql): [Data Collection 4] Apply GraphQL policies
2 parents 160ae58 + c7a9cbb commit 45b2728

20 files changed

Lines changed: 665 additions & 59 deletions

File tree

‎sentry-apollo-3/api/sentry-apollo-3.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public final class io/sentry/apollo3/SentryApollo3HttpInterceptor$Companion {
3535

3636
public final class io/sentry/apollo3/SentryApollo3Interceptor : com/apollographql/apollo3/interceptor/ApolloInterceptor {
3737
public fun <init> ()V
38+
public fun <init> (Lio/sentry/IScopes;)V
39+
public synthetic fun <init> (Lio/sentry/IScopes;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
3840
public fun intercept (Lcom/apollographql/apollo3/api/ApolloRequest;Lcom/apollographql/apollo3/interceptor/ApolloInterceptorChain;)Lkotlinx/coroutines/flow/Flow;
3941
}
4042

‎sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import io.sentry.exception.ExceptionMechanismException
2727
import io.sentry.protocol.Mechanism
2828
import io.sentry.protocol.Request
2929
import io.sentry.protocol.Response
30+
import io.sentry.util.GraphqlUtils
3031
import io.sentry.util.HttpUtils
3132
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
3233
import io.sentry.util.Platform
@@ -174,7 +175,9 @@ constructor(
174175

175176
operationId?.let { setData("operationId", it) }
176177

177-
variables?.let { setData("variables", it) }
178+
if (scopes.options.dataCollectionResolver.isGraphqlVariablesWithLegacyAlways) {
179+
variables?.let { setData("variables", it) }
180+
}
178181
setData(HTTP_METHOD_KEY, method.uppercase())
179182
}
180183
}
@@ -366,7 +369,7 @@ constructor(
366369

367370
try {
368371
it.writeTo(buffer)
369-
data = buffer.readUtf8()
372+
data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options)
370373
} catch (e: Throwable) {
371374
scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e)
372375
// continue because the response body alone can already give some insights

‎sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3Interceptor.kt‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import com.apollographql.apollo3.api.Subscription
1010
import com.apollographql.apollo3.api.variables
1111
import com.apollographql.apollo3.interceptor.ApolloInterceptor
1212
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain
13+
import io.sentry.IScopes
14+
import io.sentry.ScopesAdapter
1315
import io.sentry.apollo3.SentryApollo3HttpInterceptor.Companion.SENTRY_APOLLO_3_OPERATION_TYPE
1416
import io.sentry.apollo3.SentryApollo3HttpInterceptor.Companion.SENTRY_APOLLO_3_VARIABLES
1517
import io.sentry.vendor.Base64
1618
import kotlinx.coroutines.flow.Flow
1719

18-
class SentryApollo3Interceptor : ApolloInterceptor {
20+
class SentryApollo3Interceptor
21+
@JvmOverloads
22+
constructor(private val scopes: IScopes = ScopesAdapter.getInstance()) : ApolloInterceptor {
1923
override fun <D : Operation.Data> intercept(
2024
request: ApolloRequest<D>,
2125
chain: ApolloInterceptorChain,
@@ -28,14 +32,16 @@ class SentryApollo3Interceptor : ApolloInterceptor {
2832
Base64.encodeToString(operationType(request).toByteArray(), Base64.NO_WRAP),
2933
)
3034

31-
request.scalarAdapters?.let {
32-
builder.addHttpHeader(
33-
SENTRY_APOLLO_3_VARIABLES,
34-
Base64.encodeToString(
35-
request.operation.variables(it).valueMap.toString().toByteArray(),
36-
Base64.NO_WRAP,
37-
),
38-
)
35+
if (scopes.options.dataCollectionResolver.isGraphqlVariablesWithLegacyAlways) {
36+
request.scalarAdapters?.let {
37+
builder.addHttpHeader(
38+
SENTRY_APOLLO_3_VARIABLES,
39+
Base64.encodeToString(
40+
request.operation.variables(it).valueMap.toString().toByteArray(),
41+
Base64.NO_WRAP,
42+
),
43+
)
44+
}
3945
}
4046
return chain.proceed(builder.build())
4147
}

‎sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApolloBuilderExtensions.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fun ApolloClient.Builder.sentryTracing(
1313
failedRequestTargets: List<String> = listOf(DEFAULT_PROPAGATION_TARGETS),
1414
beforeSpan: SentryApollo3HttpInterceptor.BeforeSpanCallback? = null,
1515
): ApolloClient.Builder {
16-
addInterceptor(SentryApollo3Interceptor())
16+
addInterceptor(SentryApollo3Interceptor(scopes))
1717
addHttpInterceptor(
1818
SentryApollo3HttpInterceptor(
1919
scopes = scopes,

‎sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorClientErrors.kt‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class SentryApollo3InterceptorClientErrors {
7272
responseBody: String = responseBodyOk,
7373
sendDefaultPii: Boolean = false,
7474
socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN,
75+
configureOptions: SentryOptions.() -> Unit = {},
7576
): ApolloClient {
7677
SentryIntegrationPackageStorage.getInstance().clearStorage()
7778

@@ -83,6 +84,7 @@ class SentryApollo3InterceptorClientErrors {
8384
dsn = "https://key@sentry.io/proj"
8485
sdkVersion = SdkVersion("test", "1.2.3")
8586
isSendDefaultPii = sendDefaultPii
87+
configureOptions()
8688
}
8789
)
8890
}
@@ -266,6 +268,60 @@ class SentryApollo3InterceptorClientErrors {
266268
)
267269
}
268270

271+
@Test
272+
fun `data collection can disable the GraphQL document independently`() {
273+
val sut =
274+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
275+
dataCollection.graphql.setDocument(false)
276+
}
277+
executeQuery(sut)
278+
279+
verify(fixture.scopes)
280+
.captureEvent(
281+
check {
282+
val body = it.request!!.data as String
283+
assertFalse(body.contains("\"query\""))
284+
assertTrue(body.contains("\"variables\""))
285+
},
286+
any<Hint>(),
287+
)
288+
}
289+
290+
@Test
291+
fun `data collection can disable GraphQL variables independently`() {
292+
val sut =
293+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
294+
dataCollection.graphql.setVariables(false)
295+
}
296+
executeQuery(sut)
297+
298+
verify(fixture.scopes)
299+
.captureEvent(
300+
check {
301+
val body = it.request!!.data as String
302+
assertTrue(body.contains("\"query\""))
303+
assertFalse(body.contains("\"variables\""))
304+
},
305+
any<Hint>(),
306+
)
307+
}
308+
309+
@Test
310+
fun `data collection can disable the GraphQL request body`() {
311+
val sut =
312+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
313+
dataCollection.graphql.setDocument(false)
314+
dataCollection.graphql.setVariables(false)
315+
}
316+
executeQuery(sut)
317+
318+
verify(fixture.scopes)
319+
.captureEvent(
320+
check { assertNull(it.request!!.data) },
321+
any<Hint>(),
322+
)
323+
}
324+
269325
@Test
270326
fun `capture errors with more request context if sendDefaultPii is enabled`() {
271327
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true)

‎sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorWithVariablesTest.kt‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class SentryApollo3InterceptorWithVariablesTest {
5555
}""",
5656
socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN,
5757
beforeSpan: BeforeSpanCallback? = null,
58+
options: SentryOptions = SentryOptions().apply { dsn = "http://key@localhost/proj" },
5859
): ApolloClient {
59-
whenever(scopes.options)
60-
.thenReturn(SentryOptions().apply { dsn = "http://key@localhost/proj" })
60+
whenever(scopes.options).thenReturn(options)
6161

6262
server.enqueue(
6363
MockResponse()
@@ -91,6 +91,28 @@ class SentryApollo3InterceptorWithVariablesTest {
9191
)
9292
}
9393

94+
@Test
95+
fun `does not attach GraphQL variables when data collection disables them`() {
96+
val options =
97+
SentryOptions().apply {
98+
dsn = "http://key@localhost/proj"
99+
dataCollection.graphql.setVariables(false)
100+
}
101+
102+
executeQuery(fixture.getSut(options = options))
103+
104+
verify(fixture.scopes)
105+
.captureTransaction(
106+
check {
107+
assertNull(it.spans.first().data?.get("variables"))
108+
assertNotNull(it.spans.first().data?.get("operationId"))
109+
},
110+
anyOrNull<TraceContext>(),
111+
anyOrNull(),
112+
anyOrNull(),
113+
)
114+
}
115+
94116
@Test
95117
fun `creates a span around the failed request`() {
96118
executeQuery(fixture.getSut(httpStatusCode = 403))

‎sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4HttpInterceptor.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.sentry.exception.ExceptionMechanismException
2525
import io.sentry.protocol.Mechanism
2626
import io.sentry.protocol.Request
2727
import io.sentry.protocol.Response
28+
import io.sentry.util.GraphqlUtils
2829
import io.sentry.util.HttpUtils
2930
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
3031
import io.sentry.util.Platform
@@ -173,7 +174,9 @@ constructor(
173174

174175
operationId?.let { setData("operationId", it) }
175176

176-
variables?.let { setData("variables", it) }
177+
if (scopes.options.dataCollectionResolver.isGraphqlVariablesWithLegacyAlways) {
178+
variables?.let { setData("variables", it) }
179+
}
177180
setData(HTTP_METHOD_KEY, method.uppercase(Locale.ROOT))
178181
}
179182
}
@@ -365,7 +368,7 @@ constructor(
365368

366369
try {
367370
it.writeTo(buffer)
368-
data = buffer.readUtf8()
371+
data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options)
369372
} catch (e: Throwable) {
370373
scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e)
371374
// continue because the response body alone can already give some insights

‎sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4Interceptor.kt‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ constructor(@ApiStatus.Internal private val scopes: IScopes = ScopesAdapter.getI
3535
.addHttpHeader(OPERATION_NAME_HEADER_NAME, encodeHeaderValue(request.operation.name()))
3636
.addHttpHeader(OPERATION_TYPE_HEADER_NAME, encodeHeaderValue(operationType(request)))
3737

38-
request.scalarAdapters?.let {
39-
builder.addHttpHeader(
40-
VARIABLES_HEADER_NAME,
41-
encodeHeaderValue(request.operation.variables(it).valueMap.toString()),
42-
)
38+
if (scopes.options.dataCollectionResolver.isGraphqlVariablesWithLegacyAlways) {
39+
request.scalarAdapters?.let {
40+
builder.addHttpHeader(
41+
VARIABLES_HEADER_NAME,
42+
encodeHeaderValue(request.operation.variables(it).valueMap.toString()),
43+
)
44+
}
4345
}
4446

4547
return chain.proceed(builder.build())

‎sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApolloBuilderExtensions.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fun ApolloClient.Builder.sentryTracing(
1313
failedRequestTargets: List<String> = listOf(DEFAULT_PROPAGATION_TARGETS),
1414
beforeSpan: SentryApollo4HttpInterceptor.BeforeSpanCallback? = null,
1515
): ApolloClient.Builder {
16-
addInterceptor(SentryApollo4Interceptor())
16+
addInterceptor(SentryApollo4Interceptor(scopes))
1717
addHttpInterceptor(
1818
SentryApollo4HttpInterceptor(
1919
scopes = scopes,

‎sentry-apollo-4/src/test/java/io/sentry/apollo4/SentryApollo4BuilderExtensionsClientErrorsTest.kt‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ abstract class SentryApollo4BuilderExtensionsClientErrorsTest(
8686
responseBody: String = responseBodyOk,
8787
sendDefaultPii: Boolean = false,
8888
socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN,
89+
configureOptions: SentryOptions.() -> Unit = {},
8990
): ApolloClient {
9091
SentryIntegrationPackageStorage.getInstance().clearStorage()
9192

@@ -97,6 +98,7 @@ abstract class SentryApollo4BuilderExtensionsClientErrorsTest(
9798
dsn = "https://key@sentry.io/proj"
9899
sdkVersion = SdkVersion("test", "1.2.3")
99100
isSendDefaultPii = sendDefaultPii
101+
configureOptions()
100102
}
101103
)
102104
}
@@ -280,6 +282,60 @@ abstract class SentryApollo4BuilderExtensionsClientErrorsTest(
280282
)
281283
}
282284

285+
@Test
286+
fun `data collection can disable the GraphQL document independently`() {
287+
val sut =
288+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
289+
dataCollection.graphql.setDocument(false)
290+
}
291+
executeQuery(sut)
292+
293+
verify(fixture.scopes)
294+
.captureEvent(
295+
check {
296+
val body = it.request!!.data as String
297+
assertFalse(body.contains("\"query\""))
298+
assertTrue(body.contains("\"variables\""))
299+
},
300+
any<Hint>(),
301+
)
302+
}
303+
304+
@Test
305+
fun `data collection can disable GraphQL variables independently`() {
306+
val sut =
307+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
308+
dataCollection.graphql.setVariables(false)
309+
}
310+
executeQuery(sut)
311+
312+
verify(fixture.scopes)
313+
.captureEvent(
314+
check {
315+
val body = it.request!!.data as String
316+
assertTrue(body.contains("\"query\""))
317+
assertFalse(body.contains("\"variables\""))
318+
},
319+
any<Hint>(),
320+
)
321+
}
322+
323+
@Test
324+
fun `data collection can disable the GraphQL request body`() {
325+
val sut =
326+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
327+
dataCollection.graphql.setDocument(false)
328+
dataCollection.graphql.setVariables(false)
329+
}
330+
executeQuery(sut)
331+
332+
verify(fixture.scopes)
333+
.captureEvent(
334+
check { assertNull(it.request!!.data) },
335+
any<Hint>(),
336+
)
337+
}
338+
283339
@Test
284340
fun `capture errors with more request context if sendDefaultPii is enabled`() {
285341
val sut = fixture.getSut(responseBody = fixture.responseBodyNotOk, sendDefaultPii = true)

0 commit comments

Comments
 (0)