From 9b5c2c386a925abd3df3323d2b2a86fdd55f5e60 Mon Sep 17 00:00:00 2001 From: drivebyer Date: Mon, 17 Jul 2023 16:14:01 +0800 Subject: [PATCH] fix: return error when requested interface has no stats available (#17666) --- internal/net/net.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/net/net.go b/internal/net/net.go index 9443156d5..1252baa07 100644 --- a/internal/net/net.go +++ b/internal/net/net.go @@ -18,6 +18,8 @@ package net import ( + "fmt" + "github.com/prometheus/procfs" ) @@ -31,5 +33,9 @@ func GetInterfaceNetStats(interf string) (procfs.NetDevLine, error) { if err != nil { return procfs.NetDevLine{}, err } - return netDev[interf], nil + ndl, ok := netDev[interf] + if !ok { + return procfs.NetDevLine{}, fmt.Errorf("%v interface not found", interf) + } + return ndl, nil }