@@ -8,11 +8,14 @@ import (
8
8
"sync"
9
9
"sync/atomic"
10
10
"time"
11
+
12
+ "github.com/shirou/gopsutil/process"
11
13
)
12
14
13
15
type (
14
16
// Cube defines the LabStack cube service.
15
17
Cube struct {
18
+ process * process.Process
16
19
client * Client
17
20
requests []* Request
18
21
activeRequests int64
@@ -43,25 +46,27 @@ type (
43
46
44
47
// Request defines a request payload to be corded.
45
48
Request struct {
46
- ID string `json:"id"`
47
- Time time.Time `json:"time"`
48
- Node string `json:"node"`
49
- Group string `json:"group"`
50
- Tags []string `json:"tags,omitempty"`
51
- Host string `json:"host"`
52
- Path string `json:"path"`
53
- Method string `json:"method"`
54
- Status int `json:"status"`
55
- BytesIn int64 `json:"bytes_in"`
56
- BytesOut int64 `json:"bytes_out"`
57
- Latency int64 `json:"latency"`
58
- ClientID string `json:"client_id"`
59
- RemoteIP string `json:"remote_ip"`
60
- UserAgent string `json:"user_agent"`
61
- Active int64 `json:"active"`
62
- // TODO: CPU, Uptime, Memory
63
- Error string `json:"error"`
64
- StackTrace string `json:"stack_trace"`
49
+ ID string `json:"id"`
50
+ Time time.Time `json:"time"`
51
+ Node string `json:"node"`
52
+ Group string `json:"group"`
53
+ Tags []string `json:"tags,omitempty"`
54
+ Host string `json:"host"`
55
+ Path string `json:"path"`
56
+ Method string `json:"method"`
57
+ Status int `json:"status"`
58
+ BytesIn int64 `json:"bytes_in"`
59
+ BytesOut int64 `json:"bytes_out"`
60
+ Latency int64 `json:"latency"`
61
+ ClientID string `json:"client_id"`
62
+ RemoteIP string `json:"remote_ip"`
63
+ UserAgent string `json:"user_agent"`
64
+ Active int64 `json:"active"`
65
+ Error string `json:"error"`
66
+ StackTrace string `json:"stack_trace"`
67
+ Uptime int64 `json:"uptime"`
68
+ CPUPercent float32 `json:"cpu_percent"`
69
+ MemoryPercent float32 `json:"memory_percent"`
65
70
}
66
71
)
67
72
@@ -131,17 +136,23 @@ func (c *Cube) Start(r *http.Request, w http.ResponseWriter) (req *Request) {
131
136
atomic .AddInt64 (& c .started , 1 )
132
137
}
133
138
139
+ start , _ := c .process .CreateTime ()
140
+ cpu , _ := c .process .CPUPercent ()
141
+ mem , _ := c .process .MemoryPercent ()
134
142
req = & Request {
135
- ID : RequestID (r , w ),
136
- Time : time .Now (),
137
- Node : c .Node ,
138
- Group : c .Group ,
139
- Tags : c .Tags ,
140
- Host : r .Host ,
141
- Path : r .URL .Path ,
142
- Method : r .Method ,
143
- UserAgent : r .UserAgent (),
144
- RemoteIP : RealIP (r ),
143
+ ID : RequestID (r , w ),
144
+ Time : time .Now (),
145
+ Node : c .Node ,
146
+ Group : c .Group ,
147
+ Tags : c .Tags ,
148
+ Host : r .Host ,
149
+ Path : r .URL .Path ,
150
+ Method : r .Method ,
151
+ UserAgent : r .UserAgent (),
152
+ RemoteIP : RealIP (r ),
153
+ Uptime : time .Now ().Unix () - start ,
154
+ CPUPercent : float32 (cpu ),
155
+ MemoryPercent : mem ,
145
156
}
146
157
req .ClientID = req .RemoteIP
147
158
atomic .AddInt64 (& c .activeRequests , 1 )
0 commit comments