From ffa673f7d813d7c8f4f1677aca9859340d4d4629 Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Mon, 26 Feb 2018 08:58:10 +0100 Subject: [PATCH] General simplifications (#3887) Another try as in #1516 --- cmd/prometheus/main.go | 16 ++++------------ discovery/kubernetes/node_test.go | 1 - discovery/manager.go | 10 ++++------ discovery/manager_test.go | 4 ++-- .../remote_storage_adapter/graphite/client.go | 6 +----- promql/parse.go | 1 - rules/manager.go | 4 ++-- scrape/manager.go | 4 +--- storage/remote/queue_manager.go | 3 +-- storage/remote/storage.go | 3 +-- util/httputil/client_test.go | 4 ++-- util/stats/stats_test.go | 3 +-- util/testutil/error.go | 6 +----- web/web.go | 5 +---- 14 files changed, 21 insertions(+), 49 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 7a94b2623..faa27b20c 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -420,10 +420,7 @@ func main() { // it needs to read a valid config for each job. // It depends on the config being in sync with the discovery manager so // we wait until the config is fully loaded. - select { - case <-reloadReady.C: - break - } + <-reloadReady.C err := scrapeManager.Run(discoveryManagerScrape.SyncCh()) level.Info(logger).Log("msg", "Scrape manager stopped") @@ -445,10 +442,7 @@ func main() { cancel := make(chan struct{}) g.Add( func() error { - select { - case <-reloadReady.C: - break - } + <-reloadReady.C for { select { @@ -573,10 +567,8 @@ func main() { // it needs to read a valid config for each job. // It depends on the config being in sync with the discovery manager // so we wait until the config is fully loaded. - select { - case <-reloadReady.C: - break - } + <-reloadReady.C + notifier.Run(discoveryManagerNotify.SyncCh()) level.Info(logger).Log("msg", "Notifier manager stopped") return nil diff --git a/discovery/kubernetes/node_test.go b/discovery/kubernetes/node_test.go index 551ee60a0..3266841b7 100644 --- a/discovery/kubernetes/node_test.go +++ b/discovery/kubernetes/node_test.go @@ -65,7 +65,6 @@ func (i *fakeInformer) GetController() cache.Controller { } func (i *fakeInformer) Run(stopCh <-chan struct{}) { - return } func (i *fakeInformer) HasSynced() bool { diff --git a/discovery/manager.go b/discovery/manager.go index 0b6716048..5b5753f6f 100644 --- a/discovery/manager.go +++ b/discovery/manager.go @@ -90,13 +90,11 @@ type Manager struct { // Run starts the background processing func (m *Manager) Run() error { - for { - select { - case <-m.ctx.Done(): - m.cancelDiscoverers() - return m.ctx.Err() - } + for range m.ctx.Done() { + m.cancelDiscoverers() + return m.ctx.Err() } + return nil } // SyncCh returns a read only channel used by all Discoverers to send target updates. diff --git a/discovery/manager_test.go b/discovery/manager_test.go index cbfd651b3..2016fd7aa 100644 --- a/discovery/manager_test.go +++ b/discovery/manager_test.go @@ -750,7 +750,7 @@ scrape_configs: } discoveryManager.ApplyConfig(c) - _ = <-discoveryManager.SyncCh() + <-discoveryManager.SyncCh() verifyPresence(discoveryManager.targets, poolKey{setName: "prometheus", provider: "static/0"}, "{__address__=\"foo:9090\"}", true) verifyPresence(discoveryManager.targets, poolKey{setName: "prometheus", provider: "static/0"}, "{__address__=\"bar:9090\"}", true) @@ -769,7 +769,7 @@ scrape_configs: } discoveryManager.ApplyConfig(c) - _ = <-discoveryManager.SyncCh() + <-discoveryManager.SyncCh() verifyPresence(discoveryManager.targets, poolKey{setName: "prometheus", provider: "static/0"}, "{__address__=\"foo:9090\"}", true) verifyPresence(discoveryManager.targets, poolKey{setName: "prometheus", provider: "static/0"}, "{__address__=\"bar:9090\"}", false) } diff --git a/documentation/examples/remote_storage/remote_storage_adapter/graphite/client.go b/documentation/examples/remote_storage/remote_storage_adapter/graphite/client.go index edd4546ee..973572913 100644 --- a/documentation/examples/remote_storage/remote_storage_adapter/graphite/client.go +++ b/documentation/examples/remote_storage/remote_storage_adapter/graphite/client.go @@ -100,11 +100,7 @@ func (c *Client) Write(samples model.Samples) error { } _, err = conn.Write(buf.Bytes()) - if err != nil { - return err - } - - return nil + return err } // Name identifies the client as a Graphite client. diff --git a/promql/parse.go b/promql/parse.go index 7008b148a..b2d5cf963 100644 --- a/promql/parse.go +++ b/promql/parse.go @@ -348,7 +348,6 @@ func (p *parser) recover(errp *error) { *errp = e.(error) } } - return } // stmt parses any statement. diff --git a/rules/manager.go b/rules/manager.go index 211d6580d..b2a8a51c8 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -286,12 +286,12 @@ func (g *Group) copyState(from *Group) { ruleMap := make(map[string][]int, len(from.rules)) for fi, fromRule := range from.rules { - l, _ := ruleMap[fromRule.Name()] + l := ruleMap[fromRule.Name()] ruleMap[fromRule.Name()] = append(l, fi) } for i, rule := range g.rules { - indexes, _ := ruleMap[rule.Name()] + indexes := ruleMap[rule.Name()] if len(indexes) == 0 { continue } diff --git a/scrape/manager.go b/scrape/manager.go index af27ccf64..64fc100b0 100644 --- a/scrape/manager.go +++ b/scrape/manager.go @@ -139,9 +139,7 @@ func (m *Manager) DroppedTargets() []*Target { var droppedTargets []*Target for _, p := range m.scrapePools { p.mtx.RLock() - for _, tt := range p.droppedTargets { - droppedTargets = append(droppedTargets, tt) - } + droppedTargets = append(droppedTargets, p.droppedTargets...) p.mtx.RUnlock() } return droppedTargets diff --git a/storage/remote/queue_manager.go b/storage/remote/queue_manager.go index 9c7f66a94..54495ca0d 100644 --- a/storage/remote/queue_manager.go +++ b/storage/remote/queue_manager.go @@ -196,8 +196,7 @@ func NewQueueManager(logger log.Logger, cfg config.QueueConfig, externalLabels m // sample on the floor if the queue is full. // Always returns nil. func (t *QueueManager) Append(s *model.Sample) error { - var snew model.Sample - snew = *s + snew := *s snew.Metric = s.Metric.Clone() for ln, lv := range t.externalLabels { diff --git a/storage/remote/storage.go b/storage/remote/storage.go index c4cfd7234..5192126ea 100644 --- a/storage/remote/storage.go +++ b/storage/remote/storage.go @@ -99,8 +99,7 @@ func (s *Storage) ApplyConfig(conf *config.Config) error { return err } - var q storage.Queryable - q = QueryableClient(c) + q := QueryableClient(c) q = ExternablLabelsHandler(q, conf.GlobalConfig.ExternalLabels) if len(rrConf.RequiredMatchers) > 0 { q = RequiredMatchersFilter(q, labelsToEqualityMatchers(rrConf.RequiredMatchers)) diff --git a/util/httputil/client_test.go b/util/httputil/client_test.go index 5c714f560..7d1a67252 100644 --- a/util/httputil/client_test.go +++ b/util/httputil/client_test.go @@ -157,7 +157,7 @@ func TestNewClientFromConfig(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() - if ok == false { + if !ok { fmt.Fprintf(w, "The Authorization header wasn't set") } else if ExpectedUsername != username { fmt.Fprintf(w, "The expected username (%s) differs from the obtained username (%s).", ExpectedUsername, username) @@ -336,7 +336,7 @@ func TestBasicAuthRoundTripper(t *testing.T) { fakeRoundTripper := testutil.NewRoundTripCheckRequest(func(req *http.Request) { username, password, ok := req.BasicAuth() - if ok == false { + if !ok { t.Errorf("The Authorization header wasn't set") } if ExpectedUsername != username { diff --git a/util/stats/stats_test.go b/util/stats/stats_test.go index 3f82885ce..f5175a027 100644 --- a/util/stats/stats_test.go +++ b/util/stats/stats_test.go @@ -45,8 +45,7 @@ func TestQueryStatsWithTimers(t *testing.T) { time.Sleep(2 * time.Millisecond) timer.Stop() - var qs *QueryStats - qs = NewQueryStats(tg) + qs := NewQueryStats(tg) actual, err := json.Marshal(qs) if err != nil { t.Fatalf("Unexpected error during serialization: %v", err) diff --git a/util/testutil/error.go b/util/testutil/error.go index a132abf8f..38fa71adb 100644 --- a/util/testutil/error.go +++ b/util/testutil/error.go @@ -20,11 +20,7 @@ func ErrorEqual(left, right error) bool { } if left != nil && right != nil { - if left.Error() == right.Error() { - return true - } - - return false + return left.Error() == right.Error() } return false diff --git a/web/web.go b/web/web.go index 63324435f..f1d1095b7 100644 --- a/web/web.go +++ b/web/web.go @@ -346,10 +346,7 @@ func (h *Handler) Ready() { // Verifies whether the server is ready or not. func (h *Handler) isReady() bool { ready := atomic.LoadUint32(&h.ready) - if ready == 0 { - return false - } - return true + return ready > 0 } // Checks if server is ready, calls f if it is, returns 503 if it is not.