-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix to enable streaming container logs reliably #441
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
Conversation
+1 for this patch. I've applied this patch in a project I'm working on, and it works great. Do keep in mind that the newline for every log line is not stripped by docker-py, so simply printing/logging the line will add an extra newline (unless you explicitly deal with it). |
While working with this patch some more, I've discovered that this often leads to a read timeout when no data is being returned for a while (for example a long running Docker process). File "/Users/lbrouwer/dev/vdist/lib/python2.7/site-packages/docker/client.py", line 323, in _multiplexed_response_stream_helper |
Yeah, you need to disable the timeout on the underlying socket. I have this in my version of the code:
(I believe |
@kevinastone added the settimeout in my patch. thanks! @objectified can you please try the updated patch? |
Thanks, the updated patch seems to fix this problem. I ran a number of tests with running/stalling/running/stalling/etc. behavior inside a few different docker containers, and I could not reproduce the original problem. |
@objectified thanks for the quick response! |
@shin- Any feedback on this PR? Please let me know. |
Sorry for the delay. Looks good, any reason you didn't entirely replace |
Good point about Python 3 - I've just tested this functionality using Python 3, and one thing I noticed while doing so is that the yielded data is a byte object, not a string. I'm not sure if that's intentional, but since this functionality is used to get logging from a container, I'd assume a string type would make the most sense. It can be easily fixed by replacing:
with:
But again, I don't know whether this is intentional. |
Hey @objectified , thank you for testing! this is not a change brought up by this PR. We've always yielded bytestrings in Py3. |
@objectified Am on it. thanks for the poke |
Started a ubuntu container that just runs "ping 8.8.8.8" and tried the sample code in https://gist.github.com/dims/c3327f633c526847c8e5 to recreate the problem mentioned in: #300 To debug the problem i printed the byte array read in recvall when reading STREAM_HEADER_SIZE_BYTES and realized that the data being read was far ahead of the actual start of the header documented in the vnd.docker.raw-stream of the docker remote api. This is possibly because the requests/urllib3 is reading ahead a bit more and we shouldn't be trying to hack the internals of those projects. So just using the documented file-like response.raw is good enough for us to get the functionality we need which is being able to read for exactly where the stream header starts. With this change i can reliably stream the logs just like "docker logs --follow". Note that we still need to access the underlying socket to set the timeout to prevent read time outs. The original fix was for client.logs() only but on further review it made sense to replace all occurances of _multiplexed_socket_stream_helper with the new method.
@shin-, @objectified - tested with python2.7 and python3.4 using this snippet - http://paste.openstack.org/show/158892/ |
Thanks @dims ! I've tested it with my project on 2.7 and 3.4 as well, and it appears to work nicely. |
@shin- Seems your input is processed now. If there's anything else related to this that needs testing or anything else that needs to be fixed, could you let us know? If this PR could be merged, that would be great. |
@shin- ping :) |
Sorry guys, I just want to run some tests on my end with this before merging, and I haven't had the time so far. This weekend at the latest. Thanks a lot for your help, and your patience. |
Thanks @shin- looking forward to feedback |
@shin- did you have a chance to run your tests yet? Sorry for hunting this one, I need this pull request merged so that my own project will work (which I can then open source). |
Alright, sorry about the delay! This all looks good :) Thanks a ton for your contribution! |
Fix to enable streaming container logs reliably
0.7.2 released with that fix :) |
Thanks @shin- |
This patch is based on @kevinastone 's code in:
#300 (comment)
Started a ubuntu container that just runs "ping 8.8.8.8" and tried
the sample code in https://gist.github.com/dims/c3327f633c526847c8e5
to recreate the problem mentioned in:
#300
To debug the problem i printed the byte array read in recvall
when reading STREAM_HEADER_SIZE_BYTES and realized that the data
being read was far ahead of the actual start of the header documented
in the vnd.docker.raw-stream of the docker remote api. This is
possibly because the requests/urllib3 is reading ahead a bit more
and we shouldn't be trying to hack the internals of those projects.
So just using the documented file-like response.raw is good enough
for us to get the functionality we need which is being able to
read for exactly where the stream header starts. With this change
i can reliably stream the logs just like "docker logs --follow".