forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
295 lines (249 loc) · 8.43 KB
/
host.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
285
286
287
288
289
290
291
292
293
294
295
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package host // import "go.opentelemetry.io/contrib/instrumentation/host"
import (
"context"
"errors"
"fmt"
"math"
"os"
"sync"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
"github.com/shirou/gopsutil/v4/process"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
// ScopeName is the instrumentation scope name.
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/host"
// Host reports the work-in-progress conventional host metrics specified by OpenTelemetry.
type host struct {
config config
meter metric.Meter
}
// config contains optional settings for reporting host metrics.
type config struct {
// MeterProvider sets the metric.MeterProvider. If nil, the global
// Provider will be used.
MeterProvider metric.MeterProvider
}
// Option supports configuring optional settings for host metrics.
type Option interface {
apply(*config)
}
// WithMeterProvider sets the Metric implementation to use for
// reporting. If this option is not used, the global metric.MeterProvider
// will be used. `provider` must be non-nil.
func WithMeterProvider(provider metric.MeterProvider) Option {
return metricProviderOption{provider}
}
type metricProviderOption struct{ metric.MeterProvider }
func (o metricProviderOption) apply(c *config) {
if o.MeterProvider != nil {
c.MeterProvider = o.MeterProvider
}
}
// Attribute sets.
var (
// Attribute sets for CPU time measurements.
AttributeCPUTimeUser = attribute.NewSet(attribute.String("state", "user"))
AttributeCPUTimeSystem = attribute.NewSet(attribute.String("state", "system"))
AttributeCPUTimeOther = attribute.NewSet(attribute.String("state", "other"))
AttributeCPUTimeIdle = attribute.NewSet(attribute.String("state", "idle"))
// Attribute sets used for Memory measurements.
AttributeMemoryAvailable = attribute.NewSet(attribute.String("state", "available"))
AttributeMemoryUsed = attribute.NewSet(attribute.String("state", "used"))
// Attribute sets used for Network measurements.
AttributeNetworkTransmit = attribute.NewSet(attribute.String("direction", "transmit"))
AttributeNetworkReceive = attribute.NewSet(attribute.String("direction", "receive"))
)
// newConfig computes a config from a list of Options.
func newConfig(opts ...Option) config {
c := config{
MeterProvider: otel.GetMeterProvider(),
}
for _, opt := range opts {
opt.apply(&c)
}
return c
}
// Start initializes reporting of host metrics using the supplied config.
func Start(opts ...Option) error {
c := newConfig(opts...)
if c.MeterProvider == nil {
c.MeterProvider = otel.GetMeterProvider()
}
h := &host{
meter: c.MeterProvider.Meter(
ScopeName,
metric.WithInstrumentationVersion(Version()),
),
config: c,
}
return h.register()
}
func (h *host) register() error {
var (
err error
processCPUTime metric.Float64ObservableCounter
hostCPUTime metric.Float64ObservableCounter
hostMemoryUsage metric.Int64ObservableGauge
hostMemoryUtilization metric.Float64ObservableGauge
networkIOUsage metric.Int64ObservableCounter
// lock prevents a race between batch observer and instrument registration.
lock sync.Mutex
)
pid := os.Getpid()
if pid > math.MaxInt32 || pid < math.MinInt32 {
return fmt.Errorf("invalid process ID: %d", pid)
}
proc, err := process.NewProcess(int32(pid)) // nolint: gosec // Overflow checked above.
if err != nil {
return fmt.Errorf("could not find this process: %w", err)
}
lock.Lock()
defer lock.Unlock()
// TODO: .time units are in seconds, but "unit" package does
// not include this string.
// https://github.com/open-telemetry/opentelemetry-specification/issues/705
if processCPUTime, err = h.meter.Float64ObservableCounter(
"process.cpu.time",
metric.WithUnit("s"),
metric.WithDescription(
"Accumulated CPU time spent by this process attributed by state (User, System, ...)",
),
); err != nil {
return err
}
if hostCPUTime, err = h.meter.Float64ObservableCounter(
"system.cpu.time",
metric.WithUnit("s"),
metric.WithDescription(
"Accumulated CPU time spent by this host attributed by state (User, System, Other, Idle)",
),
); err != nil {
return err
}
if hostMemoryUsage, err = h.meter.Int64ObservableGauge(
"system.memory.usage",
metric.WithUnit("By"),
metric.WithDescription(
"Memory usage of this process attributed by memory state (Used, Available)",
),
); err != nil {
return err
}
if hostMemoryUtilization, err = h.meter.Float64ObservableGauge(
"system.memory.utilization",
metric.WithUnit("1"),
metric.WithDescription(
"Memory utilization of this process attributed by memory state (Used, Available)",
),
); err != nil {
return err
}
if networkIOUsage, err = h.meter.Int64ObservableCounter(
"system.network.io",
metric.WithUnit("By"),
metric.WithDescription(
"Bytes transferred attributed by direction (Transmit, Receive)",
),
); err != nil {
return err
}
_, err = h.meter.RegisterCallback(
func(ctx context.Context, o metric.Observer) error {
lock.Lock()
defer lock.Unlock()
// This follows the OpenTelemetry Collector's "hostmetrics"
// receiver/hostmetricsreceiver/internal/scraper/processscraper
// measures User and System IOwait time.
// TODO: the Collector has per-OS compilation modules to support
// specific metrics that are not universal.
processTimes, err := proc.TimesWithContext(ctx)
if err != nil {
return err
}
hostTimeSlice, err := cpu.TimesWithContext(ctx, false)
if err != nil {
return err
}
if len(hostTimeSlice) != 1 {
return errors.New("host CPU usage: incorrect summary count")
}
vmStats, err := mem.VirtualMemoryWithContext(ctx)
if err != nil {
return err
}
ioStats, err := net.IOCountersWithContext(ctx, false)
if err != nil {
return err
}
if len(ioStats) != 1 {
return errors.New("host network usage: incorrect summary count")
}
hostTime := hostTimeSlice[0]
opt := metric.WithAttributeSet(AttributeCPUTimeUser)
o.ObserveFloat64(processCPUTime, processTimes.User, opt)
o.ObserveFloat64(hostCPUTime, hostTime.User, opt)
opt = metric.WithAttributeSet(AttributeCPUTimeSystem)
o.ObserveFloat64(processCPUTime, processTimes.System, opt)
o.ObserveFloat64(hostCPUTime, hostTime.System, opt)
// TODO(#244): "other" is a placeholder for actually dealing
// with these states. Do users actually want this
// (unconditionally)? How should we handle "iowait"
// if not all systems expose it? Should we break
// these down by CPU? If so, are users going to want
// to aggregate in-process? See:
// https://github.com/open-telemetry/opentelemetry-go-contrib/issues/244
other := hostTime.Nice +
hostTime.Iowait +
hostTime.Irq +
hostTime.Softirq +
hostTime.Steal +
hostTime.Guest +
hostTime.GuestNice
opt = metric.WithAttributeSet(AttributeCPUTimeOther)
o.ObserveFloat64(hostCPUTime, other, opt)
opt = metric.WithAttributeSet(AttributeCPUTimeIdle)
o.ObserveFloat64(hostCPUTime, hostTime.Idle, opt)
// Host memory usage
opt = metric.WithAttributeSet(AttributeMemoryUsed)
o.ObserveInt64(hostMemoryUsage, clampInt64(vmStats.Used), opt)
opt = metric.WithAttributeSet(AttributeMemoryAvailable)
o.ObserveInt64(hostMemoryUsage, clampInt64(vmStats.Available), opt)
// Host memory utilization
opt = metric.WithAttributeSet(AttributeMemoryUsed)
o.ObserveFloat64(hostMemoryUtilization, float64(vmStats.Used)/float64(vmStats.Total), opt)
opt = metric.WithAttributeSet(AttributeMemoryAvailable)
o.ObserveFloat64(hostMemoryUtilization, float64(vmStats.Available)/float64(vmStats.Total), opt)
// Host network usage
//
// TODO: These can be broken down by network
// interface, with similar questions to those posed
// about per-CPU measurements above.
opt = metric.WithAttributeSet(AttributeNetworkTransmit)
o.ObserveInt64(networkIOUsage, clampInt64(ioStats[0].BytesSent), opt)
opt = metric.WithAttributeSet(AttributeNetworkReceive)
o.ObserveInt64(networkIOUsage, clampInt64(ioStats[0].BytesRecv), opt)
return nil
},
processCPUTime,
hostCPUTime,
hostMemoryUsage,
hostMemoryUtilization,
networkIOUsage,
)
if err != nil {
return err
}
return nil
}
func clampInt64(v uint64) int64 {
if v > math.MaxInt64 {
return math.MaxInt64
}
return int64(v) // nolint: gosec // Overflow checked.
}