Skip to content

Commit 5e3fa6c

Browse files
authored
Merge pull request #6036 from getsentry/feat/data-collection-external-options
feat(core): [Data Collection 21] Add external configuration
2 parents 14c363d + c55b259 commit 5e3fa6c

5 files changed

Lines changed: 319 additions & 0 deletions

File tree

‎sentry/api/sentry.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ public final class io/sentry/ExternalOptions {
548548
public fun getBundleIds ()Ljava/util/Set;
549549
public fun getContextTags ()Ljava/util/List;
550550
public fun getCron ()Lio/sentry/SentryOptions$Cron;
551+
public fun getDataCollection ()Lio/sentry/DataCollection;
551552
public fun getDebug ()Ljava/lang/Boolean;
552553
public fun getDist ()Ljava/lang/String;
553554
public fun getDsn ()Ljava/lang/String;
@@ -597,6 +598,7 @@ public final class io/sentry/ExternalOptions {
597598
public fun isStrictTraceContinuation ()Ljava/lang/Boolean;
598599
public fun setCaptureOpenTelemetryEvents (Ljava/lang/Boolean;)V
599600
public fun setCron (Lio/sentry/SentryOptions$Cron;)V
601+
public fun setDataCollection (Lio/sentry/DataCollection;)V
600602
public fun setDebug (Ljava/lang/Boolean;)V
601603
public fun setDist (Ljava/lang/String;)V
602604
public fun setDsn (Ljava/lang/String;)V

‎sentry/src/main/java/io/sentry/ExternalOptions.java‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class ExternalOptions {
5555

5656
private @Nullable Boolean sendModules;
5757
private @Nullable Boolean sendDefaultPii;
58+
private @Nullable DataCollection dataCollection;
5859
private @Nullable Boolean enableBackpressureHandling;
5960
private @Nullable Boolean enableDatabaseTransactionTracing;
6061
private @Nullable Boolean enableCacheTracing;
@@ -157,6 +158,7 @@ public final class ExternalOptions {
157158

158159
options.setSendModules(propertiesProvider.getBooleanProperty("send-modules"));
159160
options.setSendDefaultPii(propertiesProvider.getBooleanProperty("send-default-pii"));
161+
options.setDataCollection(parseDataCollection(propertiesProvider));
160162

161163
options.setIgnoredCheckIns(propertiesProvider.getListOrNull("ignored-checkins"));
162164
options.setIgnoredTransactions(propertiesProvider.getListOrNull("ignored-transactions"));
@@ -246,6 +248,101 @@ public final class ExternalOptions {
246248
return options;
247249
}
248250

251+
private static @Nullable DataCollection parseDataCollection(
252+
final @NotNull PropertiesProvider propertiesProvider) {
253+
final DataCollection dataCollection = new DataCollection(false);
254+
255+
final Boolean userInfo = propertiesProvider.getBooleanProperty("data-collection.user-info");
256+
if (userInfo != null) {
257+
dataCollection.setUserInfo(userInfo);
258+
}
259+
260+
final Set<HttpBodyType> httpBodies = parseHttpBodies(propertiesProvider);
261+
if (httpBodies != null) {
262+
dataCollection.setHttpBodies(httpBodies);
263+
}
264+
265+
final KeyValueCollectionBehavior cookies =
266+
parseKeyValueCollectionBehavior(propertiesProvider, "data-collection.cookies");
267+
if (cookies != null) {
268+
dataCollection.setCookies(cookies);
269+
}
270+
271+
final KeyValueCollectionBehavior requestHeaders =
272+
parseKeyValueCollectionBehavior(propertiesProvider, "data-collection.http-headers.request");
273+
if (requestHeaders != null) {
274+
dataCollection.getHttpHeaders().setRequest(requestHeaders);
275+
}
276+
277+
final KeyValueCollectionBehavior responseHeaders =
278+
parseKeyValueCollectionBehavior(
279+
propertiesProvider, "data-collection.http-headers.response");
280+
if (responseHeaders != null) {
281+
dataCollection.getHttpHeaders().setResponse(responseHeaders);
282+
}
283+
284+
final KeyValueCollectionBehavior queryParams =
285+
parseKeyValueCollectionBehavior(propertiesProvider, "data-collection.url-query-params");
286+
if (queryParams != null) {
287+
dataCollection.setUrlQueryParams(queryParams);
288+
}
289+
290+
final Boolean graphqlDocument =
291+
propertiesProvider.getBooleanProperty("data-collection.graphql.document");
292+
if (graphqlDocument != null) {
293+
dataCollection.getGraphql().setDocument(graphqlDocument);
294+
}
295+
296+
final Boolean graphqlVariables =
297+
propertiesProvider.getBooleanProperty("data-collection.graphql.variables");
298+
if (graphqlVariables != null) {
299+
dataCollection.getGraphql().setVariables(graphqlVariables);
300+
}
301+
302+
final Boolean databaseQueryData =
303+
propertiesProvider.getBooleanProperty("data-collection.database-query-data");
304+
if (databaseQueryData != null) {
305+
dataCollection.setDatabaseQueryData(databaseQueryData);
306+
}
307+
308+
return dataCollection.isExplicitlyConfigured() ? dataCollection : null;
309+
}
310+
311+
private static @Nullable Set<HttpBodyType> parseHttpBodies(
312+
final @NotNull PropertiesProvider propertiesProvider) {
313+
final List<String> bodyTypes = propertiesProvider.getListOrNull("data-collection.http-bodies");
314+
if (bodyTypes == null) {
315+
return null;
316+
}
317+
if (bodyTypes.size() == 1 && bodyTypes.get(0).isEmpty()) {
318+
return Collections.emptySet();
319+
}
320+
321+
final Set<HttpBodyType> httpBodies = EnumSet.noneOf(HttpBodyType.class);
322+
for (final String bodyType : bodyTypes) {
323+
httpBodies.add(HttpBodyType.valueOf(bodyType.toUpperCase(Locale.ROOT)));
324+
}
325+
return httpBodies;
326+
}
327+
328+
private static @Nullable KeyValueCollectionBehavior parseKeyValueCollectionBehavior(
329+
final @NotNull PropertiesProvider propertiesProvider, final @NotNull String property) {
330+
final String modeValue = propertiesProvider.getProperty(property + ".mode");
331+
final List<String> terms = propertiesProvider.getListOrNull(property + ".terms");
332+
if (modeValue == null && terms == null) {
333+
return null;
334+
}
335+
336+
final KeyValueCollectionBehavior behavior = new KeyValueCollectionBehavior();
337+
if (modeValue != null) {
338+
behavior.setMode(KeyValueCollectionBehavior.Mode.valueOf(modeValue.toUpperCase(Locale.ROOT)));
339+
}
340+
if (terms != null) {
341+
behavior.setTerms(terms);
342+
}
343+
return behavior;
344+
}
345+
249346
public @Nullable String getDsn() {
250347
return dsn;
251348
}
@@ -501,6 +598,14 @@ public void setSendDefaultPii(final @Nullable Boolean sendDefaultPii) {
501598
this.sendDefaultPii = sendDefaultPii;
502599
}
503600

601+
public @Nullable DataCollection getDataCollection() {
602+
return dataCollection;
603+
}
604+
605+
public void setDataCollection(final @Nullable DataCollection dataCollection) {
606+
this.dataCollection = dataCollection;
607+
}
608+
504609
public void setIgnoredCheckIns(final @Nullable List<String> ignoredCheckIns) {
505610
this.ignoredCheckIns = ignoredCheckIns;
506611
}

‎sentry/src/main/java/io/sentry/SentryOptions.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,9 @@ public void merge(final @NotNull ExternalOptions options) {
37443744
if (options.isSendDefaultPii() != null) {
37453745
setSendDefaultPii(options.isSendDefaultPii());
37463746
}
3747+
if (options.getDataCollection() != null) {
3748+
mergeDataCollection(options.getDataCollection());
3749+
}
37473750
if (options.isCaptureOpenTelemetryEvents() != null) {
37483751
setCaptureOpenTelemetryEvents(options.isCaptureOpenTelemetryEvents());
37493752
}
@@ -3809,6 +3812,40 @@ public void merge(final @NotNull ExternalOptions options) {
38093812
}
38103813
}
38113814

3815+
private void mergeDataCollection(final @NotNull DataCollection externalDataCollection) {
3816+
if (externalDataCollection.getUserInfo() != null) {
3817+
dataCollection.setUserInfo(externalDataCollection.getUserInfo());
3818+
}
3819+
if (externalDataCollection.getHttpBodies() != null) {
3820+
dataCollection.setHttpBodies(externalDataCollection.getHttpBodies());
3821+
}
3822+
if (externalDataCollection.getCookies() != null) {
3823+
dataCollection.setCookies(externalDataCollection.getCookies());
3824+
}
3825+
if (externalDataCollection.getHttpHeaders().getRequest() != null) {
3826+
dataCollection
3827+
.getHttpHeaders()
3828+
.setRequest(externalDataCollection.getHttpHeaders().getRequest());
3829+
}
3830+
if (externalDataCollection.getHttpHeaders().getResponse() != null) {
3831+
dataCollection
3832+
.getHttpHeaders()
3833+
.setResponse(externalDataCollection.getHttpHeaders().getResponse());
3834+
}
3835+
if (externalDataCollection.getUrlQueryParams() != null) {
3836+
dataCollection.setUrlQueryParams(externalDataCollection.getUrlQueryParams());
3837+
}
3838+
if (externalDataCollection.getGraphql().getDocument() != null) {
3839+
dataCollection.getGraphql().setDocument(externalDataCollection.getGraphql().getDocument());
3840+
}
3841+
if (externalDataCollection.getGraphql().getVariables() != null) {
3842+
dataCollection.getGraphql().setVariables(externalDataCollection.getGraphql().getVariables());
3843+
}
3844+
if (externalDataCollection.getDatabaseQueryData() != null) {
3845+
dataCollection.setDatabaseQueryData(externalDataCollection.getDatabaseQueryData());
3846+
}
3847+
}
3848+
38123849
private @NotNull SdkVersion createSdkVersion() {
38133850
final String version = BuildConfig.VERSION_NAME;
38143851
final SdkVersion sdkVersion = new SdkVersion(BuildConfig.SENTRY_JAVA_SDK_NAME, version);

‎sentry/src/test/java/io/sentry/ExternalOptionsTest.kt‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.sentry
22

3+
import com.google.common.truth.Truth.assertThat
34
import io.sentry.config.PropertiesProviderFactory
45
import java.lang.RuntimeException
56
import kotlin.test.Test
67
import kotlin.test.assertEquals
8+
import kotlin.test.assertFailsWith
79
import kotlin.test.assertFalse
810
import kotlin.test.assertNotNull
911
import kotlin.test.assertNull
@@ -15,6 +17,98 @@ import org.mockito.kotlin.mock
1517
import org.mockito.kotlin.verify
1618

1719
class ExternalOptionsTest {
20+
@Test
21+
fun `does not create data collection when external properties are absent`() {
22+
withPropertiesFile { assertThat(it.dataCollection).isNull() }
23+
}
24+
25+
@Test
26+
fun `creates data collection using external properties`() {
27+
withPropertiesFile(
28+
listOf(
29+
"data-collection.user-info=false",
30+
"data-collection.http-bodies=incoming_request,outgoing_response",
31+
"data-collection.cookies.mode=deny_list",
32+
"data-collection.cookies.terms=authorization,session",
33+
"data-collection.http-headers.request.mode=allow_list",
34+
"data-collection.http-headers.request.terms=x-request-id,content-type",
35+
"data-collection.http-headers.response.mode=off",
36+
"data-collection.url-query-params.terms=search",
37+
"data-collection.graphql.document=false",
38+
"data-collection.graphql.variables=true",
39+
"data-collection.database-query-data=false",
40+
)
41+
) { options ->
42+
val dataCollection = options.dataCollection
43+
44+
assertThat(dataCollection).isNotNull()
45+
assertThat(dataCollection!!.userInfo).isFalse()
46+
assertThat(dataCollection.httpBodies)
47+
.containsExactly(HttpBodyType.INCOMING_REQUEST, HttpBodyType.OUTGOING_RESPONSE)
48+
assertThat(dataCollection.cookies)
49+
.isEqualTo(KeyValueCollectionBehavior.denyList("authorization", "session"))
50+
assertThat(dataCollection.httpHeaders.request)
51+
.isEqualTo(KeyValueCollectionBehavior.allowList("x-request-id", "content-type"))
52+
assertThat(dataCollection.httpHeaders.response).isEqualTo(KeyValueCollectionBehavior.off())
53+
assertThat(dataCollection.urlQueryParams)
54+
.isEqualTo(KeyValueCollectionBehavior.denyList("search"))
55+
assertThat(dataCollection.graphql.document).isFalse()
56+
assertThat(dataCollection.graphql.variables).isTrue()
57+
assertThat(dataCollection.databaseQueryData).isFalse()
58+
}
59+
}
60+
61+
@Test
62+
fun `empty HTTP bodies externally disables body collection`() {
63+
withPropertiesFile("data-collection.http-bodies=") { options ->
64+
assertThat(options.dataCollection).isNotNull()
65+
assertThat(options.dataCollection!!.httpBodies).isEmpty()
66+
}
67+
}
68+
69+
@Test
70+
fun `invalid HTTP body type fails external parsing`() {
71+
assertFailsWith<IllegalArgumentException> {
72+
withPropertiesFile("data-collection.http-bodies=invalid") {}
73+
}
74+
}
75+
76+
@Test
77+
fun `invalid collection mode fails external parsing`() {
78+
assertFailsWith<IllegalArgumentException> {
79+
withPropertiesFile("data-collection.cookies.mode=invalid") {}
80+
}
81+
}
82+
83+
@Test
84+
fun `data collection booleans use default external parsing`() {
85+
withPropertiesFile(
86+
listOf(
87+
"data-collection.user-info=invalid",
88+
"data-collection.graphql.document=invalid",
89+
"data-collection.graphql.variables=invalid",
90+
"data-collection.database-query-data=invalid",
91+
)
92+
) { options ->
93+
assertThat(options.dataCollection!!.userInfo).isFalse()
94+
assertThat(options.dataCollection!!.graphql.document).isFalse()
95+
assertThat(options.dataCollection!!.graphql.variables).isFalse()
96+
assertThat(options.dataCollection!!.databaseQueryData).isFalse()
97+
}
98+
}
99+
100+
@Test
101+
fun `external data collection takes precedence over external send default PII`() {
102+
withPropertiesFile(listOf("send-default-pii=false", "data-collection.cookies.mode=off")) {
103+
externalOptions ->
104+
val options = SentryOptions().apply { merge(externalOptions) }
105+
106+
assertThat(options.isSendDefaultPii).isFalse()
107+
assertThat(options.dataCollectionResolver.isUserInfo).isTrue()
108+
assertThat(options.dataCollectionResolver.cookies).isEqualTo(KeyValueCollectionBehavior.off())
109+
}
110+
}
111+
18112
@Test
19113
fun `creates options with proxy using external properties`() {
20114
withPropertiesFile(

‎sentry/src/test/java/io/sentry/SentryOptionsTest.kt‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,87 @@ class SentryOptionsTest {
8282
assertThat(options.dataCollection.userInfo).isFalse()
8383
}
8484

85+
@Test
86+
fun `merging absent external data collection preserves legacy mode`() {
87+
val options = SentryOptions()
88+
89+
options.merge(ExternalOptions())
90+
91+
assertThat(options.dataCollection.isExplicitlyConfigured()).isFalse()
92+
}
93+
94+
@Test
95+
fun `merging external data collection applies only configured values`() {
96+
val options =
97+
SentryOptions().apply {
98+
dataCollection.setUserInfo(false)
99+
dataCollection.cookies = KeyValueCollectionBehavior.allowList("safe")
100+
}
101+
val externalOptions =
102+
ExternalOptions().apply {
103+
dataCollection = DataCollection().apply { graphql.setVariables(false) }
104+
}
105+
106+
options.merge(externalOptions)
107+
108+
assertThat(options.dataCollection.userInfo).isFalse()
109+
assertThat(options.dataCollection.cookies)
110+
.isEqualTo(KeyValueCollectionBehavior.allowList("safe"))
111+
assertThat(options.dataCollection.graphql.variables).isFalse()
112+
}
113+
114+
@Test
115+
fun `merging external data collection applies every supported value`() {
116+
val externalDataCollection =
117+
DataCollection().apply {
118+
setUserInfo(false)
119+
httpBodies = setOf(HttpBodyType.INCOMING_REQUEST, HttpBodyType.OUTGOING_RESPONSE)
120+
cookies = KeyValueCollectionBehavior.denyList("cookie")
121+
httpHeaders.request = KeyValueCollectionBehavior.allowList("request")
122+
httpHeaders.response = KeyValueCollectionBehavior.off()
123+
urlQueryParams = KeyValueCollectionBehavior.denyList("query")
124+
graphql.setDocument(false)
125+
graphql.setVariables(false)
126+
setDatabaseQueryData(false)
127+
}
128+
val options = SentryOptions()
129+
130+
options.merge(ExternalOptions().apply { dataCollection = externalDataCollection })
131+
132+
assertThat(options.dataCollection.userInfo).isFalse()
133+
assertThat(options.dataCollection.httpBodies)
134+
.containsExactly(HttpBodyType.INCOMING_REQUEST, HttpBodyType.OUTGOING_RESPONSE)
135+
assertThat(options.dataCollection.cookies)
136+
.isEqualTo(KeyValueCollectionBehavior.denyList("cookie"))
137+
assertThat(options.dataCollection.httpHeaders.request)
138+
.isEqualTo(KeyValueCollectionBehavior.allowList("request"))
139+
assertThat(options.dataCollection.httpHeaders.response)
140+
.isEqualTo(KeyValueCollectionBehavior.off())
141+
assertThat(options.dataCollection.urlQueryParams)
142+
.isEqualTo(KeyValueCollectionBehavior.denyList("query"))
143+
assertThat(options.dataCollection.graphql.document).isFalse()
144+
assertThat(options.dataCollection.graphql.variables).isFalse()
145+
assertThat(options.dataCollection.databaseQueryData).isFalse()
146+
}
147+
148+
@Test
149+
fun `external data collection takes precedence over send default PII`() {
150+
val externalOptions =
151+
ExternalOptions().apply {
152+
isSendDefaultPii = false
153+
dataCollection = DataCollection().apply { cookies = KeyValueCollectionBehavior.off() }
154+
}
155+
val options = SentryOptions()
156+
157+
options.merge(externalOptions)
158+
159+
assertThat(options.isSendDefaultPii).isFalse()
160+
assertThat(options.dataCollection.isExplicitlyConfigured()).isTrue()
161+
assertThat(options.dataCollectionResolver.isUserInfo).isTrue()
162+
assertThat(options.dataCollectionResolver.isDatabaseQueryData).isTrue()
163+
assertThat(options.dataCollectionResolver.cookies).isEqualTo(KeyValueCollectionBehavior.off())
164+
}
165+
85166
@Test
86167
fun `when options is initialized, logger is not null`() {
87168
assertNotNull(SentryOptions().logger)

0 commit comments

Comments
 (0)