From 1ae4231e221408aef2ba33dbfccd341974084f4c Mon Sep 17 00:00:00 2001 From: maier Date: Tue, 28 Nov 2023 11:02:56 -0500 Subject: [PATCH] fix(lint): G601: Implicit memory aliasing in for loop, struct alignment, typos --- plugins/inputs/vsphere/client.go | 6 +-- plugins/inputs/vsphere/endpoint.go | 68 ++++++++++++++++-------------- plugins/inputs/vsphere/tscache.go | 4 +- plugins/inputs/vsphere/vsphere.go | 49 ++++++++++----------- 4 files changed, 64 insertions(+), 63 deletions(-) diff --git a/plugins/inputs/vsphere/client.go b/plugins/inputs/vsphere/client.go index a47dcab6..41625efb 100644 --- a/plugins/inputs/vsphere/client.go +++ b/plugins/inputs/vsphere/client.go @@ -31,21 +31,21 @@ const absoluteMaxMetrics = 10000 // is periodically recycled to avoid authentication expiration issues. type ClientFactory struct { client *Client - mux sync.Mutex url *url.URL parent *VSphere + mux sync.Mutex } // Client represents a connection to vSphere and is backed by a govmomi connection type Client struct { + log cua.Logger Client *govmomi.Client Views *view.Manager Root *view.ContainerView Perf *performance.Manager - Valid bool Timeout time.Duration closeGate sync.Once - log cua.Logger + Valid bool } // NewClientFactory creates a new ClientFactory and prepares it for use. diff --git a/plugins/inputs/vsphere/endpoint.go b/plugins/inputs/vsphere/endpoint.go index c1259fb7..c8564474 100644 --- a/plugins/inputs/vsphere/endpoint.go +++ b/plugins/inputs/vsphere/endpoint.go @@ -45,64 +45,64 @@ type queryJob func(queryChunk) // Endpoint is a high-level representation of a connected vCenter endpoint. It is backed by the lower // level Client type. type Endpoint struct { - Parent *VSphere - URL *url.URL - resourceKinds map[string]*resourceKind - hwMarks *TSCache + customAttrFilter filter.Filter + log cua.Logger + clientFactory *ClientFactory + metricNameLookup map[int32]string lun2ds map[string]string discoveryTicker *time.Ticker + URL *url.URL + Parent *VSphere + hwMarks *TSCache + customFields map[int32]string + resourceKinds map[string]*resourceKind collectMux sync.RWMutex - initialized bool - clientFactory *ClientFactory + metricNameMux sync.RWMutex busy sync.Mutex - customFields map[int32]string - customAttrFilter filter.Filter customAttrEnabled bool - metricNameLookup map[int32]string - metricNameMux sync.RWMutex - log cua.Logger + initialized bool } type resourceKind struct { + lastColl time.Time + latestSample time.Time + filters filter.Filter + getObjects func(context.Context, *Endpoint, *ResourceFilter) (objectMap, error) + objects objectMap name string vcName string pKey string parentTag string - enabled bool - realTime bool - sampling int32 - objects objectMap - filters filter.Filter - paths []string + parent string excludePaths []string - collectInstances bool - getObjects func(context.Context, *Endpoint, *ResourceFilter) (objectMap, error) include []string - simple bool metrics performance.MetricList - parent string - latestSample time.Time - lastColl time.Time + paths []string + sampling int32 + collectInstances bool + enabled bool + simple bool + realTime bool } type metricEntry struct { - tags map[string]string - name string ts time.Time + tags map[string]string fields map[string]interface{} + name string } type objectMap map[string]*objectRef type objectRef struct { + parentRef *types.ManagedObjectReference + customValues map[string]string + lookup map[string]string + ref types.ManagedObjectReference name string altID string - ref types.ManagedObjectReference - parentRef *types.ManagedObjectReference // Pointer because it must be nillable guest string dcname string - customValues map[string]string - lookup map[string]string } func (e *Endpoint) getParent(obj *objectRef, res *resourceKind) (*objectRef, bool) { @@ -275,7 +275,7 @@ func (e *Endpoint) startDiscovery(ctx context.Context) { }() } -func (e *Endpoint) initalDiscovery(ctx context.Context) { +func (e *Endpoint) initialDiscovery(ctx context.Context) { err := e.discover(ctx) if err != nil && !errors.Is(err, context.Canceled) { e.log.Errorf("Discovery for %s: %s", e.URL.Host, err.Error()) @@ -301,7 +301,7 @@ func (e *Endpoint) init(ctx context.Context) error { if e.Parent.ObjectDiscoveryInterval.Duration > 0 { e.Parent.Log.Debug("Running initial discovery") - e.initalDiscovery(ctx) + e.initialDiscovery(ctx) } e.initialized = true return nil @@ -596,6 +596,7 @@ func getDatacenters(ctx context.Context, e *Endpoint, filter *ResourceFilter) (o } m := make(objectMap, len(resources)) for _, r := range resources { + r := r m[r.ExtensibleManagedObject.Reference().Value] = &objectRef{ name: r.Name, ref: r.ExtensibleManagedObject.Reference(), @@ -618,6 +619,7 @@ func getClusters(ctx context.Context, e *Endpoint, filter *ResourceFilter) (obje cache := make(map[string]*types.ManagedObjectReference) m := make(objectMap, len(resources)) for _, r := range resources { + r := r // Wrap in a function to make defer work correctly. err := func() error { // We're not interested in the immediate parent (a folder), but the data center. @@ -667,6 +669,7 @@ func getHosts(ctx context.Context, e *Endpoint, filter *ResourceFilter) (objectM } m := make(objectMap) for _, r := range resources { + r := r m[r.ExtensibleManagedObject.Reference().Value] = &objectRef{ name: r.Name, ref: r.ExtensibleManagedObject.Reference(), @@ -687,6 +690,7 @@ func getVMs(ctx context.Context, e *Endpoint, filter *ResourceFilter) (objectMap } m := make(objectMap) for _, r := range resources { + r := r if r.Runtime.PowerState != "poweredOn" { continue } @@ -701,6 +705,7 @@ func getVMs(ctx context.Context, e *Endpoint, filter *ResourceFilter) (objectMap // Collect network information for _, net := range r.Guest.Net { + net := net if net.DeviceConfigId == -1 { continue } @@ -777,6 +782,7 @@ func getDatastores(ctx context.Context, e *Endpoint, filter *ResourceFilter) (ob } m := make(objectMap) for _, r := range resources { + r := r lunID := "" if r.Info != nil { info := r.Info.GetDatastoreInfo() diff --git a/plugins/inputs/vsphere/tscache.go b/plugins/inputs/vsphere/tscache.go index c312260c..f8b03a16 100644 --- a/plugins/inputs/vsphere/tscache.go +++ b/plugins/inputs/vsphere/tscache.go @@ -6,10 +6,10 @@ import ( "time" ) -// TSCache is a cache of timestamps used to determine the validity of datapoints +// TSCache is a cache of timestamps used to determine the validity of data points type TSCache struct { - ttl time.Duration table map[string]time.Time + ttl time.Duration mux sync.RWMutex } diff --git a/plugins/inputs/vsphere/vsphere.go b/plugins/inputs/vsphere/vsphere.go index c652f060..984420fb 100644 --- a/plugins/inputs/vsphere/vsphere.go +++ b/plugins/inputs/vsphere/vsphere.go @@ -17,55 +17,50 @@ import ( // VSphere is the top level type for the vSphere input plugin. It contains all the configuration // and a list of connected vSphere endpoints type VSphere struct { - Vcenters []string - Username string - Password string - DatacenterInstances bool + Log cua.Logger + cancel context.CancelFunc + Separator string + Username string + Password string + tls.ClientConfig + VMExclude []string `toml:"vm_exclude"` + DatastoreMetricExclude []string DatacenterMetricInclude []string - DatacenterMetricExclude []string - DatacenterInclude []string - DatacenterExclude []string - ClusterInstances bool ClusterMetricInclude []string ClusterMetricExclude []string ClusterInclude []string ClusterExclude []string - HostInstances bool + DatacenterMetricExclude []string HostMetricInclude []string HostMetricExclude []string HostInclude []string HostExclude []string - VMInstances bool `toml:"vm_instances"` + endpoints []*Endpoint VMMetricInclude []string `toml:"vm_metric_include"` VMMetricExclude []string `toml:"vm_metric_exclude"` VMInclude []string `toml:"vm_include"` - VMExclude []string `toml:"vm_exclude"` - DatastoreInstances bool + Vcenters []string + IPAddresses []string DatastoreMetricInclude []string - DatastoreMetricExclude []string + DatacenterExclude []string DatastoreInclude []string DatastoreExclude []string - Separator string + DatacenterInclude []string CustomAttributeInclude []string CustomAttributeExclude []string - UseIntSamples bool - IPAddresses []string - + ObjectDiscoveryInterval internal.Duration + Timeout internal.Duration MaxQueryObjects int MaxQueryMetrics int CollectConcurrency int DiscoverConcurrency int ForceDiscoverOnInit bool - ObjectDiscoveryInterval internal.Duration - Timeout internal.Duration - - endpoints []*Endpoint - cancel context.CancelFunc - - // Mix in the TLS/SSL goodness from core - tls.ClientConfig - - Log cua.Logger + UseIntSamples bool + DatastoreInstances bool + VMInstances bool `toml:"vm_instances"` + HostInstances bool + ClusterInstances bool + DatacenterInstances bool } var sampleConfig = `