Make variable name more meaningful

This commit is contained in:
chenjiandongx 2020-05-09 21:44:18 +08:00
parent bd35f53950
commit c1f38e5ca4
8 changed files with 89 additions and 82 deletions

View File

@ -11,15 +11,17 @@ import (
"github.com/toolkits/pkg/nux" "github.com/toolkits/pkg/nux"
) )
func DeviceMetrics() (L []*dataobj.MetricValue) { func DeviceMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
mountPoints, err := nux.ListMountPoint() mountPoints, err := nux.ListMountPoint()
fsFileFilter := make(map[string]struct{}) //过滤 /proc/mounts 出现重复的fsFile fsFileFilter := make(map[string]struct{}) //过滤 /proc/mounts 出现重复的fsFile
if err != nil { if err != nil {
logger.Error("collect device metrics fail:", err) logger.Error("collect device metrics fail:", err)
return return ret
} }
var myMountPoints map[string]bool = make(map[string]bool) var myMountPoints = make(map[string]bool)
if len(sys.Config.MountPoint) > 0 { if len(sys.Config.MountPoint) > 0 {
for _, mp := range sys.Config.MountPoint { for _, mp := range sys.Config.MountPoint {
myMountPoints[mp] = true myMountPoints[mp] = true
@ -39,6 +41,7 @@ func DeviceMetrics() (L []*dataobj.MetricValue) {
continue continue
} }
} }
if _, exists := fsFileFilter[fsFile]; exists { if _, exists := fsFileFilter[fsFile]; exists {
logger.Debugf("mount point %s was collected", fsFile) logger.Debugf("mount point %s was collected", fsFile)
continue continue
@ -65,29 +68,29 @@ func DeviceMetrics() (L []*dataobj.MetricValue) {
diskUsed += du.BlocksUsed diskUsed += du.BlocksUsed
tags := fmt.Sprintf("mount=%s", du.FsFile) tags := fmt.Sprintf("mount=%s", du.FsFile)
L = append(L, GaugeValue("disk.bytes.total", du.BlocksAll, tags)) ret = append(ret, GaugeValue("disk.bytes.total", du.BlocksAll, tags))
L = append(L, GaugeValue("disk.bytes.free", du.BlocksFree, tags)) ret = append(ret, GaugeValue("disk.bytes.free", du.BlocksFree, tags))
L = append(L, GaugeValue("disk.bytes.used", du.BlocksUsed, tags)) ret = append(ret, GaugeValue("disk.bytes.used", du.BlocksUsed, tags))
L = append(L, GaugeValue("disk.bytes.used.percent", du.BlocksUsedPercent, tags)) ret = append(ret, GaugeValue("disk.bytes.used.percent", du.BlocksUsedPercent, tags))
if du.InodesAll == 0 { if du.InodesAll == 0 {
continue continue
} }
L = append(L, GaugeValue("disk.inodes.total", du.InodesAll, tags)) ret = append(ret, GaugeValue("disk.inodes.total", du.InodesAll, tags))
L = append(L, GaugeValue("disk.inodes.free", du.InodesFree, tags)) ret = append(ret, GaugeValue("disk.inodes.free", du.InodesFree, tags))
L = append(L, GaugeValue("disk.inodes.used", du.InodesUsed, tags)) ret = append(ret, GaugeValue("disk.inodes.used", du.InodesUsed, tags))
L = append(L, GaugeValue("disk.inodes.used.percent", du.InodesUsedPercent, tags)) ret = append(ret, GaugeValue("disk.inodes.used.percent", du.InodesUsedPercent, tags))
} }
if len(L) > 0 && diskTotal > 0 { if len(ret) > 0 && diskTotal > 0 {
L = append(L, GaugeValue("disk.cap.bytes.total", float64(diskTotal))) ret = append(ret, GaugeValue("disk.cap.bytes.total", float64(diskTotal)))
L = append(L, GaugeValue("disk.cap.bytes.used", float64(diskUsed))) ret = append(ret, GaugeValue("disk.cap.bytes.used", float64(diskUsed)))
L = append(L, GaugeValue("disk.cap.bytes.free", float64(diskTotal-diskUsed))) ret = append(ret, GaugeValue("disk.cap.bytes.free", float64(diskTotal-diskUsed)))
L = append(L, GaugeValue("disk.cap.bytes.used.percent", float64(diskUsed)*100.0/float64(diskTotal))) ret = append(ret, GaugeValue("disk.cap.bytes.used.percent", float64(diskUsed)*100.0/float64(diskTotal)))
} }
return return ret
} }
func hasIgnorePrefix(fsFile string, ignoreMountPointsPrefix []string) bool { func hasIgnorePrefix(fsFile string, ignoreMountPointsPrefix []string) bool {

View File

@ -97,7 +97,9 @@ func IODelta(device string, f func([2]*nux.DiskStats) uint64) uint64 {
return f(val) return f(val)
} }
func IOStatsMetrics() (L []*dataobj.MetricValue) { func IOStatsMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
dsLock.RLock() dsLock.RLock()
defer dsLock.RUnlock() defer dsLock.RUnlock()
@ -109,38 +111,38 @@ func IOStatsMetrics() (L []*dataobj.MetricValue) {
tags := "device=" + device tags := "device=" + device
rio := IODelta(device, IOReadRequests) rio := IODelta(device, IOReadRequests)
wio := IODelta(device, IOWriteRequests) wio := IODelta(device, IOWriteRequests)
delta_rsec := IODelta(device, IOReadSectors) deltaRsec := IODelta(device, IOReadSectors)
delta_wsec := IODelta(device, IOWriteSectors) deltaWsec := IODelta(device, IOWriteSectors)
ruse := IODelta(device, IOMsecRead) ruse := IODelta(device, IOMsecRead)
wuse := IODelta(device, IOMsecWrite) wuse := IODelta(device, IOMsecWrite)
use := IODelta(device, IOMsecTotal) use := IODelta(device, IOMsecTotal)
n_io := rio + wio nio := rio + wio
avgrq_sz := 0.0 avgrqSz := 0.0
await := 0.0 await := 0.0
svctm := 0.0 svctm := 0.0
if n_io != 0 { if nio != 0 {
avgrq_sz = float64(delta_rsec+delta_wsec) / float64(n_io) avgrqSz = float64(deltaRsec+deltaWsec) / float64(nio)
await = float64(ruse+wuse) / float64(n_io) await = float64(ruse+wuse) / float64(nio)
svctm = float64(use) / float64(n_io) svctm = float64(use) / float64(nio)
} }
duration := IODelta(device, TS) duration := IODelta(device, TS)
L = append(L, GaugeValue("disk.io.read.request", float64(rio), tags)) ret = append(ret, GaugeValue("disk.io.read.request", float64(rio), tags))
L = append(L, GaugeValue("disk.io.write.request", float64(wio), tags)) ret = append(ret, GaugeValue("disk.io.write.request", float64(wio), tags))
L = append(L, GaugeValue("disk.io.read.bytes", float64(delta_rsec)*512.0, tags)) ret = append(ret, GaugeValue("disk.io.read.bytes", float64(deltaRsec)*512.0, tags))
L = append(L, GaugeValue("disk.io.write.bytes", float64(delta_wsec)*512.0, tags)) ret = append(ret, GaugeValue("disk.io.write.bytes", float64(deltaWsec)*512.0, tags))
L = append(L, GaugeValue("disk.io.avgrq_sz", avgrq_sz, tags)) ret = append(ret, GaugeValue("disk.io.avgrq_sz", avgrqSz, tags))
L = append(L, GaugeValue("disk.io.avgqu_sz", float64(IODelta(device, IOMsecWeightedTotal))/1000.0, tags)) ret = append(ret, GaugeValue("disk.io.avgqu_sz", float64(IODelta(device, IOMsecWeightedTotal))/1000.0, tags))
L = append(L, GaugeValue("disk.io.await", await, tags)) ret = append(ret, GaugeValue("disk.io.await", await, tags))
L = append(L, GaugeValue("disk.io.svctm", svctm, tags)) ret = append(ret, GaugeValue("disk.io.svctm", svctm, tags))
tmp := float64(use) * 100.0 / float64(duration) tmp := float64(use) * 100.0 / float64(duration)
if tmp > 100.0 { if tmp > 100.0 {
tmp = 100.0 tmp = 100.0
} }
L = append(L, GaugeValue("disk.io.util", tmp, tags)) ret = append(ret, GaugeValue("disk.io.util", tmp, tags))
} }
return return ret
} }
func ShouldHandleDevice(device string) bool { func ShouldHandleDevice(device string) bool {

View File

@ -15,11 +15,13 @@ import (
"github.com/toolkits/pkg/nux" "github.com/toolkits/pkg/nux"
) )
func FsRWMetrics() (L []*dataobj.MetricValue) { func FsRWMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
mountPoints, err := nux.ListMountPoint() mountPoints, err := nux.ListMountPoint()
if err != nil { if err != nil {
logger.Error("failed to call ListMountPoint:", err) logger.Errorf("failed to call ListMountPoint:%v\n", err)
return return ret
} }
fsFileFilter := make(map[string]struct{}) //过滤 /proc/mounts 出现重复的fsFile fsFileFilter := make(map[string]struct{}) //过滤 /proc/mounts 出现重复的fsFile
@ -50,14 +52,14 @@ func FsRWMetrics() (L []*dataobj.MetricValue) {
f, err := os.Open(du.FsFile) f, err := os.Open(du.FsFile)
if err != nil { if err != nil {
logger.Error("target mount point open failed:", err) logger.Error("target mount point open failed:", err)
L = append(L, GaugeValue("disk.rw.error", 1, tags)) ret = append(ret, GaugeValue("disk.rw.error", 1, tags))
continue continue
} }
fs, err := f.Stat() fs, err := f.Stat()
if err != nil { if err != nil {
logger.Error("get target mount point status failed:", err) logger.Error("get target mount point status failed:", err)
L = append(L, GaugeValue("disk.rw.error", 2, tags)) ret = append(ret, GaugeValue("disk.rw.error", 2, tags))
continue continue
} }
@ -70,13 +72,13 @@ func FsRWMetrics() (L []*dataobj.MetricValue) {
content := "FS-RW" + now content := "FS-RW" + now
err = CheckFS(file, content) err = CheckFS(file, content)
if err != nil { if err != nil {
L = append(L, GaugeValue("disk.rw.error", 3, tags)) ret = append(ret, GaugeValue("disk.rw.error", 3, tags))
} else { } else {
L = append(L, GaugeValue("disk.rw.error", 0, tags)) ret = append(ret, GaugeValue("disk.rw.error", 0, tags))
} }
} }
return return ret
} }
func CheckFS(file string, content string) error { func CheckFS(file string, content string) error {
@ -101,7 +103,7 @@ func CheckFS(file string, content string) error {
} }
if string(read) != content { if string(read) != content {
logger.Error("Read content failed: ", string(read)) logger.Error("Read content failed: ", string(read))
return errors.New("Read content failed") return errors.New("read content failed")
} }
//clean the file //clean the file
err = os.Remove(file) err = os.Remove(file)

View File

@ -14,7 +14,7 @@ func NfMetrics() []*dataobj.MetricValue {
if !file.IsExist(connMaxFile) { if !file.IsExist(connMaxFile) {
return []*dataobj.MetricValue{} return []*dataobj.MetricValue{}
} }
res := []*dataobj.MetricValue{} var res []*dataobj.MetricValue
nfConntrackMax, err := file.ToInt64(connMaxFile) nfConntrackMax, err := file.ToInt64(connMaxFile)
if err != nil { if err != nil {

View File

@ -12,10 +12,12 @@ import (
var ntpServer string var ntpServer string
func NtpOffsetMetrics() (L []*dataobj.MetricValue) { func NtpOffsetMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
ntpServers := sys.Config.NtpServers ntpServers := sys.Config.NtpServers
if len(ntpServers) <= 0 { if len(ntpServers) == 0 {
return return ret
} }
for idx, server := range ntpServers { for idx, server := range ntpServers {
@ -42,15 +44,11 @@ func NtpOffsetMetrics() (L []*dataobj.MetricValue) {
logger.Debug("ntp: client receive time, ", dstTime) logger.Debug("ntp: client receive time, ", dstTime)
delta := duration / 1e6 // 转换成 ms delta := duration / 1e6 // 转换成 ms
L = append(L, GaugeValue("sys.ntp.offset.ms", delta)) ret = append(ret, GaugeValue("sys.ntp.offset.ms", delta))
//one ntp server's response is enough //one ntp server's response is enough
break
return
} }
//keep silence when no config ntp server return ret
if len(ntpServers) > 0 {
logger.Error("sys.ntp.offset error. all ntp servers response failed.")
}
return
} }

View File

@ -68,10 +68,10 @@ func Push(metricItems []*dataobj.MetricValue) error {
retry += 1 retry += 1
if retry == 3 { if retry == 3 {
retry = 0
break break
} }
} }
return err return err
} }
@ -84,14 +84,14 @@ func rpcCall(addr string, items []*dataobj.MetricValue) (dataobj.TransferResp, e
client, err = rpcClient(addr) client, err = rpcClient(addr)
if err != nil { if err != nil {
return reply, err return reply, err
} else { }
affected := rpcClients.Put(addr, client) affected := rpcClients.Put(addr, client)
if !affected { if !affected {
defer func() { defer func() {
// 我尝试把自己这个client塞进map失败说明已经有一个client塞进去了那我自己用完了就关闭 // 我尝试把自己这个client塞进map失败说明已经有一个client塞进去了那我自己用完了就关闭
client.Close() client.Close()
}() }()
}
} }
} }
@ -105,7 +105,7 @@ func rpcCall(addr string, items []*dataobj.MetricValue) (dataobj.TransferResp, e
select { select {
case <-time.After(timeout): case <-time.After(timeout):
logger.Warningf("rpc call timeout, transfer addr: %s", addr) logger.Warningf("rpc call timeout, transfer addr: %s\n", addr)
rpcClients.Put(addr, nil) rpcClients.Put(addr, nil)
client.Close() client.Close()
return reply, fmt.Errorf("%s rpc call timeout", addr) return reply, fmt.Errorf("%s rpc call timeout", addr)

View File

@ -7,16 +7,17 @@ import (
"github.com/didi/nightingale/src/dataobj" "github.com/didi/nightingale/src/dataobj"
) )
func SocketStatSummaryMetrics() (L []*dataobj.MetricValue) { func SocketStatSummaryMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
ssMap, err := nux.SocketStatSummary() ssMap, err := nux.SocketStatSummary()
if err != nil { if err != nil {
logger.Error("failed to collect SocketStatSummaryMetrics:", err) logger.Errorf("failed to collect SocketStatSummaryMetrics:%v\n", err)
return return ret
} }
for k, v := range ssMap { for k, v := range ssMap {
L = append(L, GaugeValue("net."+k, v)) ret = append(ret, GaugeValue("net."+k, v))
} }
return return ret
} }

View File

@ -12,25 +12,26 @@ import (
"github.com/didi/nightingale/src/dataobj" "github.com/didi/nightingale/src/dataobj"
) )
func FsKernelMetrics() (L []*dataobj.MetricValue) { func FsKernelMetrics() []*dataobj.MetricValue {
maxFiles, err := nux.KernelMaxFiles() maxFiles, err := nux.KernelMaxFiles()
if err != nil { if err != nil {
logger.Error("failed collect kernel metrics:", err) logger.Errorf("failed to call collect KernelMaxFiles:%v\n", err)
return return nil
} }
allocateFiles, err := nux.KernelAllocateFiles() allocateFiles, err := nux.KernelAllocateFiles()
if err != nil { if err != nil {
logger.Error("failed to call KernelAllocateFiles:", err) logger.Errorf("failed to call KernelAllocateFiles:%v\n", err)
return return nil
} }
v := math.Ceil(float64(allocateFiles) * 100 / float64(maxFiles)) v := math.Ceil(float64(allocateFiles) * 100 / float64(maxFiles))
L = append(L, GaugeValue("sys.fs.files.max", maxFiles)) return []*dataobj.MetricValue{
L = append(L, GaugeValue("sys.fs.files.free", maxFiles-allocateFiles)) GaugeValue("sys.fs.files.max", maxFiles),
L = append(L, GaugeValue("sys.fs.files.used", allocateFiles)) GaugeValue("sys.fs.files.free", maxFiles-allocateFiles),
L = append(L, GaugeValue("sys.fs.files.used.percent", v)) GaugeValue("sys.fs.files.used", allocateFiles),
return GaugeValue("sys.fs.files.used.percent", v),
}
} }
func ProcsNumMetrics() []*dataobj.MetricValue { func ProcsNumMetrics() []*dataobj.MetricValue {