-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add docker stats support #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/github/dockerjava/api/command/StatsCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.github.dockerjava.api.command; | ||
|
|
||
| import com.github.dockerjava.api.model.Statistics; | ||
|
|
||
| /** | ||
| * Stats callback | ||
| */ | ||
| public interface StatsCallback { | ||
| public void onStats(Statistics stats); | ||
| public void onException(Throwable throwable); | ||
| public void onCompletion(int numStats); | ||
| public boolean isReceiving(); | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/github/dockerjava/api/command/StatsCmd.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.github.dockerjava.api.command; | ||
|
|
||
| import java.util.concurrent.ExecutorService; | ||
|
|
||
|
|
||
| /** | ||
| * Get stats | ||
| * | ||
| */ | ||
| public interface StatsCmd extends DockerCmd<ExecutorService> { | ||
| public StatsCmd withContainerId(String containerId); | ||
|
|
||
| public String getContainerId(); | ||
|
|
||
| public StatsCmd withStatsCallback(StatsCallback statsCallback); | ||
|
|
||
| public StatsCallback getStatsCallback(); | ||
|
|
||
| public static interface Exec extends DockerCmdExec<StatsCmd, ExecutorService> { | ||
| } | ||
|
|
||
| } |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/github/dockerjava/api/model/Statistics.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.github.dockerjava.api.model; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.apache.commons.lang.builder.ToStringBuilder; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| /** | ||
| * Representation of a Docker statistics. | ||
| */ | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| public class Statistics { | ||
|
|
||
| @JsonProperty("read") | ||
| private String read; | ||
|
|
||
| @JsonProperty("network") | ||
| private Map<String,Object> networkStats; | ||
|
|
||
| @JsonProperty("memory_stats") | ||
| private Map<String,Object> memoryStats; | ||
|
|
||
| @JsonProperty("blkio_stats") | ||
| private Map<String,Object> blkioStats; | ||
|
|
||
| @JsonProperty("cpu_stats") | ||
| private Map<String,Object> cpuStats; | ||
|
|
||
| public Map<String,Object> getNetworkStats() { | ||
| return networkStats; | ||
| } | ||
|
|
||
| public Map<String,Object> getCpuStats() { | ||
| return cpuStats; | ||
| } | ||
|
|
||
| public Map<String,Object> getMemoryStats() { | ||
| return memoryStats; | ||
| } | ||
|
|
||
| public Map<String,Object> getBlkioStats() { | ||
| return blkioStats; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return ToStringBuilder.reflectionToString(this); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/github/dockerjava/core/command/StatsCmdImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.github.dockerjava.core.command; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import java.util.concurrent.ExecutorService; | ||
|
|
||
| import com.github.dockerjava.api.command.EventCallback; | ||
| import com.github.dockerjava.api.command.EventsCmd; | ||
| import com.github.dockerjava.api.command.StatsCallback; | ||
| import com.github.dockerjava.api.command.StatsCmd; | ||
| import com.github.dockerjava.api.command.TopContainerCmd; | ||
|
|
||
| /** | ||
| * Stream docker stats | ||
| */ | ||
| public class StatsCmdImpl extends AbstrDockerCmd<StatsCmd, ExecutorService> implements StatsCmd { | ||
|
|
||
| private String containerId; | ||
| private StatsCallback statsCallback; | ||
|
|
||
| public StatsCmdImpl(StatsCmd.Exec exec, StatsCallback statsCallback) { | ||
| super(exec); | ||
| withStatsCallback(statsCallback); | ||
| } | ||
|
|
||
| @Override | ||
| public StatsCmd withContainerId(String containerId) { | ||
| checkNotNull(containerId, "containerId was not specified"); | ||
| this.containerId = containerId; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public String getContainerId() { | ||
| return containerId; | ||
| } | ||
|
|
||
| @Override | ||
| public StatsCmd withStatsCallback(StatsCallback statsCallback) { | ||
| this.statsCallback = statsCallback; | ||
| return this; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public StatsCallback getStatsCallback() { | ||
| return statsCallback; | ||
| } | ||
|
|
||
| @Override | ||
| public ExecutorService exec() { | ||
| return super.exec(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return new StringBuilder("stats") | ||
| .append(containerId != null ? " --id=" + containerId : "") | ||
| .toString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/github/dockerjava/jaxrs/StatsCmdExec.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.github.dockerjava.jaxrs; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| import javax.ws.rs.client.WebTarget; | ||
| import javax.ws.rs.core.Response; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.github.dockerjava.api.command.StatsCallback; | ||
| import com.github.dockerjava.api.command.StatsCmd; | ||
| import com.github.dockerjava.api.model.Statistics; | ||
| import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream; | ||
|
|
||
| public class StatsCmdExec extends AbstrDockerCmdExec<StatsCmd, ExecutorService> implements StatsCmd.Exec { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(StatsCmdExec.class); | ||
|
|
||
| public StatsCmdExec(WebTarget baseResource) { | ||
| super(baseResource); | ||
| } | ||
|
|
||
| @Override | ||
| protected ExecutorService execute(StatsCmd command) { | ||
| ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
|
|
||
| WebTarget webResource = getBaseResource().path("/containers/{id}/stats") | ||
| .resolveTemplate("id", command.getContainerId()); | ||
|
|
||
| LOGGER.trace("GET: {}", webResource); | ||
| StatsNotifier eventNotifier = StatsNotifier.create(command.getStatsCallback(), webResource); | ||
| executorService.submit(eventNotifier); | ||
| return executorService; | ||
| } | ||
|
|
||
| private static class StatsNotifier implements Callable<Void> { | ||
| private static final JsonFactory JSON_FACTORY = new JsonFactory(); | ||
| private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
|
||
| private final StatsCallback statsCallback; | ||
| private final WebTarget webTarget; | ||
|
|
||
| private StatsNotifier(StatsCallback statsCallback, WebTarget webTarget) { | ||
| this.statsCallback = statsCallback; | ||
| this.webTarget = webTarget; | ||
| } | ||
|
|
||
| public static StatsNotifier create(StatsCallback statsCallback, WebTarget webTarget) { | ||
| checkNotNull(statsCallback, "An StatsCallback must be provided"); | ||
| checkNotNull(webTarget, "An WebTarget must be provided"); | ||
| return new StatsNotifier(statsCallback, webTarget); | ||
| } | ||
|
|
||
| @Override | ||
| public Void call() throws Exception { | ||
| int numStats=0; | ||
| Response response = null; | ||
| try { | ||
| response = webTarget.request().get(Response.class); | ||
| InputStream inputStream = new WrappedResponseInputStream(response); | ||
| JsonParser jp = JSON_FACTORY.createParser(inputStream); | ||
| while (jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed() && statsCallback.isReceiving()) { | ||
| statsCallback.onStats(OBJECT_MAPPER.readValue(jp, Statistics.class)); | ||
| numStats++; | ||
| } | ||
| statsCallback.onCompletion(numStats); | ||
| LOGGER.info("Finished collecting stats"); | ||
| return null ; | ||
| } | ||
| catch(Throwable t) { | ||
| statsCallback.onException(t); | ||
| } | ||
| finally { | ||
| if (response != null) { | ||
| response.close(); | ||
| } | ||
| } | ||
| return null ; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be needed. Could you remove this please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well spotted !
Done.
Thanks !
On 7 Apr 2015 at 14:23:49, marcuslinke ([email protected]) wrote:
In src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java:
—
Reply to this email directly or view it on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Markus,
My apologies, I’m still quite new to github.
I believe it should be correct now.
Best regards,
Eric
On 7 Apr 2015 at 14:40:24, Eric Fjøsne ([email protected]) wrote:
Well spotted !
Done.
Thanks !
On 7 Apr 2015 at 14:23:49, marcuslinke ([email protected]) wrote:
In src/test/java/com/github/dockerjava/client/AbstractDockerClientTest.java:
This should not be needed. Could you remove this please?
—
Reply to this email directly or view it on GitHub.