diff options
author | Ilya Mashchenko <ilya@netdata.cloud> | 2024-05-19 21:43:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-19 21:43:42 +0300 |
commit | 011d15a626fdbe817f826b6f2916b5310de16c2b (patch) | |
tree | e3a93d76bc8ba94d2323c69ae8f6d67abec12b3f | |
parent | 1ff4beab64cd72979f5dd07c13eddf11d582ac69 (diff) |
go.d fix some JB code inspection issues (#17702)
23 files changed, 68 insertions, 58 deletions
diff --git a/src/collectors/perf.plugin/metadata.yaml b/src/collectors/perf.plugin/metadata.yaml index db95c0c97a..1c3fa3e8d9 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 40c8071d5c..8b369e6530 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 a271eb093c..4fa93d6901 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 05dad4ac1b..65af0ea2e6 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 2163b9624d..55c669000a 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 2892da1c60..9981313946 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 1d39df10e9..72bac88ee3 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 c10673ed54..a21672e0b3 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 7a8c5969f3..e860ec408b 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 ba28cbd8d9..5a4e460d78 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 0d16496905..29c0e43a03 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 c75f79aa7a..e60dfbf681 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 4085809a5a..521811b112 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 f913b88e4d..3ceb845eca 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 df4765c914..99ab731ef4 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 24116f9984..6e20863cee 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 27282795f3..846e6f3715 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 7f04a85873..8f967f16ce 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 d803fa4140..adda665cc3 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 cad0ae1696..3b5eebdb27 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 ee526a9e32..34544eac6b 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 0c1d69fc68..91a0a3bbd3 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 25827d0d80..43ba43eb3c 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) } |