Skip to content

Commit f665ca5

Browse files
authored
Merge pull request #5804 from getsentry/feat/data-collection-outgoing-request-body
feat(apollo): [Data Collection 8] Apply outgoing request body policy
2 parents 0d69665 + 7a9630f commit f665ca5

4 files changed

Lines changed: 62 additions & 20 deletions

File tree

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,18 @@ constructor(
365365
request.body?.let {
366366
bodySize = it.contentLength
367367

368-
val buffer = Buffer()
369-
370-
try {
371-
it.writeTo(buffer)
372-
data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options)
373-
} catch (e: Throwable) {
374-
scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e)
375-
// continue because the response body alone can already give some insights
376-
} finally {
377-
buffer.close()
368+
if (scopes.options.dataCollectionResolver.isOutgoingRequestBody) {
369+
val buffer = Buffer()
370+
371+
try {
372+
it.writeTo(buffer)
373+
data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options)
374+
} catch (e: Throwable) {
375+
scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e)
376+
// continue because the response body alone can already give some insights
377+
} finally {
378+
buffer.close()
379+
}
378380
}
379381
}
380382
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.apollographql.apollo3.api.http.HttpRequest
55
import com.apollographql.apollo3.api.http.HttpResponse
66
import com.apollographql.apollo3.exception.ApolloException
77
import io.sentry.Hint
8+
import io.sentry.HttpBodyType
89
import io.sentry.IScopes
910
import io.sentry.SentryIntegrationPackageStorage
1011
import io.sentry.SentryOptions
@@ -268,6 +269,24 @@ class SentryApollo3InterceptorClientErrors {
268269
)
269270
}
270271

272+
@Test
273+
fun `data collection can disable outgoing request body`() {
274+
val sut =
275+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
276+
dataCollection.httpBodies = setOf(HttpBodyType.INCOMING_RESPONSE)
277+
}
278+
executeQuery(sut)
279+
280+
verify(fixture.scopes)
281+
.captureEvent(
282+
check {
283+
assertEquals(193L, it.request!!.bodySize)
284+
assertNull(it.request!!.data)
285+
},
286+
any<Hint>(),
287+
)
288+
}
289+
271290
@Test
272291
fun `data collection can disable the GraphQL document independently`() {
273292
val sut =

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,18 @@ constructor(
364364
request.body?.let {
365365
bodySize = it.contentLength
366366

367-
val buffer = Buffer()
368-
369-
try {
370-
it.writeTo(buffer)
371-
data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options)
372-
} catch (e: Throwable) {
373-
scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e)
374-
// continue because the response body alone can already give some insights
375-
} finally {
376-
buffer.close()
367+
if (scopes.options.dataCollectionResolver.isOutgoingRequestBody) {
368+
val buffer = Buffer()
369+
370+
try {
371+
it.writeTo(buffer)
372+
data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options)
373+
} catch (e: Throwable) {
374+
scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e)
375+
// continue because the response body alone can already give some insights
376+
} finally {
377+
buffer.close()
378+
}
377379
}
378380
}
379381
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.apollographql.apollo.api.http.HttpRequest
88
import com.apollographql.apollo.api.http.HttpResponse
99
import com.apollographql.apollo.exception.ApolloException
1010
import io.sentry.Hint
11+
import io.sentry.HttpBodyType
1112
import io.sentry.IScopes
1213
import io.sentry.SentryIntegrationPackageStorage
1314
import io.sentry.SentryOptions
@@ -282,6 +283,24 @@ abstract class SentryApollo4BuilderExtensionsClientErrorsTest(
282283
)
283284
}
284285

286+
@Test
287+
fun `data collection can disable outgoing request body`() {
288+
val sut =
289+
fixture.getSut(responseBody = fixture.responseBodyNotOk) {
290+
dataCollection.httpBodies = setOf(HttpBodyType.INCOMING_RESPONSE)
291+
}
292+
executeQuery(sut)
293+
294+
verify(fixture.scopes)
295+
.captureEvent(
296+
check {
297+
assertEquals(193L, it.request!!.bodySize)
298+
assertNull(it.request!!.data)
299+
},
300+
any<Hint>(),
301+
)
302+
}
303+
285304
@Test
286305
fun `data collection can disable the GraphQL document independently`() {
287306
val sut =

0 commit comments

Comments
 (0)