/
activity.go
105 lines (87 loc) · 2.74 KB
/
activity.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
package collector
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/uptrace/bun"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/1and1/pg-exporter/collector/models"
)
const (
// subsystem
activity = "sessions"
)
// additional command line options
var (
withUsername = kingpin.Flag("collect.pg_stat_activity.with_username",
"Include username in session statistics").
Default("false").Bool()
withApplicationName = kingpin.Flag("collect.pg_stat_activity.with_appname",
"Include application name in session statistics").
Default("false").Bool()
withClientAddr = kingpin.Flag("collect.pg_stat_activity.with_clientaddr",
"Include application name in session statistics").
Default("false").Bool()
withState = kingpin.Flag("collect.pg_stat_activity.with_state",
"Include session state in session statistics").
Default("true").Bool()
withWaitEventType = kingpin.Flag("collect.pg_stat_activity.with_wait_type",
"Include wait_event_type in session statistics").
Default("false").Bool()
withBackendType = kingpin.Flag("collect.pg_stat_activity.with_backend_type",
"Include backend_type in session statistics").
Default("false").Bool()
)
// ScrapeActivity scrapes from pg_stat_bgwriter
type ScrapeActivity struct{}
// Name of the Scraper
func (ScrapeActivity) Name() string {
return "pg_stat_activity"
}
// Help describes the role of the Scraper
func (ScrapeActivity) Help() string {
return "Collect from pg_stat_activity"
}
// Version returns minimum PostgreSQL version
func (ScrapeActivity) Version() int {
return 0
}
// Type returns the scrape type
func (ScrapeActivity) Type() ScrapeType {
return SCRAPEGLOBAL
}
// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeActivity) Scrape(ctx context.Context, db *bun.DB, ch chan<- prometheus.Metric) error {
// we create a query based on the given commandline flags
columns := ""
if *withUsername {
columns += ", usename"
}
if *withApplicationName {
columns += ", application_name"
}
if *withClientAddr {
columns += ", client_addr"
}
if *withState {
columns += ", state"
}
if *withWaitEventType {
columns += ", wait_event_type"
}
if pgversion >= 110000 && *withBackendType {
columns += ", backend_type"
}
qs := fmt.Sprintf(`SELECT datid, datname %s, count(1) as connections FROM`+
` pg_stat_activity WHERE datname IN (?) AND state IS NOT NULL and state != '' GROUP BY datid, datname %s`,
columns, columns)
var statActivity models.PgStatActivitySlice
rows, err := db.QueryContext(ctx, qs, bun.In(collectDatabases))
if err != nil {
return err
}
if err := db.ScanRows(ctx, rows, &statActivity); err != nil {
return err
}
return statActivity.ToMetrics(namespace, activity, ch)
}