Skip to content

Commit 5c478cc

Browse files
committed
add enabled check similar to tags
1 parent 80755b7 commit 5c478cc

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

sentry/src/main/java/io/sentry/CombinedScopeView.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.ArrayList;
1515
import java.util.Collection;
1616
import java.util.Collections;
17-
import java.util.HashMap;
1817
import java.util.List;
1918
import java.util.Map;
2019
import java.util.Queue;

sentry/src/main/java/io/sentry/Scopes.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,22 +1231,52 @@ public void reportFullyDisplayed() {
12311231

12321232
@Override
12331233
public void setAttribute(final @Nullable String key, final @Nullable Object value) {
1234-
combinedScope.setAttribute(key, value);
1234+
if (!isEnabled()) {
1235+
getOptions()
1236+
.getLogger()
1237+
.log(
1238+
SentryLevel.WARNING, "Instance is disabled and this 'setAttribute' call is a no-op.");
1239+
} else {
1240+
getCombinedScopeView().setAttribute(key, value);
1241+
}
12351242
}
12361243

12371244
@Override
12381245
public void setAttribute(final @NotNull SentryAttribute attribute) {
1239-
combinedScope.setAttribute(attribute);
1246+
if (!isEnabled()) {
1247+
getOptions()
1248+
.getLogger()
1249+
.log(
1250+
SentryLevel.WARNING, "Instance is disabled and this 'setAttribute' call is a no-op.");
1251+
} else {
1252+
getCombinedScopeView().setAttribute(attribute);
1253+
}
12401254
}
12411255

12421256
@Override
12431257
public void setAttributes(final @NotNull SentryAttributes attributes) {
1244-
combinedScope.setAttributes(attributes);
1258+
if (!isEnabled()) {
1259+
getOptions()
1260+
.getLogger()
1261+
.log(
1262+
SentryLevel.WARNING,
1263+
"Instance is disabled and this 'setAttributes' call is a no-op.");
1264+
} else {
1265+
getCombinedScopeView().setAttributes(attributes);
1266+
}
12451267
}
12461268

12471269
@Override
12481270
public void removeAttribute(final @Nullable String key) {
1249-
combinedScope.removeAttribute(key);
1271+
if (!isEnabled()) {
1272+
getOptions()
1273+
.getLogger()
1274+
.log(
1275+
SentryLevel.WARNING,
1276+
"Instance is disabled and this 'removeAttribute' call is a no-op.");
1277+
} else {
1278+
getCombinedScopeView().removeAttribute(key);
1279+
}
12501280
}
12511281

12521282
@Override

sentry/src/test/java/io/sentry/ScopesTest.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,50 @@ class ScopesTest {
12901290

12911291
// endregion
12921292

1293+
// region setAttribute tests
1294+
@Test
1295+
fun `when setAttribute is called on disabled client, do nothing`() {
1296+
val scopes = generateScopes()
1297+
var scope: IScope? = null
1298+
scopes.configureScope { scope = it }
1299+
scopes.close()
1300+
1301+
scopes.setAttribute("test", "test")
1302+
assertEquals(0, scope?.attributes?.count())
1303+
}
1304+
1305+
@Test
1306+
fun `when setAttribute with SentryAttribute is called on disabled client, do nothing`() {
1307+
val scopes = generateScopes()
1308+
var scope: IScope? = null
1309+
scopes.configureScope { scope = it }
1310+
scopes.close()
1311+
1312+
scopes.setAttribute(SentryAttribute.stringAttribute("test", "test"))
1313+
assertEquals(0, scope?.attributes?.count())
1314+
}
1315+
1316+
@Test
1317+
fun `when setAttributes is called on disabled client, do nothing`() {
1318+
val scopes = generateScopes()
1319+
var scope: IScope? = null
1320+
scopes.configureScope { scope = it }
1321+
scopes.close()
1322+
1323+
scopes.setAttributes(SentryAttributes.of(SentryAttribute.stringAttribute("test", "test")))
1324+
assertEquals(0, scope?.attributes?.count())
1325+
}
1326+
1327+
@Test
1328+
fun `when removeAttribute is called on disabled client, do nothing`() {
1329+
val scopes = generateScopes()
1330+
scopes.close()
1331+
1332+
scopes.removeAttribute("test")
1333+
}
1334+
1335+
// endregion
1336+
12931337
// region captureEnvelope tests
12941338
@Test
12951339
fun `when captureEnvelope is called and envelope is null, throws IllegalArgumentException`() {

0 commit comments

Comments
 (0)