Skip to content

Commit

Permalink
feat(outputs.sql): Add settings for go sql.DB settings (#11551)
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda authored Jul 29, 2022
1 parent 5b9aee1 commit 6721187
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
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,
}
}

0 comments on commit 6721187

Please sign in to comment.