@@ -9,7 +9,6 @@ package metrics
99import (
1010 "runtime/metrics"
1111 "runtime/pprof"
12- "time"
1312)
1413
1514var (
@@ -103,102 +102,3 @@ func readRuntimeStats(v *runtimeStats) {
103102 }
104103 }
105104}
106-
107- // CollectProcessMetrics periodically collects various metrics about the running process.
108- func CollectProcessMetrics (refresh time.Duration ) {
109- // Short circuit if the metrics system is disabled
110- if ! metricsEnabled {
111- return
112- }
113-
114- // Create the various data collectors
115- var (
116- cpustats = make ([]CPUStats , 2 )
117- diskstats = make ([]DiskStats , 2 )
118- rstats = make ([]runtimeStats , 2 )
119- )
120-
121- // This scale factor is used for the runtime's time metrics. It's useful to convert to
122- // ns here because the runtime gives times in float seconds, but runtimeHistogram can
123- // only provide integers for the minimum and maximum values.
124- const secondsToNs = float64 (time .Second )
125-
126- // Define the various metrics to collect
127- var (
128- cpuSysLoad = GetOrRegisterGauge ("system/cpu/sysload" , DefaultRegistry )
129- cpuSysWait = GetOrRegisterGauge ("system/cpu/syswait" , DefaultRegistry )
130- cpuProcLoad = GetOrRegisterGauge ("system/cpu/procload" , DefaultRegistry )
131- cpuSysLoadTotal = GetOrRegisterCounterFloat64 ("system/cpu/sysload/total" , DefaultRegistry )
132- cpuSysWaitTotal = GetOrRegisterCounterFloat64 ("system/cpu/syswait/total" , DefaultRegistry )
133- cpuProcLoadTotal = GetOrRegisterCounterFloat64 ("system/cpu/procload/total" , DefaultRegistry )
134- cpuThreads = GetOrRegisterGauge ("system/cpu/threads" , DefaultRegistry )
135- cpuGoroutines = GetOrRegisterGauge ("system/cpu/goroutines" , DefaultRegistry )
136- cpuSchedLatency = getOrRegisterRuntimeHistogram ("system/cpu/schedlatency" , secondsToNs , nil )
137- memPauses = getOrRegisterRuntimeHistogram ("system/memory/pauses" , secondsToNs , nil )
138- memAllocs = GetOrRegisterMeter ("system/memory/allocs" , DefaultRegistry )
139- memFrees = GetOrRegisterMeter ("system/memory/frees" , DefaultRegistry )
140- memTotal = GetOrRegisterGauge ("system/memory/held" , DefaultRegistry )
141- heapUsed = GetOrRegisterGauge ("system/memory/used" , DefaultRegistry )
142- heapObjects = GetOrRegisterGauge ("system/memory/objects" , DefaultRegistry )
143- diskReads = GetOrRegisterMeter ("system/disk/readcount" , DefaultRegistry )
144- diskReadBytes = GetOrRegisterMeter ("system/disk/readdata" , DefaultRegistry )
145- diskReadBytesCounter = GetOrRegisterCounter ("system/disk/readbytes" , DefaultRegistry )
146- diskWrites = GetOrRegisterMeter ("system/disk/writecount" , DefaultRegistry )
147- diskWriteBytes = GetOrRegisterMeter ("system/disk/writedata" , DefaultRegistry )
148- diskWriteBytesCounter = GetOrRegisterCounter ("system/disk/writebytes" , DefaultRegistry )
149- )
150-
151- var lastCollectTime time.Time
152-
153- // Iterate loading the different stats and updating the meters.
154- now , prev := 0 , 1
155- for ; ; now , prev = prev , now {
156- // Gather CPU times.
157- ReadCPUStats (& cpustats [now ])
158- collectTime := time .Now ()
159- secondsSinceLastCollect := collectTime .Sub (lastCollectTime ).Seconds ()
160- lastCollectTime = collectTime
161- if secondsSinceLastCollect > 0 {
162- sysLoad := cpustats [now ].GlobalTime - cpustats [prev ].GlobalTime
163- sysWait := cpustats [now ].GlobalWait - cpustats [prev ].GlobalWait
164- procLoad := cpustats [now ].LocalTime - cpustats [prev ].LocalTime
165- // Convert to integer percentage.
166- cpuSysLoad .Update (int64 (sysLoad / secondsSinceLastCollect * 100 ))
167- cpuSysWait .Update (int64 (sysWait / secondsSinceLastCollect * 100 ))
168- cpuProcLoad .Update (int64 (procLoad / secondsSinceLastCollect * 100 ))
169- // increment counters (ms)
170- cpuSysLoadTotal .Inc (sysLoad )
171- cpuSysWaitTotal .Inc (sysWait )
172- cpuProcLoadTotal .Inc (procLoad )
173- }
174-
175- // Threads
176- cpuThreads .Update (int64 (threadCreateProfile .Count ()))
177-
178- // Go runtime metrics
179- readRuntimeStats (& rstats [now ])
180-
181- cpuGoroutines .Update (int64 (rstats [now ].Goroutines ))
182- cpuSchedLatency .update (rstats [now ].SchedLatency )
183- memPauses .update (rstats [now ].GCPauses )
184-
185- memAllocs .Mark (int64 (rstats [now ].GCAllocBytes - rstats [prev ].GCAllocBytes ))
186- memFrees .Mark (int64 (rstats [now ].GCFreedBytes - rstats [prev ].GCFreedBytes ))
187-
188- memTotal .Update (int64 (rstats [now ].MemTotal ))
189- heapUsed .Update (int64 (rstats [now ].MemTotal - rstats [now ].HeapUnused - rstats [now ].HeapFree - rstats [now ].HeapReleased ))
190- heapObjects .Update (int64 (rstats [now ].HeapObjects ))
191-
192- // Disk
193- if ReadDiskStats (& diskstats [now ]) == nil {
194- diskReads .Mark (diskstats [now ].ReadCount - diskstats [prev ].ReadCount )
195- diskReadBytes .Mark (diskstats [now ].ReadBytes - diskstats [prev ].ReadBytes )
196- diskWrites .Mark (diskstats [now ].WriteCount - diskstats [prev ].WriteCount )
197- diskWriteBytes .Mark (diskstats [now ].WriteBytes - diskstats [prev ].WriteBytes )
198- diskReadBytesCounter .Inc (diskstats [now ].ReadBytes - diskstats [prev ].ReadBytes )
199- diskWriteBytesCounter .Inc (diskstats [now ].WriteBytes - diskstats [prev ].WriteBytes )
200- }
201-
202- time .Sleep (refresh )
203- }
204- }
0 commit comments