Skip to content

Commit

Permalink
feat: handled review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardikl committed Aug 22, 2024
1 parent e484ce2 commit cec6ce9
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions cmd/collectors/zapiperf/plugins/nic/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (n *Nic) Init() error {
}

if n.client, err = zapi.New(conf.ZapiPoller(n.ParentParams), n.Auth); err != nil {
n.Logger.Error().Stack().Err(err).Msg("connecting")
n.Logger.Error().Err(err).Msg("connecting")
return err
}

Expand All @@ -80,12 +80,11 @@ func (n *Nic) Init() error {
metricName, display, _, _ := util.ParseMetric(obj)
_, err := n.data.NewMetricFloat64(metricName, display)
if err != nil {
n.Logger.Error().Stack().Err(err).Msg("add metric")
n.Logger.Error().Err(err).Msg("add metric")
return err
}
}

n.Logger.Debug().Msgf("added data with %d metrics", len(n.data.GetMetrics()))
return nil
}

Expand Down Expand Up @@ -155,13 +154,13 @@ func (n *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Me
if strings.HasSuffix(s, "M") {
base, err = strconv.Atoi(strings.TrimSuffix(s, "M"))
if err != nil {
n.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Str("speed", s).Msg("convert")
} else {
// NIC speed value converted from Mbps to Bps(bytes per second)
speed = base * 125000
}
} else if speed, err = strconv.Atoi(s); err != nil {
n.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Str("speed", s).Msg("convert")
}

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

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

Expand All @@ -190,7 +189,7 @@ func (n *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Me
if rxOk || txOk {
err := utilPercent.SetValueFloat64(instance, math.Max(rxPercent, txPercent))
if err != nil {
n.Logger.Error().Stack().Err(err).Msg("error")
n.Logger.Error().Err(err).Msg("error")
}
}
}
Expand All @@ -199,7 +198,7 @@ func (n *Nic) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Me
if s = instance.GetLabel("speed"); strings.HasSuffix(s, "M") {
base, err = strconv.Atoi(strings.TrimSuffix(s, "M"))
if err != nil {
n.Logger.Warn().Msgf("convert speed [%s]", s)
n.Logger.Warn().Str("speed", s).Msg("convert")
} else {
// NIC speed value converted from Mbps to bps(bits per second)
speed = base * 1_000_000
Expand Down Expand Up @@ -268,9 +267,9 @@ func (n *Nic) getIfgroupInfo() map[string]string {
ifgroupsData = append(ifgroupsData, ifgroups...)
}
for _, ifgroup := range ifgroupsData {
nodeName := ifgroup.GetChildContentS("node")
port := ifgroup.GetChildContentS("port")
if ifgrpPort := ifgroup.GetChildContentS("ifgrp-port"); ifgrpPort != "" {
nodeName := ifgroup.GetChildContentS("node")
port := ifgroup.GetChildContentS("port")
portIfgroupMap[nodeName+port] = ifgrpPort
}
}
Expand All @@ -284,14 +283,14 @@ func (n *Nic) populateIfgroupMetrics(portIfgroupMap map[string]string, portDataM
nodeName := portInfo.node
port := portInfo.port
readBytes := portInfo.read
WriteBytes := portInfo.write
writeBytes := portInfo.write

ifgrpupInstanceKey := nodeName + ifgroupName
ifgroupInstance := n.data.GetInstance(ifgrpupInstanceKey)
if ifgroupInstance == nil {
ifgroupInstance, err = n.data.NewInstance(ifgrpupInstanceKey)
if err != nil {
n.Logger.Debug().Msgf("add (%s) instance: %v", ifgrpupInstanceKey, err)
n.Logger.Debug().Str("ifgrpupInstanceKey", ifgrpupInstanceKey).Err(err).Msg("Failed to add instance")
return err
}
}
Expand All @@ -308,13 +307,13 @@ func (n *Nic) populateIfgroupMetrics(portIfgroupMap map[string]string, portDataM
rx := n.data.GetMetric("rx_bytes")
rxv, _ := rx.GetValueFloat64(ifgroupInstance)
if err = rx.SetValueFloat64(ifgroupInstance, readBytes+rxv); err != nil {
n.Logger.Debug().Msgf("failed to parse value (%f): %v", readBytes, err)
n.Logger.Debug().Float64("value", readBytes).Err(err).Msg("Failed to parse value")
}

tx := n.data.GetMetric("tx_bytes")
txv, _ := tx.GetValueFloat64(ifgroupInstance)
if err = tx.SetValueFloat64(ifgroupInstance, WriteBytes+txv); err != nil {
n.Logger.Debug().Msgf("failed to parse value (%f): %v", WriteBytes, err)
if err = tx.SetValueFloat64(ifgroupInstance, writeBytes+txv); err != nil {
n.Logger.Debug().Float64("value", writeBytes).Err(err).Msg("Failed to parse value")
}

}
Expand Down

0 comments on commit cec6ce9

Please sign in to comment.