forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsandbox_metrics.go
227 lines (192 loc) · 5.56 KB
/
sandbox_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2020 Ant Financial
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
mutils "github.com/kata-containers/kata-containers/src/runtime/pkg/utils"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
)
const namespaceHypervisor = "kata_hypervisor"
const namespaceKatashim = "kata_shim"
const namespaceVirtiofsd = "kata_virtiofsd"
var (
// hypervisor
hypervisorThreads = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespaceHypervisor,
Name: "threads",
Help: "Hypervisor process threads.",
})
hypervisorProcStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceHypervisor,
Name: "proc_status",
Help: "Hypervisor process status.",
},
[]string{"item"},
)
hypervisorProcStat = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceHypervisor,
Name: "proc_stat",
Help: "Hypervisor process statistics.",
},
[]string{"item"},
)
hypervisorNetdev = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceHypervisor,
Name: "netdev",
Help: "Net devices statistics.",
},
[]string{"interface", "item"},
)
hypervisorIOStat = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceHypervisor,
Name: "io_stat",
Help: "Process IO statistics.",
},
[]string{"item"},
)
hypervisorOpenFDs = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespaceHypervisor,
Name: "fds",
Help: "Open FDs for hypervisor.",
})
// agent
agentRPCDurationsHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespaceKatashim,
Name: "agent_rpc_durations_histogram_milliseconds",
Help: "RPC latency distributions.",
Buckets: prometheus.ExponentialBuckets(1, 2, 10),
},
[]string{"action"},
)
// virtiofsd
virtiofsdThreads = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespaceVirtiofsd,
Name: "threads",
Help: "Virtiofsd process threads.",
})
virtiofsdProcStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceVirtiofsd,
Name: "proc_status",
Help: "Virtiofsd process status.",
},
[]string{"item"},
)
virtiofsdProcStat = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceVirtiofsd,
Name: "proc_stat",
Help: "Virtiofsd process statistics.",
},
[]string{"item"},
)
virtiofsdIOStat = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespaceVirtiofsd,
Name: "io_stat",
Help: "Process IO statistics.",
},
[]string{"item"},
)
virtiofsdOpenFDs = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespaceVirtiofsd,
Name: "fds",
Help: "Open FDs for virtiofsd.",
})
)
func RegisterMetrics() {
// hypervisor
prometheus.MustRegister(hypervisorThreads)
prometheus.MustRegister(hypervisorProcStatus)
prometheus.MustRegister(hypervisorProcStat)
prometheus.MustRegister(hypervisorNetdev)
prometheus.MustRegister(hypervisorIOStat)
prometheus.MustRegister(hypervisorOpenFDs)
// agent
prometheus.MustRegister(agentRPCDurationsHistogram)
// virtiofsd
prometheus.MustRegister(virtiofsdThreads)
prometheus.MustRegister(virtiofsdProcStatus)
prometheus.MustRegister(virtiofsdProcStat)
prometheus.MustRegister(virtiofsdIOStat)
prometheus.MustRegister(virtiofsdOpenFDs)
}
// UpdateRuntimeMetrics update shim/hypervisor's metrics
func (s *Sandbox) UpdateRuntimeMetrics() error {
pids := s.hypervisor.GetPids()
if len(pids) == 0 {
return nil
}
hypervisorPid := pids[0]
proc, err := procfs.NewProc(hypervisorPid)
if err != nil {
return err
}
// process FDs
if fds, err := proc.FileDescriptorsLen(); err == nil {
hypervisorOpenFDs.Set(float64(fds))
}
// process net device statistics
if netdev, err := proc.NetDev(); err == nil {
// netdev: map[string]NetDevLine
for _, v := range netdev {
mutils.SetGaugeVecNetDev(hypervisorNetdev, v)
}
}
// process statistics
if procStat, err := proc.Stat(); err == nil {
hypervisorThreads.Set(float64(procStat.NumThreads))
mutils.SetGaugeVecProcStat(hypervisorProcStat, procStat)
}
// process status
if procStatus, err := proc.NewStatus(); err == nil {
mutils.SetGaugeVecProcStatus(hypervisorProcStatus, procStatus)
}
// process IO statistics
if ioStat, err := proc.IO(); err == nil {
mutils.SetGaugeVecProcIO(hypervisorIOStat, ioStat)
}
// virtiofs metrics
err = s.UpdateVirtiofsdMetrics()
if err != nil {
return err
}
return nil
}
func (s *Sandbox) UpdateVirtiofsdMetrics() error {
vfsPid := s.hypervisor.GetVirtioFsPid()
if vfsPid == nil {
// virtiofsd is not mandatory for a VMM.
return nil
}
proc, err := procfs.NewProc(*vfsPid)
if err != nil {
return err
}
// process FDs
if fds, err := proc.FileDescriptorsLen(); err == nil {
virtiofsdOpenFDs.Set(float64(fds))
}
// process statistics
if procStat, err := proc.Stat(); err == nil {
virtiofsdThreads.Set(float64(procStat.NumThreads))
mutils.SetGaugeVecProcStat(virtiofsdProcStat, procStat)
}
// process status
if procStatus, err := proc.NewStatus(); err == nil {
mutils.SetGaugeVecProcStatus(virtiofsdProcStatus, procStatus)
}
// process IO statistics
if ioStat, err := proc.IO(); err == nil {
mutils.SetGaugeVecProcIO(virtiofsdIOStat, ioStat)
}
return nil
}
func (s *Sandbox) GetAgentMetrics(ctx context.Context) (string, error) {
r, err := s.agent.getAgentMetrics(ctx, &grpc.GetMetricsRequest{})
if err != nil {
return "", err
}
return r.Metrics, nil
}