Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add original metric in String to the Metric object #178

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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