-
Notifications
You must be signed in to change notification settings - Fork 170
/
postgres.go
89 lines (71 loc) Β· 1.87 KB
/
postgres.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
package model
import (
"fmt"
"github.com/coroot/coroot/timeseries"
)
type PgConnectionKey struct {
Db string
User string
State string
Query string
WaitEventType string
}
func (k PgConnectionKey) String() string {
return fmt.Sprintf("%s@%s: %s", k.User, k.Db, k.Query)
}
type PgSetting struct {
Samples *timeseries.TimeSeries
Unit string
}
type QueryKey struct {
Db string
User string
Query string
}
func (k QueryKey) String() string {
return fmt.Sprintf("%s@%s: %s", k.User, k.Db, k.Query)
}
type QueryStat struct {
Calls *timeseries.TimeSeries
TotalTime *timeseries.TimeSeries
IoTime *timeseries.TimeSeries
}
type Postgres struct {
Up *timeseries.TimeSeries
Version LabelLastValue
Connections map[PgConnectionKey]*timeseries.TimeSeries
AwaitingQueriesByLockingQuery map[QueryKey]*timeseries.TimeSeries
Settings map[string]PgSetting
PerQuery map[QueryKey]*QueryStat
QueriesByDB map[string]*timeseries.TimeSeries
Avg *timeseries.TimeSeries
P50 *timeseries.TimeSeries
P95 *timeseries.TimeSeries
P99 *timeseries.TimeSeries
WalCurrentLsn *timeseries.TimeSeries
WalReceiveLsn *timeseries.TimeSeries
WalReplayLsn *timeseries.TimeSeries
}
func NewPostgres() *Postgres {
return &Postgres{
Connections: map[PgConnectionKey]*timeseries.TimeSeries{},
AwaitingQueriesByLockingQuery: map[QueryKey]*timeseries.TimeSeries{},
Settings: map[string]PgSetting{},
PerQuery: map[QueryKey]*QueryStat{},
QueriesByDB: map[string]*timeseries.TimeSeries{},
}
}
func (p *Postgres) IsUp() bool {
return p.Up.Last() > 0
}
func (p *Postgres) Unavailability() *timeseries.TimeSeries {
if p.Up.IsEmpty() {
return nil
}
return p.Up.Map(func(t timeseries.Time, v float32) float32 {
if v != 1 {
return 1
}
return 0
})
}