/
postgresql_extensible.go
283 lines (242 loc) · 6.12 KB
/
postgresql_extensible.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
//go:generate ../../../tools/readme_config_includer/generator
package postgresql_extensible
import (
"bytes"
_ "embed"
"fmt"
"io"
"os"
"strings"
"time"
// Required for SQL framework driver
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/postgresql"
)
// DO NOT REMOVE THE NEXT TWO LINES! This is required to embed the sampleConfig data.
//go:embed sample.conf
var sampleConfig string
type Postgresql struct {
postgresql.Service
Databases []string `deprecated:"1.22.4;use the sqlquery option to specify database to use"`
AdditionalTags []string
Timestamp string
Query query
Debug bool
PreparedStatements bool `toml:"prepared_statements"`
Log telegraf.Logger
}
type query []struct {
Sqlquery string
Script string
Version int
Withdbname bool `deprecated:"1.22.4;use the sqlquery option to specify database to use"`
Tagvalue string
Measurement string
Timestamp string
}
var ignoredColumns = map[string]bool{"stats_reset": true}
func (*Postgresql) SampleConfig() string {
return sampleConfig
}
func (p *Postgresql) Init() error {
var err error
for i := range p.Query {
if p.Query[i].Sqlquery == "" {
p.Query[i].Sqlquery, err = ReadQueryFromFile(p.Query[i].Script)
if err != nil {
return err
}
}
}
p.Service.IsPgBouncer = !p.PreparedStatements
return nil
}
func (p *Postgresql) IgnoredColumns() map[string]bool {
return ignoredColumns
}
func ReadQueryFromFile(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
query, err := io.ReadAll(file)
if err != nil {
return "", err
}
return string(query), err
}
func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
var (
err error
sqlQuery string
queryAddon string
dbVersion int
query string
measName string
)
// Retrieving the database version
query = `SELECT setting::integer / 100 AS version FROM pg_settings WHERE name = 'server_version_num'`
if err = p.DB.QueryRow(query).Scan(&dbVersion); err != nil {
dbVersion = 0
}
// We loop in order to process each query
// Query is not run if Database version does not match the query version.
for i := range p.Query {
sqlQuery = p.Query[i].Sqlquery
if p.Query[i].Measurement != "" {
measName = p.Query[i].Measurement
} else {
measName = "postgresql"
}
if p.Query[i].Withdbname {
if len(p.Databases) != 0 {
queryAddon = fmt.Sprintf(` IN ('%s')`, strings.Join(p.Databases, "','"))
} else {
queryAddon = " is not null"
}
} else {
queryAddon = ""
}
sqlQuery += queryAddon
if p.Query[i].Version <= dbVersion {
p.gatherMetricsFromQuery(acc, sqlQuery, p.Query[i].Tagvalue, p.Query[i].Timestamp, measName)
}
}
return nil
}
func (p *Postgresql) gatherMetricsFromQuery(acc telegraf.Accumulator, sqlQuery string, tagValue string, timestamp string, measName string) {
var columns []string
rows, err := p.DB.Query(sqlQuery)
if err != nil {
acc.AddError(err)
return
}
defer rows.Close()
// grab the column information from the result
if columns, err = rows.Columns(); err != nil {
acc.AddError(err)
return
}
p.AdditionalTags = nil
if tagValue != "" {
tagList := strings.Split(tagValue, ",")
for t := range tagList {
p.AdditionalTags = append(p.AdditionalTags, tagList[t])
}
}
p.Timestamp = timestamp
for rows.Next() {
err = p.accRow(measName, rows, acc, columns)
if err != nil {
acc.AddError(err)
break
}
}
}
type scanner interface {
Scan(dest ...interface{}) error
}
func (p *Postgresql) accRow(measName string, row scanner, acc telegraf.Accumulator, columns []string) error {
var (
err error
columnVars []interface{}
dbname bytes.Buffer
tagAddress string
timestamp time.Time
)
// this is where we'll store the column name with its *interface{}
columnMap := make(map[string]*interface{})
for _, column := range columns {
columnMap[column] = new(interface{})
}
// populate the array of interface{} with the pointers in the right order
for i := 0; i < len(columnMap); i++ {
columnVars = append(columnVars, columnMap[columns[i]])
}
// deconstruct array of variables and send to Scan
if err = row.Scan(columnVars...); err != nil {
return err
}
if c, ok := columnMap["datname"]; ok && *c != nil {
// extract the database name from the column map
switch datname := (*c).(type) {
case string:
if _, err := dbname.WriteString(datname); err != nil {
return err
}
default:
if _, err := dbname.WriteString("postgres"); err != nil {
return err
}
}
} else {
if _, err := dbname.WriteString("postgres"); err != nil {
return err
}
}
if tagAddress, err = p.SanitizedAddress(); err != nil {
return err
}
// Process the additional tags
tags := map[string]string{
"server": tagAddress,
"db": dbname.String(),
}
// set default timestamp to Now
timestamp = time.Now()
fields := make(map[string]interface{})
COLUMN:
for col, val := range columnMap {
p.Log.Debugf("Column: %s = %T: %v\n", col, *val, *val)
_, ignore := ignoredColumns[col]
if ignore || *val == nil {
continue
}
if col == p.Timestamp {
if v, ok := (*val).(time.Time); ok {
timestamp = v
}
continue
}
for _, tag := range p.AdditionalTags {
if col != tag {
continue
}
switch v := (*val).(type) {
case string:
tags[col] = v
case []byte:
tags[col] = string(v)
case int64, int32, int:
tags[col] = fmt.Sprintf("%d", v)
default:
p.Log.Debugf("Failed to add %q as additional tag", col)
}
continue COLUMN
}
if v, ok := (*val).([]byte); ok {
fields[col] = string(v)
} else {
fields[col] = *val
}
}
acc.AddFields(measName, fields, tags, timestamp)
return nil
}
func init() {
inputs.Add("postgresql_extensible", func() telegraf.Input {
return &Postgresql{
Service: postgresql.Service{
MaxIdle: 1,
MaxOpen: 1,
MaxLifetime: config.Duration(0),
IsPgBouncer: false,
},
PreparedStatements: true,
}
})
}