Skip to content

Instantly share code, notes, and snippets.

@lgolubyev
Created June 2, 2022 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgolubyev/6381c91455712323b4e54bc9d43480ad to your computer and use it in GitHub Desktop.
Save lgolubyev/6381c91455712323b4e54bc9d43480ad to your computer and use it in GitHub Desktop.
static Meter s_meter = new Meter("Microsoft.Extensions.Caching.Memory.MemoryCache", "1.0.0");
static IMemoryCache? mc1;
static IMemoryCache? mc2;
static void Main(string[] args)
{
s_meter.CreateObservableGauge<long>("cache-hits", GetCacheHits);
mc1 = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true, SizeLimit = 30 });
mc2 = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true, SizeLimit = 30 });
// call to: mc1.TryGetValue(key1, out object? value)
// or: mc2.TryGetValue(key2, out value2)
// increments TotalHits
}
// metrics callback for cache hits
static IEnumerable<Measurement<long>> GetCacheHits()
{
return new Measurement<long>[]
{
new Measurement<long>(mc1!.GetCurrentStatistics()!.TotalHits, new KeyValuePair<string,object?>("CacheName", "mc1")),
new Measurement<long>(mc2!.GetCurrentStatistics()!.TotalHits, new KeyValuePair<string,object?>("CacheName", "mc2")),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment