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

View File

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

View File

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

View File

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

View File

@ -12,10 +12,12 @@ import (
var ntpServer string
func NtpOffsetMetrics() (L []*dataobj.MetricValue) {
func NtpOffsetMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
ntpServers := sys.Config.NtpServers
if len(ntpServers) <= 0 {
return
if len(ntpServers) == 0 {
return ret
}
for idx, server := range ntpServers {
@ -42,15 +44,11 @@ func NtpOffsetMetrics() (L []*dataobj.MetricValue) {
logger.Debug("ntp: client receive time, ", dstTime)
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
return
break
}
//keep silence when no config ntp server
if len(ntpServers) > 0 {
logger.Error("sys.ntp.offset error. all ntp servers response failed.")
}
return
return ret
}

View File

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

View File

@ -7,16 +7,17 @@ import (
"github.com/didi/nightingale/src/dataobj"
)
func SocketStatSummaryMetrics() (L []*dataobj.MetricValue) {
func SocketStatSummaryMetrics() []*dataobj.MetricValue {
ret := make([]*dataobj.MetricValue, 0)
ssMap, err := nux.SocketStatSummary()
if err != nil {
logger.Error("failed to collect SocketStatSummaryMetrics:", err)
return
logger.Errorf("failed to collect SocketStatSummaryMetrics:%v\n", err)
return ret
}
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"
)
func FsKernelMetrics() (L []*dataobj.MetricValue) {
func FsKernelMetrics() []*dataobj.MetricValue {
maxFiles, err := nux.KernelMaxFiles()
if err != nil {
logger.Error("failed collect kernel metrics:", err)
return
logger.Errorf("failed to call collect KernelMaxFiles:%v\n", err)
return nil
}
allocateFiles, err := nux.KernelAllocateFiles()
if err != nil {
logger.Error("failed to call KernelAllocateFiles:", err)
return
logger.Errorf("failed to call KernelAllocateFiles:%v\n", err)
return nil
}
v := math.Ceil(float64(allocateFiles) * 100 / float64(maxFiles))
L = append(L, GaugeValue("sys.fs.files.max", maxFiles))
L = append(L, GaugeValue("sys.fs.files.free", maxFiles-allocateFiles))
L = append(L, GaugeValue("sys.fs.files.used", allocateFiles))
L = append(L, GaugeValue("sys.fs.files.used.percent", v))
return
return []*dataobj.MetricValue{
GaugeValue("sys.fs.files.max", maxFiles),
GaugeValue("sys.fs.files.free", maxFiles-allocateFiles),
GaugeValue("sys.fs.files.used", allocateFiles),
GaugeValue("sys.fs.files.used.percent", v),
}
}
func ProcsNumMetrics() []*dataobj.MetricValue {