dnet-explore-metrics/src/main/java/eu/dnetlib/dnetexploremetrics/utils/PrometheusMetricsFormatter....

41 lines
2.3 KiB
Java

package eu.dnetlib.dnetexploremetrics.utils;
import eu.dnetlib.dnetexploremetrics.model.Metrics;
import org.apache.logging.log4j.message.StringFormattedMessage;
public class PrometheusMetricsFormatter {
private enum PrometheusMetrics {
RECORDS("# TYPE explore_total_records gauge\nexplore_total_records %s"),
PUBLICATIONS("# TYPE explore_total_publications gauge\nexplore_total_publications %s"),
DATASETS("# TYPE explore_total_datasets gauge\nexplore_total_datasets %s"),
SOFTWARE("# TYPE explore_total_software gauge\nexplore_total_software %s"),
ORP("# TYPE explore_total_otherresearchproducts gauge\nexplore_total_otherresearchproducts %s"),
FUNDERS("# TYPE explore_total_funders gauge\nexplore_total_funders %s"),
PROJECTS("#TYPE explore_total_projects gauge\nexplore_total_projects %s"),
CONTENT_PROVIDERS("#TYPE explore_total_contentproviders gauge\nexplore_total_contentproviders %s");
private String value;
PrometheusMetrics(String value) {
this.value = value;
}
private String getValue() {
return value;
}
}
public static String formatMetrics(Metrics metrics) {
StringBuilder builder = new StringBuilder();
builder.append(String.format(String.valueOf(PrometheusMetrics.RECORDS.getValue()), metrics.records)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.PUBLICATIONS.getValue()), metrics.publications)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.DATASETS.getValue()), metrics.datasets)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.SOFTWARE.getValue()), metrics.software)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.ORP.getValue()), metrics.orp)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.FUNDERS.getValue()), metrics.funders)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.PROJECTS.getValue()), metrics.projects)).append("\n").
append(String.format(String.valueOf(PrometheusMetrics.CONTENT_PROVIDERS.getValue()), metrics.contentProviders));
System.out.println(builder.toString());
return builder.toString();
}
}