-
-
Notifications
You must be signed in to change notification settings - Fork 467
feat(spring): [Cache Tracing 1] Add SentryCacheWrapper and SentryCacheManagerWrapper #5172
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
base: feat/cache-tracing
Are you sure you want to change the base?
Changes from all commits
8925435
6e4ede5
6d6f5eb
da5bde0
94ae0e1
0b1b83a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.sentry.spring7.cache; | ||
|
|
||
| import io.sentry.IScopes; | ||
| import java.util.Collection; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
|
|
||
| /** Wraps a Spring {@link CacheManager} to return Sentry-instrumented caches. */ | ||
| @ApiStatus.Internal | ||
| public final class SentryCacheManagerWrapper implements CacheManager { | ||
|
|
||
| private final @NotNull CacheManager delegate; | ||
| private final @NotNull IScopes scopes; | ||
|
|
||
| public SentryCacheManagerWrapper( | ||
| final @NotNull CacheManager delegate, final @NotNull IScopes scopes) { | ||
| this.delegate = delegate; | ||
| this.scopes = scopes; | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable Cache getCache(final @NotNull String name) { | ||
| final Cache cache = delegate.getCache(name); | ||
| if (cache == null || cache instanceof SentryCacheWrapper) { | ||
| return cache; | ||
| } | ||
| return new SentryCacheWrapper(cache, scopes); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Collection<String> getCacheNames() { | ||
| return delegate.getCacheNames(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| package io.sentry.spring7.cache; | ||
|
|
||
| import io.sentry.IScopes; | ||
| import io.sentry.ISpan; | ||
| import io.sentry.SpanDataConvention; | ||
| import io.sentry.SpanOptions; | ||
| import io.sentry.SpanStatus; | ||
| import java.util.Arrays; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.springframework.cache.Cache; | ||
|
|
||
| /** Wraps a Spring {@link Cache} to create Sentry spans for cache operations. */ | ||
| @ApiStatus.Internal | ||
| public final class SentryCacheWrapper implements Cache { | ||
|
|
||
| private static final String TRACE_ORIGIN = "auto.cache.spring"; | ||
|
|
||
| private final @NotNull Cache delegate; | ||
| private final @NotNull IScopes scopes; | ||
|
|
||
| public SentryCacheWrapper(final @NotNull Cache delegate, final @NotNull IScopes scopes) { | ||
| this.delegate = delegate; | ||
| this.scopes = scopes; | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull String getName() { | ||
| return delegate.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Object getNativeCache() { | ||
| return delegate.getNativeCache(); | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable ValueWrapper get(final @NotNull Object key) { | ||
| final ISpan span = startSpan("cache.get", key); | ||
| if (span == null) { | ||
| return delegate.get(key); | ||
| } | ||
| try { | ||
| final ValueWrapper result = delegate.get(key); | ||
| span.setData(SpanDataConvention.CACHE_HIT_KEY, result != null); | ||
| span.setStatus(SpanStatus.OK); | ||
| return result; | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable <T> T get(final @NotNull Object key, final @Nullable Class<T> type) { | ||
| final ISpan span = startSpan("cache.get", key); | ||
| if (span == null) { | ||
| return delegate.get(key, type); | ||
| } | ||
| try { | ||
| final T result = delegate.get(key, type); | ||
| span.setData(SpanDataConvention.CACHE_HIT_KEY, result != null); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixThe Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| span.setStatus(SpanStatus.OK); | ||
| return result; | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public @Nullable <T> T get(final @NotNull Object key, final @NotNull Callable<T> valueLoader) { | ||
| final ISpan span = startSpan("cache.get", key); | ||
| if (span == null) { | ||
| return delegate.get(key, valueLoader); | ||
| } | ||
| try { | ||
| final AtomicBoolean loaderInvoked = new AtomicBoolean(false); | ||
| final T result = | ||
| delegate.get( | ||
| key, | ||
| () -> { | ||
| loaderInvoked.set(true); | ||
| return valueLoader.call(); | ||
| }); | ||
| span.setData(SpanDataConvention.CACHE_HIT_KEY, !loaderInvoked.get()); | ||
| span.setStatus(SpanStatus.OK); | ||
| return result; | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void put(final @NotNull Object key, final @Nullable Object value) { | ||
| final ISpan span = startSpan("cache.put", key); | ||
| if (span == null) { | ||
| delegate.put(key, value); | ||
| return; | ||
| } | ||
| try { | ||
| delegate.put(key, value); | ||
| span.setStatus(SpanStatus.OK); | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| // putIfAbsent is not instrumented — we cannot know ahead of time whether the put | ||
| // will actually happen, and emitting a cache.put span for a no-op would be misleading. | ||
| // This matches sentry-python and sentry-javascript which also skip conditional puts. | ||
| // We must override to bypass the default implementation which calls this.get() + this.put(). | ||
| @Override | ||
| public @Nullable ValueWrapper putIfAbsent( | ||
| final @NotNull Object key, final @Nullable Object value) { | ||
| return delegate.putIfAbsent(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void evict(final @NotNull Object key) { | ||
| final ISpan span = startSpan("cache.remove", key); | ||
| if (span == null) { | ||
| delegate.evict(key); | ||
| return; | ||
| } | ||
| try { | ||
| delegate.evict(key); | ||
| span.setStatus(SpanStatus.OK); | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean evictIfPresent(final @NotNull Object key) { | ||
| final ISpan span = startSpan("cache.remove", key); | ||
| if (span == null) { | ||
| return delegate.evictIfPresent(key); | ||
| } | ||
| try { | ||
| final boolean result = delegate.evictIfPresent(key); | ||
| span.setStatus(SpanStatus.OK); | ||
| return result; | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| final ISpan span = startSpan("cache.flush", null); | ||
| if (span == null) { | ||
| delegate.clear(); | ||
| return; | ||
| } | ||
| try { | ||
| delegate.clear(); | ||
| span.setStatus(SpanStatus.OK); | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean invalidate() { | ||
| final ISpan span = startSpan("cache.flush", null); | ||
| if (span == null) { | ||
| return delegate.invalidate(); | ||
| } | ||
| try { | ||
| final boolean result = delegate.invalidate(); | ||
| span.setStatus(SpanStatus.OK); | ||
| return result; | ||
| } catch (Throwable e) { | ||
| span.setStatus(SpanStatus.INTERNAL_ERROR); | ||
| span.setThrowable(e); | ||
| throw e; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| private @Nullable ISpan startSpan(final @NotNull String operation, final @Nullable Object key) { | ||
| final ISpan activeSpan = scopes.getSpan(); | ||
| if (activeSpan == null || activeSpan.isNoOp()) { | ||
| return null; | ||
| } | ||
|
|
||
| final SpanOptions spanOptions = new SpanOptions(); | ||
| spanOptions.setOrigin(TRACE_ORIGIN); | ||
| final String keyString = key != null ? String.valueOf(key) : null; | ||
| final ISpan span = activeSpan.startChild(operation, keyString, spanOptions); | ||
| if (keyString != null) { | ||
| span.setData(SpanDataConvention.CACHE_KEY_KEY, Arrays.asList(keyString)); | ||
| } | ||
| return span; | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package io.sentry.spring7.cache | ||
|
|
||
| import io.sentry.IScopes | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNull | ||
| import kotlin.test.assertSame | ||
| import kotlin.test.assertTrue | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.whenever | ||
| import org.springframework.cache.Cache | ||
| import org.springframework.cache.CacheManager | ||
|
|
||
| class SentryCacheManagerWrapperTest { | ||
|
|
||
| private val scopes: IScopes = mock() | ||
| private val delegate: CacheManager = mock() | ||
|
|
||
| @Test | ||
| fun `getCache wraps returned cache in SentryCacheWrapper`() { | ||
| val cache = mock<Cache>() | ||
| whenever(delegate.getCache("test")).thenReturn(cache) | ||
|
|
||
| val wrapper = SentryCacheManagerWrapper(delegate, scopes) | ||
| val result = wrapper.getCache("test") | ||
|
|
||
| assertTrue(result is SentryCacheWrapper) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getCache returns null when delegate returns null`() { | ||
| whenever(delegate.getCache("missing")).thenReturn(null) | ||
|
|
||
| val wrapper = SentryCacheManagerWrapper(delegate, scopes) | ||
| val result = wrapper.getCache("missing") | ||
|
|
||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getCache does not double-wrap SentryCacheWrapper`() { | ||
| val innerCache = mock<Cache>() | ||
| val alreadyWrapped = SentryCacheWrapper(innerCache, scopes) | ||
| whenever(delegate.getCache("test")).thenReturn(alreadyWrapped) | ||
|
|
||
| val wrapper = SentryCacheManagerWrapper(delegate, scopes) | ||
| val result = wrapper.getCache("test") | ||
|
|
||
| assertSame(alreadyWrapped, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getCacheNames delegates to underlying cache manager`() { | ||
| whenever(delegate.cacheNames).thenReturn(listOf("cache1", "cache2")) | ||
|
|
||
| val wrapper = SentryCacheManagerWrapper(delegate, scopes) | ||
| val result = wrapper.cacheNames | ||
|
|
||
| assertEquals(listOf("cache1", "cache2"), result) | ||
| } | ||
| } |


Uh oh!
There was an error while loading. Please reload this page.