Skip to content

Commit 9bf7175

Browse files
committed
fix: Add guards for converting Stat interface (might be nil)
Change-Id: I54cd9ca6fb6607a70031ba5e6318a9ea58db7e6c Signed-off-by: Ondrej Fabry <[email protected]>
1 parent 454aff1 commit 9bf7175

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

core/stats.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (c *StatsConnection) updateStats(statDir **adapter.StatDir, patterns ...str
166166
} else if err == adapter.ErrStatsDirStale || err == adapter.ErrStatsDataBusy {
167167
// retrying
168168
if r > 1 {
169-
log.Debugln("sleeping for %v before next try", RetryUpdateDelay)
169+
log.Debugf("sleeping for %v before next try", RetryUpdateDelay)
170170
time.Sleep(RetryUpdateDelay)
171171
}
172172
} else {
@@ -255,23 +255,25 @@ func (c *StatsConnection) GetNodeStats(nodeStats *api.NodeStats) (err error) {
255255
}
256256
}
257257
perNode := func(stat adapter.StatEntry, fn func(*api.NodeCounters, uint64)) {
258-
s := stat.Data.(adapter.SimpleCounterStat)
259-
prepNodes(len(s[0]))
260-
for i := range nodeStats.Nodes {
261-
val := adapter.ReduceSimpleCounterStatIndex(s, i)
262-
fn(&nodeStats.Nodes[i], val)
258+
if s, ok := stat.Data.(adapter.SimpleCounterStat); ok {
259+
prepNodes(len(s[0]))
260+
for i := range nodeStats.Nodes {
261+
val := adapter.ReduceSimpleCounterStatIndex(s, i)
262+
fn(&nodeStats.Nodes[i], val)
263+
}
263264
}
264265
}
265266

266267
for _, stat := range c.nodeStatsData.Entries {
267268
switch string(stat.Name) {
268269
case NodeStats_Names:
269-
stat := stat.Data.(adapter.NameStat)
270-
prepNodes(len(stat))
271-
for i, nc := range nodeStats.Nodes {
272-
if nc.NodeName != string(stat[i]) {
273-
nc.NodeName = string(stat[i])
274-
nodeStats.Nodes[i] = nc
270+
if stat, ok := stat.Data.(adapter.NameStat); ok {
271+
prepNodes(len(stat))
272+
for i, nc := range nodeStats.Nodes {
273+
if nc.NodeName != string(stat[i]) {
274+
nc.NodeName = string(stat[i])
275+
nodeStats.Nodes[i] = nc
276+
}
275277
}
276278
}
277279
case NodeStats_Clocks:
@@ -311,31 +313,34 @@ func (c *StatsConnection) GetInterfaceStats(ifaceStats *api.InterfaceStats) (err
311313
}
312314
}
313315
perNode := func(stat adapter.StatEntry, fn func(*api.InterfaceCounters, uint64)) {
314-
s := stat.Data.(adapter.SimpleCounterStat)
315-
prep(len(s[0]))
316-
for i := range ifaceStats.Interfaces {
317-
val := adapter.ReduceSimpleCounterStatIndex(s, i)
318-
fn(&ifaceStats.Interfaces[i], val)
316+
if s, ok := stat.Data.(adapter.SimpleCounterStat); ok {
317+
prep(len(s[0]))
318+
for i := range ifaceStats.Interfaces {
319+
val := adapter.ReduceSimpleCounterStatIndex(s, i)
320+
fn(&ifaceStats.Interfaces[i], val)
321+
}
319322
}
320323
}
321324
perNodeComb := func(stat adapter.StatEntry, fn func(*api.InterfaceCounters, [2]uint64)) {
322-
s := stat.Data.(adapter.CombinedCounterStat)
323-
prep(len(s[0]))
324-
for i := range ifaceStats.Interfaces {
325-
val := adapter.ReduceCombinedCounterStatIndex(s, i)
326-
fn(&ifaceStats.Interfaces[i], val)
325+
if s, ok := stat.Data.(adapter.CombinedCounterStat); ok {
326+
prep(len(s[0]))
327+
for i := range ifaceStats.Interfaces {
328+
val := adapter.ReduceCombinedCounterStatIndex(s, i)
329+
fn(&ifaceStats.Interfaces[i], val)
330+
}
327331
}
328332
}
329333

330334
for _, stat := range c.ifaceStatsData.Entries {
331335
switch string(stat.Name) {
332336
case InterfaceStats_Names:
333-
stat := stat.Data.(adapter.NameStat)
334-
prep(len(stat))
335-
for i, nc := range ifaceStats.Interfaces {
336-
if nc.InterfaceName != string(stat[i]) {
337-
nc.InterfaceName = string(stat[i])
338-
ifaceStats.Interfaces[i] = nc
337+
if stat, ok := stat.Data.(adapter.NameStat); ok {
338+
prep(len(stat))
339+
for i, nc := range ifaceStats.Interfaces {
340+
if nc.InterfaceName != string(stat[i]) {
341+
nc.InterfaceName = string(stat[i])
342+
ifaceStats.Interfaces[i] = nc
343+
}
339344
}
340345
}
341346
case InterfaceStats_Drops:

0 commit comments

Comments
 (0)