/// <summary> /// Installs performance counters in the assembly /// </summary> /// <param name="installerAssembly"></param> /// <param name="discoverer">object that can discover aspects inside and assembly</param> /// <param name="categoryName">category name for the metrics. If not provided, it will use the assembly name</param> public static void Install(Assembly installerAssembly, IInstrumentationDiscoverer discoverer, string categoryName = null) { Uninstall(installerAssembly, discoverer, categoryName); if (string.IsNullOrEmpty(categoryName)) categoryName = installerAssembly.GetName().Name; var instrumentationInfos = discoverer.Discover(installerAssembly).ToArray(); if (instrumentationInfos.Length == 0) { throw new InvalidOperationException("There are no instrumentationInfos found by the discoverer!"); } var counterCreationDataCollection = new CounterCreationDataCollection(); Trace.TraceInformation("Number of filters: {0}", instrumentationInfos.Length); foreach (var group in instrumentationInfos.GroupBy(x => x.CategoryName)) { foreach (var instrumentationInfo in group) { Trace.TraceInformation("Setting up filters '{0}'", instrumentationInfo.Description); if (instrumentationInfo.Counters == null || instrumentationInfo.Counters.Length == 0) instrumentationInfo.Counters = CounterTypes.StandardCounters; foreach (var counterType in instrumentationInfo.Counters) { if (!HandlerFactories.ContainsKey(counterType)) throw new ArgumentException("Counter type not defined: " + counterType); // if already exists in the set then ignore if (counterCreationDataCollection.Cast<CounterCreationData>().Any(x => x.CounterName == counterType)) { Trace.TraceInformation("Counter type '{0}' was duplicate", counterType); continue; } using (var counterHandler = HandlerFactories[counterType](categoryName, instrumentationInfo.InstanceName)) { counterCreationDataCollection.AddRange(counterHandler.BuildCreationData()); Trace.TraceInformation("Added counter type '{0}'", counterType); } } } var catName = string.IsNullOrEmpty(group.Key) ? categoryName : group.Key; PerformanceCounterCategory.Create(catName, "PerfIt category for " + catName, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); } Trace.TraceInformation("Built category '{0}' with {1} items", categoryName, counterCreationDataCollection.Count); }
/// <summary> /// installs 4 standard counters for the category provided /// </summary> /// <param name="categoryName"></param> public static void InstallStandardCounters(string categoryName) { if (PerformanceCounterCategory.Exists(categoryName)) return; var creationDatas = new CounterHandlerBase[] { new AverageTimeHandler(categoryName, string.Empty), new LastOperationExecutionTimeHandler(categoryName, string.Empty), new TotalCountHandler(categoryName, string.Empty), new NumberOfOperationsPerSecondHandler(categoryName, string.Empty) , new CurrentConcurrentCountHandler(categoryName, string.Empty) }.SelectMany(x => x.BuildCreationData()); var counterCreationDataCollection = new CounterCreationDataCollection(); counterCreationDataCollection.AddRange(creationDatas.ToArray()); PerformanceCounterCategory.Create(categoryName, "PerfIt category for " + categoryName, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); }
private static void SetupCounters(IEnumerable<ICounterExample> counterWorkers) { if (PerformanceCounterCategory.Exists(CounterCategory)) { Console.WriteLine("Deleting Performance Category {0}", CounterCategory); PerformanceCounterCategory.Delete(CounterCategory); } var counterCreationDatas = counterWorkers.SelectMany(counterWorker => counterWorker.GetCounterCreationData()).ToArray(); var counterDataCollection = new CounterCreationDataCollection(); counterDataCollection.AddRange(counterCreationDatas); Console.WriteLine("Creating Performance Category {0}", CounterCategory); PerformanceCounterCategory.Create( CounterCategory, "Demonstrates usage of various performance counter types.", PerformanceCounterCategoryType.SingleInstance, counterDataCollection); }
/// <summary> /// Installs performance counters in the current assembly using PerfItFilterAttribute. /// </summary> /// /// <param name="categoryName">category name for the metrics. If not provided, it will use the assembly name</param> public static void Install(Assembly installerAssembly, string categoryName = null) { Uninstall(installerAssembly, categoryName); if (string.IsNullOrEmpty(categoryName)) categoryName = installerAssembly.GetName().Name; var perfItFilterAttributes = FindAllFilters(installerAssembly).ToArray(); var counterCreationDataCollection = new CounterCreationDataCollection(); Trace.TraceInformation("Number of filters: {0}", perfItFilterAttributes.Length); foreach (var filter in perfItFilterAttributes) { Trace.TraceInformation("Setting up filters '{0}'", filter.Description); foreach (var counterType in filter.Counters) { if (!HandlerFactories.ContainsKey(counterType)) throw new ArgumentException("Counter type not defined: " + counterType); // if already exists in the set then ignore if (counterCreationDataCollection.Cast<CounterCreationData>().Any(x => x.CounterName == counterType)) { Trace.TraceInformation("Counter type '{0}' was duplicate", counterType); continue; } using (var counterHandler = HandlerFactories[counterType](categoryName, filter.InstanceName)) { counterCreationDataCollection.AddRange(counterHandler.BuildCreationData()); Trace.TraceInformation("Added counter type '{0}'", counterType); } } } PerformanceCounterCategory.Create(categoryName, "PerfIt category for " + categoryName, PerformanceCounterCategoryType.MultiInstance, counterCreationDataCollection); Trace.TraceInformation("Built category '{0}' with {1} items", categoryName, counterCreationDataCollection.Count); }
/// <summary> /// Register Orleans perf counters with Windows /// </summary> /// <remarks>Note: Program needs to be running as Administrator to be able to delete Windows perf counters.</remarks> public static void InstallCounters() { var collection = new CounterCreationDataCollection(); collection.AddRange(GetCounterCreationData()); PerformanceCounterCategory.Create( CATEGORY_NAME, CATEGORY_DESCRIPTION, PerformanceCounterCategoryType.SingleInstance, collection); }
/// <summary> /// Registers the performance counters with the operating system. /// </summary> private void CreatePerformanceCounters() { CounterCreationDataCollection ccdc = new CounterCreationDataCollection(); ccdc.AddRange(TaskManagerInstaller.COUNTERS); if (!PerformanceCounterCategory.Exists(TaskManagerInstaller.PERFORMANCE_COUNTER_CATEGORY)) { _performanceCounterCategory = PerformanceCounterCategory.Create(TaskManagerInstaller.PERFORMANCE_COUNTER_CATEGORY, TaskManagerInstaller.PERFORMANCE_COUNTER_DESCRIPTION, PerformanceCounterCategoryType.SingleInstance, ccdc); } }