Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        private void PostEvictionCallback(object key, object value, EvictionReason reason, object state)
        {
            //
            // Handling of disposables is done through eviction from cache

            CounterMonitor monitor = value as CounterMonitor;

            if (monitor != null)
            {
                try {
                    foreach (var countersKey in monitor.Counters.ToList().Select(counter => counter.CategoryName + counter.InstanceName).Distinct())
                    {
                        _cache.Remove(countersKey);
                    }
                }
                finally {
                    monitor.Dispose();
                }

                return;
            }

            IEnumerable <IPerfCounter> counters = value as IEnumerable <IPerfCounter>;

            if (counters != null && counters.Count() > 0)
            {
                monitor = _cache.Get <CounterMonitor>(counters.First().CategoryName);

                if (monitor != null)
                {
                    monitor.RemoveCounters(counters);
                }

                return;
            }
        }
Exemplo n.º 3
0
        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);
        }