Skip to content

Commit ff555aa

Browse files
adinauerclaude
andcommitted
feat(core): Add scope-level attributes API
Add setAttribute, setAttributes, removeAttribute, and getAttributes to IScope/IScopes/Sentry so users can set attributes on the scope that are automatically included in logs and metrics events. Also refactor type inference logic into SentryAttributeType.inferFrom and add SentryLogEventAttributeValue.fromAttribute factory method, removing duplicate getType helpers from LoggerApi and MetricsApi. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 382d6c1 commit ff555aa

19 files changed

+601
-35
lines changed

sentry/api/sentry.api

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ public final class io/sentry/CombinedScopeView : io/sentry/IScope {
276276
public synthetic fun clone ()Ljava/lang/Object;
277277
public fun endSession ()Lio/sentry/Session;
278278
public fun getAttachments ()Ljava/util/List;
279+
public fun getAttributes ()Ljava/util/Map;
279280
public fun getBreadcrumbs ()Ljava/util/Queue;
280281
public fun getClient ()Lio/sentry/ISentryClient;
281282
public fun getContexts ()Lio/sentry/protocol/Contexts;
@@ -298,11 +299,15 @@ public final class io/sentry/CombinedScopeView : io/sentry/IScope {
298299
public fun getTransaction ()Lio/sentry/ITransaction;
299300
public fun getTransactionName ()Ljava/lang/String;
300301
public fun getUser ()Lio/sentry/protocol/User;
302+
public fun removeAttribute (Ljava/lang/String;)V
301303
public fun removeContexts (Ljava/lang/String;)V
302304
public fun removeExtra (Ljava/lang/String;)V
303305
public fun removeTag (Ljava/lang/String;)V
304306
public fun replaceOptions (Lio/sentry/SentryOptions;)V
305307
public fun setActiveSpan (Lio/sentry/ISpan;)V
308+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
309+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
310+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
306311
public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
307312
public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
308313
public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V
@@ -670,10 +675,14 @@ public final class io/sentry/HubAdapter : io/sentry/IHub {
670675
public fun popScope ()V
671676
public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
672677
public fun pushScope ()Lio/sentry/ISentryLifecycleToken;
678+
public fun removeAttribute (Ljava/lang/String;)V
673679
public fun removeExtra (Ljava/lang/String;)V
674680
public fun removeTag (Ljava/lang/String;)V
675681
public fun reportFullyDisplayed ()V
676682
public fun setActiveSpan (Lio/sentry/ISpan;)V
683+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
684+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
685+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
677686
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
678687
public fun setFingerprint (Ljava/util/List;)V
679688
public fun setLevel (Lio/sentry/SentryLevel;)V
@@ -742,10 +751,14 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub {
742751
public fun popScope ()V
743752
public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
744753
public fun pushScope ()Lio/sentry/ISentryLifecycleToken;
754+
public fun removeAttribute (Ljava/lang/String;)V
745755
public fun removeExtra (Ljava/lang/String;)V
746756
public fun removeTag (Ljava/lang/String;)V
747757
public fun reportFullyDisplayed ()V
748758
public fun setActiveSpan (Lio/sentry/ISpan;)V
759+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
760+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
761+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
749762
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
750763
public fun setFingerprint (Ljava/util/List;)V
751764
public fun setLevel (Lio/sentry/SentryLevel;)V
@@ -865,6 +878,7 @@ public abstract interface class io/sentry/IScope {
865878
public abstract fun clone ()Lio/sentry/IScope;
866879
public abstract fun endSession ()Lio/sentry/Session;
867880
public abstract fun getAttachments ()Ljava/util/List;
881+
public abstract fun getAttributes ()Ljava/util/Map;
868882
public abstract fun getBreadcrumbs ()Ljava/util/Queue;
869883
public abstract fun getClient ()Lio/sentry/ISentryClient;
870884
public abstract fun getContexts ()Lio/sentry/protocol/Contexts;
@@ -887,11 +901,15 @@ public abstract interface class io/sentry/IScope {
887901
public abstract fun getTransaction ()Lio/sentry/ITransaction;
888902
public abstract fun getTransactionName ()Ljava/lang/String;
889903
public abstract fun getUser ()Lio/sentry/protocol/User;
904+
public abstract fun removeAttribute (Ljava/lang/String;)V
890905
public abstract fun removeContexts (Ljava/lang/String;)V
891906
public abstract fun removeExtra (Ljava/lang/String;)V
892907
public abstract fun removeTag (Ljava/lang/String;)V
893908
public abstract fun replaceOptions (Lio/sentry/SentryOptions;)V
894909
public abstract fun setActiveSpan (Lio/sentry/ISpan;)V
910+
public abstract fun setAttribute (Lio/sentry/SentryAttribute;)V
911+
public abstract fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
912+
public abstract fun setAttributes (Lio/sentry/SentryAttributes;)V
895913
public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
896914
public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
897915
public abstract fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V
@@ -1003,10 +1021,14 @@ public abstract interface class io/sentry/IScopes {
10031021
public abstract fun popScope ()V
10041022
public abstract fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
10051023
public abstract fun pushScope ()Lio/sentry/ISentryLifecycleToken;
1024+
public abstract fun removeAttribute (Ljava/lang/String;)V
10061025
public abstract fun removeExtra (Ljava/lang/String;)V
10071026
public abstract fun removeTag (Ljava/lang/String;)V
10081027
public abstract fun reportFullyDisplayed ()V
10091028
public abstract fun setActiveSpan (Lio/sentry/ISpan;)V
1029+
public abstract fun setAttribute (Lio/sentry/SentryAttribute;)V
1030+
public abstract fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
1031+
public abstract fun setAttributes (Lio/sentry/SentryAttributes;)V
10101032
public abstract fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
10111033
public abstract fun setFingerprint (Ljava/util/List;)V
10121034
public abstract fun setLevel (Lio/sentry/SentryLevel;)V
@@ -1579,10 +1601,14 @@ public final class io/sentry/NoOpHub : io/sentry/IHub {
15791601
public fun popScope ()V
15801602
public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
15811603
public fun pushScope ()Lio/sentry/ISentryLifecycleToken;
1604+
public fun removeAttribute (Ljava/lang/String;)V
15821605
public fun removeExtra (Ljava/lang/String;)V
15831606
public fun removeTag (Ljava/lang/String;)V
15841607
public fun reportFullyDisplayed ()V
15851608
public fun setActiveSpan (Lio/sentry/ISpan;)V
1609+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
1610+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
1611+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
15861612
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
15871613
public fun setFingerprint (Ljava/util/List;)V
15881614
public fun setLevel (Lio/sentry/SentryLevel;)V
@@ -1649,6 +1675,7 @@ public final class io/sentry/NoOpScope : io/sentry/IScope {
16491675
public synthetic fun clone ()Ljava/lang/Object;
16501676
public fun endSession ()Lio/sentry/Session;
16511677
public fun getAttachments ()Ljava/util/List;
1678+
public fun getAttributes ()Ljava/util/Map;
16521679
public fun getBreadcrumbs ()Ljava/util/Queue;
16531680
public fun getClient ()Lio/sentry/ISentryClient;
16541681
public fun getContexts ()Lio/sentry/protocol/Contexts;
@@ -1672,11 +1699,15 @@ public final class io/sentry/NoOpScope : io/sentry/IScope {
16721699
public fun getTransaction ()Lio/sentry/ITransaction;
16731700
public fun getTransactionName ()Ljava/lang/String;
16741701
public fun getUser ()Lio/sentry/protocol/User;
1702+
public fun removeAttribute (Ljava/lang/String;)V
16751703
public fun removeContexts (Ljava/lang/String;)V
16761704
public fun removeExtra (Ljava/lang/String;)V
16771705
public fun removeTag (Ljava/lang/String;)V
16781706
public fun replaceOptions (Lio/sentry/SentryOptions;)V
16791707
public fun setActiveSpan (Lio/sentry/ISpan;)V
1708+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
1709+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
1710+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
16801711
public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
16811712
public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
16821713
public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V
@@ -1756,10 +1787,14 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes {
17561787
public fun popScope ()V
17571788
public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
17581789
public fun pushScope ()Lio/sentry/ISentryLifecycleToken;
1790+
public fun removeAttribute (Ljava/lang/String;)V
17591791
public fun removeExtra (Ljava/lang/String;)V
17601792
public fun removeTag (Ljava/lang/String;)V
17611793
public fun reportFullyDisplayed ()V
17621794
public fun setActiveSpan (Lio/sentry/ISpan;)V
1795+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
1796+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
1797+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
17631798
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
17641799
public fun setFingerprint (Ljava/util/List;)V
17651800
public fun setLevel (Lio/sentry/SentryLevel;)V
@@ -2325,6 +2360,7 @@ public final class io/sentry/Scope : io/sentry/IScope {
23252360
public synthetic fun clone ()Ljava/lang/Object;
23262361
public fun endSession ()Lio/sentry/Session;
23272362
public fun getAttachments ()Ljava/util/List;
2363+
public fun getAttributes ()Ljava/util/Map;
23282364
public fun getBreadcrumbs ()Ljava/util/Queue;
23292365
public fun getClient ()Lio/sentry/ISentryClient;
23302366
public fun getContexts ()Lio/sentry/protocol/Contexts;
@@ -2347,11 +2383,15 @@ public final class io/sentry/Scope : io/sentry/IScope {
23472383
public fun getTransaction ()Lio/sentry/ITransaction;
23482384
public fun getTransactionName ()Ljava/lang/String;
23492385
public fun getUser ()Lio/sentry/protocol/User;
2386+
public fun removeAttribute (Ljava/lang/String;)V
23502387
public fun removeContexts (Ljava/lang/String;)V
23512388
public fun removeExtra (Ljava/lang/String;)V
23522389
public fun removeTag (Ljava/lang/String;)V
23532390
public fun replaceOptions (Lio/sentry/SentryOptions;)V
23542391
public fun setActiveSpan (Lio/sentry/ISpan;)V
2392+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
2393+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
2394+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
23552395
public fun setContexts (Ljava/lang/String;Ljava/lang/Boolean;)V
23562396
public fun setContexts (Ljava/lang/String;Ljava/lang/Character;)V
23572397
public fun setContexts (Ljava/lang/String;Ljava/lang/Number;)V
@@ -2482,10 +2522,14 @@ public final class io/sentry/Scopes : io/sentry/IScopes {
24822522
public fun popScope ()V
24832523
public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
24842524
public fun pushScope ()Lio/sentry/ISentryLifecycleToken;
2525+
public fun removeAttribute (Ljava/lang/String;)V
24852526
public fun removeExtra (Ljava/lang/String;)V
24862527
public fun removeTag (Ljava/lang/String;)V
24872528
public fun reportFullyDisplayed ()V
24882529
public fun setActiveSpan (Lio/sentry/ISpan;)V
2530+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
2531+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
2532+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
24892533
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
24902534
public fun setFingerprint (Ljava/util/List;)V
24912535
public fun setLevel (Lio/sentry/SentryLevel;)V
@@ -2555,10 +2599,14 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes {
25552599
public fun popScope ()V
25562600
public fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
25572601
public fun pushScope ()Lio/sentry/ISentryLifecycleToken;
2602+
public fun removeAttribute (Ljava/lang/String;)V
25582603
public fun removeExtra (Ljava/lang/String;)V
25592604
public fun removeTag (Ljava/lang/String;)V
25602605
public fun reportFullyDisplayed ()V
25612606
public fun setActiveSpan (Lio/sentry/ISpan;)V
2607+
public fun setAttribute (Lio/sentry/SentryAttribute;)V
2608+
public fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
2609+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
25622610
public fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
25632611
public fun setFingerprint (Ljava/util/List;)V
25642612
public fun setLevel (Lio/sentry/SentryLevel;)V
@@ -2676,10 +2724,14 @@ public final class io/sentry/Sentry {
26762724
public static fun popScope ()V
26772725
public static fun pushIsolationScope ()Lio/sentry/ISentryLifecycleToken;
26782726
public static fun pushScope ()Lio/sentry/ISentryLifecycleToken;
2727+
public static fun removeAttribute (Ljava/lang/String;)V
26792728
public static fun removeExtra (Ljava/lang/String;)V
26802729
public static fun removeTag (Ljava/lang/String;)V
26812730
public static fun replay ()Lio/sentry/IReplayApi;
26822731
public static fun reportFullyDisplayed ()V
2732+
public static fun setAttribute (Lio/sentry/SentryAttribute;)V
2733+
public static fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
2734+
public static fun setAttributes (Lio/sentry/SentryAttributes;)V
26832735
public static fun setCurrentHub (Lio/sentry/IHub;)Lio/sentry/ISentryLifecycleToken;
26842736
public static fun setCurrentScopes (Lio/sentry/IScopes;)Lio/sentry/ISentryLifecycleToken;
26852737
public static fun setExtra (Ljava/lang/String;Ljava/lang/String;)V
@@ -2777,6 +2829,7 @@ public final class io/sentry/SentryAttributeType : java/lang/Enum {
27772829
public static final field INTEGER Lio/sentry/SentryAttributeType;
27782830
public static final field STRING Lio/sentry/SentryAttributeType;
27792831
public fun apiName ()Ljava/lang/String;
2832+
public static fun inferFrom (Ljava/lang/Object;)Lio/sentry/SentryAttributeType;
27802833
public static fun valueOf (Ljava/lang/String;)Lio/sentry/SentryAttributeType;
27812834
public static fun values ()[Lio/sentry/SentryAttributeType;
27822835
}
@@ -3286,6 +3339,7 @@ public final class io/sentry/SentryLogEvent$JsonKeys {
32863339
public final class io/sentry/SentryLogEventAttributeValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
32873340
public fun <init> (Lio/sentry/SentryAttributeType;Ljava/lang/Object;)V
32883341
public fun <init> (Ljava/lang/String;Ljava/lang/Object;)V
3342+
public static fun fromAttribute (Lio/sentry/SentryAttribute;)Lio/sentry/SentryLogEventAttributeValue;
32893343
public fun getType ()Ljava/lang/String;
32903344
public fun getUnknown ()Ljava/util/Map;
32913345
public fun getValue ()Ljava/lang/Object;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.Collection;
1616
import java.util.Collections;
17+
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Map;
1920
import java.util.Queue;
@@ -241,6 +242,35 @@ public void removeTag(@Nullable String key) {
241242
getDefaultWriteScope().removeTag(key);
242243
}
243244

245+
@Override
246+
public @NotNull Map<String, SentryAttribute> getAttributes() {
247+
final @NotNull Map<String, SentryAttribute> allAttributes = new HashMap<>();
248+
allAttributes.putAll(globalScope.getAttributes());
249+
allAttributes.putAll(isolationScope.getAttributes());
250+
allAttributes.putAll(scope.getAttributes());
251+
return allAttributes;
252+
}
253+
254+
@Override
255+
public void setAttribute(@Nullable String key, @Nullable Object value) {
256+
getDefaultWriteScope().setAttribute(key, value);
257+
}
258+
259+
@Override
260+
public void setAttribute(@NotNull SentryAttribute attribute) {
261+
getDefaultWriteScope().setAttribute(attribute);
262+
}
263+
264+
@Override
265+
public void setAttributes(@NotNull SentryAttributes attributes) {
266+
getDefaultWriteScope().setAttributes(attributes);
267+
}
268+
269+
@Override
270+
public void removeAttribute(@Nullable String key) {
271+
getDefaultWriteScope().removeAttribute(key);
272+
}
273+
244274
@Override
245275
public @NotNull Map<String, Object> getExtras() {
246276
final @NotNull Map<String, Object> allTags = new ConcurrentHashMap<>();

sentry/src/main/java/io/sentry/HubAdapter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,26 @@ public void reportFullyDisplayed() {
395395
return Sentry.getCurrentScopes().metrics();
396396
}
397397

398+
@Override
399+
public void setAttribute(final @Nullable String key, final @Nullable Object value) {
400+
Sentry.setAttribute(key, value);
401+
}
402+
403+
@Override
404+
public void setAttribute(final @NotNull SentryAttribute attribute) {
405+
Sentry.setAttribute(attribute);
406+
}
407+
408+
@Override
409+
public void setAttributes(final @NotNull SentryAttributes attributes) {
410+
Sentry.setAttributes(attributes);
411+
}
412+
413+
@Override
414+
public void removeAttribute(final @Nullable String key) {
415+
Sentry.removeAttribute(key);
416+
}
417+
398418
@Override
399419
public void addFeatureFlag(final @Nullable String flag, final @Nullable Boolean result) {
400420
Sentry.addFeatureFlag(flag, result);

sentry/src/main/java/io/sentry/HubScopesWrapper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,26 @@ public void reportFullyDisplayed() {
380380
return scopes.metrics();
381381
}
382382

383+
@Override
384+
public void setAttribute(final @Nullable String key, final @Nullable Object value) {
385+
scopes.setAttribute(key, value);
386+
}
387+
388+
@Override
389+
public void setAttribute(final @NotNull SentryAttribute attribute) {
390+
scopes.setAttribute(attribute);
391+
}
392+
393+
@Override
394+
public void setAttributes(final @NotNull SentryAttributes attributes) {
395+
scopes.setAttributes(attributes);
396+
}
397+
398+
@Override
399+
public void removeAttribute(final @Nullable String key) {
400+
scopes.removeAttribute(key);
401+
}
402+
383403
@Override
384404
public void addFeatureFlag(final @Nullable String flag, final @Nullable Boolean result) {
385405
scopes.addFeatureFlag(flag, result);

sentry/src/main/java/io/sentry/IScope.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,44 @@ void setSpanContext(
425425
@ApiStatus.Internal
426426
void replaceOptions(final @NotNull SentryOptions options);
427427

428+
/**
429+
* Sets an attribute on the Scope.
430+
*
431+
* @param key the key
432+
* @param value the value
433+
*/
434+
void setAttribute(final @Nullable String key, final @Nullable Object value);
435+
436+
/**
437+
* Sets an attribute on the Scope.
438+
*
439+
* @param attribute the attribute
440+
*/
441+
void setAttribute(final @NotNull SentryAttribute attribute);
442+
443+
/**
444+
* Sets multiple attributes on the Scope.
445+
*
446+
* @param attributes the attributes
447+
*/
448+
void setAttributes(final @NotNull SentryAttributes attributes);
449+
450+
/**
451+
* Removes an attribute from the Scope.
452+
*
453+
* @param key the key
454+
*/
455+
void removeAttribute(final @Nullable String key);
456+
457+
/**
458+
* Returns the Scope's attributes
459+
*
460+
* @return the attributes map
461+
*/
462+
@ApiStatus.Internal
463+
@NotNull
464+
Map<String, SentryAttribute> getAttributes();
465+
428466
void addFeatureFlag(final @Nullable String flag, final @Nullable Boolean result);
429467

430468
@ApiStatus.Internal

0 commit comments

Comments
 (0)