Skip to content

Commit 534ff79

Browse files
authored
Merge pull request #5118 from getsentry/feat/scope-attributes
feat(core): [Global Attributes 1] Add scope-level attributes API
2 parents 01221e2 + 1e7bcf5 commit 534ff79

File tree

17 files changed

+623
-0
lines changed

17 files changed

+623
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- Add scope-level attributes API ([#5118](https://github.com/getsentry/sentry-java/pull/5118))
78
- Create `sentry-opentelemetry-otlp` and `sentry-opentelemetry-otlp-spring` modules for combining OpenTelemetry SDK OTLP export with Sentry SDK ([#5100](https://github.com/getsentry/sentry-java/pull/5100))
89
- OpenTelemetry is configured to send spans to Sentry directly using an OTLP endpoint.
910
- Sentry only uses trace and span ID from OpenTelemetry (via `OpenTelemetryOtlpEventProcessor`) but will not send spans through OpenTelemetry nor use OpenTelemetry `Context` for `Scopes` propagation.

sentry/api/sentry.api

Lines changed: 52 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

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,35 @@ public void removeTag(@Nullable String key) {
241241
getDefaultWriteScope().removeTag(key);
242242
}
243243

244+
@Override
245+
public @NotNull Map<String, SentryAttribute> getAttributes() {
246+
final @NotNull Map<String, SentryAttribute> allAttributes = new ConcurrentHashMap<>();
247+
allAttributes.putAll(globalScope.getAttributes());
248+
allAttributes.putAll(isolationScope.getAttributes());
249+
allAttributes.putAll(scope.getAttributes());
250+
return allAttributes;
251+
}
252+
253+
@Override
254+
public void setAttribute(@Nullable String key, @Nullable Object value) {
255+
getDefaultWriteScope().setAttribute(key, value);
256+
}
257+
258+
@Override
259+
public void setAttribute(@Nullable SentryAttribute attribute) {
260+
getDefaultWriteScope().setAttribute(attribute);
261+
}
262+
263+
@Override
264+
public void setAttributes(@Nullable SentryAttributes attributes) {
265+
getDefaultWriteScope().setAttributes(attributes);
266+
}
267+
268+
@Override
269+
public void removeAttribute(@Nullable String key) {
270+
getDefaultWriteScope().removeAttribute(key);
271+
}
272+
244273
@Override
245274
public @NotNull Map<String, Object> getExtras() {
246275
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 @Nullable SentryAttribute attribute) {
405+
Sentry.setAttribute(attribute);
406+
}
407+
408+
@Override
409+
public void setAttributes(final @Nullable 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 @Nullable SentryAttribute attribute) {
390+
scopes.setAttribute(attribute);
391+
}
392+
393+
@Override
394+
public void setAttributes(final @Nullable 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 @Nullable SentryAttribute attribute);
442+
443+
/**
444+
* Sets multiple attributes on the Scope.
445+
*
446+
* @param attributes the attributes
447+
*/
448+
void setAttributes(final @Nullable 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

sentry/src/main/java/io/sentry/IScopes.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,5 +748,34 @@ default boolean isNoOp() {
748748
@NotNull
749749
IMetricsApi metrics();
750750

751+
/**
752+
* Sets an attribute.
753+
*
754+
* @param key the key
755+
* @param value the value
756+
*/
757+
void setAttribute(final @Nullable String key, final @Nullable Object value);
758+
759+
/**
760+
* Sets an attribute.
761+
*
762+
* @param attribute the attribute
763+
*/
764+
void setAttribute(final @Nullable SentryAttribute attribute);
765+
766+
/**
767+
* Sets multiple attributes.
768+
*
769+
* @param attributes the attributes
770+
*/
771+
void setAttributes(final @Nullable SentryAttributes attributes);
772+
773+
/**
774+
* Removes an attribute.
775+
*
776+
* @param key the key
777+
*/
778+
void removeAttribute(final @Nullable String key);
779+
751780
void addFeatureFlag(final @Nullable String flag, final @Nullable Boolean result);
752781
}

0 commit comments

Comments
 (0)