aboutsummaryrefslogtreecommitdiffstats
path: root/metrics
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2019-06-10 19:21:02 +0800
committerPéter Szilágyi <peterke@gmail.com>2019-06-11 14:49:13 +0800
commitb02958b9c57f35d0df085ed8e7057a52131373a3 (patch)
tree2097d29876191e20b710f323d1097d067a4a1c35 /metrics
parentf9c0e093ed83303aa1dff99f658120c34f29cae9 (diff)
downloadgo-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.tar
go-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.tar.gz
go-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.tar.bz2
go-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.tar.lz
go-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.tar.xz
go-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.tar.zst
go-tangerine-b02958b9c57f35d0df085ed8e7057a52131373a3.zip
core, ethdb, metrics, p2p: expose various counter metrics for grafana
Diffstat (limited to 'metrics')
-rw-r--r--metrics/cpu.go36
-rw-r--r--metrics/cpu_syscall.go35
-rw-r--r--metrics/cpu_windows.go23
-rw-r--r--metrics/metrics.go23
4 files changed, 113 insertions, 4 deletions
diff --git a/metrics/cpu.go b/metrics/cpu.go
new file mode 100644
index 000000000..3278d8161
--- /dev/null
+++ b/metrics/cpu.go
@@ -0,0 +1,36 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package metrics
+
+import "github.com/elastic/gosigar"
+
+// CPUStats is the system and process CPU stats.
+type CPUStats struct {
+ GlobalTime int64 // Time spent by the CPU working on all processes
+ GlobalWait int64 // Time spent by waiting on disk for all processes
+ LocalTime int64 // Time spent by the CPU working on this process
+}
+
+// ReadCPUStats retrieves the current CPU stats.
+func ReadCPUStats(stats *CPUStats) {
+ global := gosigar.Cpu{}
+ global.Get()
+
+ stats.GlobalTime = int64(global.User + global.Nice + global.Sys)
+ stats.GlobalWait = int64(global.Wait)
+ stats.LocalTime = getProcessCPUTime()
+}
diff --git a/metrics/cpu_syscall.go b/metrics/cpu_syscall.go
new file mode 100644
index 000000000..e245453e8
--- /dev/null
+++ b/metrics/cpu_syscall.go
@@ -0,0 +1,35 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+// +build !windows
+
+package metrics
+
+import (
+ "syscall"
+
+ "github.com/ethereum/go-ethereum/log"
+)
+
+// getProcessCPUTime retrieves the process' CPU time since program startup.
+func getProcessCPUTime() int64 {
+ var usage syscall.Rusage
+ if err := syscall.Getrusage(syscall.RUSAGE_SELF, &usage); err != nil {
+ log.Warn("Failed to retrieve CPU time", "err", err)
+ return 0
+ }
+ return int64(usage.Utime.Sec+usage.Stime.Sec)*100 + int64(usage.Utime.Usec+usage.Stime.Usec)/10000 //nolint:unconvert
+}
diff --git a/metrics/cpu_windows.go b/metrics/cpu_windows.go
new file mode 100644
index 000000000..fb29a52a8
--- /dev/null
+++ b/metrics/cpu_windows.go
@@ -0,0 +1,23 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package metrics
+
+// getProcessCPUTime returns 0 on Windows as there is no system call to resolve
+// the actual process' CPU time.
+func getProcessCPUTime() int64 {
+ return 0
+}
diff --git a/metrics/metrics.go b/metrics/metrics.go
index 8ae7aec43..98e8ced25 100644
--- a/metrics/metrics.go
+++ b/metrics/metrics.go
@@ -61,18 +61,27 @@ func CollectProcessMetrics(refresh time.Duration) {
if !Enabled {
return
}
+ refreshFreq := int64(refresh / time.Second)
+
// Create the various data collectors
+ cpuStats := make([]*CPUStats, 2)
memstats := make([]*runtime.MemStats, 2)
diskstats := make([]*DiskStats, 2)
for i := 0; i < len(memstats); i++ {
+ cpuStats[i] = new(CPUStats)
memstats[i] = new(runtime.MemStats)
diskstats[i] = new(DiskStats)
}
// Define the various metrics to collect
+ cpuSysLoad := GetOrRegisterGauge("system/cpu/sysload", DefaultRegistry)
+ cpuSysWait := GetOrRegisterGauge("system/cpu/syswait", DefaultRegistry)
+ cpuProcLoad := GetOrRegisterGauge("system/cpu/procload", DefaultRegistry)
+
+ memPauses := GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
memAllocs := GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
memFrees := GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
- memInuse := GetOrRegisterMeter("system/memory/inuse", DefaultRegistry)
- memPauses := GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
+ memHeld := GetOrRegisterGauge("system/memory/held", DefaultRegistry)
+ memUsed := GetOrRegisterGauge("system/memory/used", DefaultRegistry)
var diskReads, diskReadBytes, diskWrites, diskWriteBytes Meter
var diskReadBytesCounter, diskWriteBytesCounter Counter
@@ -91,11 +100,17 @@ func CollectProcessMetrics(refresh time.Duration) {
location1 := i % 2
location2 := (i - 1) % 2
+ ReadCPUStats(cpuStats[location1])
+ cpuSysLoad.Update((cpuStats[location1].GlobalTime - cpuStats[location2].GlobalTime) / refreshFreq)
+ cpuSysWait.Update((cpuStats[location1].GlobalWait - cpuStats[location2].GlobalWait) / refreshFreq)
+ cpuProcLoad.Update((cpuStats[location1].LocalTime - cpuStats[location2].LocalTime) / refreshFreq)
+
runtime.ReadMemStats(memstats[location1])
+ memPauses.Mark(int64(memstats[location1].PauseTotalNs - memstats[location2].PauseTotalNs))
memAllocs.Mark(int64(memstats[location1].Mallocs - memstats[location2].Mallocs))
memFrees.Mark(int64(memstats[location1].Frees - memstats[location2].Frees))
- memInuse.Mark(int64(memstats[location1].Alloc - memstats[location2].Alloc))
- memPauses.Mark(int64(memstats[location1].PauseTotalNs - memstats[location2].PauseTotalNs))
+ memHeld.Update(int64(memstats[location1].HeapSys - memstats[location1].HeapReleased))
+ memUsed.Update(int64(memstats[location1].Alloc))
if ReadDiskStats(diskstats[location1]) == nil {
diskReads.Mark(diskstats[location1].ReadCount - diskstats[location2].ReadCount)