Skip to content

Commit 6c19366

Browse files
authored
feat(.NET): Caches Module (#10266)
1 parent 52aa453 commit 6c19366

File tree

1 file changed

+105
-0
lines changed
  • docs/platforms/dotnet/common/performance/instrumentation/custom-instrumentation

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Instrument Caches
3+
sidebar_order: 1000
4+
description: "Learn how to manually instrument your code to use Sentry's Caches module. "
5+
---
6+
7+
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.
8+
9+
Sentry offers a [cache-monitoring dashboard](https://sentry.io/orgredirect/organizations/:orgslug/insights/caches/) for getting an overview of your application's caches.
10+
11+
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.
12+
13+
Make sure that there's a transaction running when you create the spans. If you're using a web framework like ASP.NET Core those transactions will be created for you automatically. See <PlatformLink to='/performance/'>Performance Monitoring</PlatformLink> for more information.
14+
15+
For detailed information about which data can be set, see the [Cache Module Developer Specification](https://develop.sentry.dev/sdk/performance/modules/caches/).
16+
17+
## Manual Instrumentation
18+
19+
Follow the steps below to make sure your Cache related spans end up in Sentry correctly.
20+
21+
### Add Span When Putting Data Into the Cache
22+
23+
1. Set the cache value with whatever cache library you happen to be using.
24+
2. Wrap the part of your application that uses the cached value with `Sentry.with_child_span { |span| ... }`
25+
3. Set `op` to `cache.put`.
26+
4. Set `cache.item_size` to an integer representing the size of the cached item.
27+
28+
### Add Span When Retrieving Data From the Cache
29+
30+
1. Fetch the cached value from whatever cache library you happen to be using.
31+
2. Wrap the part of your application that uses the cached value with `Sentry.with_child_span { |span| ... }`
32+
3. Set `op` to `cache.get`.
33+
4. Set `cache.hit` to a boolean value representing whether the value was successfully fetched from the cache or not.
34+
5. Set `cache.item_size` to an integer representing the size of the cached item.
35+
36+
### Example
37+
38+
Consider the following example service using `IMemoryCache` allowing you to `SetInCache` and `GetFromCache`.
39+
40+
```csharp
41+
using Microsoft.Extensions.Caching.Memory;
42+
43+
public class MyCachingService
44+
{
45+
private readonly IMemoryCache _cache;
46+
47+
public MyCachingService(IMemoryCache cache)
48+
{
49+
_cache = cache;
50+
}
51+
52+
public void SetInCache(string cacheKey, object value)
53+
{
54+
var cacheSpan = SentrySdk.GetSpan()?.StartChild("cache.put");
55+
56+
// Describe the cache server you are accessing
57+
cacheSpan?.SetExtra("network.peer.address", "cache.example.com/supercache");
58+
cacheSpan?.SetExtra("network.peer.port", 9000);
59+
60+
// Set the key you're going to use to add to the cache
61+
cacheSpan?.SetExtra("cache.key", cacheKey);
62+
63+
// Optional: You can also provide the cached item's size
64+
// cacheSpan?.SetExtra("cache.item_size", /* item size in bytes */);
65+
66+
// Add an item to your cache
67+
_cache.Set(cacheKey, value);
68+
69+
cacheSpan?.Finish();
70+
}
71+
72+
public object? GetFromCache(string cacheKey)
73+
{
74+
var cacheSpan = SentrySdk.GetSpan()?.StartChild("cache.get");
75+
76+
// Describe the cache server you are accessing
77+
cacheSpan?.SetExtra("network.peer.address", "cache.example.com/supercache");
78+
cacheSpan?.SetExtra("network.peer.port", 9000);
79+
80+
// Set the key you're going to use to retrieve from the cache
81+
cacheSpan?.SetExtra("cache.key", cacheKey);
82+
83+
// Attempt to retrieve the cached item
84+
if (_cache.TryGetValue(cacheKey, out var cachedValue))
85+
{
86+
// If you retrieved a value, the cache was hit
87+
cacheSpan?.SetExtra("cache.hit", true);
88+
89+
// Optional: You can also provide the cached item's size
90+
// cacheSpan.SetExtra("cache.item_size", /* item size in bytes */);
91+
92+
cacheSpan?.Finish();
93+
94+
return cachedValue;
95+
}
96+
97+
// If you could not retrieve a value, it was a miss
98+
cacheSpan?.SetExtra("cache.hit", false);
99+
cacheSpan?.Finish();
100+
return null;
101+
}
102+
}
103+
```
104+
105+
You should now have the right spans in place. Head over to the [Cache dashboard](https://sentry.io/orgredirect/organizations/:orgslug/performance/caches/) to see how your cache is performing.

0 commit comments

Comments
 (0)