Skip to content

Send logs in batches #4378

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-options
Choose a base branch
from
Open

Send logs in batches #4378

wants to merge 1 commit into from

Conversation

adinauer
Copy link
Member

@adinauer adinauer commented May 8, 2025

📜 Description

Instead of sending each log event on its own immediately, we now batch them and send after 5s.

💡 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

github-actions bot commented May 8, 2025

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

- Send logs in batches ([#4378](https://github.com/getsentry/sentry-java/pull/4378))

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 bc8caf7

// TODO SentryLevel.TRACE does not exists yet
// log(SentryLevel.TRACE, message, args);
// TODO SentryLevel.TRACE does not exists yet so we just report it as DEBUG for now
log(SentryLevel.DEBUG, message, args);
Copy link
Member Author

Choose a reason for hiding this comment

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

I opted for this as a quick fix to get trace() working. We can have a proper fix later but we'll have to check what consequences this has for the rest of the SDK since the enum is used in multiple places already.

traceContext = propagationContext.traceContext();
}
}
// @Nullable TraceContext traceContext = null;
Copy link
Member Author

Choose a reason for hiding this comment

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

(maybe) Updating baggage should be done in LoggerApi to freeze it on the first event going out.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, I don't fully understand when it should be done.

@Override
public void add(final @NotNull SentryLogEvent logEvent) {
queue.offer(logEvent);
maybeSchedule(false, false);
Copy link
Member Author

Choose a reason for hiding this comment

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

This means we're locking on every log event. I'll try to optimize this. Ideas:

  1. Have a bool hasScheduled or similar which we check first and avoid locking if already true
  2. Check the futures state first (not sure about performance of that)
  3. Just send a signal and have a background thread pick it up by periodically polling for it

Copy link
Contributor

github-actions bot commented May 8, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 448.32 ms 496.24 ms 47.92 ms
Size 1.58 MiB 2.08 MiB 508.55 KiB

try {
final @NotNull SentryEnvelope envelope =
buildEnvelope(new SentryLogEvents(Arrays.asList(logEvent)), traceContext);
hint.clear();
Copy link
Member

Choose a reason for hiding this comment

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

Before we were passing the hint to sendEnvelope, now we always use null as the hint.
Does it make any difference?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it might have some effect especially for Android but I haven't found a good way to pass the hint through the batch processor unless we hold on to it until it's been sent and even then which Hint should we use if there's 100 log events being batched together?

traceContext = propagationContext.traceContext();
}
}
// @Nullable TraceContext traceContext = null;
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, I don't fully understand when it should be done.

Comment on lines +76 to +79
flushBatch();
if (queue.size() >= MAX_BATCH_SIZE) {
flushInternal();
}
Copy link
Member

Choose a reason for hiding this comment

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

There is no need for recursion, personally I would prefer

Suggested change
flushBatch();
if (queue.size() >= MAX_BATCH_SIZE) {
flushInternal();
}
while (queue.size() >= MAX_BATCH_SIZE) {
flushBatch();
}

Copy link
Member Author

@adinauer adinauer May 8, 2025

Choose a reason for hiding this comment

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

This would mean we send out at most two batches at a time. The way it's implemented atm sends out as many full batches as are available and then maybe reschedules (depending on queue being empty).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I missed the while, sorry

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah we can change it.

Copy link
Member Author

Choose a reason for hiding this comment

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

But needs to be do ... while so we send out before having a full batch

}

@Override
public void close(final boolean isRestarting) {
Copy link
Member

Choose a reason for hiding this comment

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

just wondering, when is isRestarting intended to be used?

Copy link
Member Author

Choose a reason for hiding this comment

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

When the SDK is being re-initialized. LMK if you need more explanation.

Copy link
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

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

I'll approve but I think someone else should double check the baggage logic.

Copy link
Collaborator

@lbloder lbloder left a comment

Choose a reason for hiding this comment

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

LGTM 👍

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