This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
metrics.go
139 lines (122 loc) · 3.64 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Copyright 2022 SkyAPM org
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package go2sky
import (
"context"
"github.com/SkyAPM/go2sky/internal/tool"
"github.com/SkyAPM/go2sky/logger"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
"log"
"os"
"runtime"
"runtime/debug"
"time"
)
const (
defaultInterval = 15 * time.Second
defaultLogPrefix = "go2sky-golang-metric"
InstanceGolangHeap = "instance_golang_heap_alloc"
InstanceGolangStack = "instance_golang_stack_used"
InstanceGolangGCTime = "instance_golang_gc_pause_time"
InstanceGolangGCCount = "instance_golang_gc_count"
InstanceGolangThreadNum = "instance_golang_os_threads_num"
InstanceGolangGoroutineNum = "instance_golang_live_goroutines_num"
InstanceCPUUsedRate = "instance_host_cpu_used_rate"
InstanceMemUsedRate = "instance_host_mem_used_rate"
)
type RunTimeMetric struct {
// the Unix time when metrics were collected
Time int64
// the bytes of allocated heap objects
HeapAlloc int64
// the bytes in stack spans.
StackInUse int64
// the number of completed GC cycles since instance started
GCCount int64
// the total gc pause time(NS) since instance started
GCPauseTime int64
// the number of goroutines that currently exist
GoroutineNum int64
// the number of records in the thread creation profile
ThreadNum int64
// the cpu Used float64
CpuUsedRate float64
// the Percentage of RAM used by programs
MemUsedRate float64
}
type MetricCollector struct {
ctx context.Context
reporter MetricsReporter
instance string
service string
interval time.Duration
logger logger.Log
}
func InitMetricCollector(reporter MetricsReporter, interval *time.Duration, cancelCtx context.Context) {
collector := &MetricCollector{
ctx: cancelCtx,
logger: logger.NewDefaultLogger(log.New(os.Stderr, defaultLogPrefix, log.LstdFlags)),
reporter: reporter,
interval: defaultInterval,
}
if interval != nil {
collector.interval = *interval
}
go collector.collect()
}
func (c *MetricCollector) collect() {
defer func() {
// recover the panic caused by close sendCh
if err := recover(); err != nil {
c.logger.Errorf("collect metric err %v", err)
}
}()
timer := time.NewTicker(c.interval)
for {
select {
case <-c.ctx.Done():
c.logger.Infof("stop the meter collection")
return
case <-timer.C:
go c.collectMeter()
}
}
}
func (c *MetricCollector) collectMeter() {
var rtm runtime.MemStats
runtime.ReadMemStats(&rtm)
v, _ := mem.VirtualMemory()
cpuPercent, _ := cpu.Percent(0, false)
threadNum, _ := runtime.ThreadCreateProfile(nil)
var stats debug.GCStats
debug.ReadGCStats(&stats)
runTimeMetric := RunTimeMetric{
Time: tool.Millisecond(time.Now()),
HeapAlloc: int64(rtm.HeapAlloc),
StackInUse: int64(rtm.StackInuse),
GCCount: int64(rtm.NumGC),
GCPauseTime: int64(stats.PauseTotal),
GoroutineNum: int64(runtime.NumGoroutine()),
ThreadNum: int64(threadNum),
CpuUsedRate: cpuPercent[0],
MemUsedRate: v.UsedPercent,
}
c.reporter.SendMetrics(runTimeMetric)
}
type MetricsReporter interface {
SendMetrics(runTimeMeter RunTimeMetric)
}