public async Task <IEnumerable <IPerfCounter> > GetSingletonCounters(string category, IEnumerable <string> counterNames) { // // Singleton counter categories do not have instances // caching them only requires a counter monitor CounterMonitor monitor = _cache.Get <CounterMonitor>(category); if (monitor == null) { IEnumerable <IPerfCounter> counters = _counterFinder.GetSingletonCounters(category, counterNames); monitor = _concurrentCacheHelper.GetOrCreate( category, () => new CounterMonitor(_counterFinder, counters), new MemoryCacheEntryOptions() { SlidingExpiration = CacheExpiration }.RegisterPostEvictionCallback(PostEvictionCallback) ); } try { await monitor.Refresh(); } catch (MissingCountersException) { _cache.Remove(category); throw; } return(monitor.Counters); }
public async Task <IEnumerable <IPerfCounter> > GetCounters(string category, string instance, IEnumerable <string> counterNames) { string key = category + instance; CounterMonitor monitor = _concurrentCacheHelper.GetOrCreate <CounterMonitor>(category, () => new CounterMonitor(_counterFinder, Enumerable.Empty <IPerfCounter>()), new MemoryCacheEntryOptions() { SlidingExpiration = CacheExpiration }.RegisterPostEvictionCallback(PostEvictionCallback) ); IEnumerable <IPerfCounter> counters = _cache.Get <IEnumerable <IPerfCounter> >(key); if (counters == null || counters.Count() == 0) { counters = _counterFinder.GetCounters(category, instance, counterNames); if (counters.Count() > 0) { bool didCreate = _concurrentCacheHelper.GetOrCreate( key, () => counters, new MemoryCacheEntryOptions() { SlidingExpiration = CacheExpiration }.RegisterPostEvictionCallback(PostEvictionCallback), out IEnumerable <IPerfCounter> entry ); if (didCreate) { monitor.AddCounters(entry); } } } try { await monitor.Refresh(); } catch (MissingCountersException e) { foreach (string counterKey in e.Counters.Select(c => c.CategoryName + c.InstanceName).Distinct()) { // // Cache eviction will remove counters from the counter monitor _cache.Remove(counterKey); } if (e.Counters.Any(counter => counter.CategoryName.Equals(category, StringComparison.OrdinalIgnoreCase) && counter.InstanceName.Equals(instance, StringComparison.OrdinalIgnoreCase)) ) { // If requested counter category+instance was reported missing we must throw throw; } // // Some counters instances that were in the monitor for the category are gone now, but the requested instance is fine. // Repeat the request for counters now that the nonexistent counters have been cleaned away counters = await GetCounters(category, instance, counterNames); } return(counters); }