1
Vote

Counters being overwritten.

description

We have a use case in which all code is derived from templates. This makes it difficult to define all performance counters in a single project level enumerator. Instead, we would like to define the performance counter enumerator in each class that does performance counting. Ideally the attribute on the enumerator would refer to the same Category for all classes in the project, but the enumerated values would define their own counter name. This works to a degree, but when a new class is loaded and the enumerator is discovered, it deletes the existing Category with the same name and creates the new instance with the counters for that class. Is it possible for it to simply add the new counters to the existing category?

For example

In StateProvince class;
 [PerformanceCounterCategory(
            "Administration",
            System.Diagnostics.PerformanceCounterCategoryType.SingleInstance,
            "Administration Performance.")]
        private enum PerformanceCounters
        {
            [PerformanceCounter("StateProvince Fetch", "StateProvince Fetch Duration",
                System.Diagnostics.PerformanceCounterType.NumberOfItems64)] Fetch,

            [PerformanceCounter("StateProvince Insert", "StateProvince Insert Duration",
                System.Diagnostics.PerformanceCounterType.NumberOfItems64)] Insert,

            [PerformanceCounter("StateProvince Update", "StateProvince Update Duration",
                System.Diagnostics.PerformanceCounterType.NumberOfItems64)] Update,

            [PerformanceCounter("StateProvince Delete", "StateProvince Delete Duration",
                System.Diagnostics.PerformanceCounterType.NumberOfItems64)] Delete
        }
In the StateProvinceInfo class
        [PerformanceCounterCategory(
            "Administration",
            System.Diagnostics.PerformanceCounterCategoryType.SingleInstance,
            "NX Administration Performance.")]
        public enum PerformanceCounters
        {
            [PerformanceCounter("StateProvinceInfo Fetch", "StateProvinceInfo Fetch Duration",
            System.Diagnostics.PerformanceCounterType.NumberOfItems64)]
            Fetch
        }
When these get processed I would expect the Performance Category "Administration" to contain the following Counters;
StateProvince Fetch
StateProvince Insert
StateProvince Update
StateProvince Delete
StateProvinceInfo Fetch

comments

phenotype wrote Aug 2, 2014 at 2:09 PM

The following line in PerformanceHelper is the issue
            if (PerformanceCounterCategory.Exists(categoryInfo.Name))
                PerformanceCounterCategory.Delete(categoryInfo.Name);
Perhaps we can add an option to the attribute to turn it off.

phenotype wrote Aug 2, 2014 at 3:59 PM

Realizing that I can't add counters to an existing category I replaced the above with this;
            if (PerformanceCounterCategory.Exists(categoryInfo.Name))
            {
                var categories = PerformanceCounterCategory.GetCategories();
                foreach (var category in categories)
                {
                    if (category.CategoryName.Equals(categoryInfo.Name))
                    {
                        var counters = category.GetCounters();
                        foreach (var counter in counters)
                        {
                            categoryCounters.Add(new CounterCreationData(counter.CounterName, counter.CounterHelp, counter.CounterType));
                        }
                        break;
                    }
                }
                PerformanceCounterCategory.Delete(categoryInfo.Name);
            }
It adds existing counters from a category to the collection. Later, before adding a new counter to the collection, it checks to see the counter name is not already in the collection. It seems to work. As I mentioned, you could add a parm to the Category attrib like Append to turn this behavior on. This makes it much easier for me as each class now has its own local enum defining counters and I don't have to manage a global enum.

javiercanillas wrote Aug 3, 2014 at 9:39 PM

Hey guys.. I'm quite away from this code, if you want to add new features or correct them, I have added you as developers. So you can discuss them here and then apply them on the code so everyone can use it.