Skip to content

Commit

Permalink
[code]: reduce go GC work by reusing tag and fields maps
Browse files Browse the repository at this point in the history
  • Loading branch information
tbelda-ems committed Sep 5, 2022
1 parent aa41402 commit a8df9ac
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 223 deletions.
58 changes: 33 additions & 25 deletions internal/vccollector/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func (c *VcCollector) CollectClusterInfo(
acc telegraf.Accumulator,
) error {
var (
clusters []*object.ClusterComputeResource
clMo mo.ClusterComputeResource
cltags map[string]string
clfields map[string]interface{}
clusters []*object.ClusterComputeResource
resourceSum *(types.ClusterComputeResourceSummary)
usageSum *(types.ClusterUsageSummary)
numVms int32
Expand All @@ -40,6 +42,10 @@ func (c *VcCollector) CollectClusterInfo(
return fmt.Errorf("Could not get cluster and host entity list: %w", err)
}

// reserve map memory for tags and fields according to setClusterTags and setClusterFields
cltags = make(map[string]string, 4)
clfields = make(map[string]interface{}, 11)

for i, dc := range c.dcs {
clusters = c.clusters[i]
for _, cluster := range clusters {
Expand All @@ -62,13 +68,15 @@ func (c *VcCollector) CollectClusterInfo(
numVms = usageSum.TotalVmCount
}

cltags := getClusterTags(
setClusterTags(
cltags,
c.client.Client.URL().Host,
dc.Name(),
cluster.Name(),
cluster.Reference().Value,
)
clfields := getClusterFields(
setClusterFields(
clfields,
string(resourceSum.OverallStatus),
entityStatusCode(resourceSum.OverallStatus),
resourceSum.NumHosts,
Expand All @@ -88,34 +96,34 @@ func (c *VcCollector) CollectClusterInfo(
return nil
}

func getClusterTags(vcenter, dcname, clustername, moid string) map[string]string {
return map[string]string{
"dcname": dcname,
"clustername": clustername,
"moid": moid,
"vcenter": vcenter,
}
func setClusterTags(
tags map[string]string,
vcenter, dcname, clustername, moid string,
) {
tags["dcname"] = dcname
tags["clustername"] = clustername
tags["moid"] = moid
tags["vcenter"] = vcenter
}

func getClusterFields(
func setClusterFields(
fields map[string]interface{},
overallstatus string,
clusterstatuscode int16,
numhosts, numeffectivehosts int32,
numcpucores, numcputhreads int16,
totalcpu, totalmemory, effectivecpu, effectivememory int64,
numvms int32,
) map[string]interface{} {
return map[string]interface{}{
"effective_cpu": effectivecpu,
"effective_memory": effectivememory,
"num_cpu_cores": numcpucores,
"num_cpu_threads": numcputhreads,
"num_effective_hosts": numeffectivehosts,
"num_vms": numvms,
"num_hosts": numhosts,
"status": overallstatus,
"status_code": clusterstatuscode,
"total_cpu": totalcpu,
"total_memory": totalmemory,
}
) {
fields["effective_cpu"] = effectivecpu
fields["effective_memory"] = effectivememory
fields["num_cpu_cores"] = numcpucores
fields["num_cpu_threads"] = numcputhreads
fields["num_effective_hosts"] = numeffectivehosts
fields["num_vms"] = numvms
fields["num_hosts"] = numhosts
fields["status"] = overallstatus
fields["status_code"] = clusterstatuscode
fields["total_cpu"] = totalcpu
fields["total_memory"] = totalmemory
}
45 changes: 29 additions & 16 deletions internal/vccollector/dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func (c *VcCollector) CollectDatacenterInfo(
ctx context.Context,
acc telegraf.Accumulator,
) error {
var err error
var (
dctags map[string]string
dcfields map[string]interface{}
err error
)

if c.client == nil {
return fmt.Errorf("Could not get datacenters info: %w", govplus.ErrorNoClient)
Expand All @@ -29,13 +33,20 @@ func (c *VcCollector) CollectDatacenterInfo(
if err = c.getAllDatacentersEntities(ctx); err != nil {
return fmt.Errorf("Could not get all datacenters entity lists: %w", err)
}

// reserve map memory for tags and fields according to setDcTags and setDcFields
dctags = make(map[string]string, 3)
dcfields = make(map[string]interface{}, 4)

for i, dc := range c.dcs {
dctags := getDcTags(
setDcTags(
dctags,
c.client.Client.URL().Host,
dc.Name(),
dc.Reference().Value,
)
dcfields := getDcFields(
setDcFields(
dcfields,
len(c.clusters[i]),
len(c.hosts[i]),
len(c.dss[i]),
Expand Down Expand Up @@ -66,19 +77,21 @@ func (c *VcCollector) getAllDatacentersEntities(ctx context.Context) error {
return err
}

func getDcTags(vcenter, dcname, moid string) map[string]string {
return map[string]string{
"dcname": dcname,
"moid": moid,
"vcenter": vcenter,
}
func setDcTags(
tags map[string]string,
vcenter, dcname, moid string,
) {
tags["dcname"] = dcname
tags["moid"] = moid
tags["vcenter"] = vcenter
}

func getDcFields(clusters, hosts, datastores, networks int) map[string]interface{} {
return map[string]interface{}{
"num_clusters": clusters,
"num_datastores": datastores,
"num_hosts": hosts,
"num_networks": networks,
}
func setDcFields(
fields map[string]interface{},
clusters, hosts, datastores, networks int,
) {
fields["num_clusters"] = clusters
fields["num_datastores"] = datastores
fields["num_hosts"] = hosts
fields["num_networks"] = networks
}
Loading

0 comments on commit a8df9ac

Please sign in to comment.