mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
auto create apikey
This commit is contained in:
parent
cfa92eabcc
commit
3d2ff55f1c
@ -25,13 +25,24 @@ public class GrafanaClientConfig {
|
||||
private int timeoutms;
|
||||
|
||||
private final List<Authenticator> authenticators = new ArrayList<>();
|
||||
// private LinkedHashMap<String,String> headers = new LinkedHashMap<>();
|
||||
private final List<Supplier<Map<String, String>>> headerSources = new ArrayList<>();
|
||||
|
||||
public GrafanaClientConfig() {
|
||||
}
|
||||
|
||||
public void basicAuth(String username, String pw) {
|
||||
private GrafanaClientConfig(URI baseUrl, int timeoutms, List<Authenticator> authenticators,
|
||||
List<Supplier<Map<String, String>>> headerSources) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.timeoutms = timeoutms;
|
||||
this.authenticators.addAll(authenticators);
|
||||
this.headerSources.addAll(headerSources);
|
||||
}
|
||||
|
||||
public GrafanaClientConfig copy() {
|
||||
return new GrafanaClientConfig(baseUrl, timeoutms, authenticators, headerSources);
|
||||
}
|
||||
|
||||
public GrafanaClientConfig basicAuth(String username, String pw) {
|
||||
Objects.requireNonNull(username);
|
||||
String authPw = pw != null ? pw : "";
|
||||
|
||||
@ -44,9 +55,9 @@ public class GrafanaClientConfig {
|
||||
|
||||
addAuthenticator(basicAuth);
|
||||
addHeader("Authorization", encodeBasicAuth(username, authPw));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public GrafanaClientConfig addAuthenticator(Authenticator authenticator) {
|
||||
authenticators.add(authenticator);
|
||||
return this;
|
||||
@ -87,7 +98,9 @@ public class GrafanaClientConfig {
|
||||
|
||||
public HttpClient newClient() {
|
||||
HttpClient.Builder cb = HttpClient.newBuilder();
|
||||
if (timeoutms > 0) {
|
||||
cb.connectTimeout(Duration.ofMillis(timeoutms));
|
||||
}
|
||||
for (Authenticator authenticator : authenticators) {
|
||||
cb.authenticator(authenticator);
|
||||
}
|
||||
@ -106,8 +119,11 @@ public class GrafanaClientConfig {
|
||||
public HttpRequest.Builder newRequest(String path) {
|
||||
URI requestUri = makeUri(path);
|
||||
HttpRequest.Builder rqb = HttpRequest.newBuilder(requestUri);
|
||||
if (timeoutms > 0) {
|
||||
rqb.timeout(Duration.ofMillis(timeoutms));
|
||||
}
|
||||
getHeaders().forEach(rqb::setHeader);
|
||||
|
||||
return rqb;
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package io.nosqlbench.engine.core.metrics;
|
||||
package io.nosqlbench.engine.clients.grafana;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@ -14,11 +14,14 @@ public class GrafanaKeyFileReader implements Supplier<String> {
|
||||
|
||||
private final Path keyfilePath;
|
||||
|
||||
public GrafanaKeyFileReader(Path path) {
|
||||
this.keyfilePath = path;
|
||||
}
|
||||
|
||||
public GrafanaKeyFileReader(String sourcePath) {
|
||||
this.keyfilePath = Path.of(sourcePath);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
if (!Files.exists(keyfilePath)) {
|
||||
@ -27,6 +30,7 @@ public class GrafanaKeyFileReader implements Supplier<String> {
|
||||
} else {
|
||||
try {
|
||||
String apikey = Files.readString(keyfilePath, StandardCharsets.UTF_8);
|
||||
apikey = apikey.trim();
|
||||
return apikey;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
@ -1,15 +1,18 @@
|
||||
package io.nosqlbench.engine.core.metrics;
|
||||
package io.nosqlbench.engine.clients.grafana;
|
||||
|
||||
import io.nosqlbench.engine.clients.grafana.GrafanaClient;
|
||||
import io.nosqlbench.engine.clients.grafana.GrafanaClientConfig;
|
||||
import io.nosqlbench.engine.clients.grafana.transfer.GrafanaAnnotation;
|
||||
import io.nosqlbench.nb.annotations.Service;
|
||||
import io.nosqlbench.nb.api.Environment;
|
||||
import io.nosqlbench.nb.api.OnError;
|
||||
import io.nosqlbench.nb.api.SystemId;
|
||||
import io.nosqlbench.nb.api.annotations.Annotation;
|
||||
import io.nosqlbench.nb.api.annotations.Annotator;
|
||||
import io.nosqlbench.nb.api.config.*;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@ -38,16 +41,16 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
|
||||
ga.setTimeEnd(annotation.getEnd());
|
||||
|
||||
annotation.getLabels().forEach((k, v) -> {
|
||||
ga.getTags().put(k, v);
|
||||
ga.getTags().add(k + ":" + v);
|
||||
});
|
||||
ga.getTags().put("layer", annotation.getLayer().toString());
|
||||
ga.getTags().add("layer:" + annotation.getLayer().toString());
|
||||
|
||||
Map<String, String> labels = annotation.getLabels();
|
||||
|
||||
Optional.ofNullable(labels.get("alertId" ))
|
||||
.map(Integer::parseInt).ifPresent(ga::setAlertId);
|
||||
|
||||
ga.setData(annotation.toString());
|
||||
ga.setText(annotation.toString());
|
||||
|
||||
annotation.getSession();
|
||||
|
||||
@ -111,20 +114,6 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
|
||||
this.tags = ParamsParser.parse(cfg.param("tags", String.class), false);
|
||||
}
|
||||
|
||||
if (cfg.containsKey("apikeyfile")) {
|
||||
String apikeyfile = cfg.paramEnv("apikeyfile", String.class);
|
||||
AuthWrapper authHeaderSupplier = new AuthWrapper(
|
||||
"Authorization",
|
||||
new GrafanaKeyFileReader(apikeyfile),
|
||||
s -> "Bearer " + s + ";"
|
||||
);
|
||||
gc.addHeaderSource(authHeaderSupplier);
|
||||
}
|
||||
|
||||
if (cfg.containsKey("apikey")) {
|
||||
gc.addHeaderSource(() -> Map.of("Authorization", "Bearer " + cfg.param("apikey", String.class)));
|
||||
}
|
||||
|
||||
if (cfg.containsKey("username" )) {
|
||||
if (cfg.containsKey("password" )) {
|
||||
gc.basicAuth(
|
||||
@ -136,9 +125,44 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
|
||||
}
|
||||
}
|
||||
|
||||
Path keyfilePath = null;
|
||||
if (cfg.containsKey("apikeyfile" )) {
|
||||
String apikeyfile = cfg.paramEnv("apikeyfile", String.class);
|
||||
keyfilePath = Path.of(apikeyfile);
|
||||
} else if (cfg.containsKey("apikey" )) {
|
||||
gc.addHeaderSource(() -> Map.of("Authorization", "Bearer " + cfg.param("apikey", String.class)));
|
||||
} else {
|
||||
Optional<String> apikeyLocation = Environment.INSTANCE.interpolate("$NBSTATEDIR/grafana_apikey" );
|
||||
keyfilePath = apikeyLocation.map(Path::of).orElseThrow();
|
||||
}
|
||||
|
||||
if (!Files.exists(keyfilePath)) {
|
||||
logger.info("Auto-configuring grafana apikey." );
|
||||
GrafanaClientConfig apiClientConf = gc.copy().basicAuth("admin", "admin" );
|
||||
GrafanaClient apiClient = new GrafanaClient(apiClientConf);
|
||||
try {
|
||||
String nodeId = SystemId.getNodeId();
|
||||
|
||||
String keyName = "nosqlbench-" + nodeId + "-" + System.currentTimeMillis();
|
||||
ApiToken apiToken = apiClient.createApiToken(keyName, "Admin", Long.MAX_VALUE);
|
||||
Files.writeString(keyfilePath, apiToken.getKey());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
AuthWrapper authHeaderSupplier = new AuthWrapper(
|
||||
"Authorization",
|
||||
new GrafanaKeyFileReader(keyfilePath),
|
||||
s -> "Bearer " + s
|
||||
);
|
||||
gc.addHeaderSource(authHeaderSupplier);
|
||||
|
||||
this.onError = OnError.valueOfName(cfg.get("onerror" ).toString());
|
||||
|
||||
this.client = new GrafanaClient(gc);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -146,7 +170,7 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
|
||||
return new MutableConfigModel(this)
|
||||
.required("baseurl", String.class,
|
||||
"The base url of the grafana node, like http://localhost:3000/" )
|
||||
.defaultto("apikeyfile", "$NBSTATEDIR/grafana_key",
|
||||
.defaultto("apikeyfile", "$NBSTATEDIR/grafana_apikey",
|
||||
"The file that contains the api key, supersedes apikey" )
|
||||
.optional("apikey", String.class,
|
||||
"The api key to use, supersedes basic username and password" )
|
@ -17,7 +17,8 @@ public class GrafanaAnnotation {
|
||||
private String text;
|
||||
private String metric;
|
||||
private String type;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private final List<String> tags = new ArrayList<>();
|
||||
// private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private Object data;
|
||||
|
||||
public Integer getId() {
|
||||
@ -124,12 +125,22 @@ public class GrafanaAnnotation {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
public List<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void addTag(String tag) {
|
||||
this.tags.add(tag);
|
||||
}
|
||||
|
||||
public void setTags(List<String> tags) {
|
||||
tags.forEach(this::addTag);
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags;
|
||||
tags.forEach((k, v) -> {
|
||||
this.addTag(k + ":" + v);
|
||||
});
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
|
@ -37,4 +37,12 @@ public class ConfigReader extends LinkedHashMap<String, Object> {
|
||||
T typeCastedValue = type.cast(o);
|
||||
return typeCastedValue;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(this.configModel.getOf().getSimpleName()).append(":" );
|
||||
sb.append(this.configModel.toString());
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user