forked from tonkeeper/api_monitoring_stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
68 lines (60 loc) · 2.27 KB
/
workers.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
package main
import (
"context"
"fmt"
"time"
"api_monitoring_stats/services"
)
type metrics[T services.DAppMetrics | services.ApiMetrics | services.BridgeMetrics] interface {
GetMetrics(ctx context.Context) T
}
func workerMetrics[T services.ApiMetrics | services.DAppMetrics | services.BridgeMetrics](sources []metrics[T], f func(m T)) {
time.Sleep(time.Second)
for _, s := range sources {
go func(m metrics[T]) {
for {
collect(m, f)
sleep := time.Second * 30
i, ok := m.(interface{ CheckInterval() time.Duration })
if ok {
sleep = i.CheckInterval()
}
time.Sleep(sleep)
}
}(s)
time.Sleep(time.Second * 2)
}
}
func collect[T services.ApiMetrics | services.DAppMetrics | services.BridgeMetrics](s metrics[T], f func(m T)) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
m := s.GetMetrics(ctx)
f(m)
}
func apiMetricsCollect(m services.ApiMetrics) {
MetricServiceTimeHistogramVec.WithLabelValues(m.ServiceName).Observe(m.HttpsLatency)
MetricServiceIndexingLatencyHistogramVec.WithLabelValues(m.ServiceName).Observe(m.IndexingLatency)
MetricServiceRequest.WithLabelValues(m.ServiceName).Set(float64(m.SuccessChecks) / float64(m.TotalChecks))
for _, err := range m.Errors {
fmt.Println("Service", m.ServiceName, err.Error())
}
}
func dappsMetricsCollect(m services.DAppMetrics) {
MetricDAppAvailability.WithLabelValues(m.ServiceName).Set(float64(m.SuccessChecks) / float64(m.TotalChecks))
MetricDAppMainPageLatency.WithLabelValues(m.ServiceName).Observe(m.MainPageLoadLatency)
MetricDAppTimeHistogramVec.WithLabelValues(m.ServiceName).Observe(m.ApiLatency)
if m.IndexationLatency != nil {
MetricDAppIndexingLatencyHistogramVec.WithLabelValues(m.ServiceName).Observe(*m.IndexationLatency)
}
for _, err := range m.Errors {
fmt.Println("Service", m.ServiceName, err.Error())
}
}
func bridgeMetricsCollect(m services.BridgeMetrics) {
metricBridgeAvailability.WithLabelValues(m.ServiceName).Set(float64(m.SuccessChecks) / float64(m.TotalChecks))
metricBridgeReconnects.WithLabelValues(m.ServiceName).Set(float64(m.Reconnects))
metricBridgeLatencyHistogramVec.WithLabelValues(m.ServiceName).Observe(m.TransferLatency)
for _, err := range m.Errors {
fmt.Println("Service", m.ServiceName, err.Error())
}
}