Skip to content
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

Allow pulling stats once and disconnecting. #10766

Merged
merged 1 commit into from May 5, 2015

Conversation

cpuguy83
Copy link
Member

Adds a once query param to the stats API which allows API users to
only collect one stats entry and disconnect instead of keeping the
connection alive to stream more stats.

Open to changing the query param here... maybe have a "follow" param that defaults to true (since today we follow by default)?
I've seen several people wanting to just be able to grab and go instead of streaming... and then I saw: discourse/discourse_docker@03b5043#commitcomment-9731330

Figure simple change on the Docker side so clients don't have to deal with it.

@phemmer
Copy link
Contributor

phemmer commented Feb 13, 2015

Why would this be a server side option, and not client side? The client would just disconnect when it has had enough.
If this is going to be server side, I would argue for a count, not a 'once' parameter. As in "give me 1 stats entry".

@cpuguy83
Copy link
Member Author

@phemmer As stated above, purely for convenience. Easy change, we can take it or leave it (or change it for a better query param behavior).

@jessfraz
Copy link
Contributor

@crosbymichael wdyt

@icecrime
Copy link
Contributor

icecrime commented Mar 7, 2015

Reviewed with @crosbymichael: we suggest having a boolean stream parameter instead, which would be true by default, and stream only once when false. This would have to be available from the cli.

@cpuguy83
Copy link
Member Author

cpuguy83 commented Mar 7, 2015

+1 I'll implement.

@cpuguy83
Copy link
Member Author

cpuguy83 commented Mar 7, 2015

This is now implemented with the stream query param.
In the CLI I implemented as --no-stream

@@ -2722,6 +2732,7 @@ func (s *containerStats) Display(w io.Writer) error {

func (cli *DockerCli) CmdStats(args ...string) error {
cmd := cli.Subcmd("stats", "CONTAINER [CONTAINER...]", "Display a live stream of one or more containers' resource usage statistics", true)
noStream := cmd.Bool([]string{"-no-stream"}, false, "Disable streaming stats and only pull the first result")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the bash completion scrip should also be update, the --no-stream flag should be add to it:)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, but we usually do it at release time rather than for each CLI change it seems (ping @jfrazelle as 1.6.0 release captain: is it ok for you?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally would prefer we take the habit of updating the completion scripts in the same PR :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiborvass I agree it would be better, but I hate asking for contributors to learn about those (for example the fish completion requires to use a python script which generates completion file from Docker's help message) :-(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and added these completion scripts. Please check!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well 😄 re-LGTM!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you not call the flag stream also? --stream=true|false so much cleaner

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it would have to be defaulted to true, and as such someone would have to type out the full --stream=false, instead of simply --no-stream

@@ -2660,6 +2666,10 @@ func (s *containerStats) Collect(cli *DockerCli) {
for {
var v *types.Stats
if err := dec.Decode(&v); err != nil {
if err == io.EOF && !streamStats {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about the use of && !streamStats here. If the server breaks after 1 stream anyway, isn't the stop condition properly covered by the err = io.EOF part?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I didn't want to change the existing behavior.
And I specifically had to adjust this so that the CLI would actually output data and not an EOF.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sorry, I misunderstood on first read. So this is basically saying: EOF is expected when you didn't ask for streamStats.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@icecrime
Copy link
Contributor

icecrime commented Mar 9, 2015

LGTM


chErr := make(chan error, 1)
go func() {
_, err := runCommand(exec.Command(dockerBinary, "stats", "--no-stream", name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpuguy83 chErr <- exec.Command(...).Run()

@cpuguy83 cpuguy83 force-pushed the stats_pull_once branch 2 times, most recently from 3a34c7c to 967721a Compare March 13, 2015 15:01
@cpuguy83
Copy link
Member Author

Updated

@tiborvass
Copy link
Contributor

@cpuguy83 is this a breaking change in API? I see stream being false by default.

@@ -430,6 +430,13 @@ func getContainersStats(eng *engine.Engine, version version.Version, w http.Resp
}
name := vars["name"]
job := eng.Job("container_stats", name)

stream := r.Form.Get("stream")
if stream == "" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiborvass It shouldn't be defaulting to false because of this right here.
Tested again just to make sure.

@jamtur01
Copy link
Contributor

Docs LGTM

@cpuguy83
Copy link
Member Author

Rebased.
Also exits more cleanly/safely now, and fixed a display issue from the last rebase.

@icecrime icecrime removed the dco/yes label Apr 23, 2015
@icecrime
Copy link
Contributor

Needs rebase :-(

@cpuguy83 cpuguy83 force-pushed the stats_pull_once branch 2 times, most recently from d9b26ed to 8d346c3 Compare April 28, 2015 02:57
@cpuguy83
Copy link
Member Author

rebased

@tobegit3hub
Copy link

Great job @cpuguy83 👍

name = "statscontainer"
runCmd = exec.Command(dockerBinary, "run", "-d", "--name", name, "busybox", "top")
)
out, _, err := runCommandWithOutput(runCmd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would inline runCmd

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, no idea why I had it this way.

Adds a `stream` query param to the stats API which allows API users to
only collect one stats entry and disconnect instead of keeping the
connection alive to stream more stats.

Also adds a `--no-stream` flag to `docker stats` which does the same

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
@cpuguy83
Copy link
Member Author

cpuguy83 commented May 4, 2015

Updated

@tiborvass
Copy link
Contributor

LGTM

@tiborvass
Copy link
Contributor

Ping @moxiegirl @fredlf @SvenDowideit

@SvenDowideit
Copy link
Contributor

docs LGTM

@thaJeztah
Copy link
Member

docs LGTM

@tiborvass @icecrime do we need another maintainer to LGTM, because there were a number of code changes made after the original LGTM's?

@tiborvass
Copy link
Contributor

Not for this one but thanks @thaJeztah !

tiborvass added a commit that referenced this pull request May 5, 2015
Allow pulling stats once and disconnecting.
@tiborvass tiborvass merged commit 91fb4d6 into moby:master May 5, 2015
@tobegit3hub
Copy link

Great job @cpuguy83 and @tiborvass 👍

We gonna have this feature soon!

@Priyanky
Copy link

Will this fix work for API versions before 1.19 ?
I use 1.18 and streaming seems to be the only way

@cpuguy83 cpuguy83 deleted the stats_pull_once branch August 27, 2015 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet