Skip to content

Improve Log Attributes API #4416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: feat/logs-attributes-in-api
Choose a base branch
from

Conversation

adinauer
Copy link
Member

@adinauer adinauer commented May 19, 2025

📜 Description

Improve Log Attributes API:

  • Add Sentry.logger().log() overload that takes a LogParams param object
    • This avoids having tons of overloads and creating a large pyramid of methods
    • Having multiple overloads is especially problematic with varargs since it can easily become ambiguous
  • Add SentryAttributes and SentryAttribute
  • SentryAttributeType enum

💡 Motivation and Context

💚 How did you test it?

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link
Contributor

Fails
🚫 Please consider adding a changelog entry for the next release.
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

- Improve Log Attributes API ([#4416](https://github.com/getsentry/sentry-java/pull/4416))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description.

Generated by 🚫 dangerJS against ffb15c2

Copy link
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 401.20 ms 545.47 ms 144.27 ms
Size 1.58 MiB 2.08 MiB 510.53 KiB

Baseline results on branch: feat/logs-attributes-in-api

Startup times

Revision Plain With Sentry Diff
93da41e 429.70 ms 484.57 ms 54.87 ms
878e381 402.20 ms 417.65 ms 15.45 ms

App size

Revision Plain With Sentry Diff
93da41e 1.58 MiB 2.08 MiB 510.52 KiB
878e381 1.58 MiB 2.08 MiB 510.51 KiB

return new SentryAttribute(name, null, value);
}

public static @NotNull SentryAttribute booleanAttribute(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L: how about boolean, integer, etc. instead?

@Nullable Map<String, Object> attributes,
@NotNull SentryLogLevel level,
@Nullable SentryDate timestamp,
@NotNull LogParams params,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L: should it be named SentryLogParams or SentryLogParameters maybe?

Comment on lines +29 to +37
public static @NotNull LogParams create(
final @Nullable SentryDate timestamp, final @Nullable SentryAttributes attributes) {
final @NotNull LogParams params = new LogParams();

params.setTimestamp(timestamp);
params.setAttributes(attributes);

return params;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this not directly the constructor?

Comment on lines +33 to +36
public static @NotNull SentryAttribute named(
final @NotNull String name, final @Nullable Object value) {
return new SentryAttribute(name, null, value);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We call this in sentry/src/main/java/io/sentry/SentryAttributes.java when building attributes from a Map.
As we take in a generic Map, the key for an entry could be null.
We should check for nulls either here (making the name param nullable and e.g. not constructing the attribute if it is) or in sentry/src/main/java/io/sentry/SentryAttributes.java when iterating the entry set.

}

public static @NotNull SentryAttributes of(SentryAttribute... attributes) {
return new SentryAttributes(Arrays.asList(attributes));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where we use this class, we rely on this.attributes not containing nulls, however one or more elements in the list could be null and we would include them in the list, risking a NPE down the line.

Comment on lines +38 to +40
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
sentryAttributes.add(SentryAttribute.named(attribute.getKey(), attribute.getValue()));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, we could filter for nulls here.

Comment on lines +169 to +173
for (SentryAttribute attribute : incomingAttributes.getAttributes()) {
final @Nullable Object value = attribute.getValue();
final @NotNull SentryAttributeType type =
attribute.getType() == null ? getType(value) : attribute.getType();
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another case where we rely on the individual attribute being not null.


public final class SentryAttributes {

private final @NotNull List<SentryAttribute> attributes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change this to Map<String, SentryAttribute>?
Now it's possible to add multiple times the same attribute, but only the last one will actually be sent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants