|
| 1 | +package io.sentry.util; |
| 2 | + |
| 3 | +import io.sentry.Breadcrumb; |
| 4 | +import io.sentry.Hint; |
| 5 | +import io.sentry.SentryEvent; |
| 6 | +import io.sentry.SentryLevel; |
| 7 | +import io.sentry.SentryOptions; |
| 8 | +import io.sentry.protocol.SentryException; |
| 9 | +import io.sentry.protocol.SentryStackFrame; |
| 10 | +import io.sentry.protocol.SentryStackTrace; |
| 11 | +import io.sentry.protocol.SentryThread; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import org.jetbrains.annotations.ApiStatus; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +/** |
| 19 | + * Utility class that limits event size to 1MB by incrementally dropping fields when the event |
| 20 | + * exceeds the limit. |
| 21 | + */ |
| 22 | +@ApiStatus.Internal |
| 23 | +public final class EventSizeLimitingUtils { |
| 24 | + |
| 25 | + private static final long MAX_EVENT_SIZE_BYTES = 1024 * 1024; |
| 26 | + private static final int FRAMES_PER_SIDE = 250; |
| 27 | + |
| 28 | + private EventSizeLimitingUtils() {} |
| 29 | + |
| 30 | + /** |
| 31 | + * Limits the size of an event by incrementally dropping fields when it exceeds the limit. |
| 32 | + * |
| 33 | + * @param event the event to limit |
| 34 | + * @param hint the hint |
| 35 | + * @param options the SentryOptions |
| 36 | + * @return the potentially reduced event |
| 37 | + */ |
| 38 | + public static @Nullable SentryEvent limitEventSize( |
| 39 | + final @NotNull SentryEvent event, |
| 40 | + final @NotNull Hint hint, |
| 41 | + final @NotNull SentryOptions options) { |
| 42 | + if (!options.isEnableEventSizeLimiting()) { |
| 43 | + return event; |
| 44 | + } |
| 45 | + |
| 46 | + if (isSizeOk(event, options)) { |
| 47 | + return event; |
| 48 | + } |
| 49 | + |
| 50 | + options |
| 51 | + .getLogger() |
| 52 | + .log( |
| 53 | + SentryLevel.INFO, |
| 54 | + "Event %s exceeds %d bytes limit. Reducing size by dropping fields.", |
| 55 | + event.getEventId(), |
| 56 | + MAX_EVENT_SIZE_BYTES); |
| 57 | + |
| 58 | + @NotNull SentryEvent reducedEvent = event; |
| 59 | + |
| 60 | + final @Nullable SentryOptions.OnOversizedErrorCallback callback = options.getOnOversizedError(); |
| 61 | + if (callback != null) { |
| 62 | + try { |
| 63 | + reducedEvent = callback.execute(reducedEvent, hint); |
| 64 | + if (isSizeOk(reducedEvent, options)) { |
| 65 | + return reducedEvent; |
| 66 | + } |
| 67 | + } catch (Exception e) { |
| 68 | + options |
| 69 | + .getLogger() |
| 70 | + .log( |
| 71 | + SentryLevel.ERROR, |
| 72 | + "The onOversizedError callback threw an exception. It will be ignored and automatic reduction will continue.", |
| 73 | + e); |
| 74 | + reducedEvent = event; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + reducedEvent = removeAllBreadcrumbs(reducedEvent, options); |
| 79 | + if (isSizeOk(reducedEvent, options)) { |
| 80 | + return reducedEvent; |
| 81 | + } |
| 82 | + |
| 83 | + reducedEvent = truncateStackFrames(reducedEvent, options); |
| 84 | + if (!isSizeOk(reducedEvent, options)) { |
| 85 | + options |
| 86 | + .getLogger() |
| 87 | + .log( |
| 88 | + SentryLevel.WARNING, |
| 89 | + "Event %s still exceeds size limit after reducing all fields. Event may be rejected by server.", |
| 90 | + event.getEventId()); |
| 91 | + } |
| 92 | + |
| 93 | + return reducedEvent; |
| 94 | + } |
| 95 | + |
| 96 | + private static boolean isSizeOk( |
| 97 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 98 | + final long size = |
| 99 | + JsonSerializationUtils.byteSizeOf(options.getSerializer(), options.getLogger(), event); |
| 100 | + return size <= MAX_EVENT_SIZE_BYTES; |
| 101 | + } |
| 102 | + |
| 103 | + private static @NotNull SentryEvent removeAllBreadcrumbs( |
| 104 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 105 | + final List<Breadcrumb> breadcrumbs = event.getBreadcrumbs(); |
| 106 | + if (breadcrumbs != null && !breadcrumbs.isEmpty()) { |
| 107 | + event.setBreadcrumbs(null); |
| 108 | + options |
| 109 | + .getLogger() |
| 110 | + .log( |
| 111 | + SentryLevel.DEBUG, |
| 112 | + "Removed breadcrumbs to reduce size of event %s", |
| 113 | + event.getEventId()); |
| 114 | + } |
| 115 | + return event; |
| 116 | + } |
| 117 | + |
| 118 | + private static @NotNull SentryEvent truncateStackFrames( |
| 119 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 120 | + final @Nullable List<SentryException> exceptions = event.getExceptions(); |
| 121 | + if (exceptions != null) { |
| 122 | + for (final @NotNull SentryException exception : exceptions) { |
| 123 | + final @Nullable SentryStackTrace stacktrace = exception.getStacktrace(); |
| 124 | + if (stacktrace != null) { |
| 125 | + final @Nullable List<SentryStackFrame> frames = stacktrace.getFrames(); |
| 126 | + if (frames != null && frames.size() > (FRAMES_PER_SIDE * 2)) { |
| 127 | + final @NotNull List<SentryStackFrame> truncatedFrames = new ArrayList<>(); |
| 128 | + truncatedFrames.addAll(frames.subList(0, FRAMES_PER_SIDE)); |
| 129 | + truncatedFrames.addAll(frames.subList(frames.size() - FRAMES_PER_SIDE, frames.size())); |
| 130 | + stacktrace.setFrames(truncatedFrames); |
| 131 | + options |
| 132 | + .getLogger() |
| 133 | + .log( |
| 134 | + SentryLevel.DEBUG, |
| 135 | + "Truncated exception stack frames of event %s", |
| 136 | + event.getEventId()); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + final @Nullable List<SentryThread> threads = event.getThreads(); |
| 143 | + if (threads != null) { |
| 144 | + for (final SentryThread thread : threads) { |
| 145 | + final @Nullable SentryStackTrace stacktrace = thread.getStacktrace(); |
| 146 | + if (stacktrace != null) { |
| 147 | + final @Nullable List<SentryStackFrame> frames = stacktrace.getFrames(); |
| 148 | + if (frames != null && frames.size() > (FRAMES_PER_SIDE * 2)) { |
| 149 | + final @NotNull List<SentryStackFrame> truncatedFrames = new ArrayList<>(); |
| 150 | + truncatedFrames.addAll(frames.subList(0, FRAMES_PER_SIDE)); |
| 151 | + truncatedFrames.addAll(frames.subList(frames.size() - FRAMES_PER_SIDE, frames.size())); |
| 152 | + stacktrace.setFrames(truncatedFrames); |
| 153 | + options |
| 154 | + .getLogger() |
| 155 | + .log( |
| 156 | + SentryLevel.DEBUG, |
| 157 | + "Truncated thread stack frames for event %s", |
| 158 | + event.getEventId()); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return event; |
| 165 | + } |
| 166 | +} |
0 commit comments