Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request weaveworks#2148 from kinvolk/iaguis/refactor-probe…
Browse files Browse the repository at this point in the history
…main

probe: refactor probeMain
  • Loading branch information
Alfonso Acosta authored Jan 23, 2017
2 parents fe81ef9 + f0d9876 commit 036dd07
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions prog/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params)
handleResponse(resp, err)
checkpoint.CheckInterval(&params, 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(&params)
handleResponse(resp, err)
checkpoint.CheckInterval(&params, versionCheckPeriod, handleResponse)
}

// Main runs the probe
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 036dd07

Please sign in to comment.