-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
66 lines (56 loc) · 1.72 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
package db
import (
"agent/errors"
"agent/logger"
"time"
)
type Metric struct {
Name string
Value float64
ServerID ServerID
Entity string
MeasuredAt int64
}
func NewMetric(name string, value float64, entity string, serverID ServerID, measuredAt int64) *Metric {
return &Metric{
Name: name,
Value: value,
Entity: entity,
ServerID: serverID,
MeasuredAt: measuredAt,
}
}
type MetricMonitor struct {
metricsChannel chan []*Metric
databaseStatsState *DatabaseStatsState
}
func (m *MetricMonitor) Run(postgresClient *PostgresClient) {
metrics := m.FindUsedConnectionsMetric(postgresClient)
metrics = append(metrics, m.FindDatabaseStatMetrics(postgresClient)...)
metrics = append(metrics, m.FindDatabaseCacheHitMetrics(postgresClient)...)
select {
case m.metricsChannel <- metrics:
// sent
default:
logger.Warn("Dropping metrics: channel buffer full")
}
}
func (m *MetricMonitor) FindUsedConnectionsMetric(postgresClient *PostgresClient) []*Metric {
// used connections include active connections and reserved connections
query := `select used, reserved from
(select count(*) used from pg_stat_activity) q1,
(select setting::int reserved from pg_settings where name='superuser_reserved_connections') q2` + postgresMonitorQueryComment()
now := time.Now().UTC().Unix()
var used float64
var reserved float64
err := postgresClient.client.QueryRow(query).Scan(&used, &reserved)
if err != nil {
logger.Error("Connection metrics error", "err", err)
errors.Report(err)
return []*Metric{}
}
return []*Metric{
NewMetric("connections.used", used, "", *postgresClient.serverID, now),
NewMetric("connections.reserved", reserved, "", *postgresClient.serverID, now),
}
}