/
runtime.go
176 lines (156 loc) · 5.5 KB
/
runtime.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"runtime"
"strconv"
"sync/atomic"
"time"
"github.com/uber-go/tally"
"github.com/uber/cadence/common/log"
)
var (
// Revision is the VCS revision associated with this build. Overridden using ldflags
// at compile time. Example:
// $ go build -ldflags "-X github.com/uber/cadence/common/metrics.Revision=abcdef" ...
// see go-build-ldflags.sh for GIT_REVISION
Revision = "unknown"
// Branch is the VCS branch associated with this build.
Branch = "unknown"
// ReleaseVersion is the version associated with this build.
ReleaseVersion = "unknown"
// BuildDate is the date this build was created.
BuildDate = "unknown"
// BuildTimeUnix is the seconds since epoch representing the date this build was created.
BuildTimeUnix = "0"
// goVersion is the current runtime version.
goVersion = runtime.Version()
)
const (
// buildInfoMetricName is the emitted build information metric's name.
buildInfoMetricName = "build_information"
// buildAgeMetricName is the emitted build age metric's name.
buildAgeMetricName = "build_age"
)
// RuntimeMetricsReporter A struct containing the state of the RuntimeMetricsReporter.
type RuntimeMetricsReporter struct {
scope tally.Scope
buildInfoScope tally.Scope
reportInterval time.Duration
started int32
quit chan struct{}
logger log.Logger
lastNumGC uint32
buildTime time.Time
}
// NewRuntimeMetricsReporter Creates a new RuntimeMetricsReporter.
func NewRuntimeMetricsReporter(
scope tally.Scope,
reportInterval time.Duration,
logger log.Logger,
instanceID string,
) *RuntimeMetricsReporter {
const (
base = 10
bitSize = 64
)
if len(instanceID) > 0 {
scope = scope.Tagged(map[string]string{instance: instanceID})
}
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
rReporter := &RuntimeMetricsReporter{
scope: scope,
reportInterval: reportInterval,
logger: logger,
lastNumGC: memstats.NumGC,
quit: make(chan struct{}),
}
rReporter.buildInfoScope = scope.Tagged(
map[string]string{
revisionTag: Revision,
branchTag: Branch,
buildDateTag: BuildDate,
buildVersionTag: ReleaseVersion,
goVersionTag: goVersion,
},
)
sec, err := strconv.ParseInt(BuildTimeUnix, base, bitSize)
if err != nil || sec < 0 {
sec = 0
}
rReporter.buildTime = time.Unix(sec, 0)
return rReporter
}
// report Sends runtime metrics to the local metrics collector.
func (r *RuntimeMetricsReporter) report() {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
r.scope.Gauge(NumGoRoutinesGauge).Update(float64(runtime.NumGoroutine()))
r.scope.Gauge(GoMaxProcsGauge).Update(float64(runtime.GOMAXPROCS(0)))
r.scope.Gauge(MemoryAllocatedGauge).Update(float64(memStats.Alloc))
r.scope.Gauge(MemoryHeapGauge).Update(float64(memStats.HeapAlloc))
r.scope.Gauge(MemoryHeapIdleGauge).Update(float64(memStats.HeapIdle))
r.scope.Gauge(MemoryHeapInuseGauge).Update(float64(memStats.HeapInuse))
r.scope.Gauge(MemoryStackGauge).Update(float64(memStats.StackInuse))
// memStats.NumGC is a perpetually incrementing counter (unless it wraps at 2^32)
num := memStats.NumGC
lastNum := atomic.SwapUint32(&r.lastNumGC, num) // reset for the next iteration
if delta := num - lastNum; delta > 0 {
r.scope.Counter(NumGCCounter).Inc(int64(delta))
if delta > 255 {
// too many GCs happened, the timestamps buffer got wrapped around. Report only the last 256
lastNum = num - 256
}
for i := lastNum; i != num; i++ {
pause := memStats.PauseNs[i%256]
r.scope.Timer(GcPauseMsTimer).Record(time.Duration(pause))
}
}
// report build info
buildInfoGauge := r.buildInfoScope.Gauge(buildInfoMetricName)
buildAgeGauge := r.buildInfoScope.Gauge(buildAgeMetricName)
buildInfoGauge.Update(1.0)
buildAgeGauge.Update(float64(time.Since(r.buildTime)))
}
// Start Starts the reporter thread that periodically emits metrics.
func (r *RuntimeMetricsReporter) Start() {
if !atomic.CompareAndSwapInt32(&r.started, 0, 1) {
return
}
go func() {
ticker := time.NewTicker(r.reportInterval)
for {
select {
case <-ticker.C:
r.report()
case <-r.quit:
ticker.Stop()
return
}
}
}()
r.logger.Info("RuntimeMetricsReporter started")
}
// Stop Stops reporting of runtime metrics. The reporter cannot be started again after it's been stopped.
func (r *RuntimeMetricsReporter) Stop() {
close(r.quit)
r.logger.Info("RuntimeMetricsReporter stopped")
}