1

I've pulled docker stats using docker API endpoint http://localhost:2375/containers/containerId/stats?stream=0(stream=0 will pull stats once only) (see my answer here) and used the response to calculate memory and CPU usage percentage of that container.

My requirement is that I want to show a continuous graph of memory and CPU usage percentages over time. I know this can be done by repeatedly pulling stats using AJAX after let's say 2 seconds and update the graph but that is not how I want to do it.

Now the docker stats API is over HTTP which is a request-response protocol, so have can I receive the continuous stream of JSON given from http://localhost:2375/containers/containerId/stats?stream=1 if the response will never finish as it is a continuous stream?

This is what I tried-

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

@RestController
public class DockerController {
    @GetMapping("/getStats/{containerId}")
    public String getStats(@PathVariable String containerId){
        String statistics = null;
        RestTemplate restTemplate =  new RestTemplate();
        statistics = restTemplate.getForObject("http://localhost:2375/containers/"+containerId+"/stats?stream=1", String.class);
        if(statistics != null){
            System.out.println("Container statistics - \n" + statistics); //I'll never receive this
        }
        return statistics;
    }
}

I've just started reading WebSockets and as per my understanding, both stream producer and consumer need to communicate over ws protocol. But as docker API endpoint is already producing the stream of JSON over HTTP I think WebSockets won't work here.

Any help would be greatly appreciated.

Mithilesh Tipkari
  • 697
  • 1
  • 10
  • 16
  • Indeed, HTTP responses are not usually understood as a stream, but it's valid (and that's what some technologies like *Comet* and *HTTP long polling* did before Websockets were available). I think in Java you'll need an InputStream over the HTTP response. Have a look at: https://stackoverflow.com/questions/36379835/getting-inputstream-with-resttemplate . – jjmontes Nov 28 '19 at 12:57
  • @jjmontes thanks I'll have a look at that. Just one more doubt, after firing `http://localhost:2375/containers/containerId/stats?stream=1` URL on the browser I get a continuous response, what technology does the browser use in this case ? – Mithilesh Tipkari Nov 29 '19 at 05:03
  • Browsers actively try to show as much of the response as they can even if it isn't finished. They keep following the stream and updating the page. This way you can start reading a TXT file that hasn't been fully loaded yet, or partially see images (btw that's what interlaced images are for!). This was pretty necessary the old days. For complex HTML pages, they sometimes have to wait until some necessary content arrives, but they still try hard to show as much as they have. Downloads are partially written to disk. As you can see, they handle each case in a slight different way. – jjmontes Nov 29 '19 at 22:52

0 Answers0