forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstats.go
477 lines (436 loc) · 11.5 KB
/
stats.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package analytics
import (
"bytes"
"context"
"encoding/json"
"expvar"
"fmt"
"io"
"math"
"net/http"
"runtime"
"strings"
"sync"
"time"
"github.com/grafana/loki/v3/pkg/util/build"
"github.com/cespare/xxhash/v2"
jsoniter "github.com/json-iterator/go"
prom "github.com/prometheus/prometheus/web/api/v1"
"go.uber.org/atomic"
)
var (
httpClient = http.Client{Timeout: 5 * time.Second}
usageStatsURL = "https://stats.grafana.org/loki-usage-report"
statsPrefix = "github.com/grafana/loki/"
targetKey = "target"
editionKey = "edition"
createLock sync.RWMutex
)
func createOrRetrieveExpvar[K any](check func() (*K, error), create func() *K) *K {
// check if string exists holding read lock
createLock.RLock()
s, err := check()
createLock.RUnlock()
if err != nil {
panic(err.Error())
}
if s != nil {
return s
}
// acquire write lock and check again and create if still missing
createLock.Lock()
defer createLock.Unlock()
s, err = check()
if err != nil {
panic(err.Error())
}
if s != nil {
return s
}
return create()
}
// Report is the JSON object sent to the stats server
type Report struct {
ClusterID string `json:"clusterID"`
CreatedAt time.Time `json:"createdAt"`
Interval time.Time `json:"interval"`
IntervalPeriod float64 `json:"intervalPeriod"`
Target string `json:"target"`
prom.PrometheusVersion `json:"version"`
Os string `json:"os"`
Arch string `json:"arch"`
Edition string `json:"edition"`
Metrics map[string]interface{} `json:"metrics"`
}
// sendReport sends the report to the stats server
func sendReport(ctx context.Context, seed *ClusterSeed, interval time.Time, URL string) error {
report := buildReport(seed, interval)
out, err := jsoniter.MarshalIndent(report, "", " ")
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBuffer(out))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("failed to send usage stats: %s body: %s", resp.Status, string(data))
}
return nil
}
// buildReport builds the report to be sent to the stats server
func buildReport(seed *ClusterSeed, interval time.Time) Report {
var (
targetName string
editionName string
)
if target := expvar.Get(statsPrefix + targetKey); target != nil {
if target, ok := target.(*expvar.String); ok {
targetName = target.Value()
}
}
if edition := expvar.Get(statsPrefix + editionKey); edition != nil {
if edition, ok := edition.(*expvar.String); ok {
editionName = edition.Value()
}
}
return Report{
ClusterID: seed.UID,
PrometheusVersion: build.GetVersion(),
CreatedAt: seed.CreatedAt,
Interval: interval,
IntervalPeriod: reportInterval.Seconds(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Target: targetName,
Edition: editionName,
Metrics: buildMetrics(),
}
}
// buildMetrics builds the metrics part of the report to be sent to the stats server
func buildMetrics() map[string]interface{} {
result := map[string]interface{}{
"memstats": memstats(),
"num_cpu": runtime.NumCPU(),
"num_goroutine": runtime.NumGoroutine(),
// the highest recorded cpu usage over the interval
"cpu_usage": cpuUsage.Value(),
}
// reset cpu usage
cpuUsage.Set(0)
expvar.Do(func(kv expvar.KeyValue) {
if !strings.HasPrefix(kv.Key, statsPrefix) || kv.Key == statsPrefix+targetKey || kv.Key == statsPrefix+editionKey || kv.Key == statsPrefix+cpuUsageKey {
return
}
var value interface{}
switch v := kv.Value.(type) {
case *expvar.Int:
value = v.Value()
case *expvar.Float:
value = v.Value()
case *expvar.String:
value = v.Value()
case *Statistics:
value = v.Value()
case *Counter:
v.updateRate()
value = v.Value()
v.reset()
case *WordCounter:
value = v.Value()
default:
value = v.String()
}
result[strings.TrimPrefix(kv.Key, statsPrefix)] = value
})
return result
}
func memstats() interface{} {
stats := new(runtime.MemStats)
runtime.ReadMemStats(stats)
return map[string]interface{}{
"alloc": stats.Alloc,
"total_alloc": stats.TotalAlloc,
"sys": stats.Sys,
"heap_alloc": stats.HeapAlloc,
"heap_inuse": stats.HeapInuse,
"stack_inuse": stats.StackInuse,
"pause_total_ns": stats.PauseTotalNs,
"num_gc": stats.NumGC,
"gc_cpu_fraction": stats.GCCPUFraction,
}
}
// NewFloat returns a new Float stats object.
// If a Float stats object with the same name already exists it is returned.
func NewFloat(name string) *expvar.Float {
return createOrRetrieveExpvar(
func() (*expvar.Float, error) { // check
existing := expvar.Get(statsPrefix + name)
if existing != nil {
if f, ok := existing.(*expvar.Float); ok {
return f, nil
}
return nil, fmt.Errorf("%v is set to a non-float value", name)
}
return nil, nil
},
func() *expvar.Float { // create
return expvar.NewFloat(statsPrefix + name)
},
)
}
// NewInt returns a new Int stats object.
// If an Int stats object object with the same name already exists it is returned.
func NewInt(name string) *expvar.Int {
return createOrRetrieveExpvar(
func() (*expvar.Int, error) { // check
existing := expvar.Get(statsPrefix + name)
if existing != nil {
if i, ok := existing.(*expvar.Int); ok {
return i, nil
}
return nil, fmt.Errorf("%v is set to a non-int value", name)
}
return nil, nil
},
func() *expvar.Int { // create
return expvar.NewInt(statsPrefix + name)
},
)
}
// NewString returns a new String stats object.
// If a String stats object with the same name already exists it is returned.
func NewString(name string) *expvar.String {
return createOrRetrieveExpvar(
func() (*expvar.String, error) { // check
existing := expvar.Get(statsPrefix + name)
if existing != nil {
if s, ok := existing.(*expvar.String); ok {
return s, nil
}
return nil, fmt.Errorf("%v is set to a non-string value", name)
}
return nil, nil
},
func() *expvar.String { // create
return expvar.NewString(statsPrefix + name)
},
)
}
// Target sets the target name. This can be set multiple times.
func Target(target string) {
NewString(targetKey).Set(target)
}
// Edition sets the edition name. This can be set multiple times.
func Edition(edition string) {
NewString(editionKey).Set(edition)
}
type Statistics struct {
min *atomic.Float64
max *atomic.Float64
count *atomic.Int64
avg *atomic.Float64
// require for stddev and stdvar
mean *atomic.Float64
value *atomic.Float64
}
// NewStatistics returns a new Statistics object.
// Statistics object is thread-safe and compute statistics on the fly based on sample recorded.
// Available statistics are:
// - min
// - max
// - avg
// - count
// - stddev
// - stdvar
// If a Statistics object with the same name already exists it is returned.
func NewStatistics(name string) *Statistics {
return createOrRetrieveExpvar(
func() (*Statistics, error) { // check
existing := expvar.Get(statsPrefix + name)
if existing != nil {
if s, ok := existing.(*Statistics); ok {
return s, nil
}
return nil, fmt.Errorf("%v is set to a non-Statistics value", name)
}
return nil, nil
},
func() *Statistics { // create
s := &Statistics{
min: atomic.NewFloat64(math.Inf(0)),
max: atomic.NewFloat64(math.Inf(-1)),
count: atomic.NewInt64(0),
avg: atomic.NewFloat64(0),
mean: atomic.NewFloat64(0),
value: atomic.NewFloat64(0),
}
expvar.Publish(statsPrefix+name, s)
return s
},
)
}
func (s *Statistics) String() string {
b, _ := json.Marshal(s.Value())
return string(b)
}
func (s *Statistics) Value() map[string]interface{} {
stdvar := s.value.Load() / float64(s.count.Load())
stddev := math.Sqrt(stdvar)
min := s.min.Load()
max := s.max.Load()
result := map[string]interface{}{
"avg": s.avg.Load(),
"count": s.count.Load(),
}
if !math.IsInf(min, 0) {
result["min"] = min
}
if !math.IsInf(max, 0) {
result["max"] = s.max.Load()
}
if !math.IsNaN(stddev) {
result["stddev"] = stddev
}
if !math.IsNaN(stdvar) {
result["stdvar"] = stdvar
}
return result
}
func (s *Statistics) Record(v float64) {
for {
min := s.min.Load()
if min <= v {
break
}
if s.min.CompareAndSwap(min, v) {
break
}
}
for {
max := s.max.Load()
if max >= v {
break
}
if s.max.CompareAndSwap(max, v) {
break
}
}
for {
avg := s.avg.Load()
count := s.count.Load()
mean := s.mean.Load()
value := s.value.Load()
delta := v - mean
newCount := count + 1
newMean := mean + (delta / float64(newCount))
newValue := value + (delta * (v - newMean))
newAvg := avg + ((v - avg) / float64(newCount))
if s.avg.CompareAndSwap(avg, newAvg) && s.count.CompareAndSwap(count, newCount) && s.mean.CompareAndSwap(mean, newMean) && s.value.CompareAndSwap(value, newValue) {
break
}
}
}
type Counter struct {
total *atomic.Int64
rate *atomic.Float64
resetTime time.Time
}
// NewCounter returns a new Counter stats object.
// If a Counter stats object with the same name already exists it is returned.
func NewCounter(name string) *Counter {
return createOrRetrieveExpvar(
func() (*Counter, error) { // check
existing := expvar.Get(statsPrefix + name)
if existing != nil {
if c, ok := existing.(*Counter); ok {
return c, nil
}
return nil, fmt.Errorf("%v is set to a non-Counter value", name)
}
return nil, nil
},
func() *Counter { // create
c := &Counter{
total: atomic.NewInt64(0),
rate: atomic.NewFloat64(0),
resetTime: time.Now(),
}
expvar.Publish(statsPrefix+name, c)
return c
},
)
}
func (c *Counter) updateRate() {
total := c.total.Load()
c.rate.Store(float64(total) / time.Since(c.resetTime).Seconds())
}
func (c *Counter) reset() {
c.total.Store(0)
c.rate.Store(0)
c.resetTime = time.Now()
}
func (c *Counter) Inc(i int64) {
c.total.Add(i)
}
func (c *Counter) String() string {
b, _ := json.Marshal(c.Value())
return string(b)
}
func (c *Counter) Value() map[string]interface{} {
return map[string]interface{}{
"total": c.total.Load(),
"rate": c.rate.Load(),
}
}
type WordCounter struct {
words sync.Map
count *atomic.Int64
}
// NewWordCounter returns a new WordCounter stats object.
// The WordCounter object is thread-safe and counts the number of words recorded.
// If a WordCounter stats object with the same name already exists it is returned.
func NewWordCounter(name string) *WordCounter {
return createOrRetrieveExpvar(
func() (*WordCounter, error) { // check
existing := expvar.Get(statsPrefix + name)
if existing != nil {
if w, ok := existing.(*WordCounter); ok {
return w, nil
}
return nil, fmt.Errorf("%v is set to a non-WordCounter value", name)
}
return nil, nil
},
func() *WordCounter { // create
c := &WordCounter{
count: atomic.NewInt64(0),
words: sync.Map{},
}
expvar.Publish(statsPrefix+name, c)
return c
},
)
}
func (w *WordCounter) Add(word string) {
if _, loaded := w.words.LoadOrStore(xxhash.Sum64String(word), struct{}{}); !loaded {
w.count.Add(1)
}
}
func (w *WordCounter) String() string {
b, _ := json.Marshal(w.Value())
return string(b)
}
func (w *WordCounter) Value() int64 {
return w.count.Load()
}