Skip to content

Commit e91f397

Browse files
Only send session update for dropped events if state changed (#2002)
* Change order of filtering mechanisms and add early return * Only send session update for dropped events if state changed * Extract variable for throwable * Add changelog * Add changelog * Rename method * Rename things * Make var final * Apply suggestions from code review Co-authored-by: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> * Update sentry/src/main/java/io/sentry/SentryClient.java Co-authored-by: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> * Add tests to verify order, session updates and sending * Add debug log for dropped events ... ... where the session update is not sent because it does not change the health of the sesssion * Fix merge mistake Co-authored-by: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com>
1 parent 4fd621e commit e91f397

File tree

3 files changed

+385
-6
lines changed

3 files changed

+385
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fix
66

77
* Change order of event filtering mechanisms (#2001)
8+
* Only send session update for dropped events if state changed (#2002)
89

910
## 6.0.0-beta.2
1011

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ private boolean shouldApplyScopeData(
126126
return SentryId.EMPTY_ID;
127127
}
128128

129-
Session session = null;
129+
@Nullable
130+
Session sessionBeforeUpdate =
131+
scope != null ? scope.withSession((@Nullable Session session) -> {}) : null;
132+
@Nullable Session session = null;
130133

131134
if (event != null) {
132135
session = updateSessionData(event, hint, scope);
@@ -146,6 +149,18 @@ private boolean shouldApplyScopeData(
146149
}
147150
}
148151

152+
final boolean shouldSendSessionUpdate =
153+
shouldSendSessionUpdateForDroppedEvent(sessionBeforeUpdate, session);
154+
155+
if (event == null && !shouldSendSessionUpdate) {
156+
options
157+
.getLogger()
158+
.log(
159+
SentryLevel.DEBUG,
160+
"Not sending session update for dropped event as it did not cause the session health to change.");
161+
return SentryId.EMPTY_ID;
162+
}
163+
149164
SentryId sentryId = SentryId.EMPTY_ID;
150165
if (event != null && event.getEventId() != null) {
151166
sentryId = event.getEventId();
@@ -156,8 +171,9 @@ private boolean shouldApplyScopeData(
156171
scope != null && scope.getTransaction() != null
157172
? scope.getTransaction().traceState()
158173
: null;
159-
final SentryEnvelope envelope =
160-
buildEnvelope(event, getAttachments(scope, hint), session, traceState, null);
174+
final boolean shouldSendAttachments = event != null;
175+
List<Attachment> attachments = shouldSendAttachments ? getAttachments(scope, hint) : null;
176+
final SentryEnvelope envelope = buildEnvelope(event, attachments, session, traceState, null);
161177

162178
if (envelope != null) {
163179
transport.send(envelope, hint);
@@ -172,6 +188,32 @@ private boolean shouldApplyScopeData(
172188
return sentryId;
173189
}
174190

191+
private boolean shouldSendSessionUpdateForDroppedEvent(
192+
@Nullable Session sessionBeforeUpdate, @Nullable Session sessionAfterUpdate) {
193+
if (sessionAfterUpdate == null) {
194+
return false;
195+
}
196+
197+
if (sessionBeforeUpdate == null) {
198+
return true;
199+
}
200+
201+
final boolean didSessionMoveToCrashedState =
202+
sessionAfterUpdate.getStatus() == Session.State.Crashed
203+
&& sessionBeforeUpdate.getStatus() != Session.State.Crashed;
204+
if (didSessionMoveToCrashedState) {
205+
return true;
206+
}
207+
208+
final boolean didSessionMoveToErroredState =
209+
sessionAfterUpdate.errorCount() > 0 && sessionBeforeUpdate.errorCount() <= 0;
210+
if (didSessionMoveToErroredState) {
211+
return true;
212+
}
213+
214+
return false;
215+
}
216+
175217
private @Nullable List<Attachment> getAttachments(
176218
final @Nullable Scope scope, final @NotNull Map<String, Object> hint) {
177219
List<Attachment> attachments = null;

0 commit comments

Comments
 (0)