Instrument Caches

Learn how to manually instrument your code to use Sentry's Caches module.

A cache can be used to speed up data retrieval, thereby improving application performance. Because instead of getting data from a potentially slow data layer, your application will be getting data from memory (in a best case scenario). Caching can speed up read-heavy workloads for applications like Q&A portals, gaming, media sharing, and social networking.

Sentry offers a cache-monitoring dashboard. For now you can manually instrument your caching solution and use Sentry to get a look into how it is performing by following the setup instructions below. We might be adding integrations to do this automatically, please head over to GitHub and let us know which caching solution you'd like us to support.

To make it possible for Sentry to give you an overview of your cache performance, you'll need to create two spans - one indicating that something is being put into the cache, and a second one indicating that something is being fetched from the cache.

Make sure that there's a transaction running when you create the spans. If you're using a web framework those transactions will be created for you automatically. See Performance Monitoring for more information.

For detailed information about which data can be set, see the Cache Module Developer Specification.

For now you'll need to manually instrument the Cache Module by following the steps below.

You can use the custom instrumentation instructions below to emit cache spans:

  1. Set the cache value with whatever cache library you happen to be using.
  2. Wrap the part of your application that uses the cached value with a span
  3. Set op to cache.put.
  4. Set cache.item_size to an integer representing the size of the cached item.

(The steps described above are documented in the snippet.)

Copied
import java.util.Arrays;
import io.sentry.ISpan;
import io.sentry.Sentry;

@Override
public void put(String key, Object value) {
    final ISpan parentSpan = Sentry.getSpan();
    if (parentSpan == null) {
        delegate.put(key, value);
    } else {
        ISpan childSpan = parentSpan.startChild("cache.put", key);
        try {
            // Describe the cache server you are accessing
            childSpan.setData("network.peer.address", "cache.example.com/supercache");
            childSpan.setData("network.peer.port", 9000);

            // Add the key you want to set
            childSpan.setData("cache.key", Arrays.asList(key));

            // Set size of the cached value
            childSpan.setData("cache.item_size", 123);

            delegate.put(key, value);
        } catch (Exception e) {
            childSpan.setThrowable(e);
            childSpan.setStatus(SpanStatus.INTERNAL_ERROR);
            throw e;
        } finally {
            childSpan.finish();
        }
    }
}

You can use the custom instrumentation instructions below to emit cache spans:

  1. Fetch the cached value from whatever cache library you happen to be using.
  2. Wrap the part of your application that uses the cached value with with sentry_sdk.start_span(...)
  3. Set op to cache.get.
  4. Set cache.hit to a boolean value representing whether the value was successfully fetched from the cache or not.
  5. Set cache.item_size to an integer representing the size of the cached item.

(The steps described above are documented in the snippet.)

Copied
import java.util.Arrays;
import io.sentry.ISpan;
import io.sentry.Sentry;

@Override
public Object get(String key) {
    final ISpan parentSpan = Sentry.getSpan();
    if (parentSpan == null) {
        return delegate.get(key);
    } else {
        ISpan childSpan = parentSpan.startChild("cache.get", key);
        try {
            // Describe the cache server you are accessing
            childSpan.setData("network.peer.address", "cache.example.com/supercache");
            childSpan.setData("network.peer.port", 9000);

            // Add the key you want to set
            childSpan.setData("cache.key", Arrays.asList(key));

            Object value = delegate.get(key);
            childSpan.setData("cache.hit", value != null);

            // Set size of the cached value
            childSpan.setData("cache.item_size", 123);

            return value;
        } catch (Exception e) {
            childSpan.setThrowable(e);
            childSpan.setStatus(SpanStatus.INTERNAL_ERROR);
            throw e;
        } finally {
            childSpan.finish();
        }
    }
}

You should now have the right spans in place. Head over to the Cache dashboard to see how your cache is performing.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").