Skip to content

Commit de80a23

Browse files
adinauerclaude
andcommitted
ref: Split out LoggerApi/MetricsApi changes for stacked PR
Move factory method extractions (SentryAttributeType.inferFrom, SentryLogEventAttributeValue.fromAttribute) and LoggerApi/MetricsApi scope attribute integration to a separate stacked PR. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7cd38e6 commit de80a23

File tree

6 files changed

+36
-53
lines changed

6 files changed

+36
-53
lines changed

CHANGELOG.md

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

55
### Features
66

7-
- Add API to set attributes on scope that are automatically included in logs and metrics ([#5118](https://github.com/getsentry/sentry-java/pull/5118))
7+
- Add scope-level attributes API ([#5118](https://github.com/getsentry/sentry-java/pull/5118))
88

99
### Fixes
1010

sentry/api/sentry.api

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,6 @@ public final class io/sentry/SentryAttributeType : java/lang/Enum {
28292829
public static final field INTEGER Lio/sentry/SentryAttributeType;
28302830
public static final field STRING Lio/sentry/SentryAttributeType;
28312831
public fun apiName ()Ljava/lang/String;
2832-
public static fun inferFrom (Ljava/lang/Object;)Lio/sentry/SentryAttributeType;
28332832
public static fun valueOf (Ljava/lang/String;)Lio/sentry/SentryAttributeType;
28342833
public static fun values ()[Lio/sentry/SentryAttributeType;
28352834
}
@@ -3339,7 +3338,6 @@ public final class io/sentry/SentryLogEvent$JsonKeys {
33393338
public final class io/sentry/SentryLogEventAttributeValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
33403339
public fun <init> (Lio/sentry/SentryAttributeType;Ljava/lang/Object;)V
33413340
public fun <init> (Ljava/lang/String;Ljava/lang/Object;)V
3342-
public static fun fromAttribute (Lio/sentry/SentryAttribute;)Lio/sentry/SentryLogEventAttributeValue;
33433341
public fun getType ()Ljava/lang/String;
33443342
public fun getUnknown ()Ljava/util/Map;
33453343
public fun getValue ()Ljava/lang/Object;

sentry/src/main/java/io/sentry/SentryAttributeType.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.Locale;
44
import org.jetbrains.annotations.NotNull;
5-
import org.jetbrains.annotations.Nullable;
65

76
public enum SentryAttributeType {
87
STRING,
@@ -13,17 +12,4 @@ public enum SentryAttributeType {
1312
public @NotNull String apiName() {
1413
return name().toLowerCase(Locale.ROOT);
1514
}
16-
17-
public static @NotNull SentryAttributeType inferFrom(final @Nullable Object value) {
18-
if (value instanceof Boolean) {
19-
return BOOLEAN;
20-
}
21-
if (value instanceof Integer) {
22-
return INTEGER;
23-
}
24-
if (value instanceof Number) {
25-
return DOUBLE;
26-
}
27-
return STRING;
28-
}
2915
}

sentry/src/main/java/io/sentry/SentryLogEventAttributeValue.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ public SentryLogEventAttributeValue(
2727
this(type.apiName(), value);
2828
}
2929

30-
/**
31-
* Creates a {@link SentryLogEventAttributeValue} from a {@link SentryAttribute}, inferring the
32-
* type if not explicitly set.
33-
*
34-
* @param attribute the attribute
35-
* @return the attribute value
36-
*/
37-
public static @NotNull SentryLogEventAttributeValue fromAttribute(
38-
final @NotNull SentryAttribute attribute) {
39-
final @Nullable Object value = attribute.getValue();
40-
final @NotNull SentryAttributeType type =
41-
attribute.getType() == null ? SentryAttributeType.inferFrom(value) : attribute.getType();
42-
return new SentryLogEventAttributeValue(type, value);
43-
}
44-
4530
public @NotNull String getType() {
4631
return type;
4732
}

sentry/src/main/java/io/sentry/logger/LoggerApi.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import io.sentry.util.Platform;
2222
import io.sentry.util.TracingUtils;
2323
import java.util.HashMap;
24-
import java.util.Map;
2524
import org.jetbrains.annotations.NotNull;
2625
import org.jetbrains.annotations.Nullable;
2726

@@ -164,14 +163,6 @@ private void captureLog(
164163
final @NotNull String message,
165164
final @Nullable Object... args) {
166165
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();
167-
168-
final @NotNull Map<String, SentryAttribute> scopeAttributes =
169-
scopes.getCombinedScopeView().getAttributes();
170-
for (SentryAttribute scopeAttribute : scopeAttributes.values()) {
171-
attributes.put(
172-
scopeAttribute.getName(), SentryLogEventAttributeValue.fromAttribute(scopeAttribute));
173-
}
174-
175166
final @NotNull String origin = params.getOrigin();
176167
if (!"manual".equalsIgnoreCase(origin)) {
177168
attributes.put(
@@ -182,14 +173,17 @@ private void captureLog(
182173

183174
if (incomingAttributes != null) {
184175
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
185-
attributes.put(attribute.getName(), SentryLogEventAttributeValue.fromAttribute(attribute));
176+
final @Nullable Object value = attribute.getValue();
177+
final @NotNull SentryAttributeType type =
178+
attribute.getType() == null ? getType(value) : attribute.getType();
179+
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
186180
}
187181
}
188182

189183
if (args != null) {
190184
int i = 0;
191185
for (Object arg : args) {
192-
final @NotNull SentryAttributeType type = SentryAttributeType.inferFrom(arg);
186+
final @NotNull SentryAttributeType type = getType(arg);
193187
attributes.put(
194188
"sentry.message.parameter." + i, new SentryLogEventAttributeValue(type, arg));
195189
i++;
@@ -298,4 +292,17 @@ private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue
298292
}
299293
}
300294
}
295+
296+
private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
297+
if (arg instanceof Boolean) {
298+
return SentryAttributeType.BOOLEAN;
299+
}
300+
if (arg instanceof Integer) {
301+
return SentryAttributeType.INTEGER;
302+
}
303+
if (arg instanceof Number) {
304+
return SentryAttributeType.DOUBLE;
305+
}
306+
return SentryAttributeType.STRING;
307+
}
301308
}

sentry/src/main/java/io/sentry/metrics/MetricsApi.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import io.sentry.util.Platform;
2121
import io.sentry.util.TracingUtils;
2222
import java.util.HashMap;
23-
import java.util.Map;
2423
import org.jetbrains.annotations.NotNull;
2524
import org.jetbrains.annotations.Nullable;
2625

@@ -168,14 +167,6 @@ private void captureMetrics(
168167
private @NotNull HashMap<String, SentryLogEventAttributeValue> createAttributes(
169168
final @NotNull SentryMetricsParameters params) {
170169
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();
171-
172-
final @NotNull Map<String, SentryAttribute> scopeAttributes =
173-
scopes.getCombinedScopeView().getAttributes();
174-
for (SentryAttribute scopeAttribute : scopeAttributes.values()) {
175-
attributes.put(
176-
scopeAttribute.getName(), SentryLogEventAttributeValue.fromAttribute(scopeAttribute));
177-
}
178-
179170
final @NotNull String origin = params.getOrigin();
180171
if (!"manual".equalsIgnoreCase(origin)) {
181172
attributes.put(
@@ -186,7 +177,10 @@ private void captureMetrics(
186177

187178
if (incomingAttributes != null) {
188179
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
189-
attributes.put(attribute.getName(), SentryLogEventAttributeValue.fromAttribute(attribute));
180+
final @Nullable Object value = attribute.getValue();
181+
final @NotNull SentryAttributeType type =
182+
attribute.getType() == null ? getType(value) : attribute.getType();
183+
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
190184
}
191185
}
192186

@@ -285,4 +279,17 @@ private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue
285279
}
286280
}
287281
}
282+
283+
private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
284+
if (arg instanceof Boolean) {
285+
return SentryAttributeType.BOOLEAN;
286+
}
287+
if (arg instanceof Integer) {
288+
return SentryAttributeType.INTEGER;
289+
}
290+
if (arg instanceof Number) {
291+
return SentryAttributeType.DOUBLE;
292+
}
293+
return SentryAttributeType.STRING;
294+
}
288295
}

0 commit comments

Comments
 (0)