Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
docker-stats-on-exit-shim

# Vim swap files
.*.swp
.gitignore
.travis.yml
Dockerfile
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.12 as builder
RUN go get -u github.com/golang/dep/...

WORKDIR $GOPATH/src/github.com/delcypher/docker-stats-on-exit-shim

COPY Gopkg.toml .
COPY main.go .

RUN dep ensure

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /docker-stats-on-exit-shim

FROM alpine:latest

WORKDIR /

COPY --from=builder /docker-stats-on-exit-shim .

ENTRYPOINT ["/docker-stats-on-exit-shim"]

CMD ["sleep", "1"]
4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[[override]]
name = "github.com/opencontainers/runc"
revision = "a2a6e82"
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ real command by waiting for it to exit and then querying the active Cgroup subsy
to gather their statistics. It dumps these statistics to a file as JSON and then exits
with the exit code of the real command.

## Example
## Environment variables
- `STATS_OUTPUT_FILE=/dev/stdout` file path you want your output to be saved to (Default /dev/stdout)
- `STATS_OUTPUT_PREFIX=` any prefix you want to add before stats output (Default blank)
- `STATS_OUTPUT_MINIFIED=false` true|false (Default true)

## Example
Dockerfile
```
COPY --from=hasnat/docker-stats-on-exit-shim /docker-stats-on-exit-shim .
ENTRYPOINT ["/docker-stats-on-exit-shim"]
CMD ["sleep", "1"]
```
Example Run
```bash
$ docker run --rm -ti -v`pwd`:/tmp/:rw ubuntu /tmp/docker-stats-on-exit-shim /tmp/output.json /bin/sleep 1
$ cat output.json
$ docker run --rm -ti hasnat/docker-stats-on-exit-shim /bin/sleep 1
```
Output example
```json
{
"wall_time": 1000765975,
Expand Down Expand Up @@ -53,10 +64,7 @@ $ cat output.json
mkdir -p src/github.com/delcypher
export GOPATH=`pwd`
cd src/github.com/delcypher
git clone git@github.com:delcypher/docker-stats-on-exit-shim.git
cd docker-stats-on-exit-shim
git submodule init && git submodule update
go get .
dep ensure
go build
```

Expand Down
33 changes: 26 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
"os/signal"
"syscall"
"time"
"strconv"
cgroups "github.com/opencontainers/runc/libcontainer/cgroups"
cgroups_fs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
)

func printUsage() {
fmt.Printf("%s <stats_file> <command> [arg...]\n", os.Args[0])
fmt.Println("")
fmt.Printf("%s <command> [arg...]\n", os.Args[0])
fmt.Println("environment variables STATS_OUTPUT_FILE=/dev/stdout STATS_OUTPUT_MINIFIED=false STATS_OUTPUT_PREFIX=")
fmt.Println("Runs <command> and on termination outputs cgroup usage information")
fmt.Println("as JSON to <stats_file>")
}

type Stats struct {
Expand Down Expand Up @@ -62,16 +62,24 @@ func fail(template string, args ...interface{}) {
os.Exit(FailExitCode)
}

func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}

func main() {
exitCode := 0;

if len(os.Args) < 3 {
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}

// Open file for writing stats
f, err := os.Create(os.Args[1])
f, err := os.Create(getEnv("STATS_OUTPUT_FILE", "/dev/stdout"))
if err != nil {
fail("Failed to create stats file %s: %s\n", os.Args[1], err)
}
Expand Down Expand Up @@ -109,7 +117,7 @@ func main() {


// Run the subproccess
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd := exec.Command(os.Args[1], os.Args[2:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -168,10 +176,21 @@ func main() {
}

//fmt.Printf("Stats: %+v", combinedStats)
statsAsBytes, err := json.MarshalIndent(&combinedStats, "", " ")
statsPrefix := getEnv("STATS_OUTPUT_PREFIX", "")
minified := false
minified, _ = strconv.ParseBool(getEnv("STATS_OUTPUT_MINIFIED", "false"))
var statsAsBytes []byte
if minified {
statsAsBytes, err = json.Marshal(&combinedStats)
} else {
statsAsBytes, err = json.MarshalIndent(&combinedStats, "", " ")
}

if err != nil {
fail("Failed to serialize stats to JSON: %s\n", err)
}

_, err = f.Write([]byte(statsPrefix))
_, err = f.Write(statsAsBytes)
if err != nil {
fail("Failed to write stats to file: %s\n", err)
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/opencontainers/runc
Submodule runc deleted from a2a6e8