/// <summary> /// Creates a metric telemetry associated with the PerformanceCounterData, with the respective float value. /// </summary> /// <param name="pc">PerformanceCounterData for which we are generating the telemetry.</param> /// <param name="value">The metric value for the respective performance counter data.</param> /// <returns>Metric telemetry object associated with the specific counter.</returns> private static MetricTelemetry CreateTelemetry(PerformanceCounterData pc, double value) { var metricName = !string.IsNullOrWhiteSpace(pc.ReportAs) ? pc.ReportAs : string.Format( CultureInfo.InvariantCulture, "{0} - {1}", pc.PerformanceCounter.CategoryName, pc.PerformanceCounter.CounterName); var metricTelemetry = new MetricTelemetry() { Name = metricName, Count = 1, Sum = value, Min = value, Max = value, StandardDeviation = 0, }; metricTelemetry.Properties.Add("CounterInstanceName", pc.PerformanceCounter.InstanceName); metricTelemetry.Properties.Add("CustomPerfCounter", "true"); return(metricTelemetry); }
/// <summary> /// Register a performance counter for collection. /// </summary> /// <param name="originalString">Original string definition of the counter.</param> /// <param name="reportAs">Alias to report the counter as.</param> /// <param name="categoryName">Category name.</param> /// <param name="counterName">Counter name.</param> /// <param name="instanceName">Instance name.</param> /// <param name="usesInstanceNamePlaceholder">Indicates whether the counter uses a placeholder in the instance name.</param> private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder) { ICounterValue performanceCounter = null; try { performanceCounter = CounterFactory.GetCounter(originalString, categoryName, counterName, instanceName); } catch (Exception e) { // we want to have another crack at it if instance placeholder is used, // notably due to the fact that CLR process ID counter only starts returning values after the first garbage collection if (!usesInstanceNamePlaceholder) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, "Failed to register performance counter. Category: {0}, counter: {1}, instance: {2}.", categoryName, counterName, instanceName), e); } } bool firstReadOk = false; try { // perform the first read. For many counters the first read will always return 0 // since a single sample is not enough to calculate a value performanceCounter.Collect(); firstReadOk = true; } catch (Exception e) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, "Failed to perform the first read for performance counter. Please make sure it exists. Category: {0}, counter: {1}, instance {2}", categoryName, counterName, instanceName), e); } finally { PerformanceCounterData perfData = new PerformanceCounterData( originalString, reportAs, usesInstanceNamePlaceholder, !firstReadOk, categoryName, counterName, instanceName); this.performanceCounters.Add(new Tuple <PerformanceCounterData, ICounterValue>(perfData, performanceCounter)); } }
/// <summary> /// Rebinds performance counters to Windows resources. /// </summary> public void RefreshPerformanceCounter(PerformanceCounterData pcd) { Tuple <PerformanceCounterData, ICounterValue> tupleToRemove = this.performanceCounters.FirstOrDefault(t => t.Item1 == pcd); if (tupleToRemove != null) { this.performanceCounters.Remove(tupleToRemove); } try { this.RegisterPerformanceCounter( pcd.OriginalString, pcd.ReportAs, pcd.PerformanceCounter.CategoryName, pcd.PerformanceCounter.CounterName, pcd.PerformanceCounter.InstanceName, pcd.UsesInstanceNamePlaceholder); } catch (InvalidOperationException e) { PerformanceCollectorEventSource.Log.CounterRegistrationFailedEvent( e.Message, PerformanceCounterUtility.FormatPerformanceCounter(pcd.PerformanceCounter)); } }
private void RefreshCounter( PerformanceCounterData pcd, IEnumerable <string> win32Instances, IEnumerable <string> clrInstances) { string dummy; bool usesInstanceNamePlaceholder; var pc = this.CreateCounter( pcd.OriginalString, win32Instances, clrInstances, out usesInstanceNamePlaceholder, out dummy); try { this.collector.RefreshPerformanceCounter(pcd, pc); PerformanceCollectorEventSource.Log.CounterRegisteredEvent( PerformanceCounterUtility.FormatPerformanceCounter(pc)); } catch (InvalidOperationException e) { PerformanceCollectorEventSource.Log.CounterRegistrationFailedEvent( e.Message, PerformanceCounterUtility.FormatPerformanceCounter(pc)); } }
/// <summary> /// Create handler for performance counter telemetry. /// </summary> private Action <ITelemetry> CreateHandlerForPerformanceCounterTelemetry(EventSource eventSource, MethodInfo writeGenericMethod, Type eventSourceOptionsType, PropertyInfo eventSourceOptionsKeywordsProperty) { var eventSourceOptions = Activator.CreateInstance(eventSourceOptionsType); var keywords = Keywords.PerformanceCounters; eventSourceOptionsKeywordsProperty.SetValue(eventSourceOptions, keywords); var dummyPerfData = new PerformanceCounterData(); var writeMethod = writeGenericMethod.MakeGenericMethod(new { PartA_iKey = this.dummyPartAiKeyValue, PartA_Tags = this.dummyPartATagsValue, PartB_PerformanceCounterData = new { // The properties and layout should be the same as PerformanceCounterData_types.cs dummyPerfData.ver, dummyPerfData.categoryName, dummyPerfData.counterName, dummyPerfData.instanceName, dummyPerfData.kind, dummyPerfData.count, dummyPerfData.min, dummyPerfData.max, dummyPerfData.stdDev, dummyPerfData.value, dummyPerfData.properties } }.GetType()); return((item) => { if (this.EventSourceInternal.IsEnabled(EventLevel.Verbose, keywords)) { var telemetryItem = item as PerformanceCounterTelemetry; var data = telemetryItem.Data; var extendedData = new { // The properties and layout should be the same as the anonymous type in the above MakeGenericMethod PartA_iKey = telemetryItem.Context.InstrumentationKey, PartA_Tags = telemetryItem.Context.Tags, PartB_PerformanceCounterData = new { data.ver, data.categoryName, data.counterName, data.instanceName, data.kind, data.count, data.min, data.max, data.stdDev, data.value, data.properties } }; writeMethod.Invoke(eventSource, new object[] { PerformanceCounterTelemetry.TelemetryName, eventSourceOptions, extendedData }); } }); }
public ViewType(IPushData view, object[] viewParams, DataType dataType, PerformanceCounterData performanceCounterData, string instanceName) { this.view = view; this.viewParams = viewParams; this.dataType = dataType; this.performanceCounterData = performanceCounterData; this.instanceName = instanceName; }
private void PercentCounterHandler(object o, EventArgs e) { PerformanceCounterData pcd = (PerformanceCounterData)o; pcd.percent = pcd.performanceCounter[0].NextValue(); pcd.total = Convert.ToDouble(pcd.param[0]); pcd.load = pcd.total * pcd.percent; pcd.available = pcd.total - pcd.load; }
private void CurrentLoadCounterHandler(object o, EventArgs e) { PerformanceCounterData pcd = (PerformanceCounterData)o; pcd.percent = 100; pcd.total = pcd.performanceCounter[0].NextValue(); pcd.load = pcd.total; pcd.available = 0; }
public void SetDataToView(DataType type, IPushData view, string instanceName = "_Total", object[] viewParams = null) { PerformanceCounterData pcd = this.CreatePercentCounterData(type, instanceName); if (!this.views.ContainsKey(view)) { this.views.Add(view, new List <ViewType>()); } this.views[view].Add(new ViewType(view, viewParams, type, pcd, instanceName)); }
/// <summary> /// Register a performance counter for collection. /// </summary> private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder, bool isCustomCounter) { ICounterValue counter = null; try { counter = this.factory.GetCounter(originalString, reportAs); } catch { PerformanceCollectorEventSource.Log.CounterNotWebAppSupported(originalString); return; } bool firstReadOk = false; try { // perform the first read. For many counters the first read will always return 0 // since a single sample is not enough to calculate a value var value = counter.GetValueAndReset(); firstReadOk = true; } catch (Exception e) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, "Failed to perform the first read for web app performance counter. Please make sure it exists. Counter: {0}", counterName), e); } finally { PerformanceCounterData perfData = new PerformanceCounterData( originalString, reportAs, usesInstanceNamePlaceholder, isCustomCounter, !firstReadOk, categoryName, counterName, instanceName); this.performanceCounters.Add(new Tuple <PerformanceCounterData, ICounterValue>(perfData, counter)); } }
/// <summary> /// Rebinds performance counters to Windows resources. /// </summary> public void RefreshPerformanceCounter(PerformanceCounterData pcd) { Tuple <PerformanceCounterData, ICounterValue> tupleToRemove = this.performanceCounters.FirstOrDefault(t => t.Item1 == pcd); if (tupleToRemove != null) { this.performanceCounters.Remove(tupleToRemove); } this.RegisterPerformanceCounter( pcd.OriginalString, pcd.ReportAs, pcd.PerformanceCounter.CategoryName, pcd.PerformanceCounter.CounterName, pcd.PerformanceCounter.InstanceName, pcd.UsesInstanceNamePlaceholder); }
/// <summary> /// Register a performance counter for collection. /// </summary> private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder, bool isCustomCounter) { ICounterValue counter = null; try { counter = this.factory.GetCounter(originalString, reportAs); } catch { PerformanceCollectorEventSource.Log.CounterNotWebAppSupported(originalString); return; } bool firstReadOk = false; try { // perform the first read. For many counters the first read will always return 0 // since a single sample is not enough to calculate a value var value = counter.GetValueAndReset(); firstReadOk = true; } catch (Exception e) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.WebAppPerformanceCounterFirstReadFailed, counterName), e); } finally { PerformanceCounterData perfData = new PerformanceCounterData( originalString, reportAs, usesInstanceNamePlaceholder, isCustomCounter, !firstReadOk, categoryName, counterName, instanceName); this.performanceCounters.Add(new Tuple<PerformanceCounterData, ICounterValue>(perfData, counter)); } }
/// <summary> /// Rebinds performance counters to Windows resources. /// </summary> public void RefreshPerformanceCounter(PerformanceCounterData pcd) { }
/// <summary> /// Initializes a new instance of the <see cref="PerformanceCounterTelemetry"/> class. /// </summary> public PerformanceCounterTelemetry() { this.Data = new PerformanceCounterData(); this.context = new TelemetryContext(this.Data.properties); }
private PerformanceCounterData CreatePercentCounterData(DataType type, string instanceName) { string key = type.ToString() + instanceName; switch (type) { case DataType.ProcessorLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Processor", "% Processor Time", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.PercentCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.MemoryLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Memory", "Available KBytes") }, type, GetDataType.Available, instanceName, new object[] { this.systemInfo.PhysicalMemorySize }); pcd.CountHandle += this.AvaiableCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.LogicalDiskLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("LogicalDisk", "Disk Bytes/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.LogicalDiskReadLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("LogicalDisk", "Disk Read Bytes/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.LogicalDiskWriteLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("LogicalDisk", "Disk Write Bytes/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.NetworkInterfaceLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Network Interface", "Bytes Total/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.NetworkInterfaceReceivedLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Network Interface", "Bytes Received/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); case DataType.NetworkInterfaceSentLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Network Interface", "Bytes Sent/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return(pcd); } return(this.performanceCounters[key]); default: return(null); } }
private PerformanceCounterData CreatePercentCounterData(DataType type,string instanceName) { string key = type.ToString() + instanceName; switch (type) { case DataType.ProcessorLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Processor", "% Processor Time", instanceName) }, type, GetDataType.Percent, instanceName,new object[] { 100 }); pcd.CountHandle += this.PercentCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.MemoryLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[]{new PerformanceCounter("Memory", "Available KBytes")}, type,GetDataType.Available,instanceName,new object[]{this.systemInfo.PhysicalMemorySize}); pcd.CountHandle += this.AvaiableCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.LogicalDiskLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("LogicalDisk", "Disk Bytes/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.LogicalDiskReadLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("LogicalDisk", "Disk Read Bytes/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.LogicalDiskWriteLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("LogicalDisk", "Disk Write Bytes/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.NetworkInterfaceLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Network Interface", "Bytes Total/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.NetworkInterfaceReceivedLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Network Interface", "Bytes Received/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; case DataType.NetworkInterfaceSentLoadPercent: if (!this.performanceCounters.ContainsKey(key)) { PerformanceCounterData pcd = new PerformanceCounterData(new PerformanceCounter[] { new PerformanceCounter("Network Interface", "Bytes Sent/sec", instanceName) }, type, GetDataType.Percent, instanceName, new object[] { 100 }); pcd.CountHandle += this.CurrentLoadCounterHandler; this.performanceCounters.Add(key, pcd); return pcd; } return this.performanceCounters[key]; default: return null; } }
/// <summary> /// Initializes a new instance of the <see cref="PerformanceCounterTelemetry"/> class. /// </summary> public PerformanceCounterTelemetry() { this.Data = new PerformanceCounterData(); }
/// <summary> /// Rebinds performance counters to Windows resources. /// </summary> public void RefreshPerformanceCounter(PerformanceCounterData pcd) { Tuple<PerformanceCounterData, ICounterValue> tupleToRemove = this.performanceCounters.FirstOrDefault(t => t.Item1 == pcd); if (tupleToRemove != null) { this.performanceCounters.Remove(tupleToRemove); } this.RegisterPerformanceCounter( pcd.OriginalString, pcd.ReportAs, pcd.CategoryName, pcd.CounterName, pcd.InstanceName, pcd.UsesInstanceNamePlaceholder, pcd.IsCustomCounter); }
/// <summary> /// Register a performance counter for collection. /// </summary> /// <param name="originalString">Original string definition of the counter.</param> /// <param name="reportAs">Alias to report the counter as.</param> /// <param name="categoryName">Category name.</param> /// <param name="counterName">Counter name.</param> /// <param name="instanceName">Instance name.</param> /// <param name="usesInstanceNamePlaceholder">Indicates whether the counter uses a placeholder in the instance name.</param> /// <param name="isCustomCounter">Indicates whether the counter is a custom counter.</param> private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder, bool isCustomCounter) { PerformanceCounter performanceCounter = null; try { performanceCounter = new PerformanceCounter(categoryName, counterName, instanceName, true); } catch (Exception e) { // we want to have another crack at it if instance placeholder is used, // notably due to the fact that CLR process ID counter only starts returning values after the first garbage collection if (!usesInstanceNamePlaceholder) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.PerformanceCounterRegistrationFailed, categoryName, counterName, instanceName), e); } } bool firstReadOk = false; try { // perform the first read. For many counters the first read will always return 0 // since a single sample is not enough to calculate a value performanceCounter.NextValue(); firstReadOk = true; } catch (Exception e) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.PerformanceCounterFirstReadFailed, categoryName, counterName, instanceName), e); } finally { PerformanceCounterData perfData = new PerformanceCounterData( originalString, reportAs, usesInstanceNamePlaceholder, isCustomCounter, !firstReadOk, categoryName, counterName, instanceName); this.performanceCounters.Add(new Tuple<PerformanceCounterData, PerformanceCounter>(perfData, performanceCounter)); } }
/// <summary> /// Refreshes the counter associated with a specific performance counter data. /// </summary> /// <param name="pcd">Target performance counter data to refresh.</param> private void RefreshCounter(PerformanceCounterData pcd) { string dummy; bool usesInstanceNamePlaceholder; var pc = PerformanceCounterUtility.CreateAndValidateCounter( pcd.OriginalString, this.win32Instances, this.clrInstances, out usesInstanceNamePlaceholder, out dummy); try { this.RefreshPerformanceCounter(pcd); PerformanceCollectorEventSource.Log.CounterRegisteredEvent( PerformanceCounterUtility.FormatPerformanceCounter(pc)); } catch (InvalidOperationException e) { PerformanceCollectorEventSource.Log.CounterRegistrationFailedEvent( e.Message, PerformanceCounterUtility.FormatPerformanceCounter(pc)); } }
public PerformanceCounterTelemetry() { Data = new PerformanceCounterData(); }
/// <summary> /// Register a performance counter for collection. /// </summary> /// <param name="originalString">Original string definition of the counter.</param> /// <param name="reportAs">Alias to report the counter as.</param> /// <param name="categoryName">Category name.</param> /// <param name="counterName">Counter name.</param> /// <param name="instanceName">Instance name.</param> /// <param name="usesInstanceNamePlaceholder">Indicates whether the counter uses a placeholder in the instance name.</param> /// <param name="isCustomCounter">Indicates whether the counter is a custom counter.</param> private void RegisterPerformanceCounter(string originalString, string reportAs, string categoryName, string counterName, string instanceName, bool usesInstanceNamePlaceholder, bool isCustomCounter) { PerformanceCounter performanceCounter = null; try { performanceCounter = new PerformanceCounter(categoryName, counterName, instanceName, true); } catch (Exception e) { // we want to have another crack at it if instance placeholder is used, // notably due to the fact that CLR process ID counter only starts returning values after the first garbage collection if (!usesInstanceNamePlaceholder) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.PerformanceCounterRegistrationFailed, categoryName, counterName, instanceName), e); } } bool firstReadOk = false; try { // perform the first read. For many counters the first read will always return 0 // since a single sample is not enough to calculate a value performanceCounter.NextValue(); firstReadOk = true; } catch (Exception e) { throw new InvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.PerformanceCounterFirstReadFailed, categoryName, counterName, instanceName), e); } finally { PerformanceCounterData perfData = new PerformanceCounterData( originalString, reportAs, usesInstanceNamePlaceholder, isCustomCounter, !firstReadOk, categoryName, counterName, instanceName); this.performanceCounters.Add(new Tuple <PerformanceCounterData, PerformanceCounter>(perfData, performanceCounter)); } }