Skip to content
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

Calling Sentry.init and specifying contextTags now has an effect on the Logback SentryAppender #2052

Merged
merged 8 commits into from
May 25, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

- SentryThread.current flag will not be overridden by DefaultAndroidEventProcessor if already set ([#2050](https://github.com/getsentry/sentry-java/pull/2050))
- Fix serialization of Long inside of Request.data ([#2051](https://github.com/getsentry/sentry-java/pull/2051))
- Calling Sentry.init and specifying contextTags now has an effect on the Logback SentryAppender ([#2052](https://github.com/getsentry/sentry-java/pull/2052))

## 6.0.0-beta.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.sentry.Breadcrumb;
import io.sentry.DateUtils;
import io.sentry.Hint;
import io.sentry.HubAdapter;
import io.sentry.ITransportFactory;
import io.sentry.Sentry;
import io.sentry.SentryEvent;
Expand All @@ -33,13 +34,15 @@
/** Appender for logback in charge of sending the logged events to a Sentry server. */
@Open
public class SentryAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
// WARNING: Do not use these options in here, they are only to be used for startup
romtsn marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should remove this options altogether and set the values directly in the configuration callback when initing the SDK, unless there's a reason to not do so.

Copy link
Member Author

Choose a reason for hiding this comment

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

See comment above regarding #2053

private @NotNull SentryOptions options = new SentryOptions();
private @Nullable ITransportFactory transportFactory;
private @NotNull Level minimumBreadcrumbLevel = Level.INFO;
private @NotNull Level minimumEventLevel = Level.ERROR;

@Override
public void start() {
// NOTE: logback.xml properties will only be applied if the SDK has not yet been initialized
if (!Sentry.isEnabled()) {
if (options.getDsn() == null || !options.getDsn().endsWith("_IS_UNDEFINED")) {
options.setEnableExternalConfiguration(true);
Expand Down Expand Up @@ -112,8 +115,11 @@ protected void append(@NotNull ILoggingEvent eventObject) {
CollectionUtils.filterMapEntries(
loggingEvent.getMDCPropertyMap(), entry -> entry.getValue() != null);
if (!mdcProperties.isEmpty()) {
if (!options.getContextTags().isEmpty()) {
for (final String contextTag : options.getContextTags()) {
// get tags from HubAdapter options to allow getting the correct tags if Sentry has been
// initialized somewhere else
final List<String> contextTags = HubAdapter.getInstance().getOptions().getContextTags();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit confusing, we have the options property, but we read from HubAdapter.getInstance().getOptions().
Should we rather get rid of the options then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's why i created #2053 I just didn't want to do it now as this quick fix solves the current problem and further improvements didn't seem urgent

adinauer marked this conversation as resolved.
Show resolved Hide resolved
if (!contextTags.isEmpty()) {
for (final String contextTag : contextTags) {
// if mdc tag is listed in SentryOptions, apply as event tag
if (mdcProperties.containsKey(contextTag)) {
event.setTag(contextTag, mdcProperties.get(contextTag));
Expand Down