Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ androidx-browser = { module = "androidx.browser:browser", version = "1.8.0" }
async-profiler = { module = "tools.profiler:async-profiler", version.ref = "asyncProfiler" }
async-profiler-jfr-converter = { module = "tools.profiler:jfr-converter", version.ref = "asyncProfiler" }
caffeine = { module = "com.github.ben-manes.caffeine:caffeine" }
caffeine-jcache = { module = "com.github.ben-manes.caffeine:jcache", version = "3.2.0" }
coil-compose = { module = "io.coil-kt:coil-compose", version = "2.6.0" }
commons-compress = {module = "org.apache.commons:commons-compress", version = "1.25.0"}
context-propagation = { module = "io.micrometer:context-propagation", version = "1.1.0" }
Expand Down
3 changes: 3 additions & 0 deletions sentry-samples/sentry-samples-console/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ tasks.withType<KotlinCompile>().configureEach {
dependencies {
implementation(projects.sentry)
implementation(projects.sentryAsyncProfiler)
implementation(projects.sentryJcache)
implementation(libs.jcache)
implementation(libs.caffeine.jcache)

testImplementation(kotlin(Config.kotlinStdLib))
testImplementation(projects.sentry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import io.sentry.*;
import io.sentry.clientreport.DiscardReason;
import io.sentry.jcache.SentryJCacheWrapper;
import io.sentry.protocol.Message;
import io.sentry.protocol.User;
import java.util.Collections;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;

public class Main {

Expand Down Expand Up @@ -88,6 +93,9 @@ public static void main(String[] args) throws InterruptedException {
// Set what percentage of traces should be collected
options.setTracesSampleRate(1.0); // set 0.5 to send 50% of traces

// Enable cache tracing to create spans for cache operations
options.setEnableCacheTracing(true);

// Determine traces sample rate based on the sampling context
// options.setTracesSampler(
// context -> {
Expand Down Expand Up @@ -162,6 +170,12 @@ public static void main(String[] args) throws InterruptedException {
Sentry.captureEvent(event, hint);
}

// Cache tracing with JCache (JSR-107)
//
// Wrapping a JCache Cache with SentryJCacheWrapper creates cache.get, cache.put,
// cache.remove, and cache.flush spans as children of the active transaction.
demonstrateCacheTracing();

// Performance feature
//
// Transactions collect execution time of the piece of code that's executed between the start
Expand Down Expand Up @@ -189,6 +203,42 @@ public static void main(String[] args) throws InterruptedException {
// Sentry.close();
}

private static void demonstrateCacheTracing() {
// Create a JCache CacheManager and Cache using standard JSR-107 API
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
MutableConfiguration<String, String> config =
new MutableConfiguration<String, String>().setTypes(String.class, String.class);
Cache<String, String> rawCache = cacheManager.createCache("myCache", config);

// Wrap with SentryJCacheWrapper to enable cache tracing
Cache<String, String> cache = new SentryJCacheWrapper<>(rawCache, Sentry.getCurrentScopes());

// All cache operations inside a transaction produce child spans
ITransaction transaction = Sentry.startTransaction("cache-demo", "demo");
try (ISentryLifecycleToken ignored = transaction.makeCurrent()) {
// cache.put span
cache.put("greeting", "hello");

// cache.get span (hit — returns "hello", cache.hit = true)
cache.get("greeting");

// cache.get span (miss — returns null, cache.hit = false)
cache.get("nonexistent");

// cache.remove span
cache.remove("greeting");

// cache.flush span
cache.clear();
} finally {
transaction.finish();
}

// Clean up
cacheManager.destroyCache("myCache");
cacheManager.close();
}

private static void captureMetrics() {
Sentry.metrics().count("countMetric");
Sentry.metrics().gauge("gaugeMetric", 5.0);
Expand Down
Loading