forked from chrissnell/crabby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_metrics.go
50 lines (42 loc) · 1.25 KB
/
internal_metrics.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
package main
import (
"context"
"log"
"runtime"
"sync"
"time"
)
// startInternalMetrics collects metrics from Crabby's Go runtime and reports them as metrics
func startInternalMetrics(ctx context.Context, wg *sync.WaitGroup, storage *Storage, interval uint) {
var memstats runtime.MemStats
var metricsTicker *time.Ticker
if interval > 0 {
metricsTicker = time.NewTicker(time.Duration(interval) * time.Second)
} else {
metricsTicker = time.NewTicker(15 * time.Second)
}
wg.Add(1)
defer wg.Done()
for {
select {
case <-metricsTicker.C:
runtime.ReadMemStats(&memstats)
storage.MetricDistributor <- makeInternalMetric("mem.alloc", float64(memstats.Alloc))
storage.MetricDistributor <- makeInternalMetric("heap.alloc", float64(memstats.HeapAlloc))
storage.MetricDistributor <- makeInternalMetric("heap.in_use", float64(memstats.HeapInuse))
storage.MetricDistributor <- makeInternalMetric("num_goroutines", float64(runtime.NumGoroutine()))
case <-ctx.Done():
log.Println("Cancellation request received. Cancelling job runner.")
return
}
}
}
func makeInternalMetric(name string, value float64) Metric {
m := Metric{
Job: "internal_metrics",
Timing: name,
Value: value,
Timestamp: time.Now(),
}
return m
}