-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
client.go
595 lines (534 loc) · 16.2 KB
/
client.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package postgresqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
import (
"context"
"database/sql"
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/lib/pq"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.uber.org/multierr"
)
// databaseName is a name that refers to a database so that it can be uniquely referred to later
// i.e. database1
type databaseName string
// tableIdentifier is an identifier that contains both the database and table separated by a "|"
// i.e. database1|table2
type tableIdentifier string
// indexIdentifier is a unique string that identifies a particular index and is separated by the "|" character
type indexIdentifer string
// errNoLastArchive is an error that occurs when there is no previous wal archive, so there is no way to compute the
// last archived point
var errNoLastArchive = errors.New("no last archive found, not able to calculate oldest WAL age")
type client interface {
Close() error
getDatabaseStats(ctx context.Context, databases []string) (map[databaseName]databaseStats, error)
getDatabaseLocks(ctx context.Context) ([]databaseLocks, error)
getBGWriterStats(ctx context.Context) (*bgStat, error)
getBackends(ctx context.Context, databases []string) (map[databaseName]int64, error)
getDatabaseSize(ctx context.Context, databases []string) (map[databaseName]int64, error)
getDatabaseTableMetrics(ctx context.Context, db string) (map[tableIdentifier]tableStats, error)
getBlocksReadByTable(ctx context.Context, db string) (map[tableIdentifier]tableIOStats, error)
getReplicationStats(ctx context.Context) ([]replicationStats, error)
getLatestWalAgeSeconds(ctx context.Context) (int64, error)
getMaxConnections(ctx context.Context) (int64, error)
getIndexStats(ctx context.Context, database string) (map[indexIdentifer]indexStat, error)
listDatabases(ctx context.Context) ([]string, error)
}
type postgreSQLClient struct {
client *sql.DB
database string
}
var _ client = (*postgreSQLClient)(nil)
type postgreSQLConfig struct {
username string
password string
database string
address confignet.NetAddr
tls configtls.TLSClientSetting
}
func sslConnectionString(tls configtls.TLSClientSetting) string {
if tls.Insecure {
return "sslmode='disable'"
}
conn := ""
if tls.InsecureSkipVerify {
conn += "sslmode='require'"
} else {
conn += "sslmode='verify-full'"
}
if tls.CAFile != "" {
conn += fmt.Sprintf(" sslrootcert='%s'", tls.CAFile)
}
if tls.KeyFile != "" {
conn += fmt.Sprintf(" sslkey='%s'", tls.KeyFile)
}
if tls.CertFile != "" {
conn += fmt.Sprintf(" sslcert='%s'", tls.CertFile)
}
return conn
}
func newPostgreSQLClient(conf postgreSQLConfig) (*postgreSQLClient, error) {
// postgres will assume the supplied user as the database name if none is provided,
// so we must specify a databse name even when we are just collecting the list of databases.
dbField := "dbname=postgres"
if conf.database != "" {
dbField = fmt.Sprintf("dbname=%s ", conf.database)
}
host, port, err := net.SplitHostPort(conf.address.Endpoint)
if err != nil {
return nil, err
}
if conf.address.Transport == "unix" {
// lib/pg expects a unix socket host to start with a "/" and appends the appropriate .s.PGSQL.port internally
host = fmt.Sprintf("/%s", host)
}
connStr := fmt.Sprintf("port=%s host=%s user=%s password=%s %s %s", port, host, conf.username, conf.password, dbField, sslConnectionString(conf.tls))
conn, err := pq.NewConnector(connStr)
if err != nil {
return nil, err
}
db := sql.OpenDB(conn)
return &postgreSQLClient{
client: db,
database: conf.database,
}, nil
}
func (c *postgreSQLClient) Close() error {
return c.client.Close()
}
type databaseStats struct {
transactionCommitted int64
transactionRollback int64
deadlocks int64
tempFiles int64
}
func (c *postgreSQLClient) getDatabaseStats(ctx context.Context, databases []string) (map[databaseName]databaseStats, error) {
query := filterQueryByDatabases("SELECT datname, xact_commit, xact_rollback, deadlocks, temp_files FROM pg_stat_database", databases, false)
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
var errs error
dbStats := map[databaseName]databaseStats{}
for rows.Next() {
var datname string
var transactionCommitted, transactionRollback, deadlocks, tempFiles int64
err = rows.Scan(&datname, &transactionCommitted, &transactionRollback, &deadlocks, &tempFiles)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
if datname != "" {
dbStats[databaseName(datname)] = databaseStats{
transactionCommitted: transactionCommitted,
transactionRollback: transactionRollback,
deadlocks: deadlocks,
tempFiles: tempFiles,
}
}
}
return dbStats, errs
}
type databaseLocks struct {
relation string
mode string
lockType string
locks int64
}
func (c *postgreSQLClient) getDatabaseLocks(ctx context.Context) ([]databaseLocks, error) {
query := `SELECT relname AS relation, mode, locktype,COUNT(pid)
AS locks FROM pg_locks
JOIN pg_class ON pg_locks.relation = pg_class.oid
GROUP BY relname, mode, locktype;`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_locks and pg_locks.relation: %w", err)
}
defer rows.Close()
var dl []databaseLocks
var errs []error
for rows.Next() {
var relation, mode, lockType string
var locks int64
err = rows.Scan(&relation, &mode, &lockType, &locks)
if err != nil {
errs = append(errs, err)
continue
}
dl = append(dl, databaseLocks{
relation: relation,
mode: mode,
lockType: lockType,
locks: locks,
})
}
return dl, multierr.Combine(errs...)
}
// getBackends returns a map of database names to the number of active connections
func (c *postgreSQLClient) getBackends(ctx context.Context, databases []string) (map[databaseName]int64, error) {
query := filterQueryByDatabases("SELECT datname, count(*) as count from pg_stat_activity", databases, true)
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
ars := map[databaseName]int64{}
var errors error
for rows.Next() {
var datname string
var count int64
err = rows.Scan(&datname, &count)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
if datname != "" {
ars[databaseName(datname)] = count
}
}
return ars, errors
}
func (c *postgreSQLClient) getDatabaseSize(ctx context.Context, databases []string) (map[databaseName]int64, error) {
query := filterQueryByDatabases("SELECT datname, pg_database_size(datname) FROM pg_catalog.pg_database WHERE datistemplate = false", databases, false)
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
sizes := map[databaseName]int64{}
var errors error
for rows.Next() {
var datname string
var size int64
err = rows.Scan(&datname, &size)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
if datname != "" {
sizes[databaseName(datname)] = size
}
}
return sizes, errors
}
// tableStats contains a result for a row of the getDatabaseTableMetrics result
type tableStats struct {
database string
table string
live int64
dead int64
inserts int64
upd int64
del int64
hotUpd int64
seqScans int64
size int64
vacuumCount int64
}
func (c *postgreSQLClient) getDatabaseTableMetrics(ctx context.Context, db string) (map[tableIdentifier]tableStats, error) {
query := `SELECT schemaname || '.' || relname AS table,
n_live_tup AS live,
n_dead_tup AS dead,
n_tup_ins AS ins,
n_tup_upd AS upd,
n_tup_del AS del,
n_tup_hot_upd AS hot_upd,
seq_scan AS seq_scans,
pg_relation_size(relid) AS table_size,
vacuum_count
FROM pg_stat_user_tables;`
ts := map[tableIdentifier]tableStats{}
var errors error
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
for rows.Next() {
var table string
var live, dead, ins, upd, del, hotUpd, seqScans, tableSize, vacuumCount int64
err = rows.Scan(&table, &live, &dead, &ins, &upd, &del, &hotUpd, &seqScans, &tableSize, &vacuumCount)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
ts[tableKey(db, table)] = tableStats{
database: db,
table: table,
live: live,
inserts: ins,
upd: upd,
del: del,
hotUpd: hotUpd,
seqScans: seqScans,
size: tableSize,
vacuumCount: vacuumCount,
}
}
return ts, errors
}
type tableIOStats struct {
database string
table string
heapRead int64
heapHit int64
idxRead int64
idxHit int64
toastRead int64
toastHit int64
tidxRead int64
tidxHit int64
}
func (c *postgreSQLClient) getBlocksReadByTable(ctx context.Context, db string) (map[tableIdentifier]tableIOStats, error) {
query := `SELECT schemaname || '.' || relname AS table,
coalesce(heap_blks_read, 0) AS heap_read,
coalesce(heap_blks_hit, 0) AS heap_hit,
coalesce(idx_blks_read, 0) AS idx_read,
coalesce(idx_blks_hit, 0) AS idx_hit,
coalesce(toast_blks_read, 0) AS toast_read,
coalesce(toast_blks_hit, 0) AS toast_hit,
coalesce(tidx_blks_read, 0) AS tidx_read,
coalesce(tidx_blks_hit, 0) AS tidx_hit
FROM pg_statio_user_tables;`
tios := map[tableIdentifier]tableIOStats{}
var errors error
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
for rows.Next() {
var table string
var heapRead, heapHit, idxRead, idxHit, toastRead, toastHit, tidxRead, tidxHit int64
err = rows.Scan(&table, &heapRead, &heapHit, &idxRead, &idxHit, &toastRead, &toastHit, &tidxRead, &tidxHit)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
tios[tableKey(db, table)] = tableIOStats{
database: db,
table: table,
heapRead: heapRead,
heapHit: heapHit,
idxRead: idxRead,
idxHit: idxHit,
toastRead: toastRead,
toastHit: toastHit,
tidxRead: tidxRead,
tidxHit: tidxHit,
}
}
return tios, errors
}
type indexStat struct {
index string
table string
database string
size int64
scans int64
}
func (c *postgreSQLClient) getIndexStats(ctx context.Context, database string) (map[indexIdentifer]indexStat, error) {
query := `SELECT relname, indexrelname,
pg_relation_size(indexrelid) AS index_size,
idx_scan
FROM pg_stat_user_indexes;`
stats := map[indexIdentifer]indexStat{}
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var errs []error
for rows.Next() {
var (
table, index string
indexSize, indexScans int64
)
err := rows.Scan(&table, &index, &indexSize, &indexScans)
if err != nil {
errs = append(errs, err)
continue
}
stats[indexKey(database, table, index)] = indexStat{
index: index,
table: table,
database: database,
size: indexSize,
scans: indexScans,
}
}
return stats, multierr.Combine(errs...)
}
type bgStat struct {
checkpointsReq int64
checkpointsScheduled int64
checkpointWriteTime float64
checkpointSyncTime float64
bgWrites int64
backendWrites int64
bufferBackendWrites int64
bufferFsyncWrites int64
bufferCheckpoints int64
buffersAllocated int64
maxWritten int64
}
func (c *postgreSQLClient) getBGWriterStats(ctx context.Context) (*bgStat, error) {
query := `SELECT
checkpoints_req AS checkpoint_req,
checkpoints_timed AS checkpoint_scheduled,
checkpoint_write_time AS checkpoint_duration_write,
checkpoint_sync_time AS checkpoint_duration_sync,
buffers_clean AS bg_writes,
buffers_backend AS backend_writes,
buffers_backend_fsync AS buffers_written_fsync,
buffers_checkpoint AS buffers_checkpoints,
buffers_alloc AS buffers_allocated,
maxwritten_clean AS maxwritten_count
FROM pg_stat_bgwriter;`
row := c.client.QueryRowContext(ctx, query)
var (
checkpointsReq, checkpointsScheduled int64
checkpointSyncTime, checkpointWriteTime float64
bgWrites, bufferCheckpoints, bufferAllocated int64
bufferBackendWrites, bufferFsyncWrites, maxWritten int64
)
err := row.Scan(
&checkpointsReq,
&checkpointsScheduled,
&checkpointWriteTime,
&checkpointSyncTime,
&bgWrites,
&bufferBackendWrites,
&bufferFsyncWrites,
&bufferCheckpoints,
&bufferAllocated,
&maxWritten,
)
if err != nil {
return nil, err
}
return &bgStat{
checkpointsReq: checkpointsReq,
checkpointsScheduled: checkpointsScheduled,
checkpointWriteTime: checkpointWriteTime,
checkpointSyncTime: checkpointSyncTime,
bgWrites: bgWrites,
backendWrites: bufferBackendWrites,
bufferBackendWrites: bufferBackendWrites,
bufferFsyncWrites: bufferFsyncWrites,
bufferCheckpoints: bufferCheckpoints,
buffersAllocated: bufferAllocated,
maxWritten: maxWritten,
}, nil
}
func (c *postgreSQLClient) getMaxConnections(ctx context.Context) (int64, error) {
query := `SHOW max_connections;`
row := c.client.QueryRowContext(ctx, query)
var maxConns int64
err := row.Scan(&maxConns)
return maxConns, err
}
type replicationStats struct {
clientAddr string
pendingBytes int64
flushLag int64
replayLag int64
writeLag int64
}
func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicationStats, error) {
query := `SELECT
client_addr,
coalesce(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn), -1) AS replication_bytes_pending,
extract('epoch' from coalesce(write_lag, '-1 seconds')),
extract('epoch' from coalesce(flush_lag, '-1 seconds')),
extract('epoch' from coalesce(replay_lag, '-1 seconds'))
FROM pg_stat_replication;
`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_replication: %w", err)
}
defer rows.Close()
var rs []replicationStats
var errors error
for rows.Next() {
var client string
var replicationBytes, writeLag, flushLag, replayLag int64
err = rows.Scan(&client, &replicationBytes, &writeLag, &flushLag, &replayLag)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
rs = append(rs, replicationStats{
clientAddr: client,
pendingBytes: replicationBytes,
replayLag: replayLag,
writeLag: writeLag,
flushLag: flushLag,
})
}
return rs, errors
}
func (c *postgreSQLClient) getLatestWalAgeSeconds(ctx context.Context) (int64, error) {
query := `SELECT
coalesce(last_archived_time, CURRENT_TIMESTAMP) AS last_archived_wal,
CURRENT_TIMESTAMP
FROM pg_stat_archiver;
`
row := c.client.QueryRowContext(ctx, query)
var lastArchivedWal, currentInstanceTime time.Time
err := row.Scan(&lastArchivedWal, ¤tInstanceTime)
if err != nil {
return 0, err
}
if lastArchivedWal.Equal(currentInstanceTime) {
return 0, errNoLastArchive
}
age := int64(currentInstanceTime.Sub(lastArchivedWal).Seconds())
return age, nil
}
func (c *postgreSQLClient) listDatabases(ctx context.Context) ([]string, error) {
query := `SELECT datname FROM pg_database
WHERE datistemplate = false;`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var databases []string
for rows.Next() {
var database string
if err := rows.Scan(&database); err != nil {
return nil, err
}
databases = append(databases, database)
}
return databases, nil
}
func filterQueryByDatabases(baseQuery string, databases []string, groupBy bool) string {
if len(databases) > 0 {
var queryDatabases []string
for _, db := range databases {
queryDatabases = append(queryDatabases, fmt.Sprintf("'%s'", db))
}
if strings.Contains(baseQuery, "WHERE") {
baseQuery += fmt.Sprintf(" AND datname IN (%s)", strings.Join(queryDatabases, ","))
} else {
baseQuery += fmt.Sprintf(" WHERE datname IN (%s)", strings.Join(queryDatabases, ","))
}
}
if groupBy {
baseQuery += " GROUP BY datname"
}
return baseQuery + ";"
}
func tableKey(database, table string) tableIdentifier {
return tableIdentifier(fmt.Sprintf("%s|%s", database, table))
}
func indexKey(database, table, index string) indexIdentifer {
return indexIdentifer(fmt.Sprintf("%s|%s|%s", database, table, index))
}