From 8f6b9895acb6cd1567149ded1f4cb898171f84ab Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 26 May 2015 16:49:47 +0200 Subject: [PATCH 1/3] Fix errcheck errors in probe --- probe/main.go | 2 +- probe/process_mapper_test.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/probe/main.go b/probe/main.go index f2bf1ffc26..dacc1db436 100644 --- a/probe/main.go +++ b/probe/main.go @@ -53,7 +53,7 @@ func main() { log.Printf("exposing Prometheus endpoint at %s%s", *httpListen, *prometheusEndpoint) http.Handle(*prometheusEndpoint, makePrometheusHandler()) } - go http.ListenAndServe(*httpListen, nil) + go func(err error) { log.Print(err) }(http.ListenAndServe(*httpListen, nil)) } if *spyProcs && os.Getegid() != 0 { diff --git a/probe/process_mapper_test.go b/probe/process_mapper_test.go index 61f9694280..99421a9fc5 100644 --- a/probe/process_mapper_test.go +++ b/probe/process_mapper_test.go @@ -17,7 +17,7 @@ func TestCgroupMapper(t *testing.T) { "/netscape/notify_on_release": "0\n", "/weirdfile": "", }) - defer os.RemoveAll(tmp) + defer removeAll(t, tmp) m := newCgroupMapper(tmp, 1*time.Second) for pid, want := range map[uint]string{ @@ -44,14 +44,20 @@ func setupTmpFS(t *testing.T, fs map[string]string) string { for file, content := range fs { dir := path.Dir(file) if err := os.MkdirAll(filepath.Join(tmp, dir), 0777); err != nil { - os.RemoveAll(tmp) + removeAll(t, tmp) t.Fatalf("MkdirAll: %v", err) } if err := ioutil.WriteFile(filepath.Join(tmp, file), []byte(content), 0655); err != nil { - os.RemoveAll(tmp) + removeAll(t, tmp) t.Fatalf("WriteFile: %v", err) } } return tmp } + +func removeAll(t *testing.T, path string) { + if err := os.RemoveAll(path); err != nil { + t.Error(err) + } +} From 84454d1ba1b46218c82fc82ec7391900a16e5310 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 26 May 2015 16:52:29 +0200 Subject: [PATCH 2/3] Fix errcheck errors in app --- app/api_topology.go | 9 +++++++-- app/main.go | 11 ----------- app/server_helpers.go | 5 ++++- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/api_topology.go b/app/api_topology.go index 7431ff3113..653862de11 100644 --- a/app/api_topology.go +++ b/app/api_topology.go @@ -59,7 +59,10 @@ func makeTopologyHandlers( // Websocket for the full topology. This route overlaps with the next. get.HandleFunc(base+"/ws", func(w http.ResponseWriter, r *http.Request) { - r.ParseForm() + if err := r.ParseForm(); err != nil { + respondWith(w, http.StatusInternalServerError, err.Error()) + return + } loop := websocketLoop if t := r.Form.Get("t"); t != "" { var err error @@ -140,7 +143,9 @@ func handleWebsocket( diff := report.TopoDiff(previousTopo, newTopo) previousTopo = newTopo - conn.SetWriteDeadline(time.Now().Add(websocketTimeout)) + if err := conn.SetWriteDeadline(time.Now().Add(websocketTimeout)); err != nil { + return + } if err := conn.WriteJSON(diff); err != nil { return } diff --git a/app/main.go b/app/main.go index 553cd174a3..4c8bdf0378 100644 --- a/app/main.go +++ b/app/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "log/syslog" "net/http" @@ -28,7 +27,6 @@ func main() { probes = flag.String("probes", strings.Join(defaultProbes, ","), "list of probe endpoints, comma separated") batch = flag.Duration("batch", 1*time.Second, "batch interval") window = flag.Duration("window", 15*time.Second, "window") - pidfile = flag.String("pidfile", "", "write PID file") listen = flag.String("http.address", ":"+strconv.Itoa(xfer.AppPort), "webserver listen address") ) flag.Parse() @@ -57,15 +55,6 @@ func main() { log.SetOutput(f) } - if *pidfile != "" { - err := ioutil.WriteFile(*pidfile, []byte(fmt.Sprint(os.Getpid())), 0644) - if err != nil { - log.Print(err) - return - } - defer os.Remove(*pidfile) - } - log.Printf("app starting, version %s", version) // Collector deals with the probes, and generates merged reports. diff --git a/app/server_helpers.go b/app/server_helpers.go index ceb7540f28..8aa1fe8285 100644 --- a/app/server_helpers.go +++ b/app/server_helpers.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "log" "net/http" ) @@ -9,5 +10,7 @@ func respondWith(w http.ResponseWriter, code int, response interface{}) { w.Header().Set("Content-Type", "application/json") w.Header().Add("Cache-Control", "no-cache") w.WriteHeader(code) - json.NewEncoder(w).Encode(response) + if err := json.NewEncoder(w).Encode(response); err != nil { + log.Print(err) + } } From 2f16729ef12e0a79f40de87b19450df829a25bd9 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 26 May 2015 16:54:23 +0200 Subject: [PATCH 3/3] Fix errcheck errors in experimental --- experimental/example/goapp/app.go | 7 +++++-- experimental/genreport/main.go | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/experimental/example/goapp/app.go b/experimental/example/goapp/app.go index fb30a15e01..a184b45b90 100644 --- a/experimental/example/goapp/app.go +++ b/experimental/example/goapp/app.go @@ -82,9 +82,12 @@ func get(target string) { log.Printf("%s: %v", target, err) return } + defer resp.Body.Close() log.Printf("%s: %s", target, resp.Status) - io.Copy(ioutil.Discard, resp.Body) - resp.Body.Close() + if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil { + log.Printf("%s: %v", target, err) + return + } } func interrupt() error { diff --git a/experimental/genreport/main.go b/experimental/genreport/main.go index 22ea22ba90..51875f6cce 100644 --- a/experimental/genreport/main.go +++ b/experimental/genreport/main.go @@ -3,11 +3,14 @@ package main import ( "encoding/json" "flag" + "log" "os" ) func main() { nodes := flag.Int("nodes", 10, "node count") flag.Parse() - json.NewEncoder(os.Stdout).Encode(DemoReport(*nodes)) + if err := json.NewEncoder(os.Stdout).Encode(DemoReport(*nodes)); err != nil { + log.Print(err) + } }