From f0d9876258b6663295d577e46f3152ea647762ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iago=20L=C3=B3pez=20Galeiras?= Date: Mon, 23 Jan 2017 12:10:58 +0100 Subject: [PATCH] probe: refactor probeMain --- prog/probe.go | 72 ++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/prog/probe.go b/prog/probe.go index 32d835b982..b9a0ca47cb 100644 --- a/prog/probe.go +++ b/prog/probe.go @@ -46,25 +46,46 @@ var ( dockerEndpoint = "unix:///var/run/docker.sock" ) -func check(flags map[string]string) { - handleResponse := func(r *checkpoint.CheckResponse, err error) { - if err != nil { - log.Errorf("Error checking version: %v", err) - } else if r.Outdated { - log.Infof("Scope version %s is available; please update at %s", - r.CurrentVersion, r.CurrentDownloadURL) - } +func checkNewScopeVersion(flags probeFlags) { + checkpointFlags := map[string]string{} + if flags.kubernetesEnabled { + checkpointFlags["kubernetes_enabled"] = "true" } + if flags.ecsEnabled { + checkpointFlags["ecs_enabled"] = "true" + } + + go func() { + handleResponse := func(r *checkpoint.CheckResponse, err error) { + if err != nil { + log.Errorf("Error checking version: %v", err) + } else if r.Outdated { + log.Infof("Scope version %s is available; please update at %s", + r.CurrentVersion, r.CurrentDownloadURL) + } + } + + // Start background version checking + params := checkpoint.CheckParams{ + Product: "scope-probe", + Version: version, + Flags: checkpointFlags, + } + resp, err := checkpoint.Check(¶ms) + handleResponse(resp, err) + checkpoint.CheckInterval(¶ms, versionCheckPeriod, handleResponse) + }() +} - // Start background version checking - params := checkpoint.CheckParams{ - Product: "scope-probe", - Version: version, - Flags: flags, +func maybeExportProfileData(flags probeFlags) { + if flags.httpListen != "" { + go func() { + http.Handle("/metrics", prometheus.Handler()) + log.Infof("Profiling data being exported to %s", flags.httpListen) + log.Infof("go tool proof http://%s/debug/pprof/{profile,heap,block}", flags.httpListen) + log.Infof("Profiling endpoint %s terminated: %v", flags.httpListen, http.ListenAndServe(flags.httpListen, nil)) + }() } - resp, err := checkpoint.Check(¶ms) - handleResponse(resp, err) - checkpoint.CheckInterval(¶ms, versionCheckPeriod, handleResponse) } // Main runs the probe @@ -91,15 +112,7 @@ func probeMain(flags probeFlags, targets []appclient.Target) { hostID = hostName // TODO(pb): we should sanitize the hostname ) log.Infof("probe starting, version %s, ID %s", version, probeID) - - checkpointFlags := makeBaseCheckpointFlags() - if flags.kubernetesEnabled { - checkpointFlags["kubernetes_enabled"] = "true" - } - if flags.ecsEnabled { - checkpointFlags["ecs_enabled"] = "true" - } - go check(checkpointFlags) + checkNewScopeVersion(flags) handlerRegistry := controls.NewDefaultHandlerRegistry() clientFactory := func(hostname string, url url.URL) (appclient.AppClient, error) { @@ -263,14 +276,7 @@ func probeMain(flags probeFlags, targets []appclient.Target) { p.AddReporter(pluginRegistry) } - if flags.httpListen != "" { - go func() { - http.Handle("/metrics", prometheus.Handler()) - log.Infof("Profiling data being exported to %s", flags.httpListen) - log.Infof("go tool proof http://%s/debug/pprof/{profile,heap,block}", flags.httpListen) - log.Infof("Profiling endpoint %s terminated: %v", flags.httpListen, http.ListenAndServe(flags.httpListen, nil)) - }() - } + maybeExportProfileData(flags) p.Start() defer p.Stop()