-
Notifications
You must be signed in to change notification settings - Fork 23
/
metrics.go
125 lines (112 loc) · 2.8 KB
/
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
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
package source
import (
"sync/atomic"
"time"
"github.com/cloudquery/plugin-sdk/schema"
)
type Metrics struct {
TableClient map[string]map[string]*TableClientMetrics
}
type TableClientMetrics struct {
Resources uint64
Errors uint64
Panics uint64
StartTime time.Time
EndTime time.Time
}
func (s *TableClientMetrics) Equal(other *TableClientMetrics) bool {
return s.Resources == other.Resources && s.Errors == other.Errors && s.Panics == other.Panics
}
// Equal compares to stats. Mostly useful in testing
func (s *Metrics) Equal(other *Metrics) bool {
for table, clientStats := range s.TableClient {
for client, stats := range clientStats {
if _, ok := other.TableClient[table]; !ok {
return false
}
if _, ok := other.TableClient[table][client]; !ok {
return false
}
if !stats.Equal(other.TableClient[table][client]) {
return false
}
}
}
for table, clientStats := range other.TableClient {
for client, stats := range clientStats {
if _, ok := s.TableClient[table]; !ok {
return false
}
if _, ok := s.TableClient[table][client]; !ok {
return false
}
if !stats.Equal(s.TableClient[table][client]) {
return false
}
}
}
return true
}
func (s *Metrics) initWithClients(table *schema.Table, clients []schema.ClientMeta) {
s.TableClient[table.Name] = make(map[string]*TableClientMetrics, len(clients))
for _, client := range clients {
s.TableClient[table.Name][client.ID()] = &TableClientMetrics{}
}
for _, relation := range table.Relations {
s.initWithClients(relation, clients)
}
}
func (s *Metrics) TotalErrors() uint64 {
var total uint64
for _, clientMetrics := range s.TableClient {
for _, metrics := range clientMetrics {
total += metrics.Errors
}
}
return total
}
func (s *Metrics) TotalErrorsAtomic() uint64 {
var total uint64
for _, clientMetrics := range s.TableClient {
for _, metrics := range clientMetrics {
total += atomic.LoadUint64(&metrics.Errors)
}
}
return total
}
func (s *Metrics) TotalPanics() uint64 {
var total uint64
for _, clientMetrics := range s.TableClient {
for _, metrics := range clientMetrics {
total += metrics.Panics
}
}
return total
}
func (s *Metrics) TotalPanicsAtomic() uint64 {
var total uint64
for _, clientMetrics := range s.TableClient {
for _, metrics := range clientMetrics {
total += atomic.LoadUint64(&metrics.Panics)
}
}
return total
}
func (s *Metrics) TotalResources() uint64 {
var total uint64
for _, clientMetrics := range s.TableClient {
for _, metrics := range clientMetrics {
total += metrics.Resources
}
}
return total
}
func (s *Metrics) TotalResourcesAtomic() uint64 {
var total uint64
for _, clientMetrics := range s.TableClient {
for _, metrics := range clientMetrics {
total += atomic.LoadUint64(&metrics.Resources)
}
}
return total
}