forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
281 lines (235 loc) · 7.1 KB
/
sql.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
package sql
import (
gosql "database/sql"
"fmt"
"strings"
//Register sql drivers
_ "github.com/denisenkom/go-mssqldb" // mssql (sql server)
_ "github.com/go-sql-driver/mysql" // mysql
_ "github.com/jackc/pgx/v4/stdlib" // pgx (postgres)
_ "github.com/snowflakedb/gosnowflake" // snowflake
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs"
)
type ConvertStruct struct {
Integer string
Real string
Text string
Timestamp string
Defaultvalue string
Unsigned string
Bool string
}
type SQL struct {
Driver string
DataSourceName string
TimestampColumn string
TableTemplate string
TableExistsTemplate string
InitSQL string `toml:"init_sql"`
Convert ConvertStruct
db *gosql.DB
Log telegraf.Logger `toml:"-"`
tables map[string]bool
}
func (p *SQL) Connect() error {
db, err := gosql.Open(p.Driver, p.DataSourceName)
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
if p.InitSQL != "" {
_, err = db.Exec(p.InitSQL)
if err != nil {
return err
}
}
p.db = db
p.tables = make(map[string]bool)
return nil
}
func (p *SQL) Close() error {
return p.db.Close()
}
// Quote an identifier (table or column name)
func quoteIdent(name string) string {
return `"` + strings.Replace(sanitizeQuoted(name), `"`, `""`, -1) + `"`
}
// Quote a string literal
func quoteStr(name string) string {
return "'" + strings.Replace(name, "'", "''", -1) + "'"
}
func sanitizeQuoted(in string) string {
// https://dev.mysql.com/doc/refman/8.0/en/identifiers.html
// https://www.postgresql.org/docs/13/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
// Whitelist allowed characters
return strings.Map(func(r rune) rune {
switch {
case r >= '\u0001' && r <= '\uFFFF':
return r
default:
return '_'
}
}, in)
}
func (p *SQL) deriveDatatype(value interface{}) string {
var datatype string
switch value.(type) {
case int64:
datatype = p.Convert.Integer
case uint64:
datatype = fmt.Sprintf("%s %s", p.Convert.Integer, p.Convert.Unsigned)
case float64:
datatype = p.Convert.Real
case string:
datatype = p.Convert.Text
case bool:
datatype = p.Convert.Bool
default:
datatype = p.Convert.Defaultvalue
p.Log.Errorf("Unknown datatype: '%T' %v", value, value)
}
return datatype
}
var sampleConfig = `
## Database driver
## Valid options: mssql (Microsoft SQL Server), mysql (MySQL), pgx (Postgres),
## sqlite (SQLite3), snowflake (snowflake.com)
# driver = ""
## Data source name
## The format of the data source name is different for each database driver.
## See the plugin readme for details.
# data_source_name = ""
## Timestamp column name
# timestamp_column = "timestamp"
## Table creation template
## Available template variables:
## {TABLE} - table name as a quoted identifier
## {TABLELITERAL} - table name as a quoted string literal
## {COLUMNS} - column definitions (list of quoted identifiers and types)
# table_template = "CREATE TABLE {TABLE}({COLUMNS})"
## Table existence check template
## Available template variables:
## {TABLE} - tablename as a quoted identifier
# table_exists_template = "SELECT 1 FROM {TABLE} LIMIT 1"
## Initialization SQL
# init_sql = ""
## Metric type to SQL type conversion
#[outputs.sql.convert]
# integer = "INT"
# real = "DOUBLE"
# text = "TEXT"
# timestamp = "TIMESTAMP"
# defaultvalue = "TEXT"
# unsigned = "UNSIGNED"
`
func (p *SQL) SampleConfig() string { return sampleConfig }
func (p *SQL) Description() string { return "Send metrics to SQL Database" }
func (p *SQL) generateCreateTable(metric telegraf.Metric) string {
var columns []string
// ## {KEY_COLUMNS} is a comma-separated list of key columns (timestamp and tags)
//var pk []string
if p.TimestampColumn != "" {
//pk = append(pk, quoteIdent(p.TimestampColumn))
columns = append(columns, fmt.Sprintf("%s %s", quoteIdent(p.TimestampColumn), p.Convert.Timestamp))
}
for _, tag := range metric.TagList() {
//pk = append(pk, quoteIdent(tag.Key))
columns = append(columns, fmt.Sprintf("%s %s", quoteIdent(tag.Key), p.Convert.Text))
}
var datatype string
for _, field := range metric.FieldList() {
datatype = p.deriveDatatype(field.Value)
columns = append(columns, fmt.Sprintf("%s %s", quoteIdent(field.Key), datatype))
}
query := p.TableTemplate
query = strings.Replace(query, "{TABLE}", quoteIdent(metric.Name()), -1)
query = strings.Replace(query, "{TABLELITERAL}", quoteStr(metric.Name()), -1)
query = strings.Replace(query, "{COLUMNS}", strings.Join(columns, ","), -1)
//query = strings.Replace(query, "{KEY_COLUMNS}", strings.Join(pk, ","), -1)
return query
}
func (p *SQL) generateInsert(tablename string, columns []string) string {
var placeholders, quotedColumns []string
for _, column := range columns {
quotedColumns = append(quotedColumns, quoteIdent(column))
}
if p.Driver == "pgx" {
// Postgres uses $1 $2 $3 as placeholders
for i := 0; i < len(columns); i++ {
placeholders = append(placeholders, fmt.Sprintf("$%d", i+1))
}
} else {
// Everything else uses ? ? ? as placeholders
for i := 0; i < len(columns); i++ {
placeholders = append(placeholders, "?")
}
}
return fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)",
quoteIdent(tablename),
strings.Join(quotedColumns, ","),
strings.Join(placeholders, ","))
}
func (p *SQL) tableExists(tableName string) bool {
stmt := strings.Replace(p.TableExistsTemplate, "{TABLE}", quoteIdent(tableName), -1)
_, err := p.db.Exec(stmt)
return err == nil
}
func (p *SQL) Write(metrics []telegraf.Metric) error {
for _, metric := range metrics {
tablename := metric.Name()
// create table if needed
if !p.tables[tablename] && !p.tableExists(tablename) {
createStmt := p.generateCreateTable(metric)
_, err := p.db.Exec(createStmt)
if err != nil {
return err
}
p.tables[tablename] = true
}
var columns []string
var values []interface{}
if p.TimestampColumn != "" {
columns = append(columns, p.TimestampColumn)
values = append(values, metric.Time())
}
for column, value := range metric.Tags() {
columns = append(columns, column)
values = append(values, value)
}
for column, value := range metric.Fields() {
columns = append(columns, column)
values = append(values, value)
}
sql := p.generateInsert(tablename, columns)
_, err := p.db.Exec(sql, values...)
if err != nil {
// check if insert error was caused by column mismatch
p.Log.Errorf("Error during insert: %v, %v", err, sql)
return err
}
}
return nil
}
func init() {
outputs.Add("sql", func() telegraf.Output { return newSQL() })
}
func newSQL() *SQL {
return &SQL{
TableTemplate: "CREATE TABLE {TABLE}({COLUMNS})",
TableExistsTemplate: "SELECT 1 FROM {TABLE} LIMIT 1",
TimestampColumn: "timestamp",
Convert: ConvertStruct{
Integer: "INT",
Real: "DOUBLE",
Text: "TEXT",
Timestamp: "TIMESTAMP",
Defaultvalue: "TEXT",
Unsigned: "UNSIGNED",
Bool: "BOOL",
},
}
}