Skip to content

Commit

Permalink
add labels to the prometheus metrics (via #823)
Browse files Browse the repository at this point in the history
  • Loading branch information
antcyganov authored and eroshenkoam committed Aug 10, 2018
1 parent 0da77c4 commit a52f238
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
*/
public class PrometheusExportPlugin extends CommonMetricAggregator {

private static final String ALLURE_PROMETHEUS_LABELS = "allure.prometheus.labels";

public PrometheusExportPlugin() {
super("prometheusData.txt");
}

@Override
public List<Metric> getMetrics() {
final String labels = getPrometheusLabels();

final StatusMetric statusMetric = new StatusMetric((status, count) ->
new PrometheusMetricLine("launch_status", status.value(), String.valueOf(count)));
new PrometheusMetricLine("launch_status", status.value(), String.valueOf(count), labels));

final TimeMetric timeMetric = new TimeMetric((key, time) ->
new PrometheusMetricLine("launch_time", key, String.valueOf(time)));
new PrometheusMetricLine("launch_time", key, String.valueOf(time), labels));

final CategoriesMetric categoriesMetric = new CategoriesMetric((category, count) ->
new PrometheusMetricLine("launch_problems", category, String.valueOf(count)));
new PrometheusMetricLine("launch_problems", category, String.valueOf(count), labels));

final RetryMetric retryMetric = new RetryMetric((key, count) ->
new PrometheusMetricLine("launch_retries", key, String.valueOf(count))
new PrometheusMetricLine("launch_retries", key, String.valueOf(count), labels)
);

return Arrays.asList(
Expand All @@ -41,4 +45,11 @@ public List<Metric> getMetrics() {
retryMetric
);
}

public static String getPrometheusLabels() {
if (System.getProperties().getProperty(ALLURE_PROMETHEUS_LABELS) != null) {
return System.getProperties().getProperty(ALLURE_PROMETHEUS_LABELS);
}
return System.getenv(ALLURE_PROMETHEUS_LABELS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ public class PrometheusMetricLine implements MetricLine {
private final String name;
private final String key;
private final String value;
private final String labels;

@Override
public String asString() {
return String.format("%s_%s %s",
return String.format("%s_%s%s %s",
getName(),
normalize(getKey()),
normalizeLabels(getLabels()),
getValue()
);
}

public static String normalize(final String string) {
return string.toLowerCase().replaceAll("\\s+", "_");
}

private String normalizeLabels(final String labels) {
return labels == null ? "" : "{" + labels + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.qameta.allure.prometheus;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.junit.Assert.assertEquals;

/**
* @author Anton Tsyganov (jenkl)
*/
@RunWith(Parameterized.class)
public class PrometheusMetricLineTest {
private static final String METRIC_NAME = "launch";
private static final String METRIC_KEY = "status passed";
private static final String METRIC_VALUE = "300";

private final String labels;
private final String expectedMetric;

public PrometheusMetricLineTest(String labels, String expectedMetric, String name) {
this.labels = labels;
this.expectedMetric = expectedMetric;
}

@Parameterized.Parameters(name = "prometheus metric {2}")
public static String[][] data() {
return new String[][] {
{"evn=\"test\",suite=\"regression\"", "launch_status_passed{evn=\"test\",suite=\"regression\"} 300",
"with labels"},
{null, "launch_status_passed 300", "without labels"}
};
}

@Test
public void shouldReturnMetric() {
PrometheusMetricLine prometheusMetric = new PrometheusMetricLine(METRIC_NAME, METRIC_KEY, METRIC_VALUE, labels);
assertEquals(prometheusMetric.asString(), expectedMetric);
}
}

0 comments on commit a52f238

Please sign in to comment.