migrate --docker-metrics to okhttp

This commit is contained in:
Jonathan Shook 2020-03-23 18:30:35 -05:00
parent 4b434f871f
commit f9c9beb4c9
3 changed files with 255 additions and 216 deletions

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@ -51,14 +52,19 @@
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-jersey</artifactId>
<artifactId>docker-java-transport-okhttp</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-transport-netty</artifactId>-->
<!-- <version>3.2.0-rc2</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-transport-jersey</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-transport-netty</artifactId>-->
<!-- <version>3.2.0-rc2</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.docker-java</groupId>
@ -68,13 +74,17 @@
<artifactId>docker-java-transport-netty</artifactId>
<groupId>com.github.docker-java</groupId>
</exclusion>
<exclusion>
<artifactId>docker-java-transport-jersey</artifactId>
<groupId>com.github.docker-java</groupId>
</exclusion>
</exclusions>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>docker-java-transport-jersey</artifactId>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>docker-java-transport-jersey</artifactId>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>io.nosqlbench</groupId>
@ -87,14 +97,16 @@
<profile>
<id>shade</id>
<activation>
<activeByDefault>true</activeByDefault>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<executions>
<execution>
<goals><goal>shade</goal></goals>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
</execution>
</executions>

View File

@ -19,17 +19,19 @@ import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;
import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;
//import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;
import com.github.dockerjava.okhttp.OkHttpDockerCmdExecFactory;
import com.sun.security.auth.module.UnixSystem;
import io.nosqlbench.engine.api.util.NosqlBenchFiles;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
import java.net.Authenticator;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -45,7 +47,8 @@ public class DockerMetricsHelper {
private static final String DOCKER_HOST = "DOCKER_HOST";
private static final String DOCKER_HOST_ADDR = "unix:///var/run/docker.sock";
String userHome = System.getProperty("user.home");
private Client rsClient = ClientBuilder.newClient();
// private Client rsClient = ClientBuilder.newClient();
private DockerClientConfig config;
private DockerClient dockerClient;
private Logger logger = LoggerFactory.getLogger(DockerMetricsHelper.class);
@ -53,10 +56,14 @@ public class DockerMetricsHelper {
public DockerMetricsHelper() {
System.getProperties().setProperty(DOCKER_HOST, DOCKER_HOST_ADDR);
this.config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost(DOCKER_HOST_ADDR).build();
DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
DockerCmdExecFactory dockerCmdExecFactory = new OkHttpDockerCmdExecFactory()
.withReadTimeout(1000)
.withConnectTimeout(1000);
// DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
// .withReadTimeout(1000)
// .withConnectTimeout(1000);
this.dockerClient = DockerClientBuilder.getInstance(config)
.withDockerCmdExecFactory(dockerCmdExecFactory)
.build();
@ -240,36 +247,41 @@ public class DockerMetricsHelper {
private void configureGrafana() {
Response response = post("http://localhost:3000/api/dashboards/db", "docker/dashboards/analysis.json", true);
response = post("http://localhost:3000/api/datasources", "docker/datasources/prometheus-datasource.yaml", true);
post("http://localhost:3000/api/dashboards/db", "docker/dashboards/analysis.json", true);
post("http://localhost:3000/api/datasources", "docker/datasources/prometheus-datasource.yaml", true);
}
private Response post(String url, String path, boolean auth) {
private static String basicAuth(String username, String password) {
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}
private HttpResponse<String> post(String url, String path, boolean auth) {
HttpClient.Builder clientBuilder = HttpClient.newBuilder();
HttpClient httpClient = clientBuilder.build();
HttpRequest.Builder builder = HttpRequest.newBuilder();
builder = builder.uri(URI.create(url));
if (auth) {
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin");
rsClient.register(feature);
builder = builder.header("Authorization", basicAuth("username", "password"));
}
WebTarget webTarget
= rsClient.target(url);
Invocation.Builder invocationBuilder
= webTarget.request(MediaType.APPLICATION_JSON);
Response response;
if (path != null) {
if (path !=null) {
String dashboard = NosqlBenchFiles.readFile(path);
response
= invocationBuilder
.post(Entity.entity(dashboard, MediaType.APPLICATION_JSON));
builder = builder.POST(HttpRequest.BodyPublishers.ofString(dashboard));
} else {
response
= invocationBuilder
.post(Entity.entity(null, MediaType.APPLICATION_JSON));
builder = builder.GET();
}
return response;
HttpRequest request = builder.build();
try {
HttpResponse<String> resp = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
logger.debug("http response for configuring grafana:\n" + resp);
return resp;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

View File

@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@ -83,10 +84,10 @@
<!-- <dependency>-->
<!-- <groupId>io.nosqlbench</groupId>-->
<!-- <artifactId>nb-runtime</artifactId>-->
<!-- <version>2.12.51-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- <groupId>io.nosqlbench</groupId>-->
<!-- <artifactId>nb-runtime</artifactId>-->
<!-- <version>2.12.51-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
@ -122,15 +123,15 @@
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-api</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-api</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-core</artifactId>-->
<!-- </dependency>-->
</dependencies>
@ -149,23 +150,26 @@
<execution>
<id>shade-nb-jar</id>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- <shadedArtifactAttached>true</shadedArtifactAttached>-->
<!-- <shadedClassifierName>jarapp</shadedClassifierName>-->
<!-- <shadedArtifactId>${project.artifactId}</shadedArtifactId>-->
<!-- <shadedArtifactAttached>true</shadedArtifactAttached>-->
<!-- <shadedClassifierName>jarapp</shadedClassifierName>-->
<!-- <shadedArtifactId>${project.artifactId}</shadedArtifactId>-->
<transformers combine.children="append">
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>topics.md</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.nosqlbench.engine.cli.NBCLI</mainClass>
</transformer>
</transformers>
<!-- <finalName>${project.artifactId}</finalName>-->
<!-- <finalName>${project.artifactId}</finalName>-->
<filters>
<filter>
@ -186,7 +190,9 @@
<execution>
<id>link-nb-jar-name</id>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<environmentVariables>
<NBJAR_VERSION>${project.version}</NBJAR_VERSION>
@ -198,7 +204,9 @@
<execution>
<id>build-static-guidebook</id>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/gendocs.sh</executable>
@ -212,6 +220,7 @@
<profiles>
<profile>
<id>build-nb-appimage</id>
<activation>
<os>
<family>unix</family>
@ -228,7 +237,9 @@
<execution>
<id>build-nb-appimage</id>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/build-bin.sh</executable>
@ -249,13 +260,17 @@
<plugin>
<executions>
<execution>
<goals><goal>enforce</goal></goals>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules><dependencyConvergence /></rules>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
</plugin>
</plugins>