|
| 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. This runs after beforeSend and right before sending the event. |
| 21 | + * |
| 22 | + * <p>Fields are reduced in order of least importance: |
| 23 | + * |
| 24 | + * <ol> |
| 25 | + * <li>All breadcrumbs |
| 26 | + * <li>Exception stack frames (keep 250 frames from start and 250 frames from end, removing |
| 27 | + * middle) |
| 28 | + * </ol> |
| 29 | + * |
| 30 | + * <p>Note: Extras, tags, threads, request data, debug meta, and contexts are preserved. |
| 31 | + */ |
| 32 | +@ApiStatus.Internal |
| 33 | +public final class EventSizeLimitingUtils { |
| 34 | + |
| 35 | + private static final long MAX_EVENT_SIZE_BYTES = 1024 * 1024; // 1MB |
| 36 | + private static final int FRAMES_PER_SIDE = 250; // Keep 250 frames from start and 250 from end |
| 37 | + |
| 38 | + private EventSizeLimitingUtils() {} |
| 39 | + |
| 40 | + /** |
| 41 | + * Limits the size of an event by incrementally dropping fields when it exceeds the limit. |
| 42 | + * |
| 43 | + * @param event the event to limit |
| 44 | + * @param hint the hint |
| 45 | + * @param options the SentryOptions |
| 46 | + * @return the potentially reduced event |
| 47 | + */ |
| 48 | + public static @Nullable SentryEvent limitEventSize( |
| 49 | + final @NotNull SentryEvent event, |
| 50 | + final @NotNull Hint hint, |
| 51 | + final @NotNull SentryOptions options) { |
| 52 | + if (!options.isEnableEventSizeLimiting()) { |
| 53 | + return event; |
| 54 | + } |
| 55 | + |
| 56 | + if (!isTooLarge(event, options)) { |
| 57 | + return event; |
| 58 | + } |
| 59 | + |
| 60 | + long eventSize = byteSizeOf(event, options); |
| 61 | + options |
| 62 | + .getLogger() |
| 63 | + .log( |
| 64 | + SentryLevel.INFO, |
| 65 | + "Event size (%d bytes) exceeds %d bytes limit. Reducing size by dropping fields.", |
| 66 | + eventSize, |
| 67 | + MAX_EVENT_SIZE_BYTES); |
| 68 | + |
| 69 | + SentryEvent reducedEvent = event; |
| 70 | + |
| 71 | + // Step 0: Invoke custom callback if defined |
| 72 | + final SentryOptions.OnOversizedErrorCallback onOversizedError = options.getOnOversizedError(); |
| 73 | + if (onOversizedError != null) { |
| 74 | + try { |
| 75 | + reducedEvent = onOversizedError.execute(reducedEvent, hint); |
| 76 | + if (!isTooLarge(reducedEvent, options)) { |
| 77 | + return reducedEvent; |
| 78 | + } |
| 79 | + } catch (Exception e) { |
| 80 | + options |
| 81 | + .getLogger() |
| 82 | + .log( |
| 83 | + SentryLevel.ERROR, |
| 84 | + "The onOversizedError callback threw an exception. It will be ignored and automatic reduction will continue.", |
| 85 | + e); |
| 86 | + // Continue with automatic reduction if callback fails |
| 87 | + reducedEvent = event; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Step 1: Remove all breadcrumbs |
| 92 | + reducedEvent = removeAllBreadcrumbs(reducedEvent, options); |
| 93 | + if (!isTooLarge(reducedEvent, options)) { |
| 94 | + return reducedEvent; |
| 95 | + } |
| 96 | + |
| 97 | + // Step 2: Truncate stack frames (keep 250 from start and 250 from end) |
| 98 | + reducedEvent = truncateStackFrames(reducedEvent, options); |
| 99 | + if (isTooLarge(reducedEvent, options)) { |
| 100 | + long finalEventSize = byteSizeOf(reducedEvent, options); |
| 101 | + options |
| 102 | + .getLogger() |
| 103 | + .log( |
| 104 | + SentryLevel.WARNING, |
| 105 | + "Event size (%d bytes) still exceeds limit after reducing all fields. Event may be rejected by server.", |
| 106 | + finalEventSize); |
| 107 | + } |
| 108 | + |
| 109 | + return reducedEvent; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Checks if the event exceeds the size limit. |
| 114 | + * |
| 115 | + * @param event the event to check |
| 116 | + * @param options the SentryOptions |
| 117 | + * @return true if the event exceeds the size limit |
| 118 | + */ |
| 119 | + private static boolean isTooLarge( |
| 120 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 121 | + return byteSizeOf(event, options) > MAX_EVENT_SIZE_BYTES; |
| 122 | + } |
| 123 | + |
| 124 | + /** Calculates the size of the event when serialized to JSON without actually storing the data. */ |
| 125 | + private static long byteSizeOf( |
| 126 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 127 | + return JsonSerializationUtils.byteSizeOf(options.getSerializer(), options.getLogger(), event); |
| 128 | + } |
| 129 | + |
| 130 | + private static @NotNull SentryEvent removeAllBreadcrumbs( |
| 131 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 132 | + final List<Breadcrumb> breadcrumbs = event.getBreadcrumbs(); |
| 133 | + if (breadcrumbs != null && !breadcrumbs.isEmpty()) { |
| 134 | + event.setBreadcrumbs(null); |
| 135 | + options |
| 136 | + .getLogger() |
| 137 | + .log( |
| 138 | + SentryLevel.DEBUG, "Removed %d breadcrumbs to reduce event size", breadcrumbs.size()); |
| 139 | + } |
| 140 | + return event; |
| 141 | + } |
| 142 | + |
| 143 | + private static @NotNull SentryEvent truncateStackFrames( |
| 144 | + final @NotNull SentryEvent event, final @NotNull SentryOptions options) { |
| 145 | + final List<SentryException> exceptions = event.getExceptions(); |
| 146 | + if (exceptions != null) { |
| 147 | + for (final SentryException exception : exceptions) { |
| 148 | + final SentryStackTrace stacktrace = exception.getStacktrace(); |
| 149 | + if (stacktrace != null) { |
| 150 | + final List<SentryStackFrame> frames = stacktrace.getFrames(); |
| 151 | + if (frames != null && frames.size() > FRAMES_PER_SIDE * 2) { |
| 152 | + // Keep first 250 frames and last 250 frames, removing middle |
| 153 | + final List<SentryStackFrame> truncatedFrames = new ArrayList<>(); |
| 154 | + truncatedFrames.addAll(frames.subList(0, FRAMES_PER_SIDE)); |
| 155 | + truncatedFrames.addAll(frames.subList(frames.size() - FRAMES_PER_SIDE, frames.size())); |
| 156 | + stacktrace.setFrames(truncatedFrames); |
| 157 | + options |
| 158 | + .getLogger() |
| 159 | + .log( |
| 160 | + SentryLevel.DEBUG, |
| 161 | + "Truncated stack frames from %d to %d (removed middle) for exception %s", |
| 162 | + frames.size(), |
| 163 | + truncatedFrames.size(), |
| 164 | + exception.getType()); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Also truncate thread stack traces |
| 171 | + final List<SentryThread> threads = event.getThreads(); |
| 172 | + if (threads != null) { |
| 173 | + for (final SentryThread thread : threads) { |
| 174 | + final SentryStackTrace stacktrace = thread.getStacktrace(); |
| 175 | + if (stacktrace != null) { |
| 176 | + final List<SentryStackFrame> frames = stacktrace.getFrames(); |
| 177 | + if (frames != null && frames.size() > FRAMES_PER_SIDE * 2) { |
| 178 | + // Keep first 250 frames and last 250 frames, removing middle |
| 179 | + final List<SentryStackFrame> truncatedFrames = new ArrayList<>(); |
| 180 | + truncatedFrames.addAll(frames.subList(0, FRAMES_PER_SIDE)); |
| 181 | + truncatedFrames.addAll(frames.subList(frames.size() - FRAMES_PER_SIDE, frames.size())); |
| 182 | + stacktrace.setFrames(truncatedFrames); |
| 183 | + options |
| 184 | + .getLogger() |
| 185 | + .log( |
| 186 | + SentryLevel.DEBUG, |
| 187 | + "Truncated stack frames from %d to %d (removed middle) for thread %d", |
| 188 | + frames.size(), |
| 189 | + truncatedFrames.size(), |
| 190 | + thread.getId()); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return event; |
| 197 | + } |
| 198 | +} |
0 commit comments