Skip to content

Commit

Permalink
Print container stats on container shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and losipiuk committed Sep 14, 2020
1 parent f9e83fa commit a482e37
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
10 changes: 10 additions & 0 deletions presto-product-tests-launcher/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
<artifactId>units</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.compose;
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.logCopyingListener;
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.loggingListener;
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.statsPrintingListener;
import static java.util.Objects.requireNonNull;
import static picocli.CommandLine.Mixin;
import static picocli.CommandLine.Option;
Expand Down Expand Up @@ -144,7 +145,11 @@ public void run()
}

Optional<Path> environmentLogPath = logsDirBase.map(dir -> dir.resolve(environment));
Environment environment = builder.build(compose(loggingListener(), logCopyingListener(environmentLogPath)));
Environment environment = builder.build(compose(
loggingListener(),
logCopyingListener(environmentLogPath),
statsPrintingListener()));

environment.start();

if (background) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.compose;
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.logCopyingListener;
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.loggingListener;
import static io.prestosql.tests.product.launcher.env.EnvironmentListener.statsPrintingListener;
import static io.prestosql.tests.product.launcher.env.common.Standard.CONTAINER_TEMPTO_PROFILE_CONFIG;
import static java.util.Objects.requireNonNull;
import static org.testcontainers.containers.BindMode.READ_ONLY;
Expand Down Expand Up @@ -242,7 +243,10 @@ private Environment getEnvironment()

environmentConfig.extendEnvironment(this.environment).ifPresent(extender -> extender.extendEnvironment(environment));

return environment.build(compose(loggingListener(), logCopyingListener(logsDirBase)));
return environment.build(compose(
loggingListener(),
logCopyingListener(logsDirBase),
statsPrintingListener()));
}

private static Iterable<? extends String> reportsDirOptions(Path path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
*/
package io.prestosql.tests.product.launcher.env;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.Statistics;
import com.github.dockerjava.core.InvocationBuilder;
import com.google.common.base.Splitter;
import com.google.common.base.Stopwatch;
import com.google.common.io.RecursiveDeleteOption;
import io.airlift.log.Logger;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.SelinuxContext;
Expand Down Expand Up @@ -210,6 +214,18 @@ private Stream<String> listFilesInContainer(String path)
return Stream.empty();
}

public Statistics getStats()
{
try (DockerClient client = DockerClientFactory.lazyClient()) {
InvocationBuilder.AsyncResultCallback<Statistics> callback = new InvocationBuilder.AsyncResultCallback<>();
client.statsCmd(getContainerId()).exec(callback);
return callback.awaitResult();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package io.prestosql.tests.product.launcher.env;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.command.InspectContainerResponse;
import io.airlift.log.Logger;

Expand All @@ -23,6 +25,7 @@
public interface EnvironmentListener
{
Logger log = Logger.get(EnvironmentListener.class);
ObjectMapper mapper = new ObjectMapper();

default void environmentStarting(Environment environment)
{
Expand Down Expand Up @@ -179,6 +182,23 @@ public void containerStopping(DockerContainer container, InspectContainerRespons
};
}

static EnvironmentListener statsPrintingListener()
{
return new EnvironmentListener()
{
@Override
public void containerStopping(DockerContainer container, InspectContainerResponse response)
{
try {
log.info("Container %s stats: %s", container, mapper.writeValueAsString(container.getStats()));
}
catch (JsonProcessingException e) {
log.warn("Could not display container %s stats: %s", container, e);
}
}
};
}

static EnvironmentListener noop()
{
return new EnvironmentListener() {};
Expand Down

0 comments on commit a482e37

Please sign in to comment.