document metrics better and save to cache file

This commit is contained in:
Jonathan Shook 2023-12-22 13:37:04 -06:00
parent b91648e8a3
commit dd7c721aea
4 changed files with 43 additions and 6 deletions

View File

@ -417,7 +417,8 @@ public class NBCLI implements Function<String[], Integer>, NBLabeledElement {
Map.of(
"summary", options.getReportSummaryTo(),
"logsdir", options.getLogsDirectory().toString(),
"progress", options.getProgressSpec()
"progress", options.getProgressSpec(),
"prompush_cache", "prompush_cache.txt"
).forEach(session::setComponentProp);
options.wantsReportCsvTo().ifPresent(cfg -> {

View File

@ -21,6 +21,11 @@ public enum MetricCategory {
* Metrics which are essential to understanding the behavior of any activity
*/
Core,
/**
* Some metrics are provided only to inform the user of relative or absolute progress,
* in terms of cycles remaining or similar
*/
Progress,
/**
* Metrics which mirror configuration data, either static or dynamic during the lifetime
* of an activity, session, or container. These are shared because they may need to be known

View File

@ -17,6 +17,8 @@
package io.nosqlbench.nb.api.engine.metrics.reporters;
import com.codahale.metrics.*;
import io.nosqlbench.nb.api.engine.metrics.instruments.MetricCategory;
import io.nosqlbench.nb.api.engine.metrics.instruments.NBMetric;
import io.nosqlbench.nb.api.labels.NBLabeledElement;
import io.nosqlbench.nb.api.labels.NBLabels;
import org.apache.logging.log4j.LogManager;
@ -26,7 +28,9 @@ import java.io.IOException;
import java.io.Writer;
import java.time.Clock;
import java.time.Instant;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Format NBMetrics according to the prometheus exposition format.
@ -59,6 +63,16 @@ public class PromExpositionFormat {
for (final Object metric : metrics) {
NBLabels labels = null;
if (metric instanceof NBMetric nbm) {
MetricCategory[] categories = nbm.getCategories();
buffer.append("# CATEGORIES: ")
.append(Arrays.stream(categories).map(MetricCategory::name).collect(Collectors.joining(", ")))
.append("\n");
String description = nbm.getDescription();
buffer.append("# DESCRIPTION: ").append(description).append("\n");
}
if (metric instanceof final NBLabeledElement labeled) labels = labeled.getLabels();
else throw new RuntimeException(
"Unknown label set for metric type '" + metric.getClass().getCanonicalName() + '\''

View File

@ -33,8 +33,10 @@ import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
@ -49,18 +51,18 @@ public class PromPushReporterComponent extends PeriodicTaskComponent {
private String bearerToken;
public PromPushReporterComponent(NBComponent parent, String endpoint, long intervalMs, NBLabels nbLabels) {
super(parent,nbLabels.and("_type","prom-push"),intervalMs,true,"REPORT-PROMPUSH");
super(parent, nbLabels.and("_type", "prom-push"), intervalMs, true, "REPORT-PROMPUSH");
String jobname = getLabels().valueOfOptional("jobname").orElse("default");
String instance = getLabels().valueOfOptional("instance").orElse("default");
if (jobname.equals("default")||instance.equals("default")) {
if (jobname.equals("default") || instance.equals("default")) {
logger.warn("It is highly recommended that you set a value for labels jobname and instance other than 'default'.");
}
if (endpoint.matches("victoria:[a-zA-Z0-9._-]+:[0-9]+")) {
String[] parts = endpoint.split(":", 2);
endpoint = "https://"+parts[1]+"/api/v1/import/prometheus/metrics/job/JOBNAME/instance/INSTANCE";
endpoint = "https://" + parts[1] + "/api/v1/import/prometheus/metrics/job/JOBNAME/instance/INSTANCE";
}
endpoint=endpoint.replace("JOBNAME",jobname).replace("INSTANCE",instance);
endpoint = endpoint.replace("JOBNAME", jobname).replace("INSTANCE", instance);
if (!endpoint.contains(jobname)) {
throw new BasicError("Mismatch between jobname in prompush URI and specified jobname label. You should use the short form for --report-prompush-to victoria:addr:port and set the jobname with --add-labels");
}
@ -109,9 +111,24 @@ public class PromPushReporterComponent extends PeriodicTaskComponent {
remainingRetries--;
final HttpClient client = getCachedClient();
final HttpRequest.Builder rb = HttpRequest.newBuilder().uri(uri);
if (bearerToken!=null) {
if (bearerToken != null) {
rb.setHeader("Authorization", "Bearer " + bearerToken);
}
getComponentProp("prompush_cache")
.map(cache -> Path.of(getComponentProp("logsdir").orElse("."))
.resolve("cache")).ifPresent(
prompush_cache_path -> {
try {
Files.writeString(
prompush_cache_path,
exposition,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
final HttpRequest request = rb.POST(BodyPublishers.ofString(exposition)).build();
final BodyHandler<String> handler = HttpResponse.BodyHandlers.ofString();
HttpResponse<String> response = null;