-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpm2io.go
201 lines (177 loc) · 5.12 KB
/
pm2io.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package pm2io
import (
"os"
"runtime"
"time"
"github.com/keymetrics/pm2-io-apm-go/features"
"github.com/keymetrics/pm2-io-apm-go/features/metrics"
"github.com/keymetrics/pm2-io-apm-go/services"
"github.com/keymetrics/pm2-io-apm-go/structures"
"github.com/shirou/gopsutil/process"
)
var version = "0.0.1"
// Pm2Io to config and access all services
type Pm2Io struct {
Config *structures.Config
Notifier *features.Notifier
transporter *services.Transporter
StatusOverrider func() *structures.Status
startTime time.Time
}
// Start and prepare services + profiling
func (pm2io *Pm2Io) Start() {
pm2io.Config.InitNames()
defaultNode := "api.cloud.pm2.io"
if pm2io.Config.Node == nil {
pm2io.Config.Node = &defaultNode
}
pm2io.transporter = services.NewTransporter(pm2io.Config, version)
pm2io.Notifier = &features.Notifier{
Transporter: pm2io.transporter,
}
pm2io.prepareMetrics()
pm2io.prepareProfiling()
pm2io.startTime = time.Now()
pm2io.transporter.Connect()
go func() {
ticker := time.NewTicker(time.Second)
for {
<-ticker.C
pm2io.SendStatus()
}
}()
}
// RestartTransporter including webSocket connection
func (pm2io *Pm2Io) RestartTransporter() {
pm2io.transporter.CloseAndReconnect()
}
// SendStatus of current state
func (pm2io *Pm2Io) SendStatus() {
if pm2io.StatusOverrider != nil {
pm2io.transporter.Send("status", pm2io.StatusOverrider())
return
}
pm2io.transporter.Send("status", structures.Status{
Process: []structures.StatusProcess{pm2io.getProcess()},
Server: pm2io.getServer(),
})
}
// Panic notify KM then panic
func (pm2io *Pm2Io) Panic(err error) {
pm2io.Notifier.Error(err)
panic(err)
}
// StartTracing add global handlers for OpenCensus providers
func (pm2io *Pm2Io) StartTracing() error {
return features.InitTracing(pm2io.Config, pm2io.transporter)
}
func (pm2io *Pm2Io) prepareMetrics() {
metrics.InitMetricsMemStats()
services.AttachHandler(metrics.Handler)
services.AddMetric(metrics.GoRoutines())
services.AddMetric(metrics.CgoCalls())
services.AddMetric(metrics.GlobalMetricsMemStats.NumGC)
services.AddMetric(metrics.GlobalMetricsMemStats.NumMallocs)
services.AddMetric(metrics.GlobalMetricsMemStats.NumFree)
services.AddMetric(metrics.GlobalMetricsMemStats.HeapAlloc)
services.AddMetric(metrics.GlobalMetricsMemStats.Pause)
}
func (pm2io *Pm2Io) prepareProfiling() {
services.AddAction(&structures.Action{
ActionName: "km:heapdump",
ActionType: "internal",
Callback: func(payload map[string]interface{}) string {
data, err := features.HeapDump()
if err != nil {
pm2io.Notifier.Error(err)
}
pm2io.transporter.Send("profilings", structures.NewProfilingResponse(data, "heapdump"))
return ""
},
})
services.AddAction(&structures.Action{
ActionName: "km:cpu:profiling:start",
ActionType: "internal",
Callback: func(payload map[string]interface{}) string {
err := features.StartCPUProfile()
if err != nil {
pm2io.Notifier.Error(err)
}
if payload["opts"] != nil {
go func() {
timeout := payload["opts"].(map[string]interface{})["timeout"]
time.Sleep(time.Duration(timeout.(float64)) * time.Millisecond)
r, err := features.StopCPUProfile()
if err != nil {
pm2io.Notifier.Error(err)
}
pm2io.transporter.Send("profilings", structures.NewProfilingResponse(r, "cpuprofile"))
}()
}
return ""
},
})
services.AddAction(&structures.Action{
ActionName: "km:cpu:profiling:stop",
ActionType: "internal",
Callback: func(payload map[string]interface{}) string {
r, err := features.StopCPUProfile()
if err != nil {
pm2io.Notifier.Error(err)
}
pm2io.transporter.Send("profilings", structures.NewProfilingResponse(r, "cpuprofile"))
return ""
},
})
}
func (pm2io *Pm2Io) getServer() structures.Server {
return structures.Server{
Loadavg: metrics.CPULoad(),
TotalMem: metrics.TotalMem(),
Hostname: pm2io.Config.Hostname,
Uptime: (time.Now().UnixNano()-pm2io.startTime.UnixNano())/int64(time.Millisecond) + 600000,
Pm2Version: version,
Type: runtime.GOOS,
Interaction: true,
CPU: structures.CPU{
Number: runtime.NumCPU(),
Info: metrics.CPUName(),
},
NodeVersion: runtime.Version(),
}
}
func (pm2io *Pm2Io) getProcess() structures.StatusProcess {
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
pm2io.Notifier.Error(err)
}
cp, err := metrics.CPUPercent()
if err != nil {
pm2io.Notifier.Error(err)
}
return structures.StatusProcess{
Pid: p.Pid,
Name: pm2io.Config.Name,
Interpreter: "golang",
RestartTime: 0,
CreatedAt: pm2io.startTime.UnixNano() / int64(time.Millisecond),
ExecMode: "fork_mode",
PmUptime: pm2io.startTime.UnixNano() / int64(time.Millisecond),
Status: "online",
PmID: 0,
CPU: cp,
Memory: uint64(metrics.GlobalMetricsMemStats.HeapAlloc.Value),
UniqueID: pm2io.Config.ProcessUniqueID,
AxmActions: services.Actions,
AxmMonitor: services.GetMetricsAsMap(),
AxmOptions: structures.Options{
HeapDump: true,
Profiling: true,
CustomProbes: true,
Apm: structures.Apm{
Type: "golang",
Version: version,
},
},
}
}