private static void WriteMetric(StreamWriter streamWriter, MetricFamily family, MetricsValue metric) { var familyName = family.Name; if (metric.Gauge != null) { WriteSimpleValue(streamWriter, familyName, metric.Gauge.Value, metric.Label); } else if (metric.Counter != null) { WriteSimpleValue(streamWriter, familyName, metric.Counter.Value, metric.Label); } else if (metric.Summary != null) { WriteSimpleValue(streamWriter, familyName, metric.Summary.Sample_Sum, metric.Label, "_sum"); WriteSimpleValue(streamWriter, familyName, metric.Summary.Sample_Count, metric.Label, "_count"); foreach (var quantileValuePair in metric.Summary.Quantile) { var quantile = double.IsPositiveInfinity(quantileValuePair.QuantileValue) ? "+Inf" : quantileValuePair.QuantileValue.ToString(CultureInfo.InvariantCulture); WriteSimpleValue( streamWriter, familyName, quantileValuePair.Value, metric.Label.Concat(new[] { new LabelPair { Name = "quantile", Value = quantile } })); } } else if (metric.Histogram != null) { WriteSimpleValue(streamWriter, familyName, metric.Histogram.Sample_Sum, metric.Label, "_sum"); WriteSimpleValue(streamWriter, familyName, metric.Histogram.Sample_Count, metric.Label, "_count"); foreach (var bucket in metric.Histogram.Buckets) { var value = double.IsPositiveInfinity(bucket.Upper_Bound) ? "+Inf" : bucket.Upper_Bound.ToString(CultureInfo.InvariantCulture); WriteSimpleValue( streamWriter, familyName, bucket.Cumulative_Count, metric.Label.Concat(new[] { new LabelPair { Name = "le", Value = value } }), "_bucket"); } } else { // not supported } }
private static void WriteFamily(StreamWriter streamWriter, MetricFamily metricFamily) { streamWriter.Write("# HELP "); streamWriter.Write(metricFamily.Name); streamWriter.Write(' '); streamWriter.WriteLine(metricFamily.Help); streamWriter.Write("# TYPE "); streamWriter.Write(metricFamily.Name); streamWriter.Write(' '); streamWriter.WriteLine(ToString(metricFamily.Type)); foreach (var metric in metricFamily.MetricsValues) { WriteMetric(streamWriter, metricFamily, metric); streamWriter.WriteLine(); } }
public static IEnumerable <MetricFamily> GetPrometheusMetricsSnapshot( this MetricsDataValueSource snapshot, Func <string, string, string> metricNameFormatter) { var result = new List <MetricFamily>(); foreach (var group in snapshot.Contexts) { foreach (var metricGroup in group.ApdexScores.GroupBy( source => source.IsMultidimensional ? source.MultidimensionalName : source.Name)) { var promMetricFamily = new MetricFamily { Name = metricNameFormatter(group.Context, metricGroup.Key), Type = MetricType.GAUGE }; foreach (var metric in metricGroup) { promMetricFamily.MetricsValues.AddRange(metric.ToPrometheusMetrics()); } result.Add(promMetricFamily); } foreach (var metricGroup in group.Gauges.GroupBy( source => source.IsMultidimensional ? source.MultidimensionalName : source.Name)) { var promMetricFamily = new MetricFamily { Name = metricNameFormatter(group.Context, metricGroup.Key), Type = MetricType.GAUGE }; foreach (var metric in metricGroup) { promMetricFamily.MetricsValues.AddRange(metric.ToPrometheusMetrics()); } result.Add(promMetricFamily); } foreach (var metricGroup in group.Counters.GroupBy( source => source.IsMultidimensional ? source.MultidimensionalName : source.Name)) { var promMetricFamily = new MetricFamily { Name = metricNameFormatter(group.Context, metricGroup.Key), Type = MetricType.GAUGE }; foreach (var metric in metricGroup) { promMetricFamily.MetricsValues.AddRange(metric.ToPrometheusMetrics()); } result.Add(promMetricFamily); } foreach (var metricGroup in group.Meters.GroupBy( source => source.IsMultidimensional ? source.MultidimensionalName : source.Name)) { var promMetricFamily = new MetricFamily { Name = metricNameFormatter(group.Context, $"{metricGroup.Key}_total"), Type = MetricType.COUNTER }; foreach (var metric in metricGroup) { promMetricFamily.MetricsValues.AddRange(metric.ToPrometheusMetrics()); } result.Add(promMetricFamily); } foreach (var metricGroup in group.Histograms.GroupBy( source => source.IsMultidimensional ? source.MultidimensionalName : source.Name)) { var promMetricFamily = new MetricFamily { Name = metricNameFormatter(group.Context, metricGroup.Key), Type = MetricType.SUMMARY }; foreach (var timer in metricGroup) { promMetricFamily.MetricsValues.AddRange(timer.ToPrometheusMetrics()); } result.Add(promMetricFamily); } foreach (var metricGroup in group.Timers.GroupBy( source => source.IsMultidimensional ? source.MultidimensionalName : source.Name)) { var promMetricFamily = new MetricFamily { Name = metricNameFormatter(group.Context, metricGroup.Key), Type = MetricType.SUMMARY }; foreach (var timer in metricGroup) { promMetricFamily.MetricsValues.AddRange(timer.ToPrometheusMetrics()); } result.Add(promMetricFamily); } } return(result); }