private static void WriteFamily(StreamWriter streamWriter, CMetricFamily cMetricFamily)
 {
     streamWriter.WriteLine("# HELP {0} {1}", cMetricFamily.Name, cMetricFamily.Help);
     streamWriter.WriteLine("# TYPE {0} {1}", cMetricFamily.Name, cMetricFamily.Type);
     foreach (var metric in cMetricFamily.Metrics)
     {
         WriteMetric(streamWriter, cMetricFamily, metric);
     }
 }
        private static string WriteFamily(CMetricFamily cMetricFamily)
        {
            var s = new StringBuilder();

            s.AppendLine($"# HELP {cMetricFamily.Name} {cMetricFamily.Help}");
            s.AppendLine($"# TYPE {cMetricFamily.Name} {cMetricFamily.Type}");
            foreach (var metric in cMetricFamily.Metrics)
            {
                s.Append(WriteMetric(cMetricFamily, metric));
            }
            return(s.ToString());
        }
Beispiel #3
0
        public CMetricFamily Collect()
        {
            var result = new CMetricFamily
            {
                Name = Name,
                Help = _help,
                Type = Type
            };

            foreach (var child in LabelledMetrics.Values)
            {
                result.Metrics.Add(child.Collect());
            }

            return(result);
        }
Beispiel #4
0
        public void Family_Should_Be_Formatted_To_One_Line(string labelValue)
        {
            using (var ms = new MemoryStream())
            {
                var metricFamily = new CMetricFamily
                {
                    Name = "family1",
                    Help = "help",
                    Type = CMetricType.Counter
                };

                var metricCounter = new CCounter {
                    Value = 100
                };
                metricFamily.Metrics = new[]
                {
                    new CMetric
                    {
                        CCounter = metricCounter,
                        Labels   = new[]
                        {
                            new CLabelPair {
                                Name = "label1", Value = labelValue
                            }
                        }
                    }
                };

                TextFormatter.Format(ms, new[]
                {
                    metricFamily
                });

                using (var sr = new StringReader(Encoding.UTF8.GetString(ms.ToArray())))
                {
                    var linesCount = 0;
                    var line       = "";
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        linesCount += 1;
                    }

                    Assert.Equal(3, linesCount);
                }
            }
        }
        private static string WriteMetric(CMetricFamily family, CMetric cMetric)
        {
            var s          = new StringBuilder();
            var familyName = family.Name;

            if (cMetric.CGauge != null)
            {
                s.AppendLine(SimpleValue(familyName, cMetric.CGauge.Value, cMetric.Labels));
            }
            else if (cMetric.CCounter != null)
            {
                s.AppendLine(SimpleValue(familyName, cMetric.CCounter.Value, cMetric.Labels));
            }
            else if (cMetric.CSummary != null)
            {
                s.AppendLine(SimpleValue(familyName, cMetric.CSummary.SampleSum, cMetric.Labels, "_sum"));
                s.AppendLine(SimpleValue(familyName, cMetric.CSummary.SampleCount, cMetric.Labels, "_count"));

                foreach (var quantileValuePair in cMetric.CSummary.Quantiles)
                {
                    var quantile = double.IsPositiveInfinity(quantileValuePair.Quantile)
                        ? "+Inf"
                        : quantileValuePair.Quantile.ToString(CultureInfo.InvariantCulture);
                    s.AppendLine(SimpleValue(familyName, quantileValuePair.Value,
                                             cMetric.Labels.Concat(new[] { new CLabelPair {
                                                                               Name = "quantile", Value = quantile
                                                                           } })));
                }
            }
            else if (cMetric.CHistogram != null)
            {
                s.AppendLine(SimpleValue(familyName, cMetric.CHistogram.SampleSum, cMetric.Labels, "_sum"));
                s.AppendLine(SimpleValue(familyName, cMetric.CHistogram.SampleCount, cMetric.Labels, "_count"));
                foreach (var bucket in cMetric.CHistogram.Buckets)
                {
                    var value = double.IsPositiveInfinity(bucket.UpperBound) ? "+Inf" : bucket.UpperBound.ToString(CultureInfo.InvariantCulture);
                    s.AppendLine(SimpleValue(familyName, bucket.CumulativeCount, cMetric.Labels.Concat(new[] { new CLabelPair {
                                                                                                                   Name = "le", Value = value
                                                                                                               } }),
                                             "_bucket"));
                }
            }

            return(s.ToString());
        }
        public CMetricFamily Collect()
        {
            var result = new CMetricFamily
            {
                Name    = Name,
                Help    = _help,
                Type    = Type,
                Metrics = new CMetric[LabelledMetrics.Values.Count]
            };

            var i = 0;

            foreach (var child in LabelledMetrics.Values)
            {
                result.Metrics[i] = child.Collect();
                i++;
            }

            return(result);
        }