Commit 86fd2f22 authored by Matthew Holt's avatar Matthew Holt

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.
parent 148a6f44
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
package caddymain package caddymain
import ( import (
"bufio"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
...@@ -170,6 +171,9 @@ func Run() { ...@@ -170,6 +171,9 @@ func Run() {
NumLogical: runtime.NumCPU(), NumLogical: runtime.NumCPU(),
AESNI: cpuid.CPU.AesNi(), AESNI: cpuid.CPU.AesNi(),
}) })
if containerized := detectContainer(); containerized {
telemetry.Set("in_container", containerized)
}
telemetry.StartEmitting() telemetry.StartEmitting()
// Twiddle your thumbs // Twiddle your thumbs
...@@ -295,6 +299,46 @@ func setCPU(cpu string) error { ...@@ -295,6 +299,46 @@ func setCPU(cpu string) error {
return nil 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. // initTelemetry initializes the telemetry engine.
func initTelemetry() error { func initTelemetry() error {
uuidFilename := filepath.Join(caddy.AssetsPath(), "uuid") uuidFilename := filepath.Join(caddy.AssetsPath(), "uuid")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment