Skip to content
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 config.auto-reload switch for auto reload when the config file changed #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
148 changes: 104 additions & 44 deletions cmd/process-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/fsnotify/fsnotify"
"github.com/ncabatoff/fakescraper"
common "github.com/ncabatoff/process-exporter"
"github.com/ncabatoff/process-exporter/config"
Expand Down Expand Up @@ -299,6 +300,8 @@ func main() {
"print manual")
configPath = flag.String("config.path", "",
"path to YAML config file")
autoReload = flag.Bool("config.auto-reload", false,
"auto reload when config file changed")
recheck = flag.Bool("recheck", false,
"recheck process names on each scrape")
debug = flag.Bool("debug", false,
Expand All @@ -318,61 +321,68 @@ func main() {
return
}

var matchnamer common.MatchNamer
var pc *NamedProcessCollector

if *configPath != "" {
if *nameMapping != "" || *procNames != "" {
log.Fatalf("-config.path cannot be used with -namemapping or -procnames")
}
RegisterController := func() {
var matchnamer common.MatchNamer

cfg, err := config.ReadFile(*configPath, *debug)
if err != nil {
log.Fatalf("error reading config file %q: %v", *configPath, err)
}
log.Printf("Reading metrics from %s based on %q", *procfsPath, *configPath)
matchnamer = cfg.MatchNamers
if *debug {
log.Printf("using config matchnamer: %v", cfg.MatchNamers)
}
} else {
namemapper, err := parseNameMapper(*nameMapping)
if err != nil {
log.Fatalf("Error parsing -namemapping argument '%s': %v", *nameMapping, err)
}
if *configPath != "" {
if *nameMapping != "" || *procNames != "" {
log.Fatalf("-config.path cannot be used with -namemapping or -procnames")
}

var names []string
for _, s := range strings.Split(*procNames, ",") {
if s != "" {
if _, ok := namemapper.mapping[s]; !ok {
namemapper.mapping[s] = nil
cfg, err := config.ReadFile(*configPath, *debug)
if err != nil {
log.Fatalf("error reading config file %q: %v", *configPath, err)
}
log.Printf("Reading metrics from %s based on %q", *procfsPath, *configPath)
matchnamer = cfg.MatchNamers
if *debug {
log.Printf("using config matchnamer: %v", cfg.MatchNamers)
}
} else {
namemapper, err := parseNameMapper(*nameMapping)
if err != nil {
log.Fatalf("Error parsing -namemapping argument '%s': %v", *nameMapping, err)
}

var names []string
for _, s := range strings.Split(*procNames, ",") {
if s != "" {
if _, ok := namemapper.mapping[s]; !ok {
namemapper.mapping[s] = nil
}
names = append(names, s)
}
names = append(names, s)
}

log.Printf("Reading metrics from %s for procnames: %v", *procfsPath, names)
if *debug {
log.Printf("using cmdline matchnamer: %v", namemapper)
}
matchnamer = namemapper
}

log.Printf("Reading metrics from %s for procnames: %v", *procfsPath, names)
if *debug {
log.Printf("using cmdline matchnamer: %v", namemapper)
var err error
pc, err = NewProcessCollector(
ProcessCollectorOption{
ProcFSPath: *procfsPath,
Children: *children,
Threads: *threads,
GatherSMaps: *smaps,
Namer: matchnamer,
Recheck: *recheck,
Debug: *debug,
},
)
if err != nil {
log.Fatalf("Error initializing: %v", err)
}
matchnamer = namemapper
}

pc, err := NewProcessCollector(
ProcessCollectorOption{
ProcFSPath: *procfsPath,
Children: *children,
Threads: *threads,
GatherSMaps: *smaps,
Namer: matchnamer,
Recheck: *recheck,
Debug: *debug,
},
)
if err != nil {
log.Fatalf("Error initializing: %v", err)
prometheus.MustRegister(pc)
}

prometheus.MustRegister(pc)
RegisterController()

if *onceToStdoutDelay != 0 {
// We throw away the first result because that first collection primes the pump, and
Expand All @@ -385,6 +395,56 @@ func main() {
return
}

if *autoReload {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Start watcher failed, %v", err)
}
defer watcher.Close()

err = watcher.Add(*configPath)
if err != nil {
log.Fatalf("Add watcher for %s failed, %v", *configPath, err)
}

log.Printf("Starting file system watcher for %s", *configPath)

go func() {
for {
select {
case e := <-watcher.Events:
if e.Op&(fsnotify.Write|fsnotify.Remove|fsnotify.Rename) != 0 {
err = watcher.Remove(*configPath)
if err != nil {
log.Fatalf("Remove old watcher for %s failed, %v", *configPath, err)
}

for {
if stat, err := os.Stat(*configPath); (err != nil && !os.IsExist(err)) || stat.Size() == 0 {
log.Printf("Config file removed or renamed, waiting")
time.Sleep(5 * time.Second)
} else {
log.Printf("Config file changed, reloading")

err = watcher.Add(*configPath)
if err != nil {
log.Fatalf("Add watcher for %s failed, %v", *configPath, err)
}

prometheus.Unregister(pc)
RegisterController()

break
}
}
}
case err := <-watcher.Errors:
log.Printf("Config watcher failed, %v", err)
}
}
}()
}

http.Handle(*metricsPath, prometheus.Handler())

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/golang/protobuf v1.1.0 // indirect
github.com/google/go-cmp v0.3.1
github.com/kr/pretty v0.1.0 // indirect
Expand Down
13 changes: 3 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand All @@ -21,20 +20,14 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e h1:n/3MEhJQjQxrOUCzh1Y3Re6aJUUWRp2M9+Oc3eVn/54=
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.12-0.20200505152635-9654394ca94a h1:zTuAihdFbve/GW/PAf5sLu2vEO5kRYKLxasyYn53o8c=
github.com/prometheus/procfs v0.0.12-0.20200505152635-9654394ca94a/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.0.12-0.20200513160535-c6ff04bafc38 h1:KXSM0/EjZ5wAEa5nUl/1Ec5cG7fB/SbgSELkCevjsF8=
github.com/prometheus/procfs v0.0.12-0.20200513160535-c6ff04bafc38/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.1.3 h1:F0+tqvhOksq22sc6iCHF5WGlWjdwj92p0udFh1VFBS8=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 h1:B6caxRw+hozq68X2MY7jEpZh/cr4/aHLv9xU8Kkadrw=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
12 changes: 12 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.