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.