Refactor: Use int64 type for absolut values

This commit is contained in:
Holger Obermaier 2022-03-11 09:23:00 +01:00
parent 0bea1d53a0
commit bc9bd4e5ac
2 changed files with 20 additions and 15 deletions

View File

@ -12,6 +12,12 @@
"proc_total" "proc_total"
] ]
}, },
"netstat": {
"include_devices": [
"enp5s0"
],
"send_derived_values": true
},
"numastats": {}, "numastats": {},
"nvidia": {}, "nvidia": {},
"tempstat": { "tempstat": {

View File

@ -26,7 +26,7 @@ type NetstatCollectorMetric struct {
index int index int
tags map[string]string tags map[string]string
rate_tags map[string]string rate_tags map[string]string
lastValue float64 lastValue int64
} }
type NetstatCollector struct { type NetstatCollector struct {
@ -113,28 +113,28 @@ func (m *NetstatCollector) Init(config json.RawMessage) error {
{ {
name: "net_bytes_in", name: "net_bytes_in",
index: fieldReceiveBytes, index: fieldReceiveBytes,
lastValue: 0, lastValue: -1,
tags: tags_unit_byte, tags: tags_unit_byte,
rate_tags: tags_unit_byte_per_sec, rate_tags: tags_unit_byte_per_sec,
}, },
{ {
name: "net_pkts_in", name: "net_pkts_in",
index: fieldReceivePackets, index: fieldReceivePackets,
lastValue: 0, lastValue: -1,
tags: tags_unit_pkts, tags: tags_unit_pkts,
rate_tags: tags_unit_pkts_per_sec, rate_tags: tags_unit_pkts_per_sec,
}, },
{ {
name: "net_bytes_out", name: "net_bytes_out",
index: fieldTransmitBytes, index: fieldTransmitBytes,
lastValue: 0, lastValue: -1,
tags: tags_unit_byte, tags: tags_unit_byte,
rate_tags: tags_unit_byte_per_sec, rate_tags: tags_unit_byte_per_sec,
}, },
{ {
name: "net_pkts_out", name: "net_pkts_out",
index: fieldTransmitPackets, index: fieldTransmitPackets,
lastValue: 0, lastValue: -1,
tags: tags_unit_pkts, tags: tags_unit_pkts,
rate_tags: tags_unit_pkts_per_sec, rate_tags: tags_unit_pkts_per_sec,
}, },
@ -186,27 +186,26 @@ func (m *NetstatCollector) Read(interval time.Duration, output chan lp.CCMetric)
// Check if device is a included device // Check if device is a included device
if devmetrics, ok := m.matches[dev]; ok { if devmetrics, ok := m.matches[dev]; ok {
for i := range devmetrics { for i := range devmetrics {
data := &devmetrics[i] metric := &devmetrics[i]
// Read value // Read value
v, err := strconv.ParseFloat(f[data.index], 64) v, err := strconv.ParseInt(f[metric.index], 10, 64)
if err != nil { if err != nil {
continue continue
} }
if m.config.SendAbsoluteValues { if m.config.SendAbsoluteValues {
if y, err := lp.New(data.name, data.tags, m.meta, map[string]interface{}{"value": v}, now); err == nil { if y, err := lp.New(metric.name, metric.tags, m.meta, map[string]interface{}{"value": v}, now); err == nil {
output <- y output <- y
} }
} }
if m.config.SendDerivedValues { if m.config.SendDerivedValues {
rate := (v - data.lastValue) / timeDiff if metric.lastValue >= 0 {
if data.lastValue == 0 { rate := float64(v-metric.lastValue) / timeDiff
rate = 0 if y, err := lp.New(metric.name+"_bw", metric.rate_tags, m.meta, map[string]interface{}{"value": rate}, now); err == nil {
} output <- y
data.lastValue = v }
if y, err := lp.New(data.name+"_bw", data.rate_tags, m.meta, map[string]interface{}{"value": rate}, now); err == nil {
output <- y
} }
metric.lastValue = v
} }
} }
} }