Skip to content

Commit

Permalink
go.d fix some JB code inspection issues (netdata#17702)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored May 19, 2024
1 parent 1ff4bea commit 011d15a
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/collectors/perf.plugin/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/agent/confgroup/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 8 additions & 6 deletions src/go/collectors/go.d.plugin/examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/dnsquery/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/go/collectors/go.d.plugin/modules/haproxy/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/k8s_state/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/k8s_state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/phpdaemon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/pika/pika_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/portcheck/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions src/go/collectors/go.d.plugin/modules/scaleio/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -160,15 +160,15 @@ 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")
req.URL = u.String()
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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/traefik/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/go/collectors/go.d.plugin/modules/traefik/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/go/collectors/go.d.plugin/modules/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ func (t *Traefik) Collect() map[string]int64 {
return mx
}

func (Traefik) Cleanup() {}
func (t *Traefik) Cleanup() {}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func parseHostInclude(include string) (HostMatcher, error) {
ms = append(ms, hostClusterMatcher{m})
case hostIdx:
ms = append(ms, hostHostMatcher{m})
default:
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
32 changes: 16 additions & 16 deletions src/go/collectors/go.d.plugin/modules/vsphere/scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,49 @@ 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),
)
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),
)
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)
}
Expand All @@ -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
}

Expand Down
8 changes: 5 additions & 3 deletions src/go/collectors/go.d.plugin/modules/x509check/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 3 additions & 3 deletions src/go/collectors/go.d.plugin/pkg/logs/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions src/go/collectors/go.d.plugin/pkg/matcher/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 011d15a

Please sign in to comment.