Skip to content

Commit

Permalink
fix(lint): G601: Implicit memory aliasing in for loop, struct alignme…
Browse files Browse the repository at this point in the history
…nt, typos
  • Loading branch information
maier committed Nov 28, 2023
1 parent c62f9df commit 1ae4231
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 63 deletions.
6 changes: 3 additions & 3 deletions plugins/inputs/vsphere/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 37 additions & 31 deletions plugins/inputs/vsphere/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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.
Expand Down Expand Up @@ -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(),
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/vsphere/tscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
49 changes: 22 additions & 27 deletions plugins/inputs/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down

0 comments on commit 1ae4231

Please sign in to comment.