private static CounterCreationDataCollection GetCounterCreationDataCollectionForApmContextUsage(MethodInfo[] apmContextUsages)
        {
            var counterCreationDataCollection = new CounterCreationDataCollection();

            Trace.TraceInformation("Number of get context uses actions: {0}", apmContextUsages.Length);

            foreach (var apmContextUsage in apmContextUsages)
            {
                var methodIdentifier = ApmContext.GetMethodIdentifier(apmContextUsage);
                var eventName        = ApmContext.GetEventName(apmContextUsage);

                Trace.TraceInformation("Setting up get context uses '{0}' for event '{1}'", methodIdentifier, eventName);

                //Setup action performance counters
                foreach (var counterHandler in PerformanceCounterApmHttpClientDelegatingHandler.CounterHandlers)
                {
                    if (counterCreationDataCollection.Cast <CounterCreationData>().Any(x => x.CounterName == methodIdentifier))
                    {
                        Trace.TraceInformation("Counter for method '{0}' was duplicate", methodIdentifier);
                    }
                    else
                    {
                        var countersToCreate = counterHandler.GetCreationData(methodIdentifier);
                        foreach (var counterToCreate in countersToCreate)
                        {
                            Trace.TraceInformation("Added counter for method '{0}'", counterToCreate.CounterName);
                        }

                        counterCreationDataCollection.AddRange(countersToCreate);
                    }
                }
            }

            return(counterCreationDataCollection);
        }
Exemple #2
0
        protected void CreateCounters()
        {
            var categoryExists = false;

            // check if all the perf counters exist - this is the best that can be done with the current API
            if ((categoryExists = PerformanceCounterCategory.Exists(_categoryName)) &&
                _counterDefinitions.Cast <CounterCreationData>().All(ccd => PerformanceCounterCategory.CounterExists(ccd.CounterName, _categoryName)))
            {
                return;
            }

            try
            {
                if (categoryExists)
                {
                    PerformanceCounterCategory.Delete(_categoryName);
                }

                PerformanceCounterCategory.Create(_categoryName, _categoryHelp, _categoryType, _counterDefinitions);
            }
            catch (UnauthorizedAccessException e)
            {
                DerivedLogger.Error(e, "Error creating the performance counter category named '{0}'. Ensure the process is running with the necessary privileges.", _categoryName);
            }
            catch (SecurityException e)
            {
                DerivedLogger.Error(e, "Error creating the performance counter category named '{0}'. Ensure the process is running with the necessary privileges.", _categoryName);
            }
            catch (Exception e)
            {
                DerivedLogger.Error(e, "Unexpected error creating the performance counter category named '{0}': {1}", _categoryName, e.Message);
            }
        }
Exemple #3
0
        /// <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);

                    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().ToArray());
                            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);
        }
Exemple #4
0
 public static void AddPerformanceCounters(
     this DiagnosticMonitorConfiguration config,
     CounterCreationDataCollection counters,
     string category)
 {
     counters.
     Cast <CounterCreationData>().
     Select(x => new PerformanceCounterConfiguration
     {
         CounterSpecifier = @"\{0}\{1}".ExpandWith(category, x.CounterName),
         SampleRate       = TimeSpan.FromSeconds(10)
     }).
     ToList().
     ForEach(x => config.PerformanceCounters.DataSources.Add(x));
 }
Exemple #5
0
        /// <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);
        }
        private static CounterCreationDataCollection GetCounterCreationDataCollectionForHttpActionDescriptors(ReflectedHttpActionDescriptor[] httpActionDescriptors)
        {
            var counterCreationDataCollection = new CounterCreationDataCollection();

            Trace.TraceInformation("Number of controller actions: {0}", httpActionDescriptors.Length);

            foreach (var httpActionDescriptor in httpActionDescriptors)
            {
                var methodType = httpActionDescriptor.SupportedHttpMethods.First();

                var controllerName = httpActionDescriptor.ControllerDescriptor.ControllerName;
                var actionName     = httpActionDescriptor.ActionName;

                var param = httpActionDescriptor.MethodInfo.GetParameters()
                            .Select(parameter => string.Format("{0} {1}", parameter.ParameterType.Name, parameter.Name))
                            .ToArray();

                var arguments = string.Join(", ", param);

                var methodIdentifier = ApmWebApiFilterAttributeBase.GetMethodIdentifier(methodType, controllerName, actionName, arguments);
                var eventName        = ApmWebApiFilterAttributeBase.GetEventName(methodType, actionName, controllerName);

                Trace.TraceInformation("Setting up controller action '{0}' for event '{1}'", methodIdentifier, eventName);

                //Setup action performance counters
                foreach (var counterHandler in PerformanceCounterApmApiFilterAttribute.CounterHandlers)
                {
                    if (counterCreationDataCollection.Cast <CounterCreationData>().Any(x => x.CounterName == methodIdentifier))
                    {
                        Trace.TraceInformation("Counter for method '{0}' was duplicate", methodIdentifier);
                    }
                    else
                    {
                        var countersToCreate = counterHandler.GetCreationData(methodIdentifier);
                        foreach (var counterToCreate in countersToCreate)
                        {
                            Trace.TraceInformation("Added counter for method '{0}'", counterToCreate.CounterName);
                        }

                        counterCreationDataCollection.AddRange(countersToCreate);
                    }
                }
            }
            return(counterCreationDataCollection);
        }
Exemple #7
0
 private static bool IsCounterAlreadyIncluded(ref CounterCreationDataCollection colCounterCreationData, string counterName)
 {
     return(colCounterCreationData.Cast <CounterCreationData>().Any(objCreateCounter => objCreateCounter.CounterName == counterName));
 }