-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmeter.go
More file actions
163 lines (139 loc) · 4.47 KB
/
Copy pathmeter.go
File metadata and controls
163 lines (139 loc) · 4.47 KB
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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2016
package instana
import (
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/instana/go-sensor/acceptor"
)
const (
// defaultTransmissionInterval is the fallback metrics transmission interval in seconds.
defaultTransmissionInterval = 1
)
// SnapshotS struct to hold snapshot data
type SnapshotS acceptor.RuntimeInfo
// MemoryS struct to hold snapshot data
type MemoryS acceptor.MemoryStats
// MetricsS struct to hold snapshot data
type MetricsS acceptor.Metrics
// EntityData struct to hold snapshot data
type EntityData acceptor.GoProcessData
type meterS struct {
numGC atomic.Uint32
once sync.Once
stopOnce sync.Once
done chan struct{}
}
// MetricsOptions contains configuration for metrics collection and transmission.
// This configuration is managed internally and populated from agent configuration.
type MetricsOptions struct {
mu sync.RWMutex
transmissionInterval time.Duration
}
// getTransmissionInterval returns the current metrics transmission interval.
// This value is configured through the agent's configuration.yaml file.
func (m *MetricsOptions) getTransmissionInterval() time.Duration {
m.mu.RLock()
defer m.mu.RUnlock()
return m.transmissionInterval
}
// setTransmissionInterval sets the metrics transmission interval.
// This is an internal method called when agent configuration is received during
// the initial handshake. The only local constraint enforced here is that the value
// must be positive (> 0); range and canonical-set validation is the responsibility
// of the Instana Agent. Non-positive values fall back to defaultTransmissionInterval.
func (m *MetricsOptions) setTransmissionInterval(seconds int) {
var interval time.Duration
if seconds <= 0 {
defaultLogger.Error("poll_rate value from agent (", seconds, ") is not positive. Using default of ",
defaultTransmissionInterval, " second.")
interval = defaultTransmissionInterval * time.Second
} else {
interval = time.Duration(seconds) * time.Second
defaultLogger.Info("Metrics transmission interval set to ", seconds, " second(s) from agent configuration")
}
m.mu.Lock()
defer m.mu.Unlock()
m.transmissionInterval = interval
}
func newMeter(logger LeveledLogger) *meterS {
logger.Debug("initializing meter")
return &meterS{
done: make(chan struct{}),
}
}
// Run starts the metrics collection loop at the given interval.
// It is safe to call Run multiple times — only the first call starts the loop;
// subsequent calls (e.g. on agent reconnect) are ignored so the running loop
// continues uninterrupted with the original interval.
// The interval is fixed at the first call; changing poll_rate in the agent
// configuration after startup requires an application restart to take effect.
func (m *meterS) Run(collectInterval time.Duration) {
if m == nil {
return
}
m.once.Do(func() {
go func() {
ticker := time.NewTicker(collectInterval)
defer ticker.Stop()
for {
select {
case <-m.done:
return
case <-ticker.C:
if isAgentReady() {
go func() {
s, err := getSensor()
if err != nil {
defaultLogger.Error("meter: ", err.Error())
return
}
_ = s.Agent().SendMetrics(m.collectMetrics())
}()
}
}
}
}()
})
}
// Stop shuts down the metrics collection loop. Safe to call multiple times.
func (m *meterS) Stop() {
if m == nil {
return
}
m.stopOnce.Do(func() { close(m.done) })
}
func (m *meterS) collectMemoryMetrics() acceptor.MemoryStats {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
ret := acceptor.MemoryStats{
Alloc: memStats.Alloc,
TotalAlloc: memStats.TotalAlloc,
Sys: memStats.Sys,
Lookups: memStats.Lookups,
Mallocs: memStats.Mallocs,
Frees: memStats.Frees,
HeapAlloc: memStats.HeapAlloc,
HeapSys: memStats.HeapSys,
HeapIdle: memStats.HeapIdle,
HeapInuse: memStats.HeapInuse,
HeapReleased: memStats.HeapReleased,
HeapObjects: memStats.HeapObjects,
PauseTotalNs: memStats.PauseTotalNs,
NumGC: memStats.NumGC,
GCCPUFraction: memStats.GCCPUFraction}
if m.numGC.Load() < memStats.NumGC {
ret.PauseNs = memStats.PauseNs[(memStats.NumGC+255)%256]
m.numGC.Store(memStats.NumGC)
}
return ret
}
func (m *meterS) collectMetrics() acceptor.Metrics {
return acceptor.Metrics{
CgoCall: runtime.NumCgoCall(),
Goroutine: runtime.NumGoroutine(),
MemoryStats: m.collectMemoryMetrics(),
}
}