From 011d15a626fdbe817f826b6f2916b5310de16c2b Mon Sep 17 00:00:00 2001 From: Ilya Mashchenko Date: Sun, 19 May 2024 21:43:42 +0300 Subject: [PATCH] go.d fix some JB code inspection issues (#17702) --- src/collectors/perf.plugin/metadata.yaml | 2 +- .../go.d.plugin/agent/confgroup/cache.go | 2 +- .../go.d.plugin/examples/simple/main.go | 14 ++++---- .../go.d.plugin/modules/dnsquery/init.go | 2 +- .../go.d.plugin/modules/haproxy/init.go | 4 +-- .../go.d.plugin/modules/k8s_state/init.go | 2 +- .../go.d.plugin/modules/k8s_state/state.go | 2 +- .../modules/openvpn/client/client_test.go | 5 +++ .../go.d.plugin/modules/phpdaemon/client.go | 2 +- .../go.d.plugin/modules/pika/pika_test.go | 2 +- .../go.d.plugin/modules/portcheck/init.go | 2 +- .../modules/scaleio/client/client.go | 12 +++---- .../modules/supervisord/supervisord_test.go | 2 +- .../go.d.plugin/modules/traefik/collect.go | 2 +- .../go.d.plugin/modules/traefik/init.go | 4 +-- .../go.d.plugin/modules/traefik/traefik.go | 2 +- .../modules/vsphere/match/match.go | 1 + .../modules/vsphere/resources/resources.go | 2 +- .../modules/vsphere/scrape/scrape.go | 32 +++++++++---------- .../go.d.plugin/modules/x509check/collect.go | 8 +++-- .../collectors/go.d.plugin/pkg/logs/reader.go | 6 ++-- .../pkg/matcher/simple_patterns.go | 2 +- .../go.d.plugin/pkg/matcher/string.go | 14 ++++---- 23 files changed, 68 insertions(+), 58 deletions(-) diff --git a/src/collectors/perf.plugin/metadata.yaml b/src/collectors/perf.plugin/metadata.yaml index db95c0c97a4efb..1c3fa3e8d964a6 100644 --- a/src/collectors/perf.plugin/metadata.yaml +++ b/src/collectors/perf.plugin/metadata.yaml @@ -30,7 +30,7 @@ modules: exclude: [] multi_instance: true additional_permissions: - description: "It needs setuid to use the necessary syscall to collect perf events. Netdada sets the permission during installation time." + description: "It needs setuid to use the necessary syscall to collect perf events. Netdata sets the permission during installation time." default_behavior: auto_detection: description: "" diff --git a/src/go/collectors/go.d.plugin/agent/confgroup/cache.go b/src/go/collectors/go.d.plugin/agent/confgroup/cache.go index 40c8071d5c12be..8b369e6530cc51 100644 --- a/src/go/collectors/go.d.plugin/agent/confgroup/cache.go +++ b/src/go/collectors/go.d.plugin/agent/confgroup/cache.go @@ -85,7 +85,7 @@ func (c *Cache) addNotEmpty(group *Group) (added, removed []Config) { } } - if ok && len(set) == 0 { + if len(set) == 0 { delete(c.sources, group.Source) } diff --git a/src/go/collectors/go.d.plugin/examples/simple/main.go b/src/go/collectors/go.d.plugin/examples/simple/main.go index a271eb093c4926..4fa93d69014f07 100644 --- a/src/go/collectors/go.d.plugin/examples/simple/main.go +++ b/src/go/collectors/go.d.plugin/examples/simple/main.go @@ -21,15 +21,17 @@ import ( var version = "v0.0.1-example" -type example struct{ module.Base } +type example struct { + module.Base +} -func (example) Cleanup() {} +func (e *example) Cleanup() {} -func (example) Init() error { return nil } +func (e *example) Init() error { return nil } -func (example) Check() error { return nil } +func (e *example) Check() error { return nil } -func (example) Charts() *module.Charts { +func (e *example) Charts() *module.Charts { return &module.Charts{ { ID: "random", @@ -41,7 +43,7 @@ func (example) Charts() *module.Charts { }, } } -func (example) Configuration() any { return nil } +func (e *example) Configuration() any { return nil } func (e *example) Collect() map[string]int64 { return map[string]int64{ diff --git a/src/go/collectors/go.d.plugin/modules/dnsquery/init.go b/src/go/collectors/go.d.plugin/modules/dnsquery/init.go index 05dad4ac1bd057..65af0ea2e6e831 100644 --- a/src/go/collectors/go.d.plugin/modules/dnsquery/init.go +++ b/src/go/collectors/go.d.plugin/modules/dnsquery/init.go @@ -50,7 +50,7 @@ func (d *DNSQuery) initRecordTypes() (map[string]uint16, error) { } func (d *DNSQuery) initCharts() (*module.Charts, error) { - var charts module.Charts + charts := module.Charts{} for _, srv := range d.Servers { for _, rtype := range d.RecordTypes { diff --git a/src/go/collectors/go.d.plugin/modules/haproxy/init.go b/src/go/collectors/go.d.plugin/modules/haproxy/init.go index 2163b9624d69e0..55c669000ad2df 100644 --- a/src/go/collectors/go.d.plugin/modules/haproxy/init.go +++ b/src/go/collectors/go.d.plugin/modules/haproxy/init.go @@ -10,7 +10,7 @@ import ( "github.com/netdata/netdata/go/go.d.plugin/pkg/web" ) -func (h Haproxy) validateConfig() error { +func (h *Haproxy) validateConfig() error { if h.URL == "" { return errors.New("'url' is not set") } @@ -20,7 +20,7 @@ func (h Haproxy) validateConfig() error { return nil } -func (h Haproxy) initPrometheusClient() (prometheus.Prometheus, error) { +func (h *Haproxy) initPrometheusClient() (prometheus.Prometheus, error) { httpClient, err := web.NewHTTPClient(h.Client) if err != nil { return nil, err diff --git a/src/go/collectors/go.d.plugin/modules/k8s_state/init.go b/src/go/collectors/go.d.plugin/modules/k8s_state/init.go index 2892da1c60a299..99813139468dae 100644 --- a/src/go/collectors/go.d.plugin/modules/k8s_state/init.go +++ b/src/go/collectors/go.d.plugin/modules/k8s_state/init.go @@ -6,7 +6,7 @@ import ( "k8s.io/client-go/kubernetes" ) -func (ks KubeState) initClient() (kubernetes.Interface, error) { +func (ks *KubeState) initClient() (kubernetes.Interface, error) { return ks.newKubeClient() } diff --git a/src/go/collectors/go.d.plugin/modules/k8s_state/state.go b/src/go/collectors/go.d.plugin/modules/k8s_state/state.go index 1d39df10e9fdf3..72bac88ee342c5 100644 --- a/src/go/collectors/go.d.plugin/modules/k8s_state/state.go +++ b/src/go/collectors/go.d.plugin/modules/k8s_state/state.go @@ -101,7 +101,7 @@ type ( } ) -func (ns nodeState) id() string { return ns.name } +func (ns *nodeState) id() string { return ns.name } func (ns *nodeState) resetStats() { ns.stats = nodeStateStats{} } type ( diff --git a/src/go/collectors/go.d.plugin/modules/openvpn/client/client_test.go b/src/go/collectors/go.d.plugin/modules/openvpn/client/client_test.go index c10673ed54bc32..a21672e0b36349 100644 --- a/src/go/collectors/go.d.plugin/modules/openvpn/client/client_test.go +++ b/src/go/collectors/go.d.plugin/modules/openvpn/client/client_test.go @@ -75,6 +75,7 @@ func (m *mockSocketClient) Disconnect() error { return nil } func (m *mockSocketClient) Command(command string, process socket.Processor) error { var s *bufio.Scanner + switch command { default: return fmt.Errorf("unknown command : %s", command) @@ -91,6 +92,10 @@ func (m *mockSocketClient) Command(command string, process socket.Processor) err s = bufio.NewScanner(bytes.NewReader(testLoadStatsData)) } + if s == nil { + return nil + } + for s.Scan() { process(s.Bytes()) } diff --git a/src/go/collectors/go.d.plugin/modules/phpdaemon/client.go b/src/go/collectors/go.d.plugin/modules/phpdaemon/client.go index 7a8c5969f36d13..e860ec408bc4a6 100644 --- a/src/go/collectors/go.d.plugin/modules/phpdaemon/client.go +++ b/src/go/collectors/go.d.plugin/modules/phpdaemon/client.go @@ -56,7 +56,7 @@ func (c *client) doWithDecode(dst interface{}, decode decodeFunc, request web.Re return nil } -func (c client) doOK(req *http.Request) (*http.Response, error) { +func (c *client) doOK(req *http.Request) (*http.Response, error) { resp, err := c.httpClient.Do(req) if err != nil { return resp, fmt.Errorf("error on request : %v", err) diff --git a/src/go/collectors/go.d.plugin/modules/pika/pika_test.go b/src/go/collectors/go.d.plugin/modules/pika/pika_test.go index ba28cbd8d9ae69..5a4e460d78ba32 100644 --- a/src/go/collectors/go.d.plugin/modules/pika/pika_test.go +++ b/src/go/collectors/go.d.plugin/modules/pika/pika_test.go @@ -284,7 +284,7 @@ type mockRedisClient struct { calledClose bool } -func (m mockRedisClient) Info(_ context.Context, _ ...string) (cmd *redis.StringCmd) { +func (m *mockRedisClient) Info(_ context.Context, _ ...string) (cmd *redis.StringCmd) { if m.errOnInfo { cmd = redis.NewStringResult("", errors.New("error on Info")) } else { diff --git a/src/go/collectors/go.d.plugin/modules/portcheck/init.go b/src/go/collectors/go.d.plugin/modules/portcheck/init.go index 0d1649690572ed..29c0e43a03af95 100644 --- a/src/go/collectors/go.d.plugin/modules/portcheck/init.go +++ b/src/go/collectors/go.d.plugin/modules/portcheck/init.go @@ -30,7 +30,7 @@ func (pc *PortCheck) validateConfig() error { } func (pc *PortCheck) initCharts() (*module.Charts, error) { - var charts module.Charts + charts := module.Charts{} for _, port := range pc.Ports { if err := charts.Add(*newPortCharts(pc.Host, port)...); err != nil { diff --git a/src/go/collectors/go.d.plugin/modules/scaleio/client/client.go b/src/go/collectors/go.d.plugin/modules/scaleio/client/client.go index c75f79aa7a9dd9..e60dfbf68110ba 100644 --- a/src/go/collectors/go.d.plugin/modules/scaleio/client/client.go +++ b/src/go/collectors/go.d.plugin/modules/scaleio/client/client.go @@ -94,7 +94,7 @@ type Client struct { } // LoggedIn reports whether the client is logged in. -func (c Client) LoggedIn() bool { +func (c *Client) LoggedIn() bool { return c.token.isSet() } @@ -160,7 +160,7 @@ func (c *Client) Instances() (Instances, error) { return instances, err } -func (c Client) createLoginRequest() web.Request { +func (c *Client) createLoginRequest() web.Request { req := c.Request.Copy() u, _ := url.Parse(req.URL) u.Path = path.Join(u.Path, "/api/login") @@ -168,7 +168,7 @@ func (c Client) createLoginRequest() web.Request { return req } -func (c Client) createLogoutRequest() web.Request { +func (c *Client) createLogoutRequest() web.Request { req := c.Request.Copy() u, _ := url.Parse(req.URL) u.Path = path.Join(u.Path, "/api/logout") @@ -177,7 +177,7 @@ func (c Client) createLogoutRequest() web.Request { return req } -func (c Client) createAPIVersionRequest() web.Request { +func (c *Client) createAPIVersionRequest() web.Request { req := c.Request.Copy() u, _ := url.Parse(req.URL) u.Path = path.Join(u.Path, "/api/version") @@ -186,7 +186,7 @@ func (c Client) createAPIVersionRequest() web.Request { return req } -func (c Client) createSelectedStatisticsRequest(query []byte) web.Request { +func (c *Client) createSelectedStatisticsRequest(query []byte) web.Request { req := c.Request.Copy() u, _ := url.Parse(req.URL) u.Path = path.Join(u.Path, "/api/instances/querySelectedStatistics") @@ -200,7 +200,7 @@ func (c Client) createSelectedStatisticsRequest(query []byte) web.Request { return req } -func (c Client) createInstancesRequest() web.Request { +func (c *Client) createInstancesRequest() web.Request { req := c.Request.Copy() u, _ := url.Parse(req.URL) u.Path = path.Join(u.Path, "/api/instances") diff --git a/src/go/collectors/go.d.plugin/modules/supervisord/supervisord_test.go b/src/go/collectors/go.d.plugin/modules/supervisord/supervisord_test.go index 4085809a5a474d..521811b112203a 100644 --- a/src/go/collectors/go.d.plugin/modules/supervisord/supervisord_test.go +++ b/src/go/collectors/go.d.plugin/modules/supervisord/supervisord_test.go @@ -230,7 +230,7 @@ type mockSupervisorClient struct { calledCloseIdleConnections bool } -func (m mockSupervisorClient) getAllProcessInfo() ([]processStatus, error) { +func (m *mockSupervisorClient) getAllProcessInfo() ([]processStatus, error) { if m.errOnGetAllProcessInfo { return nil, errors.New("mock errOnGetAllProcessInfo") } diff --git a/src/go/collectors/go.d.plugin/modules/traefik/collect.go b/src/go/collectors/go.d.plugin/modules/traefik/collect.go index f913b88e4df0d2..3ceb845ecae703 100644 --- a/src/go/collectors/go.d.plugin/modules/traefik/collect.go +++ b/src/go/collectors/go.d.plugin/modules/traefik/collect.go @@ -173,7 +173,7 @@ func (t *Traefik) collectEntrypointOpenConnections(mx map[string]int64, pms prom var httpRespCodeClasses = []string{"1xx", "2xx", "3xx", "4xx", "5xx"} -func (t Traefik) updateCodeClassMetrics(mx map[string]int64) { +func (t *Traefik) updateCodeClassMetrics(mx map[string]int64) { for id, ce := range t.cache.entrypoints { if ce.requests != nil { for _, c := range httpRespCodeClasses { diff --git a/src/go/collectors/go.d.plugin/modules/traefik/init.go b/src/go/collectors/go.d.plugin/modules/traefik/init.go index df4765c9144b48..99ab731ef43b9e 100644 --- a/src/go/collectors/go.d.plugin/modules/traefik/init.go +++ b/src/go/collectors/go.d.plugin/modules/traefik/init.go @@ -10,14 +10,14 @@ import ( "github.com/netdata/netdata/go/go.d.plugin/pkg/web" ) -func (t Traefik) validateConfig() error { +func (t *Traefik) validateConfig() error { if t.URL == "" { return errors.New("'url' is not set") } return nil } -func (t Traefik) initPrometheusClient() (prometheus.Prometheus, error) { +func (t *Traefik) initPrometheusClient() (prometheus.Prometheus, error) { httpClient, err := web.NewHTTPClient(t.Client) if err != nil { return nil, err diff --git a/src/go/collectors/go.d.plugin/modules/traefik/traefik.go b/src/go/collectors/go.d.plugin/modules/traefik/traefik.go index 24116f99847005..6e20863ceeafcc 100644 --- a/src/go/collectors/go.d.plugin/modules/traefik/traefik.go +++ b/src/go/collectors/go.d.plugin/modules/traefik/traefik.go @@ -127,4 +127,4 @@ func (t *Traefik) Collect() map[string]int64 { return mx } -func (Traefik) Cleanup() {} +func (t *Traefik) Cleanup() {} diff --git a/src/go/collectors/go.d.plugin/modules/vsphere/match/match.go b/src/go/collectors/go.d.plugin/modules/vsphere/match/match.go index 27282795f36402..846e6f37152bae 100644 --- a/src/go/collectors/go.d.plugin/modules/vsphere/match/match.go +++ b/src/go/collectors/go.d.plugin/modules/vsphere/match/match.go @@ -167,6 +167,7 @@ func parseHostInclude(include string) (HostMatcher, error) { ms = append(ms, hostClusterMatcher{m}) case hostIdx: ms = append(ms, hostHostMatcher{m}) + default: } } diff --git a/src/go/collectors/go.d.plugin/modules/vsphere/resources/resources.go b/src/go/collectors/go.d.plugin/modules/vsphere/resources/resources.go index 7f04a8587319c1..8f967f16ce28b3 100644 --- a/src/go/collectors/go.d.plugin/modules/vsphere/resources/resources.go +++ b/src/go/collectors/go.d.plugin/modules/vsphere/resources/resources.go @@ -108,7 +108,7 @@ type ( } ) -func (v HierarchyValue) IsSet() bool { return v.ID != "" && v.Name != "" } +func (v *HierarchyValue) IsSet() bool { return v.ID != "" && v.Name != "" } func (v *HierarchyValue) Set(id, name string) { v.ID = id; v.Name = name } func (h ClusterHierarchy) IsSet() bool { return h.DC.IsSet() } diff --git a/src/go/collectors/go.d.plugin/modules/vsphere/scrape/scrape.go b/src/go/collectors/go.d.plugin/modules/vsphere/scrape/scrape.go index d803fa41402ac6..adda665cc3aa6e 100644 --- a/src/go/collectors/go.d.plugin/modules/vsphere/scrape/scrape.go +++ b/src/go/collectors/go.d.plugin/modules/vsphere/scrape/scrape.go @@ -34,20 +34,20 @@ type Scraper struct { } // Default settings for vCenter 6.5 and above is 256, prior versions of vCenter have this set to 64. -func (c *Scraper) calcMaxQuery() { - major, minor, err := parseVersion(c.Version()) +func (s *Scraper) calcMaxQuery() { + major, minor, err := parseVersion(s.Version()) if err != nil || major < 6 || minor == 0 { - c.maxQuery = 64 + s.maxQuery = 64 return } - c.maxQuery = 256 + s.maxQuery = 256 } -func (c Scraper) ScrapeHosts(hosts rs.Hosts) []performance.EntityMetric { +func (s *Scraper) ScrapeHosts(hosts rs.Hosts) []performance.EntityMetric { t := time.Now() pqs := newHostsPerfQuerySpecs(hosts) - ms := c.scrapeMetrics(pqs) - c.Debugf("scraping : scraped metrics for %d/%d hosts, process took %s", + ms := s.scrapeMetrics(pqs) + s.Debugf("scraping : scraped metrics for %d/%d hosts, process took %s", len(ms), len(hosts), time.Since(t), @@ -55,11 +55,11 @@ func (c Scraper) ScrapeHosts(hosts rs.Hosts) []performance.EntityMetric { return ms } -func (c Scraper) ScrapeVMs(vms rs.VMs) []performance.EntityMetric { +func (s *Scraper) ScrapeVMs(vms rs.VMs) []performance.EntityMetric { t := time.Now() pqs := newVMsPerfQuerySpecs(vms) - ms := c.scrapeMetrics(pqs) - c.Debugf("scraping : scraped metrics for %d/%d vms, process took %s", + ms := s.scrapeMetrics(pqs) + s.Debugf("scraping : scraped metrics for %d/%d vms, process took %s", len(ms), len(vms), time.Since(t), @@ -67,16 +67,16 @@ func (c Scraper) ScrapeVMs(vms rs.VMs) []performance.EntityMetric { return ms } -func (c Scraper) scrapeMetrics(pqs []types.PerfQuerySpec) []performance.EntityMetric { +func (s *Scraper) scrapeMetrics(pqs []types.PerfQuerySpec) []performance.EntityMetric { tc := newThrottledCaller(5) var ms []performance.EntityMetric lock := &sync.Mutex{} - chunks := chunkify(pqs, c.maxQuery) + chunks := chunkify(pqs, s.maxQuery) for _, chunk := range chunks { pqs := chunk job := func() { - c.scrape(&ms, lock, pqs) + s.scrape(&ms, lock, pqs) } tc.call(job) } @@ -85,10 +85,10 @@ func (c Scraper) scrapeMetrics(pqs []types.PerfQuerySpec) []performance.EntityMe return ms } -func (c Scraper) scrape(metrics *[]performance.EntityMetric, lock *sync.Mutex, pqs []types.PerfQuerySpec) { - m, err := c.PerformanceMetrics(pqs) +func (s *Scraper) scrape(metrics *[]performance.EntityMetric, lock *sync.Mutex, pqs []types.PerfQuerySpec) { + m, err := s.PerformanceMetrics(pqs) if err != nil { - c.Error(err) + s.Error(err) return } diff --git a/src/go/collectors/go.d.plugin/modules/x509check/collect.go b/src/go/collectors/go.d.plugin/modules/x509check/collect.go index cad0ae1696bbce..3b5eebdb2739e1 100644 --- a/src/go/collectors/go.d.plugin/modules/x509check/collect.go +++ b/src/go/collectors/go.d.plugin/modules/x509check/collect.go @@ -43,10 +43,12 @@ func (x *X509Check) collectRevocation(mx map[string]int64, certs []*x509.Certifi if err != nil { x.Debug(err) } - switch { - case ok && rev: + if !ok { + return + } + if rev { mx["revoked"] = 1 - case ok && !rev: + } else { mx["revoked"] = 0 } } diff --git a/src/go/collectors/go.d.plugin/pkg/logs/reader.go b/src/go/collectors/go.d.plugin/pkg/logs/reader.go index ee526a9e322a3f..34544eac6b3c6b 100644 --- a/src/go/collectors/go.d.plugin/pkg/logs/reader.go +++ b/src/go/collectors/go.d.plugin/pkg/logs/reader.go @@ -89,10 +89,10 @@ func (r *Reader) open() error { func (r *Reader) Read(p []byte) (n int, err error) { n, err = r.file.Read(p) if err != nil { - switch err { - case io.EOF: + switch { + case err == io.EOF: err = r.handleEOFErr() - case os.ErrInvalid: // r.file is nil after Close + case errors.Is(err, os.ErrInvalid): // r.file is nil after Close err = r.handleInvalidArgErr() } return diff --git a/src/go/collectors/go.d.plugin/pkg/matcher/simple_patterns.go b/src/go/collectors/go.d.plugin/pkg/matcher/simple_patterns.go index 0c1d69fc68f494..91a0a3bbd30d6e 100644 --- a/src/go/collectors/go.d.plugin/pkg/matcher/simple_patterns.go +++ b/src/go/collectors/go.d.plugin/pkg/matcher/simple_patterns.go @@ -18,7 +18,7 @@ type ( // NewSimplePatternsMatcher creates new simple patterns. It returns error in case one of patterns has bad syntax. func NewSimplePatternsMatcher(expr string) (Matcher, error) { - var ps simplePatternsMatcher + ps := simplePatternsMatcher{} for _, pattern := range strings.Fields(expr) { if err := ps.add(pattern); err != nil { diff --git a/src/go/collectors/go.d.plugin/pkg/matcher/string.go b/src/go/collectors/go.d.plugin/pkg/matcher/string.go index 25827d0d80b167..43ba43eb3cee04 100644 --- a/src/go/collectors/go.d.plugin/pkg/matcher/string.go +++ b/src/go/collectors/go.d.plugin/pkg/matcher/string.go @@ -23,16 +23,16 @@ type ( // NewStringMatcher create a new matcher with string format func NewStringMatcher(s string, startWith, endWith bool) (Matcher, error) { - switch { - case startWith && endWith: - return stringFullMatcher(s), nil - case startWith && !endWith: + if startWith { + if endWith { + return stringFullMatcher(s), nil + } return stringPrefixMatcher(s), nil - case !startWith && endWith: + } + if endWith { return stringSuffixMatcher(s), nil - default: - return stringPartialMatcher(s), nil } + return stringPartialMatcher(s), nil } func (m stringFullMatcher) Match(b []byte) bool { return string(m) == string(b) }