private Point[] FormatResult(IPerformanceCounterInfo[] performanceCounterInfos, float[] values, TimeUnit precision, DateTime timestamp) { var points = new Point[performanceCounterInfos.Length]; for (var i = 0; i < values.Count(); i++) { var performanceCounterInfo = performanceCounterInfos[i]; var value = values[i]; var categoryName = performanceCounterInfo.PerformanceCounter.CategoryName; var counterName = performanceCounterInfo.PerformanceCounter.CounterName; var key = performanceCounterInfo.PerformanceCounter.InstanceName; var tags = GetTags(_tags, categoryName, counterName); var fields = new Dictionary<string, object> { { "value", value }, //{ "readSpan", readSpan }, //Time in ms from the first, to the lats counter read in the group. //{ "timeOffset", (float)(timeOffset * 1000) } //Time difference in ms from reported time, to when read actually started. }; var point = new Point { Name = _name, Tags = tags, Fields = fields, Precision = precision, Timestamp = timestamp }; if (!string.IsNullOrEmpty(key)) { point.Tags.Add("instance", key); } points[i] = point; } return points; }
protected IEnumerable<Point> FormatResult(IPerformanceCounterInfo[] performanceCounterInfos, float?[] values, TimeUnit precision, DateTime timestamp) { for (var i = 0; i < values.Count(); i++) { var value = values[i]; if (value != null) { var performanceCounterInfo = performanceCounterInfos[i]; var categoryName = performanceCounterInfo.PerformanceCounter.CategoryName; var counterName = performanceCounterInfo.PerformanceCounter.CounterName; var key = performanceCounterInfo.PerformanceCounter.InstanceName; var instanceAlias = performanceCounterInfo.Alias; var tags = GetTags(Tags.Union(performanceCounterInfo.Tags), categoryName, counterName); var fields = new Dictionary<string, object> { { "value", value }, }; var point = new Point { Name = Name, Tags = tags, Fields = fields, Precision = precision, Timestamp = timestamp }; if (!string.IsNullOrEmpty(key)) { point.Tags.Add("instance", key); if (!string.IsNullOrEmpty(instanceAlias)) { point.Tags.Add(instanceAlias, key); } } yield return point; } } }
private static float[] ReadValues(IPerformanceCounterInfo[] performanceCounterInfos) { var values = new float[performanceCounterInfos.Length]; for (var i = 0; i < values.Count(); i++) { values[i] = performanceCounterInfos[i].PerformanceCounter.NextValue(); } return values; }
protected void RemoveObsoleteCounters(float?[] values, IPerformanceCounterInfo[] performanceCounterInfos) { if (!values.Any(x => !x.HasValue)) return; for (var i = 0; i < values.Count(); i++) { _performanceCounterGroup.RemoveCounter(performanceCounterInfos[i]); } Trace.TraceInformation("Removed {0} counters.", values.Count()); }
protected static float?[] ReadValues(IPerformanceCounterInfo[] performanceCounterInfos) { var values = new float?[performanceCounterInfos.Length]; for (var i = 0; i < values.Count(); i++) { try { values[i] = performanceCounterInfos[i].PerformanceCounter.NextValue(); } catch (InvalidOperationException) { values[i] = null; } } return values; }
public void RemoveCounter(IPerformanceCounterInfo performanceCounterInfo) { _performanceCounterInfos.Remove(performanceCounterInfo); }
private IEnumerable<Point> FormatResultWide(IPerformanceCounterInfo[] performanceCounterInfos, float?[] values, TimeUnit precision, DateTime timestamp) { var valuesByInstance = new Dictionary<string, Dictionary<string, object>>(); // first pass: we group all values by instance name // if no field name is specified, we store the value in default "value" field // if no instance name is specified, we use a default empty instance name for (var i = 0; i < values.Length; i++) { var value = values[i]; if (value != null) { var performanceCounterInfo = performanceCounterInfos[i]; var fieldName = performanceCounterInfo.FieldName ?? "value"; var key = performanceCounterInfo.InstanceName; Dictionary<string, object> fields; if (!valuesByInstance.TryGetValue(key ?? string.Empty, out fields)) { fields = new Dictionary<string, object>(); valuesByInstance[key ?? string.Empty] = fields; } fields[fieldName] = value; } } // second pass: for each instance name, we create a point if (valuesByInstance.Count != 0) { foreach (var instanceValues in valuesByInstance) { var point = new Point { Measurement = Name, Tags = GetTags(Tags, null, null, null), Fields = instanceValues.Value, Precision = precision, Timestamp = timestamp }; var instance = instanceValues.Key; if (!string.IsNullOrEmpty(instance)) { point.Tags.Add("instance", instance); } yield return point; } } }
protected IEnumerable<Point> FormatResult(IPerformanceCounterInfo[] performanceCounterInfos, float?[] values, TimeUnit precision, DateTime timestamp) { if (performanceCounterInfos.Any(i => !string.IsNullOrEmpty(i.FieldName))) { // WIDE SCHEMA // If at least one field name is specified, then we use the "wide" schema format, where all values are stored as different fields. // We will try to generate as few points as possible (only one per instance) return FormatResultWide(performanceCounterInfos, values, precision, timestamp); } else { // LONG SCHEMA // If no field name is specified we use the default format which result in each counter being sent as one point. // Each point as counter, category and instance specified as tags return FormatResultLong(performanceCounterInfos, values, precision, timestamp); } }
protected static float?[] ReadValues(IPerformanceCounterInfo[] performanceCounterInfos) { var values = new float?[performanceCounterInfos.Length]; for (var i = 0; i < values.Count(); i++) { try { var infos = performanceCounterInfos[i]; var value = infos.NextValue(); // if the counter value is greater than the max limit, then we use the max value // see https://support.microsoft.com/en-us/kb/310067 if (infos.Max.HasValue) { value = Math.Min(infos.Max.Value, value); } values[i] = value; } catch (InvalidOperationException) { values[i] = null; } } return values; }