diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Counter.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Counter.java index ebba5a4..0f56789 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Counter.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Counter.java @@ -15,12 +15,13 @@ public class Counter extends Metric { /** * Constructor * - * @param name name of metric - * @param labels labels - * @param value value + * @param name name of metric + * @param labels labels + * @param value value + * @param stringMetric original (not parsed) metric in String */ - public Counter(String name, Map labels, double value) { - super(name, labels, MetricType.COUNTER); + public Counter(String name, Map labels, String stringMetric, double value) { + super(name, labels, MetricType.COUNTER, stringMetric); this.value = value; } @@ -45,6 +46,7 @@ public String toString() { ", labels=" + labels + ", value=" + value + ", type=" + type + + ", stringMetric=" + stringMetric + '}'; } } diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Gauge.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Gauge.java index 02db9c8..846eb0d 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Gauge.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Gauge.java @@ -15,12 +15,13 @@ public class Gauge extends Metric { /** * Constructor * - * @param name name of metric - * @param labels labels - * @param value value + * @param name name of metric + * @param labels labels + * @param value value + * @param stringMetric original (not parsed) metric in String */ - public Gauge(String name, Map labels, double value) { - super(name, labels, MetricType.GAUGE); + public Gauge(String name, Map labels, String stringMetric, double value) { + super(name, labels, MetricType.GAUGE, stringMetric); this.value = value; } @@ -45,6 +46,7 @@ public String toString() { ", labels=" + labels + ", value=" + value + ", type=" + type + + ", stringMetric=" + stringMetric + '}'; } } diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Histogram.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Histogram.java index 188e807..ce20a04 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Histogram.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Histogram.java @@ -18,11 +18,12 @@ public class Histogram extends Metric { /** * Constructor * - * @param name name of metric - * @param labels labels + * @param name name of metric + * @param labels labels + * @param stringMetric original (not parsed) metric in String */ - public Histogram(String name, Map labels) { - super(name, labels, MetricType.HISTOGRAM); + public Histogram(String name, Map labels, String stringMetric) { + super(name, labels, MetricType.HISTOGRAM, stringMetric); } /** @@ -94,6 +95,7 @@ public String toString() { ", sum=" + sum + ", count=" + count + ", type=" + type + + ", stringMetric=" + stringMetric + '}'; } } diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Metric.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Metric.java index 5307999..c9ddd5c 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Metric.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Metric.java @@ -13,18 +13,21 @@ public abstract class Metric { String name; Map labels; MetricType type; + String stringMetric; /** * constructor * - * @param name name of metric - * @param labels labels - * @param type type + * @param name name of metric + * @param labels labels + * @param type type + * @param stringMetric original (not parsed) metric in String */ - Metric(String name, Map labels, MetricType type) { + Metric(String name, Map labels, MetricType type, String stringMetric) { this.name = name; this.labels = labels; this.type = type; + this.stringMetric = stringMetric; } /** @@ -54,6 +57,15 @@ public String getName() { return name; } + /** + * Get original metric, from which was the object created. + * + * @return original metric + */ + public String getStringMetric() { + return stringMetric; + } + /** * Metric string representation * @@ -65,6 +77,7 @@ public String toString() { "name='" + name + '\'' + ", labels=" + labels + ", type=" + type + + ", stringMetric=" + stringMetric + '}'; } } diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/PrometheusTextFormatParser.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/PrometheusTextFormatParser.java index 9b65219..26db344 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/PrometheusTextFormatParser.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/PrometheusTextFormatParser.java @@ -49,10 +49,10 @@ public static List parse(String data) throws IOException { Map labels = parseLabels(nameAndLabels[1]); if (name.endsWith("_total")) { - metrics.add(new Counter(name, labels, value)); + metrics.add(new Counter(name, labels, line, value)); } else if (name.contains("_bucket")) { if (currentHistogram == null || !currentHistogram.name.equals(name)) { - currentHistogram = new Histogram(name, labels); + currentHistogram = new Histogram(name, labels, line); metrics.add(currentHistogram); } double upperBound = labels.get("le").contains("+Inf") ? @@ -72,13 +72,13 @@ public static List parse(String data) throws IOException { } } else if (name.contains("{quantile=")) { if (currentSummary == null || !currentSummary.name.equals(name)) { - currentSummary = new Summary(name, labels); + currentSummary = new Summary(name, labels, line); metrics.add(currentSummary); } double quantile = Double.parseDouble(labels.get("quantile")); currentSummary.addQuantile(quantile, value); } else { - metrics.add(new Gauge(name, labels, value)); + metrics.add(new Gauge(name, labels, line, value)); } } } diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Summary.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Summary.java index 4d66654..f6fedb8 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Summary.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Summary.java @@ -18,11 +18,12 @@ public class Summary extends Metric { /** * Constructor * - * @param name name of metric - * @param labels labels + * @param name name of metric + * @param labels labels + * @param stringMetric original (not parsed) metric in String */ - public Summary(String name, Map labels) { - super(name, labels, MetricType.SUMMARY); + public Summary(String name, Map labels, String stringMetric) { + super(name, labels, MetricType.SUMMARY, stringMetric); } /** @@ -94,6 +95,7 @@ public String toString() { ", sum=" + sum + ", count=" + count + ", type=" + type + + ", stringMetric=" + stringMetric + '}'; } } diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Untyped.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Untyped.java index 1639fcf..9dcd15b 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Untyped.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/metrics/Untyped.java @@ -15,12 +15,13 @@ public class Untyped extends Metric { /** * Constructor * - * @param name name of metric - * @param labels labels - * @param value value + * @param name name of metric + * @param labels labels + * @param value value + * @param stringMetric original (not parsed) metric in String */ - public Untyped(String name, Map labels, double value) { - super(name, labels, MetricType.UNTYPED); + public Untyped(String name, Map labels, String stringMetric, double value) { + super(name, labels, MetricType.UNTYPED, stringMetric); this.value = value; } @@ -45,6 +46,7 @@ public String toString() { ", labels=" + labels + ", value=" + value + ", type=" + type + + ", stringMetric=" + stringMetric + '}'; } } diff --git a/test-frame-metrics-collector/src/test/java/io/skodjob/testframe/MetricsCollectorTest.java b/test-frame-metrics-collector/src/test/java/io/skodjob/testframe/MetricsCollectorTest.java index 546fb0f..a3a28dc 100644 --- a/test-frame-metrics-collector/src/test/java/io/skodjob/testframe/MetricsCollectorTest.java +++ b/test-frame-metrics-collector/src/test/java/io/skodjob/testframe/MetricsCollectorTest.java @@ -55,7 +55,7 @@ void testCollectMetricWithLabelsInvalidDataFormat() { // Setup HashMap> invalidData = new HashMap<>(); invalidData.put("pod", Collections.singletonList(new Gauge("metric_name", - Collections.singletonMap("label", "value"), 32))); + Collections.singletonMap("label", "value"), "metric_name {label=value} 32", 32))); this.metricsCollector.setCollectedData(invalidData); // Execution @@ -76,7 +76,7 @@ void testBuilderInvalidParameters() { void testCollectMetricWithLabelsMatchingEntries() { HashMap> invalidData = new HashMap<>(); invalidData.put("pod", Collections.singletonList(new Gauge("metric_name", - Collections.singletonMap("label", "value"), 100))); + Collections.singletonMap("label", "value"), "metric_name {label=value} 100", 100))); this.metricsCollector.setCollectedData(invalidData); List results = this.metricsCollector.collectMetricWithLabels("pod", "label");