feat(outputs.sql): Add settings for go sql.DB settings (#11551)
This commit is contained in:
parent
5b9aee11b8
commit
6721187e2e
|
|
@ -114,6 +114,20 @@ convert settings.
|
||||||
## the unsigned option. This is useful for a database like ClickHouse where
|
## the unsigned option. This is useful for a database like ClickHouse where
|
||||||
## the unsigned value should use a value like "uint64".
|
## the unsigned value should use a value like "uint64".
|
||||||
# conversion_style = "unsigned_suffix"
|
# 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
|
## Driver-specific information
|
||||||
|
|
|
||||||
|
|
@ -51,3 +51,17 @@
|
||||||
## the unsigned option. This is useful for a database like ClickHouse where
|
## the unsigned option. This is useful for a database like ClickHouse where
|
||||||
## the unsigned value should use a value like "uint64".
|
## the unsigned value should use a value like "uint64".
|
||||||
# conversion_style = "unsigned_suffix"
|
# 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
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
//Register sql drivers
|
//Register sql drivers
|
||||||
_ "github.com/ClickHouse/clickhouse-go" // clickhouse
|
_ "github.com/ClickHouse/clickhouse-go" // clickhouse
|
||||||
|
|
@ -34,13 +35,17 @@ type ConvertStruct struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SQL struct {
|
type SQL struct {
|
||||||
Driver string
|
Driver string
|
||||||
DataSourceName string
|
DataSourceName string
|
||||||
TimestampColumn string
|
TimestampColumn string
|
||||||
TableTemplate string
|
TableTemplate string
|
||||||
TableExistsTemplate string
|
TableExistsTemplate string
|
||||||
InitSQL string `toml:"init_sql"`
|
InitSQL string `toml:"init_sql"`
|
||||||
Convert ConvertStruct
|
Convert ConvertStruct
|
||||||
|
ConnectionMaxIdleTime time.Duration
|
||||||
|
ConnectionMaxLifetime time.Duration
|
||||||
|
ConnectionMaxIdle int
|
||||||
|
ConnectionMaxOpen int
|
||||||
|
|
||||||
db *gosql.DB
|
db *gosql.DB
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
@ -62,6 +67,11 @@ func (p *SQL) Connect() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.SetConnMaxIdleTime(p.ConnectionMaxIdleTime)
|
||||||
|
db.SetConnMaxLifetime(p.ConnectionMaxLifetime)
|
||||||
|
db.SetMaxIdleConns(p.ConnectionMaxIdle)
|
||||||
|
db.SetMaxOpenConns(p.ConnectionMaxOpen)
|
||||||
|
|
||||||
if p.InitSQL != "" {
|
if p.InitSQL != "" {
|
||||||
_, err = db.Exec(p.InitSQL)
|
_, err = db.Exec(p.InitSQL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -277,5 +287,11 @@ func newSQL() *SQL {
|
||||||
Bool: "BOOL",
|
Bool: "BOOL",
|
||||||
ConversionStyle: "unsigned_suffix",
|
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue