Skip to content

Commit

Permalink
refactor: fix inconsistent pointer receivers
Browse files Browse the repository at this point in the history
refactor: fix receive has generic name
refactor: Unquoted port mapping not recommended
refactor: delete redundant size hint
refactor: don't alias imported package name
refactor: Type assertion on errors fail on wrapped errors
  • Loading branch information
cgrinds committed Jul 20, 2023
1 parent 7050f59 commit 24a879a
Show file tree
Hide file tree
Showing 19 changed files with 217 additions and 102 deletions.
4 changes: 2 additions & 2 deletions cmd/collectors/rest/plugins/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func New(p *plugin.AbstractPlugin) plugin.Plugin {
return &Disk{AbstractPlugin: p}
}

func (me *Disk) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {
data := dataMap[me.Object]
func (d *Disk) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {
data := dataMap[d.Object]
for _, instance := range data.GetInstances() {

containerType := instance.GetLabel("container_type")
Expand Down
4 changes: 2 additions & 2 deletions cmd/collectors/rest/plugins/qtree/qtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (my *Qtree) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error
return []*matrix.Matrix{qtreePluginData, my.data}, nil
}

func (my Qtree) handlingHistoricalMetrics(result []gjson.Result, data *matrix.Matrix, cluster string, quotaIndex *int, numMetrics *int) error {
func (my *Qtree) handlingHistoricalMetrics(result []gjson.Result, data *matrix.Matrix, cluster string, quotaIndex *int, numMetrics *int) error {
for _, quota := range result {
var tree string
var quotaInstance *matrix.Instance
Expand Down Expand Up @@ -289,7 +289,7 @@ func (my Qtree) handlingHistoricalMetrics(result []gjson.Result, data *matrix.Ma
return nil
}

func (my Qtree) handlingQuotaMetrics(result []gjson.Result, cluster string, quotaCount *int, numMetrics *int) error {
func (my *Qtree) handlingQuotaMetrics(result []gjson.Result, cluster string, quotaCount *int, numMetrics *int) error {
for quotaIndex, quota := range result {
var tree string

Expand Down
8 changes: 4 additions & 4 deletions cmd/collectors/restperf/plugins/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (d *Disk) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)

shelfChildInstance.SetLabel("shelf", shelfName)

// Each child would have different possible values which is ugly way to write all of them,
// Each child would have different possible values which is an ugly way to write all of them,
// so normal value would be mapped to 1 and rest all are mapped to 0.
if shelfChildInstance.GetLabel("status") == "normal" {
_ = statusMetric.SetValueInt64(shelfChildInstance, 1)
Expand Down Expand Up @@ -744,7 +744,7 @@ func (d *Disk) initMaps() {

func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) {
var err error
shelfEnvironmentMetricMap := make(map[string]*shelfEnvironmentMetric, 0)
shelfEnvironmentMetricMap := make(map[string]*shelfEnvironmentMetric)
for _, o := range d.shelfData {
for k, instance := range o.GetInstances() {
firstInd := strings.Index(k, "#")
Expand Down Expand Up @@ -779,7 +779,7 @@ func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) {
if mkey == "voltage" {
if value, ok := metric.GetValueFloat64(instance); ok {
if shelfEnvironmentMetricMap[iKey].voltageSensor == nil {
shelfEnvironmentMetricMap[iKey].voltageSensor = make(map[string]float64, 0)
shelfEnvironmentMetricMap[iKey].voltageSensor = make(map[string]float64)
}
shelfEnvironmentMetricMap[iKey].voltageSensor[iKey2] = value
}
Expand All @@ -788,7 +788,7 @@ func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) {
if mkey == "current" {
if value, ok := metric.GetValueFloat64(instance); ok {
if shelfEnvironmentMetricMap[iKey].currentSensor == nil {
shelfEnvironmentMetricMap[iKey].currentSensor = make(map[string]float64, 0)
shelfEnvironmentMetricMap[iKey].currentSensor = make(map[string]float64)
}
shelfEnvironmentMetricMap[iKey].currentSensor[iKey2] = value
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/collectors/restperf/plugins/headroom/headroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func New(p *plugin.AbstractPlugin) plugin.Plugin {
return &Headroom{AbstractPlugin: p}
}

func (me *Headroom) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {
func (h *Headroom) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {

data := dataMap[me.Object]
data := dataMap[h.Object]
for _, instance := range data.GetInstances() {

// no need to continue if labels are already parsed
Expand Down
20 changes: 10 additions & 10 deletions cmd/collectors/restperf/plugins/nic/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func New(p *plugin.AbstractPlugin) plugin.Plugin {
}

// Run speed label is reported in bits-per-second and rx/tx is reported as bytes-per-second
func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {
func (n *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {

var read, write, rx, tx, util *matrix.Metric
var err error
data := dataMap[me.Object]
data := dataMap[n.Object]

if read = data.GetMetric("receive_bytes"); read == nil {
return nil, errs.New(errs.ErrNoMetric, "receive_bytes")
Expand Down Expand Up @@ -78,14 +78,14 @@ func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)
if strings.HasSuffix(s, "M") {
base, err = strconv.Atoi(strings.TrimSuffix(s, "M"))
if err != nil {
me.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Msgf("convert speed [%s]", s)
} else {
// NIC speed value converted from Mbps to Bps(bytes per second)
speed = base * 125000
me.Logger.Debug().Msgf("converted speed (%s) to numeric (%d)", s, speed)
n.Logger.Debug().Msgf("converted speed (%s) to numeric (%d)", s, speed)
}
} else if speed, err = strconv.Atoi(s); err != nil {
me.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Msgf("convert speed [%s]", s)
}

if speed != 0 {
Expand All @@ -97,22 +97,22 @@ func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)
rxPercent = rxBytes / float64(speed)
err := rx.SetValueFloat64(instance, rxPercent)
if err != nil {
me.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Stack().Err(err).Msg("error")
}
}

if txBytes, txOk = write.GetValueFloat64(instance); txOk {
txPercent = txBytes / float64(speed)
err := tx.SetValueFloat64(instance, txPercent)
if err != nil {
me.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Stack().Err(err).Msg("error")
}
}

if rxOk || txOk {
err := util.SetValueFloat64(instance, math.Max(rxPercent, txPercent))
if err != nil {
me.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Stack().Err(err).Msg("error")
}
}
}
Expand All @@ -121,12 +121,12 @@ func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)
if s = instance.GetLabel("speed"); strings.HasSuffix(s, "M") {
base, err = strconv.Atoi(strings.TrimSuffix(s, "M"))
if err != nil {
me.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Msgf("convert speed [%s]", s)
} else {
// NIC speed value converted from Mbps to bps(bits per second)
speed = base * 1_000_000
instance.SetLabel("speed", strconv.Itoa(speed))
me.Logger.Debug().Msgf("converted speed (%s) to numeric (%d)", s, speed)
n.Logger.Debug().Msgf("converted speed (%s) to numeric (%d)", s, speed)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/collectors/zapi/plugins/qtree/qtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (my *Qtree) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error
return []*matrix.Matrix{qtreePluginData, my.data}, nil
}

func (my Qtree) handlingHistoricalMetrics(quotas []*node.Node, data *matrix.Matrix, cluster string, quotaIndex *int, numMetrics *int) error {
func (my *Qtree) handlingHistoricalMetrics(quotas []*node.Node, data *matrix.Matrix, cluster string, quotaIndex *int, numMetrics *int) error {
for qIndex, quota := range quotas {
var vserver, quotaInstanceKey string
var qtreeInstance *matrix.Instance
Expand Down Expand Up @@ -328,7 +328,7 @@ func (my Qtree) handlingHistoricalMetrics(quotas []*node.Node, data *matrix.Matr
return nil
}

func (my Qtree) handlingQuotaMetrics(quotas []*node.Node, cluster string, quotaIndex *int, numMetrics *int) error {
func (my *Qtree) handlingQuotaMetrics(quotas []*node.Node, cluster string, quotaIndex *int, numMetrics *int) error {
for _, quota := range quotas {
var vserver, quotaInstanceKey, uid, uName string

Expand Down
6 changes: 3 additions & 3 deletions cmd/collectors/zapiperf/plugins/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func (d *Disk) handleShelfPower(shelves []*node.Node, output []*matrix.Matrix) (

func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) {
var err error
shelfEnvironmentMetricMap := make(map[string]*shelfEnvironmentMetric, 0)
shelfEnvironmentMetricMap := make(map[string]*shelfEnvironmentMetric)
for _, o := range d.shelfData {
for k, instance := range o.GetInstances() {
if !instance.IsExportable() {
Expand Down Expand Up @@ -702,7 +702,7 @@ func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) {
if mkey == "voltage-sensor-reading" {
if value, ok := metric.GetValueFloat64(instance); ok {
if shelfEnvironmentMetricMap[iKey].voltageSensor == nil {
shelfEnvironmentMetricMap[iKey].voltageSensor = make(map[string]float64, 0)
shelfEnvironmentMetricMap[iKey].voltageSensor = make(map[string]float64)
}
shelfEnvironmentMetricMap[iKey].voltageSensor[iKey2] = value
}
Expand All @@ -711,7 +711,7 @@ func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) {
if mkey == "current-sensor-reading" {
if value, ok := metric.GetValueFloat64(instance); ok {
if shelfEnvironmentMetricMap[iKey].currentSensor == nil {
shelfEnvironmentMetricMap[iKey].currentSensor = make(map[string]float64, 0)
shelfEnvironmentMetricMap[iKey].currentSensor = make(map[string]float64)
}
shelfEnvironmentMetricMap[iKey].currentSensor[iKey2] = value
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/collectors/zapiperf/plugins/fcvi/fcvi.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (f *FCVI) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)

for _, adapterData := range result {
adapter := adapterData.GetChildContentS("adapter-name")
node := adapterData.GetChildContentS("node-name")
nodeName := adapterData.GetChildContentS("node-name")
port := adapterData.GetChildContentS("port-name")
adapterPortMap[node+adapter] = port
adapterPortMap[nodeName+adapter] = port
}

// we would not use getInstance() as key would be `sti8300mcc-215:kernel:fcvi_device_1`
Expand Down
4 changes: 2 additions & 2 deletions cmd/collectors/zapiperf/plugins/headroom/headroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func New(p *plugin.AbstractPlugin) plugin.Plugin {
return &Headroom{AbstractPlugin: p}
}

func (me *Headroom) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {
func (h *Headroom) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {

data := dataMap[me.Object]
data := dataMap[h.Object]
for _, instance := range data.GetInstances() {
if !instance.IsExportable() {
continue
Expand Down
20 changes: 10 additions & 10 deletions cmd/collectors/zapiperf/plugins/nic/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func New(p *plugin.AbstractPlugin) plugin.Plugin {
}

// Run speed label is reported in bits-per-second and rx/tx is reported as bytes-per-second
func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {
func (n *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error) {

var read, write, rx, tx, util *matrix.Metric
var err error

data := dataMap[me.Object]
data := dataMap[n.Object]
if read = data.GetMetric("rx_bytes"); read == nil {
return nil, errs.New(errs.ErrNoMetric, "rx_bytes")
}
Expand Down Expand Up @@ -85,14 +85,14 @@ func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)
if strings.HasSuffix(s, "M") {
base, err = strconv.Atoi(strings.TrimSuffix(s, "M"))
if err != nil {
me.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Msgf("convert speed [%s]", s)
} else {
// NIC speed value converted from Mbps to Bps(bytes per second)
speed = base * 125000
me.Logger.Debug().Msgf("converted speed (%s) to Bps numeric (%d)", s, speed)
n.Logger.Debug().Msgf("converted speed (%s) to Bps numeric (%d)", s, speed)
}
} else if speed, err = strconv.Atoi(s); err != nil {
me.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Msgf("convert speed [%s]", s)
}

if speed != 0 {
Expand All @@ -103,22 +103,22 @@ func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)
rxPercent = rxBytes / float64(speed)
err := rx.SetValueFloat64(instance, rxPercent)
if err != nil {
me.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Stack().Err(err).Msg("error")
}
}

if txBytes, txOk = write.GetValueFloat64(instance); txOk {
txPercent = txBytes / float64(speed)
err := tx.SetValueFloat64(instance, txPercent)
if err != nil {
me.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Stack().Err(err).Msg("error")
}
}

if rxOk || txOk {
err := util.SetValueFloat64(instance, math.Max(rxPercent, txPercent))
if err != nil {
me.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Stack().Err(err).Msg("error")
}
}
}
Expand All @@ -127,12 +127,12 @@ func (me *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, error)
if s = instance.GetLabel("speed"); strings.HasSuffix(s, "M") {
base, err = strconv.Atoi(strings.TrimSuffix(s, "M"))
if err != nil {
me.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Msgf("convert speed [%s]", s)
} else {
// NIC speed value converted from Mbps to bps(bits per second)
speed = base * 1_000_000
instance.SetLabel("speed", strconv.Itoa(speed))
me.Logger.Debug().Msgf("converted speed (%s) to bps numeric (%d)", s, speed)
n.Logger.Debug().Msgf("converted speed (%s) to bps numeric (%d)", s, speed)
}
}

Expand Down
Loading

0 comments on commit 24a879a

Please sign in to comment.