Telemetry module for collecting performance counters.
Inheritance: ITelemetryModule, IDisposable
        private static PerformanceCollectorModule InitializePerformanceCollectionModule()
        {
            var perfModule = new PerformanceCollectorModule();

            // we're running under IIS Express, so override the default behavior designed to prevent a deadlock
            perfModule.EnableIISExpressPerformanceCounters = true;

            // set test-friendly timings
            perfModule.CollectionPeriod = TimeSpan.FromMilliseconds(10);
            perfModule.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\Memory\Available Bytes", @"\Memory\Available Bytes"));
            perfModule.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"Will not parse;\Does\NotExist", @"Will not parse;\Does\NotExist"));

            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"Will not parse", "Custom counter - will not parse"));

            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"\Does\NotExist", "Custom counter - does not exist"));
            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\IDontExist", "Custom counter with placeholder - does not exist"));

            perfModule.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process(??APP_WIN32_PROC??)\Handle Count", "Custom counter one"));

            perfModule.Counters.Add(
                new PerformanceCounterCollectionRequest(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Anonymous Requests/Sec", "Custom counter two"));

            perfModule.Initialize(TelemetryConfiguration.Active);
            return perfModule;
        }
        public PerformanceCounters(string instance)
        {
            try
            {
                var perfCollectorModule = new PerformanceCollectorModule();
                foreach (var p in typeof(PerformanceCounters).GetProperties())
                {
                    var counter = new PerformanceCounter(category, p.Name, instance, false);
                    p.SetValue(this, counter);
                    counter.RawValue = 0;

                    if (!p.Name.EndsWith("Base", StringComparison.Ordinal))
                    {
                        var perfCounterSpec = $"\\{category}({instance})\\{p.Name}";
                        var reportAs = p.Name
                            .Replace('_', ' ')
                            .Replace("Per", "/");

                        perfCollectorModule.Counters.Add(new PerformanceCounterCollectionRequest(perfCounterSpec, reportAs));
                    }
                }

                perfCollectorModule.Initialize(TelemetryConfiguration.Active);
            }
            catch (Exception e)
            {
                new TelemetryClient().TrackException(e);
            }
        }
        public static void ConfigureTelemetryModules(this TelemetryConfiguration configuration)
        {
            var performanceCounters = new PerformanceCollectorModule();
            performanceCounters.Counters.Add(new PerformanceCounterCollectionRequest(@"\Process\ID", "ID"));
            performanceCounters.Initialize(configuration);

            telemetryModules.Add(performanceCounters);

            var dependencies = new DependencyTrackingTelemetryModule();
            dependencies.Initialize(configuration);
           
            telemetryModules.Add(dependencies);
        }
        protected void Application_Start()
        {
            var setting = ConfigurationManager.AppSettings["TestApp.SendTelemetyIntemOnAppStart"];
            if (false == string.IsNullOrWhiteSpace(setting) && true == bool.Parse(setting))
            {
                new TelemetryClient().TrackTrace("Application_Start");
            }

            GlobalConfiguration.Configure(WebApiConfig.Register);

            var module = new PerformanceCollectorModule();

            // we're running under IIS Express, so override the default behavior designed to prevent a deadlock
            module.EnableIISExpressPerformanceCounters = true;

            // set test-friendly timings
            var privateObject = new PrivateObject(module);
            privateObject.SetField("collectionPeriod", TimeSpan.FromMilliseconds(10));
            privateObject.SetField(
                "defaultCounters",
                new List<string>() { @"\Memory\Available Bytes", @"Will not parse;\Does\NotExist" });

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(@"Will not parse", "Custom counter - will not parse"));

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(@"\Does\NotExist", "Custom counter - does not exist"));

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(
                                          @"\Process(??APP_WIN32_PROC??)\Handle Count",
                    "Custom counter one"));

            module.Counters.Add(
                new PerformanceCounterCollectionRequest(
                                          @"\ASP.NET Applications(??APP_W3SVC_PROC??)\Anonymous Requests/Sec",
                    "Custom counter two"));

            // necessary for .NET CLR Memory counters to start reporting process ID
            GC.Collect();

            module.Initialize(TelemetryConfiguration.Active);

            TelemetryModules.Instance.Modules.Add(module);
        }
        private static PerformanceCollectorModule CreatePerformanceCollectionModule(IPerformanceCollector collector, List<PerformanceCounterCollectionRequest> customCounterList = null)
        {
            var module = new PerformanceCollectorModule(collector);

            if (customCounterList != null)
            {
                customCounterList.ForEach(module.Counters.Add);
            }

            // set test-friendly timings
            var privateObject = new PrivateObject(module);
            privateObject.SetField("collectionPeriod", TimeSpan.FromMilliseconds(10));

            // build agent is unable to handle performance collection, so don't put any placeholders here for unit testing
            privateObject.SetField(
                "defaultCounters",
                new List<string>()
                    {
                        @"\DefaultCategory1\DefaultCounter1",
                        @"\DefaultCategory2(Instance2)\DefaultCounter2"
                    });

            return module;
        }
        public void TelemetryModuleIsNotInitializedTwiceToPreventTimerBeingRecreated()
        {
            var module = new PerformanceCollectorModule();
            PrivateObject privateObject = new PrivateObject(module);

            module.Initialize(TelemetryConfiguration.CreateDefault());
            object config1 = privateObject.GetField("telemetryConfiguration");

            module.Initialize(TelemetryConfiguration.CreateDefault());
            object config2 = privateObject.GetField("telemetryConfiguration");

            Assert.AreSame(config1, config2);
        }
        private static PerformanceCollectorModule CreatePerformanceCollectionModule(IPerformanceCollector collector, List<PerformanceCounterCollectionRequest> customCounterList = null)
        {
            var module = new PerformanceCollectorModule(collector);

            if (customCounterList != null)
            {
                customCounterList.ForEach(module.Counters.Add);
            }

            module.CollectionPeriod = TimeSpan.FromMilliseconds(10);

            module.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\DefaultCategory1\DefaultCounter1", @"\DefaultCategory1\DefaultCounter1"));
            module.DefaultCounters.Add(new PerformanceCounterCollectionRequest(@"\DefaultCategory2(Instance2)\DefaultCounter2", @"\DefaultCategory2(Instance2)\DefaultCounter2"));

            return module;
        }