feat(outputs.sql): Add settings for go sql.DB settings (#11551)

This commit is contained in:
reimda 2022-07-29 11:53:08 -06:00 committed by GitHub
parent 5b9aee11b8
commit 6721187e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 7 deletions

View File

@ -114,6 +114,20 @@ convert settings.
## the unsigned option. This is useful for a database like ClickHouse where
## the unsigned value should use a value like "uint64".
# conversion_style = "unsigned_suffix"
## Maximum amount of time a connection may be idle. "0s" means connections are
## never closed due to idle time.
# connection_max_idle_time = "0s"
## Maximum amount of time a connection may be reused. "0s" means connections
## are never closed due to age.
# connection_max_lifetime = "0s"
## Maximum number of connections in the idle connection pool. 0 means unlimited.
# connection_max_idle = 2
## Maximum number of open connections to the database. 0 means unlimited.
# connection_max_open = 0
```
## Driver-specific information

View File

@ -51,3 +51,17 @@
## the unsigned option. This is useful for a database like ClickHouse where
## the unsigned value should use a value like "uint64".
# conversion_style = "unsigned_suffix"
## Maximum amount of time a connection may be idle. "0s" means connections are
## never closed due to idle time.
# connection_max_idle_time = "0s"
## Maximum amount of time a connection may be reused. "0s" means connections
## are never closed due to age.
# connection_max_lifetime = "0s"
## Maximum number of connections in the idle connection pool. 0 means unlimited.
# connection_max_idle = 2
## Maximum number of open connections to the database. 0 means unlimited.
# connection_max_open = 0

View File

@ -6,6 +6,7 @@ import (
_ "embed"
"fmt"
"strings"
"time"
//Register sql drivers
_ "github.com/ClickHouse/clickhouse-go" // clickhouse
@ -34,13 +35,17 @@ type ConvertStruct struct {
}
type SQL struct {
Driver string
DataSourceName string
TimestampColumn string
TableTemplate string
TableExistsTemplate string
InitSQL string `toml:"init_sql"`
Convert ConvertStruct
Driver string
DataSourceName string
TimestampColumn string
TableTemplate string
TableExistsTemplate string
InitSQL string `toml:"init_sql"`
Convert ConvertStruct
ConnectionMaxIdleTime time.Duration
ConnectionMaxLifetime time.Duration
ConnectionMaxIdle int
ConnectionMaxOpen int
db *gosql.DB
Log telegraf.Logger `toml:"-"`
@ -62,6 +67,11 @@ func (p *SQL) Connect() error {
return err
}
db.SetConnMaxIdleTime(p.ConnectionMaxIdleTime)
db.SetConnMaxLifetime(p.ConnectionMaxLifetime)
db.SetMaxIdleConns(p.ConnectionMaxIdle)
db.SetMaxOpenConns(p.ConnectionMaxOpen)
if p.InitSQL != "" {
_, err = db.Exec(p.InitSQL)
if err != nil {
@ -277,5 +287,11 @@ func newSQL() *SQL {
Bool: "BOOL",
ConversionStyle: "unsigned_suffix",
},
// Defaults for the connection settings (ConnectionMaxIdleTime,
// ConnectionMaxLifetime, ConnectionMaxIdle, and ConnectionMaxOpen)
// mirror the golang defaults. As of go 1.18 all of them default to 0
// except max idle connections which is 2. See
// https://pkg.go.dev/database/sql#DB.SetMaxIdleConns
ConnectionMaxIdle: 2,
}
}