-
Notifications
You must be signed in to change notification settings - Fork 0
/
otelruntime.go
66 lines (57 loc) · 1.7 KB
/
otelruntime.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
package otelruntime
import (
"runtime"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/metric"
)
// ProcessKey is the key that differentiates
// the metric type because only one metric is exported
// for all stats. Use this key to switch between the metrics
// below
var ProcessKey = key.New("go_process")
// tag keys
const (
Goroutines = "cpu_goroutines"
Heap = "heap_alloc"
Pause = "pause_ns"
SysHeap = "sys_heap"
)
// Config for the runtime reporter
type Config struct {
meter metric.Meter
name string
}
// Option allows changes to the Config
type Option func(c *Config)
// WithMeter sets the meter that the runtime reporter will use
// otherwise global.Meter is used by default.
func WithMeter(meter metric.Meter) Option {
return func(c *Config) {
c.meter = meter
}
}
// WithMetricName customizes the exported metric name or otherwise
// will be called go_runtime by default.
func WithMetricName(name string) Option {
return func(c *Config) {
c.name = name
}
}
// Register registers an infinite loop that
// profiles the process and reports the views
func Register(options ...Option) {
c := &Config{meter: global.Meter("otelruntime"), name: "go_runtime"}
for _, o := range options {
o(c)
}
mm := metric.Must(c.meter)
mm.RegisterInt64Observer(c.name, func(result metric.Int64ObserverResult) {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
result.Observe(int64(runtime.NumCPU()), ProcessKey.String(Goroutines))
result.Observe(int64(ms.HeapAlloc), ProcessKey.String(Heap))
result.Observe(int64(ms.PauseNs[(ms.NumGC+255)%256]), ProcessKey.String(Pause))
result.Observe(int64(ms.Sys), ProcessKey.String(SysHeap))
})
}