forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
284 lines (246 loc) · 11.6 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package vcenterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver"
import (
"context"
"github.com/vmware/govmomi/performance"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/receiver/scrapererror"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
)
func (v *vcenterMetricScraper) recordHostSystemMemoryUsage(
now pcommon.Timestamp,
hs mo.HostSystem,
) {
s := hs.Summary
h := s.Hardware
z := s.QuickStats
memUsage := z.OverallMemoryUsage
v.mb.RecordVcenterHostMemoryUsageDataPoint(now, int64(memUsage))
memUtilization := 100 * float64(z.OverallMemoryUsage) / float64(h.MemorySize>>20)
v.mb.RecordVcenterHostMemoryUtilizationDataPoint(now, memUtilization)
ncpu := int32(h.NumCpuCores)
cpuUsage := z.OverallCpuUsage
cpuUtilization := 100 * float64(z.OverallCpuUsage) / float64(ncpu*h.CpuMhz)
v.mb.RecordVcenterHostCPUUsageDataPoint(now, int64(cpuUsage))
v.mb.RecordVcenterHostCPUUtilizationDataPoint(now, cpuUtilization)
}
func (v *vcenterMetricScraper) recordVMUsages(
now pcommon.Timestamp,
vm mo.VirtualMachine,
hs mo.HostSystem,
) {
memUsage := vm.Summary.QuickStats.GuestMemoryUsage
balloonedMem := vm.Summary.QuickStats.BalloonedMemory
swappedMem := vm.Summary.QuickStats.SwappedMemory
swappedSSDMem := vm.Summary.QuickStats.SsdSwappedMemory
if totalMemory := vm.Summary.Config.MemorySizeMB; totalMemory > 0 && memUsage > 0 {
memoryUtilization := float64(memUsage) / float64(totalMemory) * 100
v.mb.RecordVcenterVMMemoryUtilizationDataPoint(now, memoryUtilization)
}
v.mb.RecordVcenterVMMemoryUsageDataPoint(now, int64(memUsage))
v.mb.RecordVcenterVMMemoryBalloonedDataPoint(now, int64(balloonedMem))
v.mb.RecordVcenterVMMemorySwappedDataPoint(now, int64(swappedMem))
v.mb.RecordVcenterVMMemorySwappedSsdDataPoint(now, swappedSSDMem)
diskUsed := vm.Summary.Storage.Committed
diskFree := vm.Summary.Storage.Uncommitted
v.mb.RecordVcenterVMDiskUsageDataPoint(now, diskUsed, metadata.AttributeDiskStateUsed)
v.mb.RecordVcenterVMDiskUsageDataPoint(now, diskFree, metadata.AttributeDiskStateAvailable)
if diskFree != 0 {
diskUtilization := float64(diskUsed) / float64(diskFree+diskUsed) * 100
v.mb.RecordVcenterVMDiskUtilizationDataPoint(now, diskUtilization)
}
s := vm.Summary
z := s.QuickStats
cpuUsage := z.OverallCpuUsage
ncpu := vm.Config.Hardware.NumCPU
// if no cpu usage, we probably shouldn't return a value. Most likely the VM is unavailable
// or is unreachable.
if cpuUsage == 0 {
return
}
// https://communities.vmware.com/t5/VMware-code-Documents/Resource-Management/ta-p/2783456
// VirtualMachine.runtime.maxCpuUsage is a property of the virtual machine, indicating the limit value.
// This value is always equal to the limit value set for that virtual machine.
// If no limit, it has full host mhz * vm.Config.Hardware.NumCPU.
var cpuUtil float64
if vm.Runtime.MaxCpuUsage != 0 {
cpuUtil = 100 * float64(cpuUsage) / float64(vm.Runtime.MaxCpuUsage)
} else {
cpuUtil = 100 * float64(cpuUsage) / float64(ncpu*hs.Summary.Hardware.CpuMhz)
}
v.mb.RecordVcenterVMCPUUsageDataPoint(now, int64(cpuUsage))
v.mb.RecordVcenterVMCPUUtilizationDataPoint(now, cpuUtil)
}
func (v *vcenterMetricScraper) recordDatastoreProperties(
now pcommon.Timestamp,
ds mo.Datastore,
) {
s := ds.Summary
diskUsage := s.Capacity - s.FreeSpace
diskUtilization := float64(diskUsage) / float64(s.Capacity) * 100
v.mb.RecordVcenterDatastoreDiskUsageDataPoint(now, diskUsage, metadata.AttributeDiskStateUsed)
v.mb.RecordVcenterDatastoreDiskUsageDataPoint(now, s.Capacity, metadata.AttributeDiskStateUsed)
v.mb.RecordVcenterDatastoreDiskUtilizationDataPoint(now, diskUtilization)
}
func (v *vcenterMetricScraper) recordResourcePool(
now pcommon.Timestamp,
rp mo.ResourcePool,
) {
s := rp.Summary.GetResourcePoolSummary()
if s.QuickStats != nil {
v.mb.RecordVcenterResourcePoolCPUUsageDataPoint(now, s.QuickStats.OverallCpuUsage)
v.mb.RecordVcenterResourcePoolMemoryUsageDataPoint(now, s.QuickStats.GuestMemoryUsage)
}
v.mb.RecordVcenterResourcePoolCPUSharesDataPoint(now, int64(s.Config.CpuAllocation.Shares.Shares))
v.mb.RecordVcenterResourcePoolMemorySharesDataPoint(now, int64(s.Config.MemoryAllocation.Shares.Shares))
}
var hostPerfMetricList = []string{
// network metrics
"net.bytesTx.average",
"net.bytesRx.average",
"net.packetsTx.summation",
"net.packetsRx.summation",
"net.usage.average",
"net.errorsRx.summation",
"net.errorsTx.summation",
// disk metrics
"virtualDisk.totalWriteLatency.average",
"disk.deviceReadLatency.average",
"disk.deviceWriteLatency.average",
"disk.kernelReadLatency.average",
"disk.kernelWriteLatency.average",
"disk.maxTotalLatency.latest",
"disk.read.average",
"disk.write.average",
}
func (v *vcenterMetricScraper) recordHostPerformanceMetrics(
ctx context.Context,
host mo.HostSystem,
errs *scrapererror.ScrapeErrors,
) {
spec := types.PerfQuerySpec{
Entity: host.Reference(),
MaxSample: 5,
Format: string(types.PerfFormatNormal),
MetricId: []types.PerfMetricId{{Instance: "*"}},
// right now we are only grabbing real time metrics from the performance
// manager
IntervalId: int32(20),
}
info, err := v.client.performanceQuery(ctx, spec, hostPerfMetricList, []types.ManagedObjectReference{host.Reference()})
if err != nil {
errs.AddPartial(1, err)
return
}
v.processHostPerformance(info.results)
}
// vmPerfMetricList may be customizable in the future but here is the full list of Virtual Machine Performance Counters
// https://docs.vmware.com/en/vRealize-Operations/8.6/com.vmware.vcom.metrics.doc/GUID-1322F5A4-DA1D-481F-BBEA-99B228E96AF2.html
var vmPerfMetricList = []string{
// network metrics
"net.packetsTx.summation",
"net.packetsRx.summation",
"net.bytesRx.average",
"net.bytesTx.average",
"net.usage.average",
// disk metrics
"disk.totalWriteLatency.average",
"disk.totalReadLatency.average",
"disk.maxTotalLatency.latest",
"virtualDisk.totalWriteLatency.average",
"virtualDisk.totalReadLatency.average",
}
func (v *vcenterMetricScraper) recordVMPerformance(
ctx context.Context,
vm mo.VirtualMachine,
errs *scrapererror.ScrapeErrors,
) {
spec := types.PerfQuerySpec{
Entity: vm.Reference(),
Format: string(types.PerfFormatNormal),
// Just grabbing real time performance metrics of the current
// supported metrics by this receiver. If more are added we may need
// a system of making this user customizable or adapt to use a 5 minute interval per metric
IntervalId: int32(20),
}
info, err := v.client.performanceQuery(ctx, spec, vmPerfMetricList, []types.ManagedObjectReference{vm.Reference()})
if err != nil {
errs.AddPartial(1, err)
return
}
v.processVMPerformanceMetrics(info)
}
func (v *vcenterMetricScraper) processVMPerformanceMetrics(info *perfSampleResult) {
for _, m := range info.results {
for _, val := range m.Value {
for j, nestedValue := range val.Value {
si := m.SampleInfo[j]
switch val.Name {
// Performance monitoring level 1 metrics
case "net.bytesTx.average":
v.mb.RecordVcenterVMNetworkThroughputDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionTransmitted)
case "net.bytesRx.average":
v.mb.RecordVcenterVMNetworkThroughputDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionReceived)
case "net.usage.average":
v.mb.RecordVcenterVMNetworkUsageDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue)
case "net.packetsTx.summation":
v.mb.RecordVcenterVMNetworkPacketCountDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionTransmitted)
case "net.packetsRx.summation":
v.mb.RecordVcenterVMNetworkPacketCountDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionReceived)
// Performance monitoring level 2 metrics required
case "disk.totalReadLatency.average":
v.mb.RecordVcenterVMDiskLatencyAvgDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionRead, metadata.AttributeDiskTypePhysical)
case "virtualDisk.totalReadLatency.average":
v.mb.RecordVcenterVMDiskLatencyAvgDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionRead, metadata.AttributeDiskTypeVirtual)
case "disk.totalWriteLatency.average":
v.mb.RecordVcenterVMDiskLatencyAvgDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionWrite, metadata.AttributeDiskTypePhysical)
case "virtualDisk.totalWriteLatency.average":
v.mb.RecordVcenterVMDiskLatencyAvgDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionWrite, metadata.AttributeDiskTypeVirtual)
case "disk.maxTotalLatency.latest":
v.mb.RecordVcenterVMDiskLatencyMaxDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue)
}
}
}
}
}
func (v *vcenterMetricScraper) processHostPerformance(metrics []performance.EntityMetric) {
for _, m := range metrics {
for _, val := range m.Value {
for j, nestedValue := range val.Value {
si := m.SampleInfo[j]
switch val.Name {
// Performance monitoring level 1 metrics
case "net.usage.average":
v.mb.RecordVcenterHostNetworkUsageDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue)
case "net.bytesTx.average":
v.mb.RecordVcenterHostNetworkThroughputDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionTransmitted)
case "net.bytesRx.average":
v.mb.RecordVcenterHostNetworkThroughputDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionReceived)
case "net.packetsTx.summation":
v.mb.RecordVcenterHostNetworkPacketCountDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionTransmitted)
case "net.packetsRx.summation":
v.mb.RecordVcenterHostNetworkPacketCountDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionReceived)
// Following requires performance level 2
case "net.errorsRx.summation":
v.mb.RecordVcenterHostNetworkPacketErrorsDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionReceived)
case "net.errorsTx.summation":
v.mb.RecordVcenterHostNetworkPacketErrorsDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeThroughputDirectionTransmitted)
case "disk.totalWriteLatency.average":
v.mb.RecordVcenterHostDiskLatencyAvgDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionWrite)
case "disk.totalReadLatency.average":
v.mb.RecordVcenterHostDiskLatencyAvgDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionRead)
case "disk.maxTotalLatency.latest":
v.mb.RecordVcenterHostDiskLatencyMaxDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue)
// Following requires performance level 4
case "disk.read.average":
v.mb.RecordVcenterHostDiskThroughputDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionRead)
case "disk.write.average":
v.mb.RecordVcenterHostDiskThroughputDataPoint(pcommon.NewTimestampFromTime(si.Timestamp), nestedValue, metadata.AttributeDiskDirectionWrite)
}
}
}
}
}