-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapptelemetry.go
210 lines (174 loc) · 4.9 KB
/
apptelemetry.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
package gocbcore
import (
"time"
)
// TelemetryReporterConfig specifies options related to the ns_server telemetry reporter
// Volatile: This API is subject to change at any time.
type TelemetryReporterConfig struct {
// ExternalEndpoint overrides the reporting endpoint discovered through the cluster config.
ExternalEndpoint string
// Backoff specifies the duration to wait before attempting to reconnect to a telemetry endpoint.
// Defaults to 5 seconds.
Backoff time.Duration
// PingInterval specifies the time duration to wait between consecutive PING commands to the app telemetry server.
// Defaults to 30 seconds.
PingInterval time.Duration
// PingTimeout specifies the time to allow the server to respond to a PING, before closing the connection and
// attempting to reconnect.
// Defaults to 2 seconds.
PingTimeout time.Duration
}
type telemetryClient interface {
connectIfNotStarted()
connectionInitiated() bool
Close()
usesExternalEndpoint() bool
updateEndpoints(endpoints telemetryEndpoints)
}
type TelemetryReporter struct {
client telemetryClient
metrics telemetryStore
}
func (t *TelemetryReporter) started() bool {
if t.client != nil {
return t.client.connectionInitiated()
}
return false
}
func (t *TelemetryReporter) maybeStart() {
if t.client != nil {
t.client.connectIfNotStarted()
}
}
func (t *TelemetryReporter) Close() {
if t.client != nil {
logDebugf("Closing telemetry reporter")
t.client.Close()
}
}
func (t *TelemetryReporter) updateEndpoints(endpoints telemetryEndpoints) {
if t.client != nil {
t.client.updateEndpoints(endpoints)
}
}
func (t *TelemetryReporter) recordOp(attributes telemetryOperationAttributes) {
t.metrics.recordOp(attributes)
}
// exportMetrics returns the serialized metrics and resets all metrics histograms and counters.
func (t *TelemetryReporter) exportMetrics() string {
return t.metrics.serialize()
}
// CreateTelemetryReporter creates a new app telemetry reporter.
func CreateTelemetryReporter(cfg TelemetryReporterConfig) *TelemetryReporter {
t := &TelemetryReporter{
metrics: newTelemetryMetrics(),
}
client := newTelemetryWebsocketClient(cfg.Backoff, cfg.PingInterval, cfg.PingTimeout, func() string {
return t.exportMetrics()
})
if cfg.ExternalEndpoint != "" {
client.fixedEndpoints = true
eps := telemetryEndpoints{
epList: []routeEndpoint{{Address: cfg.ExternalEndpoint}},
}
client.endpoints = eps
client.connectIfNotStarted()
}
t.client = client
return t
}
type telemetryComponent struct {
reporter *TelemetryReporter
cfgMgr configManager
agent string
bucketName string
auth AuthProvider
tlsConfig *dynTLSConfig
}
type telemetryComponentProps struct {
reporter *TelemetryReporter
agent string
bucketName string
cfgMgr configManager
auth AuthProvider
tlsConfig *dynTLSConfig
}
func newTelemetryComponent(props telemetryComponentProps) *telemetryComponent {
tc := &telemetryComponent{
cfgMgr: props.cfgMgr,
agent: props.agent,
bucketName: props.bucketName,
auth: props.auth,
tlsConfig: props.tlsConfig,
}
if props.reporter != nil {
tc.reporter = props.reporter
}
if tc.reporter != nil {
tc.cfgMgr.AddConfigWatcher(tc)
}
return tc
}
func (tc *telemetryComponent) TelemetryEnabled() bool {
return tc != nil && tc.reporter != nil
}
func (tc *telemetryComponent) OnNewRouteConfig(cfg *routeConfig) {
eps := telemetryEndpoints{
tlsConfig: tc.tlsConfig,
auth: tc.auth,
}
if tc.tlsConfig != nil {
eps.epList = cfg.appTelemetryEpList.SSLEndpoints
} else {
eps.epList = cfg.appTelemetryEpList.NonSSLEndpoints
}
tc.reporter.updateEndpoints(eps)
if len(eps.epList) > 0 {
// This calls telemetryClient.connectIfNotStarted which does the connection asynchronously.
// Connecting must not block this goroutine.
tc.reporter.maybeStart()
}
}
func (tc *telemetryComponent) RecordOp(attributes telemetryOperationAttributes) {
if tc.reporter == nil {
return
}
if !tc.reporter.started() {
return
}
attributes.agent = tc.agent
if attributes.service == MemdService {
attributes.bucket = tc.bucketName
}
tc.reporter.recordOp(attributes)
}
func (tc *telemetryComponent) GetRecorder(attributes telemetryOperationAttributes) *telemetryOpRecorder {
if tc.reporter == nil || !tc.reporter.started() {
return nil
}
attributes.bucket = tc.bucketName
attributes.agent = tc.agent
return &telemetryOpRecorder{
reporter: tc.reporter,
attributes: attributes,
}
}
type telemetryOpRecorder struct {
reporter *TelemetryReporter
attributes telemetryOperationAttributes
startTime time.Time
}
func (tr *telemetryOpRecorder) StartLocked() {
if tr.reporter == nil {
return
}
tr.startTime = time.Now()
}
func (tr *telemetryOpRecorder) FinishAndRecordLocked(outcome telemetryOutcome) {
if tr.reporter == nil {
return
}
tr.attributes.outcome = outcome
tr.attributes.duration = time.Since(tr.startTime)
tr.reporter.recordOp(tr.attributes)
}