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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// SPDX-License-Identifier: GPL-3.0-or-later
package postgres
import (
"database/sql"
_ "embed"
"errors"
"sync"
"time"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/matcher"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrics"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
"github.com/jackc/pgx/v5/stdlib"
_ "github.com/jackc/pgx/v5/stdlib"
)
//go:embed "config_schema.json"
var configSchema string
func init() {
module.Register("postgres", module.Creator{
JobConfigSchema: configSchema,
Create: func() module.Module { return New() },
Config: func() any { return &Config{} },
})
}
func New() *Postgres {
return &Postgres{
Config: Config{
Timeout: web.Duration(time.Second * 2),
DSN: "postgres://postgres:postgres@127.0.0.1:5432/postgres",
XactTimeHistogram: []float64{.1, .5, 1, 2.5, 5, 10},
QueryTimeHistogram: []float64{.1, .5, 1, 2.5, 5, 10},
// charts: 20 x table, 4 x index.
// https://discord.com/channels/847502280503590932/1022693928874549368
MaxDBTables: 50,
MaxDBIndexes: 250,
},
charts: baseCharts.Copy(),
dbConns: make(map[string]*dbConn),
mx: &pgMetrics{
dbs: make(map[string]*dbMetrics),
indexes: make(map[string]*indexMetrics),
tables: make(map[string]*tableMetrics),
replApps: make(map[string]*replStandbyAppMetrics),
replSlots: make(map[string]*replSlotMetrics),
},
recheckSettingsEvery: time.Minute * 30,
doSlowEvery: time.Minute * 5,
addXactQueryRunningTimeChartsOnce: &sync.Once{},
addWALFilesChartsOnce: &sync.Once{},
}
}
type Config struct {
UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
DSN string `yaml:"dsn" json:"dsn"`
Timeout web.Duration `yaml:"timeout,omitempty" json:"timeout"`
DBSelector string `yaml:"collect_databases_matching,omitempty" json:"collect_databases_matching"`
XactTimeHistogram []float64 `yaml:"transaction_time_histogram,omitempty" json:"transaction_time_histogram"`
QueryTimeHistogram []float64 `yaml:"query_time_histogram,omitempty" json:"query_time_histogram"`
MaxDBTables int64 `yaml:"max_db_tables" json:"max_db_tables"`
MaxDBIndexes int64 `yaml:"max_db_indexes" json:"max_db_indexes"`
}
type (
Postgres struct {
module.Base
Config `yaml:",inline" json:""`
charts *module.Charts
addXactQueryRunningTimeChartsOnce *sync.Once
addWALFilesChartsOnce *sync.Once
db *sql.DB
dbConns map[string]*dbConn
superUser *bool
pgIsInRecovery *bool
pgVersion int
dbSr matcher.Matcher
recheckSettingsTime time.Time
recheckSettingsEvery time.Duration
doSlowTime time.Time
doSlowEvery time.Duration
mx *pgMetrics
}
dbConn struct {
db *sql.DB
connStr string
connErrors int
}
)
func (p *Postgres) Configuration() any {
return p.Config
}
func (p *Postgres) Init() error {
err := p.validateConfig()
if err != nil {
p.Errorf("config validation: %v", err)
return err
}
sr, err := p.initDBSelector()
if err != nil {
p.Errorf("config validation: %v", err)
return err
}
p.dbSr = sr
p.mx.xactTimeHist = metrics.NewHistogramWithRangeBuckets(p.XactTimeHistogram)
p.mx.queryTimeHist = metrics.NewHistogramWithRangeBuckets(p.QueryTimeHistogram)
return nil
}
func (p *Postgres) Check() error {
mx, err := p.collect()
if err != nil {
p.Error(err)
return err
}
if len(mx) == 0 {
return errors.New("no metrics collected")
}
return nil
}
func (p *Postgres) Charts() *module.Charts {
return p.charts
}
func (p *Postgres) Collect() map[string]int64 {
mx, err := p.collect()
if err != nil {
p.Error(err)
}
if len(mx) == 0 {
return nil
}
return mx
}
func (p *Postgres) Cleanup() {
if p.db == nil {
return
}
if err := p.db.Close(); err != nil {
p.Warningf("cleanup: error on closing the Postgres database [%s]: %v", p.DSN, err)
}
p.db = nil
for dbname, conn := range p.dbConns {
delete(p.dbConns, dbname)
if conn.connStr != "" {
stdlib.UnregisterConnConfig(conn.connStr)
}
if conn.db != nil {
_ = conn.db.Close()
}
}
}
|