diff options
author | Matthew Holt <[email protected]> | 2018-05-09 17:20:11 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2018-05-09 17:20:38 -0600 |
commit | 86fd2f22fb0752a5c977300f224ef93e1d9cf5b1 (patch) | |
tree | cdb0f126c65f50277847fe1120239ddb62be6034 | |
parent | 148a6f443075e4a527ee0926279ee52f657e616b (diff) | |
download | caddy-86fd2f22fb0752a5c977300f224ef93e1d9cf5b1.tar.gz caddy-86fd2f22fb0752a5c977300f224ef93e1d9cf5b1.zip |
telemetry: Add in_container metric
Knowing whether Caddy is running in a container is super-useful for
debugging and troubleshooting, as well as for making development-time
decisions, because Docker is one of the top contributors to our
user support burden.
Thanks to Eldin for helping to test it.
-rw-r--r-- | caddy/caddymain/run.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/caddy/caddymain/run.go b/caddy/caddymain/run.go index 96eb1a1e9..e684c6fe3 100644 --- a/caddy/caddymain/run.go +++ b/caddy/caddymain/run.go @@ -15,6 +15,7 @@ package caddymain import ( + "bufio" "errors" "flag" "fmt" @@ -170,6 +171,9 @@ func Run() { NumLogical: runtime.NumCPU(), AESNI: cpuid.CPU.AesNi(), }) + if containerized := detectContainer(); containerized { + telemetry.Set("in_container", containerized) + } telemetry.StartEmitting() // Twiddle your thumbs @@ -295,6 +299,46 @@ func setCPU(cpu string) error { return nil } +// detectContainer attemps to determine whether the process is +// being run inside a container. References: +// https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/ +// https://stackoverflow.com/a/20012536/1048862 +// https://gist.github.com/anantkamath/623ce7f5432680749e087cf8cfba9b69 +func detectContainer() bool { + if runtime.GOOS != "linux" { + return false + } + + file, err := os.Open("/proc/1/cgroup") + if err != nil { + return false + } + defer file.Close() + + i := 0 + scanner := bufio.NewScanner(file) + for scanner.Scan() { + i++ + if i > 1000 { + return false + } + + line := scanner.Text() + parts := strings.SplitN(line, ":", 3) + if len(parts) < 3 { + continue + } + + if strings.Contains(parts[2], "docker") || + strings.Contains(parts[2], "lxc") || + strings.Contains(parts[2], "moby") { + return true + } + } + + return false +} + // initTelemetry initializes the telemetry engine. func initTelemetry() error { uuidFilename := filepath.Join(caddy.AssetsPath(), "uuid") |