/
cputime.go
134 lines (114 loc) 路 3.57 KB
/
cputime.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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cputime // import "github.com/lightstep/otel-launcher-go/lightstep/instrumentation/cputime"
import (
"context"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
// processStartTime should be initialized before the first GC, ideally.
var processStartTime = time.Now()
// 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 = []metric.ObserveOption{
metric.WithAttributes(attribute.String("state", "user")),
}
AttributeCPUTimeSystem = []metric.ObserveOption{
metric.WithAttributes(attribute.String("state", "system")),
}
)
// 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 {
cfg := newConfig(opts...)
if cfg.MeterProvider == nil {
cfg.MeterProvider = otel.GetMeterProvider()
}
c, err := newCputime(cfg)
if err != nil {
return err
}
return c.register()
}
func (c *cputime) register() error {
var (
err error
processCPUTime metric.Float64ObservableCounter
processUptime metric.Float64ObservableUpDownCounter
)
if processCPUTime, err = c.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 processUptime, err = c.meter.Float64ObservableUpDownCounter(
"process.uptime",
metric.WithUnit("s"),
metric.WithDescription("Seconds since application was initialized"),
); err != nil {
return err
}
_, err = c.meter.RegisterCallback(
func(ctx context.Context, obs metric.Observer) error {
processUser, processSystem, uptime := c.getProcessTimes(ctx)
// Uptime
obs.ObserveFloat64(processUptime, uptime)
// Process CPU time
obs.ObserveFloat64(processCPUTime, processUser, AttributeCPUTimeUser...)
obs.ObserveFloat64(processCPUTime, processSystem, AttributeCPUTimeSystem...)
return nil
},
processCPUTime,
processUptime,
)
return err
}