Skip to content

Commit

Permalink
Fix displaying stats
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and losipiuk committed Sep 17, 2020
1 parent 8757e7f commit d548911
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Optional;
import java.util.stream.Stream;

import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.io.MoreFiles.deleteRecursively;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -214,16 +215,20 @@ private Stream<String> listFilesInContainer(String path)
return Stream.empty();
}

public Statistics getStats()
public Optional<Statistics> getStats()
{
try (DockerClient client = DockerClientFactory.lazyClient()) {
InvocationBuilder.AsyncResultCallback<Statistics> callback = new InvocationBuilder.AsyncResultCallback<>();
client.statsCmd(getContainerId()).exec(callback);
return callback.awaitResult();
return Optional.ofNullable(callback.awaitResult());
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
catch (Exception e) {
log.error("Could not fetch container %s statistics: %s", logicalName, getStackTraceAsString(e));
return Optional.empty();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ static EnvironmentListener statsPrintingListener()
@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);
}
container.getStats().ifPresent(statistics -> {
try {
log.info("Container %s stats: %s", container, mapper.writeValueAsString(statistics));
}
catch (JsonProcessingException e) {
log.warn("Could not display container %s stats: %s", container, e);
}
});
}
};
}
Expand Down

0 comments on commit d548911

Please sign in to comment.