|
| 1 | +package io.sentry.spring7.cache; |
| 2 | + |
| 3 | +import io.sentry.IScopes; |
| 4 | +import io.sentry.ISpan; |
| 5 | +import io.sentry.SpanDataConvention; |
| 6 | +import io.sentry.SpanOptions; |
| 7 | +import io.sentry.SpanStatus; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 11 | +import org.jetbrains.annotations.ApiStatus; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | +import org.springframework.cache.Cache; |
| 15 | + |
| 16 | +/** Wraps a Spring {@link Cache} to create Sentry spans for cache operations. */ |
| 17 | +@ApiStatus.Internal |
| 18 | +public final class SentryCacheWrapper implements Cache { |
| 19 | + |
| 20 | + private static final String TRACE_ORIGIN = "auto.cache.spring"; |
| 21 | + |
| 22 | + private final @NotNull Cache delegate; |
| 23 | + private final @NotNull IScopes scopes; |
| 24 | + |
| 25 | + public SentryCacheWrapper(final @NotNull Cache delegate, final @NotNull IScopes scopes) { |
| 26 | + this.delegate = delegate; |
| 27 | + this.scopes = scopes; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public @NotNull String getName() { |
| 32 | + return delegate.getName(); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public @NotNull Object getNativeCache() { |
| 37 | + return delegate.getNativeCache(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public @Nullable ValueWrapper get(final @NotNull Object key) { |
| 42 | + final ISpan span = startSpan("cache.get", key); |
| 43 | + if (span == null) { |
| 44 | + return delegate.get(key); |
| 45 | + } |
| 46 | + try { |
| 47 | + final ValueWrapper result = delegate.get(key); |
| 48 | + span.setData(SpanDataConvention.CACHE_HIT_KEY, result != null); |
| 49 | + span.setStatus(SpanStatus.OK); |
| 50 | + return result; |
| 51 | + } catch (Throwable e) { |
| 52 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 53 | + span.setThrowable(e); |
| 54 | + throw e; |
| 55 | + } finally { |
| 56 | + span.finish(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public @Nullable <T> T get(final @NotNull Object key, final @Nullable Class<T> type) { |
| 62 | + final ISpan span = startSpan("cache.get", key); |
| 63 | + if (span == null) { |
| 64 | + return delegate.get(key, type); |
| 65 | + } |
| 66 | + try { |
| 67 | + final T result = delegate.get(key, type); |
| 68 | + span.setData(SpanDataConvention.CACHE_HIT_KEY, result != null); |
| 69 | + span.setStatus(SpanStatus.OK); |
| 70 | + return result; |
| 71 | + } catch (Throwable e) { |
| 72 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 73 | + span.setThrowable(e); |
| 74 | + throw e; |
| 75 | + } finally { |
| 76 | + span.finish(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public @Nullable <T> T get(final @NotNull Object key, final @NotNull Callable<T> valueLoader) { |
| 82 | + final ISpan span = startSpan("cache.get", key); |
| 83 | + if (span == null) { |
| 84 | + return delegate.get(key, valueLoader); |
| 85 | + } |
| 86 | + try { |
| 87 | + final AtomicBoolean loaderInvoked = new AtomicBoolean(false); |
| 88 | + final T result = |
| 89 | + delegate.get( |
| 90 | + key, |
| 91 | + () -> { |
| 92 | + loaderInvoked.set(true); |
| 93 | + return valueLoader.call(); |
| 94 | + }); |
| 95 | + span.setData(SpanDataConvention.CACHE_HIT_KEY, !loaderInvoked.get()); |
| 96 | + span.setStatus(SpanStatus.OK); |
| 97 | + return result; |
| 98 | + } catch (Throwable e) { |
| 99 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 100 | + span.setThrowable(e); |
| 101 | + throw e; |
| 102 | + } finally { |
| 103 | + span.finish(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void put(final @NotNull Object key, final @Nullable Object value) { |
| 109 | + final ISpan span = startSpan("cache.put", key); |
| 110 | + if (span == null) { |
| 111 | + delegate.put(key, value); |
| 112 | + return; |
| 113 | + } |
| 114 | + try { |
| 115 | + delegate.put(key, value); |
| 116 | + span.setStatus(SpanStatus.OK); |
| 117 | + } catch (Throwable e) { |
| 118 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 119 | + span.setThrowable(e); |
| 120 | + throw e; |
| 121 | + } finally { |
| 122 | + span.finish(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // putIfAbsent is not instrumented — we cannot know ahead of time whether the put |
| 127 | + // will actually happen, and emitting a cache.put span for a no-op would be misleading. |
| 128 | + // This matches sentry-python and sentry-javascript which also skip conditional puts. |
| 129 | + // We must override to bypass the default implementation which calls this.get() + this.put(). |
| 130 | + @Override |
| 131 | + public @Nullable ValueWrapper putIfAbsent( |
| 132 | + final @NotNull Object key, final @Nullable Object value) { |
| 133 | + return delegate.putIfAbsent(key, value); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void evict(final @NotNull Object key) { |
| 138 | + final ISpan span = startSpan("cache.remove", key); |
| 139 | + if (span == null) { |
| 140 | + delegate.evict(key); |
| 141 | + return; |
| 142 | + } |
| 143 | + try { |
| 144 | + delegate.evict(key); |
| 145 | + span.setStatus(SpanStatus.OK); |
| 146 | + } catch (Throwable e) { |
| 147 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 148 | + span.setThrowable(e); |
| 149 | + throw e; |
| 150 | + } finally { |
| 151 | + span.finish(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean evictIfPresent(final @NotNull Object key) { |
| 157 | + final ISpan span = startSpan("cache.remove", key); |
| 158 | + if (span == null) { |
| 159 | + return delegate.evictIfPresent(key); |
| 160 | + } |
| 161 | + try { |
| 162 | + final boolean result = delegate.evictIfPresent(key); |
| 163 | + span.setStatus(SpanStatus.OK); |
| 164 | + return result; |
| 165 | + } catch (Throwable e) { |
| 166 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 167 | + span.setThrowable(e); |
| 168 | + throw e; |
| 169 | + } finally { |
| 170 | + span.finish(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void clear() { |
| 176 | + final ISpan span = startSpan("cache.flush", null); |
| 177 | + if (span == null) { |
| 178 | + delegate.clear(); |
| 179 | + return; |
| 180 | + } |
| 181 | + try { |
| 182 | + delegate.clear(); |
| 183 | + span.setStatus(SpanStatus.OK); |
| 184 | + } catch (Throwable e) { |
| 185 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 186 | + span.setThrowable(e); |
| 187 | + throw e; |
| 188 | + } finally { |
| 189 | + span.finish(); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public boolean invalidate() { |
| 195 | + final ISpan span = startSpan("cache.flush", null); |
| 196 | + if (span == null) { |
| 197 | + return delegate.invalidate(); |
| 198 | + } |
| 199 | + try { |
| 200 | + final boolean result = delegate.invalidate(); |
| 201 | + span.setStatus(SpanStatus.OK); |
| 202 | + return result; |
| 203 | + } catch (Throwable e) { |
| 204 | + span.setStatus(SpanStatus.INTERNAL_ERROR); |
| 205 | + span.setThrowable(e); |
| 206 | + throw e; |
| 207 | + } finally { |
| 208 | + span.finish(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + private @Nullable ISpan startSpan(final @NotNull String operation, final @Nullable Object key) { |
| 213 | + final ISpan activeSpan = scopes.getSpan(); |
| 214 | + if (activeSpan == null || activeSpan.isNoOp()) { |
| 215 | + return null; |
| 216 | + } |
| 217 | + |
| 218 | + final SpanOptions spanOptions = new SpanOptions(); |
| 219 | + spanOptions.setOrigin(TRACE_ORIGIN); |
| 220 | + final String keyString = key != null ? String.valueOf(key) : null; |
| 221 | + final ISpan span = activeSpan.startChild(operation, keyString, spanOptions); |
| 222 | + if (keyString != null) { |
| 223 | + span.setData(SpanDataConvention.CACHE_KEY_KEY, Arrays.asList(keyString)); |
| 224 | + } |
| 225 | + return span; |
| 226 | + } |
| 227 | +} |
0 commit comments