Skip to content

Commit

Permalink
Add original metric in String to the Metric object (#178)
Browse files Browse the repository at this point in the history
## Description

Because we are checking the existence of some metric using regexes, it
would be worth having the full/original metric in String stored inside
the `Metric` object.
That way we can also see from what was the particular metric object
parsed.

This PR adds `stringMetric` variable to the `Metric` object, which will
contain exactly this value. Because it enhances the default constructor,
the constructors (and the creation of each object) had to be changed as
well.

## Type of Change

* New feature (non-breaking change which adds functionality)

## Checklist

- [X] My code follows the style guidelines of this project
- [X] I have performed a self-review of my own code
- [X] I have commented my code, particularly in hard-to-understand areas
- [X] I have made corresponding changes to the documentation
- [X] My changes generate no new warnings
- [X] I have added tests that prove my fix is effective or that my
feature works
- [X] New and existing unit/integration tests pass locally with my
changes

---------

Signed-off-by: Lukas Kral <[email protected]>
  • Loading branch information
im-konge authored Aug 30, 2024
1 parent 6290b63 commit 5d59c50
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels, double value) {
super(name, labels, MetricType.COUNTER);
public Counter(String name, Map<String, String> labels, String stringMetric, double value) {
super(name, labels, MetricType.COUNTER, stringMetric);
this.value = value;
}

Expand All @@ -45,6 +46,7 @@ public String toString() {
", labels=" + labels +
", value=" + value +
", type=" + type +
", stringMetric=" + stringMetric +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels, double value) {
super(name, labels, MetricType.GAUGE);
public Gauge(String name, Map<String, String> labels, String stringMetric, double value) {
super(name, labels, MetricType.GAUGE, stringMetric);
this.value = value;
}

Expand All @@ -45,6 +46,7 @@ public String toString() {
", labels=" + labels +
", value=" + value +
", type=" + type +
", stringMetric=" + stringMetric +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels) {
super(name, labels, MetricType.HISTOGRAM);
public Histogram(String name, Map<String, String> labels, String stringMetric) {
super(name, labels, MetricType.HISTOGRAM, stringMetric);
}

/**
Expand Down Expand Up @@ -94,6 +95,7 @@ public String toString() {
", sum=" + sum +
", count=" + count +
", type=" + type +
", stringMetric=" + stringMetric +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ public abstract class Metric {
String name;
Map<String, String> 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<String, String> labels, MetricType type) {
Metric(String name, Map<String, String> labels, MetricType type, String stringMetric) {
this.name = name;
this.labels = labels;
this.type = type;
this.stringMetric = stringMetric;
}

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -65,6 +77,7 @@ public String toString() {
"name='" + name + '\'' +
", labels=" + labels +
", type=" + type +
", stringMetric=" + stringMetric +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static List<Metric> parse(String data) throws IOException {
Map<String, String> 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") ?
Expand All @@ -72,13 +72,13 @@ public static List<Metric> 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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels) {
super(name, labels, MetricType.SUMMARY);
public Summary(String name, Map<String, String> labels, String stringMetric) {
super(name, labels, MetricType.SUMMARY, stringMetric);
}

/**
Expand Down Expand Up @@ -94,6 +95,7 @@ public String toString() {
", sum=" + sum +
", count=" + count +
", type=" + type +
", stringMetric=" + stringMetric +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels, double value) {
super(name, labels, MetricType.UNTYPED);
public Untyped(String name, Map<String, String> labels, String stringMetric, double value) {
super(name, labels, MetricType.UNTYPED, stringMetric);
this.value = value;
}

Expand All @@ -45,6 +46,7 @@ public String toString() {
", labels=" + labels +
", value=" + value +
", type=" + type +
", stringMetric=" + stringMetric +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void testCollectMetricWithLabelsInvalidDataFormat() {
// Setup
HashMap<String, List<Metric>> 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
Expand All @@ -76,7 +76,7 @@ void testBuilderInvalidParameters() {
void testCollectMetricWithLabelsMatchingEntries() {
HashMap<String, List<Metric>> 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<Metric> results = this.metricsCollector.collectMetricWithLabels("pod", "label");
Expand Down

0 comments on commit 5d59c50

Please sign in to comment.