knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

dockerstats

{dockerstats} is a small wrapper around docker stats that returns the output of this command as an R data.frame.

Note that this package calls system("docker stats") so you should be able to do this from your R command line. I'll probably refactor that at some point.

Installation

You can install the released version of {dockerstats} from GitHub with:

remotes::install_github("ColinFay/dockerstats")

How to

library(dockerstats)

dockerstats()

By default, dockerstats() returns the stats for running containers.

dockerstats()

You can return stats for all containers (not just running)

dockerstats(all = TRUE)

Or from a subset of containers:

dockerstats("mongo", "proxy")

The extra param is used to add extra information to the recording, which can be usefull is you want to tag the specific recording.

For example, here we mimic a connection on the hexmake container

system("docker run --name hexmake --rm -p 2811:80 colinfay/hexmake", wait = FALSE)
Sys.sleep(5)
system("docker run --name hexmake --rm -p 2811:80 colinfay/hexmake", wait = FALSE)
library(crrri)
chrome <- Chrome$new(
  bin = pagedown::find_chrome(), 
  debug_port = httpuv::randomPort()
)

client <- chrome$connect(callback = function(client) {
  Page <- client$Page
  Page$navigate(url = "http://localhost:2811")
  print({
    dockerstats::dockerstats("hexmake", extra = "Connection via chrome")
  })
})

chrome$close()

dockerstats_recurse

dockerstats_recurse() is a wrapper around dockerstats() that runs every every seconds, calling the callback function everytime a loop is completed.

By default, the callback is print, but you can define your own. For example, this function will run the dockerstats() fun every 2 seconds and save it to a file.

dockerstats_recurse(
  "hexmake",
  callback = function(res){
    print(
      paste("Mem usage: ", res$MemUsage)
    )
    write.table(
      res, 
      "dockerstats.csv", 
      append = TRUE, 
      col.names = FALSE, 
      row.names = FALSE, 
      sep = ","
    )
  }
)

As this is a pretty common use-case, a wrapper for this is implemented:

dockerstats_recurse(
  "hexmake",
  callback = append_csv("dockerstats.csv", print = TRUE)
)

Kill the container once done

system("docker kill hexmake")

Byte conversions

To handle the MemUsage columns, expressed as byte, {dockerstats} provides a series of functions to convert to byte, kib, mib or gib.

The unit is kept in attr("units") of the result

dock_stats <- dockerstats()
dock_stats$MemUsag
# Convert to kib
to_kib(dock_stats$MemUsag)

How columns are transformed

Manipulate columns expressed in bite size

You can call as_fs_byte() from the {fs} package to manipulate the columns which are expressed in bytes.

dock_stats <- dockerstats()

dock_stats$MemUsage <- to_kib(dock_stats$MemUsag)
library(ggplot2)
ggplot(
  dock_stats, 
  aes(
    reorder(Name, MemUsage), 
    MemUsage
  )
) + 
  geom_col() +
  scale_y_continuous(labels = scales::label_bytes(units = "kiB")) + 
  coord_flip() + 
  labs(
    title = "MemUsage of running containers", 
    y = "MemUsage in kiB", 
    x = "Containers"
  )


ColinFay/dockerstats documentation built on May 18, 2020, 11:54 a.m.