Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add settings for go sql.DB settings #11551

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions plugins/outputs/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions plugins/outputs/sql/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 23 additions & 7 deletions plugins/outputs/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "embed"
"fmt"
"strings"
"time"

//Register sql drivers
_ "github.com/ClickHouse/clickhouse-go" // clickhouse
Expand Down Expand Up @@ -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:"-"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
}