Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellix committed Mar 11, 2019
1 parent 84c340f commit db0bd6a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
53 changes: 41 additions & 12 deletions couchdb-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,10 @@ func init() {
}

func main() {
flag.Parse()
// Convinces goflags that we have called Parse() to avoid noisy logs.
// Necessary due to https://github.com/golang/glog/commit/65d674618f712aa808a7d0104131b9206fc3d5ad
// and us using another flags package.
goflag.CommandLine.Parse([]string{})
goflag.Lookup("logtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.ToStderr))
goflag.Lookup("alsologtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.AlsoToStderr))
goflag.Lookup("v").Value.Set(glogadapt.Logging.Verbosity.String())
goflag.Lookup("stderrthreshold").Value.Set(glogadapt.Logging.StderrThreshold.String())
goflag.Lookup("log_dir").Value.Set(glogadapt.Logging.LogDir)
err := initFlags()
if err != nil {
glog.Fatal(err)
}

var databases []string
if *&exporterConfig.databases != "" {
Expand All @@ -76,15 +70,50 @@ func main() {

http.Handle(*&exporterConfig.metricsEndpoint, promhttp.Handler())
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "OK")
_, err = fmt.Fprint(w, "OK")
if err != nil {
glog.Error(err)
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Please GET %s", *&exporterConfig.metricsEndpoint), http.StatusNotFound)
})

glog.Infof("Starting exporter at '%s' to read from CouchDB at '%s'", *&exporterConfig.listenAddress, *&exporterConfig.couchdbURI)
err := http.ListenAndServe(*&exporterConfig.listenAddress, nil)
err = http.ListenAndServe(*&exporterConfig.listenAddress, nil)
if err != nil {
glog.Fatal(err)
}
}

func initFlags() error {
flag.Parse()
// Convinces goflags that we have called Parse() to avoid noisy logs.
// Necessary due to https://github.com/golang/glog/commit/65d674618f712aa808a7d0104131b9206fc3d5ad
// and us using another flags package.
err := goflag.CommandLine.Parse([]string{})
if err != nil {
return err
}
err = goflag.Lookup("logtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.ToStderr))
if err != nil {
return err
}
err = goflag.Lookup("alsologtostderr").Value.Set(strconv.FormatBool(*&glogadapt.Logging.AlsoToStderr))
if err != nil {
return err
}
err = goflag.Lookup("v").Value.Set(glogadapt.Logging.Verbosity.String())
if err != nil {
return err
}
err = goflag.Lookup("stderrthreshold").Value.Set(glogadapt.Logging.StderrThreshold.String())
if err != nil {
return err
}
err = goflag.Lookup("log_dir").Value.Set(glogadapt.Logging.LogDir)
if err != nil {
return err
}
return nil
}
11 changes: 10 additions & 1 deletion lib/couchdb-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,24 @@ func (c *CouchdbClient) Request(method string, uri string, body io.Reader) (resp
if err != nil {
return nil, err
}
if resp != nil {
defer func() {
if cerr := resp.Body.Close(); cerr != nil {
err = cerr
}
}()
}

respData, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
if err != nil {
respData = []byte(err.Error())
}
return nil, fmt.Errorf("status %s (%d): %s", resp.Status, resp.StatusCode, respData)
}
if err != nil {
return nil, err
}

return respData, nil
}
Expand Down

0 comments on commit db0bd6a

Please sign in to comment.