-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add a monitor that monitors the CPU usage of a container #1
Comments
Stil doesn't connect properly to the docker socket directly, as we have seen a few weeks ago |
After some debugging, I managed to have a code that works. If you put the following code the the main method of the cpu monitor, you get the statistics: id := "db"
endpoint := "unix:///var/run/docker.sock"
client, err := docker.NewClient(endpoint)
if err != nil {
log.Fatal(err)
}
errC := make(chan error, 1)
statsC := make(chan *docker.Stats)
done := make(chan bool)
go func() {
errC <- client.Stats(docker.StatsOptions{id, statsC, true, done, 0})
close(errC)
}()
for {
stats, ok := <-statsC
if !ok {
break
}
//done <- true
fmt.Println(stats.CPUStats.CPUUsage.TotalUsage)
}
err = <-errC
if err != nil {
fmt.Println(err)
} The main difference with respect to you code is that I set 0 as timeout. During the tests I noticed that you get on the socket if you set a timeout different with 0. We don't need to change it, the default behaviour is fine for us. You need to share the docker socket with the container, to make it work: docker run -v /var/run/docker.sock:/var/run/docker.sock -t --rm <IMAGE_NAME> With the approach provided before, also the done channel seems to work (ref to: benchflow/collectors#23). @Cerfoglg let me know if you manage to solve this issue. |
@VincenzoFerme For me it didn't solve the issue of the done channel by just setting the timeout to 0. However, I was only able to test with TSLClient instead of the direct socket connection (having a few issues with using a VM and docker), so it is possible that this only works with the direct socket connection. Either way, if it works like that for you, my code should work as well. |
@VincenzoFerme I was able to get it to work. Turns out the Stats function will return an error when signalling on the Done channel, but the function does indeed stop. Docker socket works as well by sharing the socket with the container upon running it. I did comment on the repo of the dockerclient API about the Stats function giving an error when stopping it via the Done channel. I believe it would be best if it didn't do that, but rather exited more gracefully. |
The required functionalities are:
Notes about CPU usage:
Some useful references:
The text was updated successfully, but these errors were encountered: