-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry.go
324 lines (281 loc) · 8.7 KB
/
telemetry.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package telemetry
import (
"context"
"fmt"
"io"
"net/url"
"sort"
"sync"
"time"
logging "github.com/ipfs/go-log/v2"
"github.com/ipni/go-libipni/find/model"
"github.com/ipni/go-libipni/pcache"
"github.com/ipni/ipni-cli/pkg/dtrack"
"github.com/ipni/telemetry/metrics"
"github.com/libp2p/go-libp2p/core/peer"
)
var log = logging.Logger("telemetry")
// Number of buckest is 3: small, medium, large
const nbuckets = 3
// Time to clear any errors not cleared by update.
const errorRefreshTime = 5 * time.Minute
type Telemetry struct {
adDepthLimit int64
cancel context.CancelFunc
dist map[peer.ID]int64
metrics *metrics.Metrics
rwmutex sync.RWMutex
done chan struct{}
pcache *pcache.ProviderCache
indexerURLs []*url.URL
nSlowest int
slowRate int64
distBuckets []int64
bucketIndexes map[peer.ID]int
}
func New(adDepthLimit int64, updateIn, updateTo time.Duration, pc *pcache.ProviderCache, met *metrics.Metrics, indexerAdminURLs []string, slowRate, nSlowest int, topic string) (*Telemetry, error) {
indexerURLs := make([]*url.URL, len(indexerAdminURLs))
for i, urlStr := range indexerAdminURLs {
var err error
indexerURLs[i], err = parseIndexerBaseURL(urlStr)
if err != nil {
return nil, err
}
}
tel := &Telemetry{
adDepthLimit: adDepthLimit,
dist: map[peer.ID]int64{},
done: make(chan struct{}),
metrics: met,
pcache: pc,
indexerURLs: indexerURLs,
nSlowest: nSlowest,
slowRate: int64(slowRate),
distBuckets: make([]int64, 3),
bucketIndexes: map[peer.ID]int{},
}
var include, exclude map[peer.ID]struct{}
ctx, cancel := context.WithCancel(context.Background())
updates, err := dtrack.RunDistanceTracker(ctx, include, exclude, pc, updateIn, updateTo,
dtrack.WithDepthLimit(adDepthLimit), dtrack.WithTopic(topic))
if err != nil {
cancel()
return nil, err
}
tel.cancel = cancel
go tel.run(ctx, updates)
return tel, nil
}
func (tel *Telemetry) Close() {
tel.cancel()
<-tel.done
tel.rwmutex.Lock()
tel.pcache = nil
tel.rwmutex.Unlock()
}
func (tel *Telemetry) run(ctx context.Context, updates <-chan dtrack.DistanceUpdate) {
defer close(tel.done)
errTimer := time.NewTimer(errorRefreshTime)
defer errTimer.Stop()
errored := make(map[peer.ID]error)
rateMap := map[peer.ID]*metrics.IngestRate{}
var distSum int64
for update := range updates {
select {
case <-errTimer.C:
tel.refreshErrors(ctx, errored)
errTimer.Reset(errorRefreshTime)
default:
}
if update.Err != nil {
log.Infow("Error getting distance", "provider", update.ID, "err", update.Err)
if prevErr, ok := errored[update.ID]; ok {
if prevErr.Error() != update.Err.Error() {
tel.metrics.NotifyProviderErrorCleared(ctx, prevErr)
errored[update.ID] = update.Err
tel.metrics.NotifyProviderErrored(ctx, update.Err)
log.Infow("Provider error updated", "provider", update.ID, "totalErroring", len(errored))
}
} else {
errored[update.ID] = update.Err
tel.metrics.NotifyProviderErrored(ctx, update.Err)
log.Infow("Provider marked as erroring", "provider", update.ID, "totalErroring", len(errored))
}
prevLen := len(rateMap)
delete(rateMap, update.ID)
if len(rateMap) != prevLen {
tel.updateIngestRates(rateMap)
}
distSum = tel.removeProviderFromDistBucket(ctx, update.ID, distSum)
} else {
if err, ok := errored[update.ID]; ok {
tel.metrics.NotifyProviderErrorCleared(ctx, err)
delete(errored, update.ID)
log.Infow("Provider error cleared", "provider", update.ID, "totalErroring", len(errored))
}
distSum = tel.updateDistBuckets(ctx, update.ID, int64(update.Distance), distSum)
}
// Get latest ingestion rate from indexer.
irate, ok, err := getIndexerTelemetry(ctx, tel.indexerURLs, update.ID)
if err != nil {
log.Errorw("Cannot get ingest rate information for provider", "err", err, "provider", update.ID)
continue
}
if !ok {
// No new rate data since last query,
continue
}
ingestRate := &metrics.IngestRate{
AvgMhPerAd: int64(irate.Count) / int64(irate.Samples),
MhPerSec: int64(float64(irate.Count) / irate.Elapsed.Seconds()),
ProviderID: update.ID,
}
rateMap[update.ID] = ingestRate
tel.updateIngestRates(rateMap)
}
}
func (tel *Telemetry) refreshErrors(ctx context.Context, errored map[peer.ID]error) {
provs := tel.pcache.List()
for _, pinfo := range provs {
if pinfo.LastError == "" {
if err, ok := errored[pinfo.AddrInfo.ID]; ok {
tel.metrics.NotifyProviderErrorCleared(ctx, err)
delete(errored, pinfo.AddrInfo.ID)
}
}
}
}
func (tel *Telemetry) updateDistBuckets(ctx context.Context, peerID peer.ID, distance, distSum int64) int64 {
if distance == -1 {
log.Infow("Distance update", "provider", peerID, "distanceExceeds", tel.adDepthLimit)
distance = tel.adDepthLimit
} else {
log.Infow("Distance update", "provider", peerID, "distance", distance)
}
bucketIndex, ok := tel.bucketIndexes[peerID]
if ok {
tel.distBuckets[bucketIndex]--
}
// bucketIndex = distance / bucketSize
bucketIndex = int(distance * nbuckets / tel.adDepthLimit)
if bucketIndex >= len(tel.distBuckets) {
bucketIndex = len(tel.distBuckets) - 1
}
tel.distBuckets[bucketIndex]++
tel.bucketIndexes[peerID] = bucketIndex
tel.rwmutex.Lock()
prevDist := tel.dist[peerID]
if prevDist != distance {
tel.dist[peerID] = distance
distSum += distance - prevDist
}
tel.rwmutex.Unlock()
tel.metrics.UpdateProviderDistanceBuckets(ctx, tel.distBuckets[0], tel.distBuckets[1], tel.distBuckets[2], distSum)
return distSum
}
func (tel *Telemetry) removeProviderFromDistBucket(ctx context.Context, peerID peer.ID, distSum int64) int64 {
bucketIndex, ok := tel.bucketIndexes[peerID]
if ok {
tel.distBuckets[bucketIndex]--
delete(tel.bucketIndexes, peerID)
}
tel.rwmutex.Lock()
prevDist, ok := tel.dist[peerID]
if ok {
distSum -= prevDist
delete(tel.dist, peerID)
}
tel.rwmutex.Unlock()
tel.metrics.UpdateProviderDistanceBuckets(ctx, tel.distBuckets[0], tel.distBuckets[1], tel.distBuckets[2], distSum)
return distSum
}
func (tel *Telemetry) updateIngestRates(rateMap map[peer.ID]*metrics.IngestRate) {
rates := make([]*metrics.IngestRate, len(rateMap))
var i, slowCount int
var totalMhPerSec int64
for _, ingestRate := range rateMap {
rates[i] = ingestRate
i++
mhPerSec := ingestRate.MhPerSec
if mhPerSec <= tel.slowRate {
slowCount++
}
totalMhPerSec += mhPerSec
}
sort.Sort(metrics.SortableRates(rates))
var slowest []*metrics.IngestRate
if len(rates) > tel.nSlowest {
slowest = rates[:tel.nSlowest]
} else {
slowest = rates
}
log.Infof("Slow providers (below %d mh/sec): %d", tel.slowRate, slowCount)
var avgMhPerSec, fastestMhPerSec, slowestMhPerSec int64
if len(rates) != 0 {
r := rates[len(rates)-1]
fastestMhPerSec = r.MhPerSec
log.Infow("Fastest", "provider", r.ProviderID, "mh/sec", r.MhPerSec, "avgMhPerAd", r.AvgMhPerAd)
r = rates[0]
slowestMhPerSec = r.MhPerSec
log.Infow("Slowest", "provider", r.ProviderID, "mh/sec", r.MhPerSec, "avgMhPerAd", r.AvgMhPerAd)
avgMhPerSec = totalMhPerSec / int64(len(rateMap))
log.Infof("Average ingest rate: %d mh/sec", avgMhPerSec)
}
tel.metrics.UpdateIngestRates(slowest, slowCount, avgMhPerSec, fastestMhPerSec, slowestMhPerSec)
}
func (tel *Telemetry) ListProviders(ctx context.Context, w io.Writer) {
tel.rwmutex.RLock()
defer tel.rwmutex.RUnlock()
if tel.pcache == nil {
return
}
provs := tel.pcache.List()
for _, pinfo := range provs {
tel.showProviderInfo(ctx, pinfo, w)
}
}
func (tel *Telemetry) GetProvider(ctx context.Context, providerID peer.ID, w io.Writer) bool {
tel.rwmutex.RLock()
defer tel.rwmutex.RUnlock()
if tel.pcache == nil {
return false
}
prov, err := tel.pcache.Get(ctx, providerID)
if err != nil || prov == nil {
return false
}
tel.showProviderInfo(ctx, prov, w)
return true
}
func (tel *Telemetry) showProviderInfo(ctx context.Context, pinfo *model.ProviderInfo, w io.Writer) {
fmt.Fprintln(w, "Provider", pinfo.AddrInfo.ID)
var timeStr string
if pinfo.LastAdvertisement.Defined() {
timeStr = pinfo.LastAdvertisementTime
}
fmt.Fprintln(w, " LastAdvertisementTime:", timeStr)
if pinfo.Publisher != nil {
fmt.Fprintln(w, " Publisher:", pinfo.Publisher.ID)
fmt.Fprintln(w, " Publisher Addrs:", pinfo.Publisher.Addrs)
} else {
fmt.Fprintln(w, " Publisher: none")
}
if pinfo.Inactive {
fmt.Fprintln(w, " Inactive: true")
}
if pinfo.LastError != "" {
fmt.Fprintln(w, " LastError:", pinfo.LastError)
} else {
dist, ok := tel.dist[pinfo.AddrInfo.ID]
if ok {
if dist == -1 {
fmt.Fprintf(w, " Distance: exceeded limit %d+", tel.adDepthLimit)
} else {
fmt.Fprintln(w, " Distance:", dist)
}
} else {
fmt.Fprintf(w, " Distance: unknown")
}
}
fmt.Fprintln(w)
}