-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Description:** <Describe what has changed.> Reuse of connections created per database (configured or discovered) vs current behavior to create & close connection per database on each scrape. **Link to tracking Issue:** #30831 **Testing:** Updated unit & integration tests. Also, ran locally multiple scenario: - no feature gate specified (default): current behavior maintained, connections created/closed on each database per scrape - feature gate connection pool enabled, no connection pool config specified (default): reduction of the number of connections created/closed - feature gate connection pool enabled, connection pool config tweaked: connections created on first scrape & closed when configured lifetime reached or collector shutdown **Documentation:** - change log - readme for the feature gate & related optional configurations linked to this feature **Note** Checking internally for getting the CLA signed
- Loading branch information
1 parent
15ceef1
commit a57bec6
Showing
14 changed files
with
3,442 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: postgresqlreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Add `receiver.postgresql.connectionPool` feature gate to reuse database connections" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [30831] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | | ||
The default implementation recreates and closes connections on each scrape per database configured/discovered. | ||
This change offers a feature gated alternative to keep connections open. Also, it exposes connection configuration to control the behavior of the pool. | ||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package postgresqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver" | ||
|
||
import ( | ||
"database/sql" | ||
"sync" | ||
|
||
"github.com/lib/pq" | ||
"go.opentelemetry.io/collector/featuregate" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
const connectionPoolGateID = "receiver.postgresql.connectionPool" | ||
|
||
var ( | ||
connectionPoolGate = featuregate.GlobalRegistry().MustRegister( | ||
connectionPoolGateID, | ||
featuregate.StageAlpha, | ||
featuregate.WithRegisterDescription("Use of connection pooling"), | ||
featuregate.WithRegisterFromVersion("0.96.0"), | ||
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/30831"), | ||
) | ||
) | ||
|
||
type postgreSQLClientFactory interface { | ||
getClient(database string) (client, error) | ||
close() error | ||
} | ||
|
||
// defaultClientFactory creates one PG connection per call | ||
type defaultClientFactory struct { | ||
baseConfig postgreSQLConfig | ||
} | ||
|
||
func newDefaultClientFactory(cfg *Config) *defaultClientFactory { | ||
return &defaultClientFactory{ | ||
baseConfig: postgreSQLConfig{ | ||
username: cfg.Username, | ||
password: string(cfg.Password), | ||
address: cfg.AddrConfig, | ||
tls: cfg.TLSClientSetting, | ||
}, | ||
} | ||
} | ||
|
||
func (d *defaultClientFactory) getClient(database string) (client, error) { | ||
db, err := getDB(d.baseConfig, database) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &postgreSQLClient{client: db, closeFn: db.Close}, nil | ||
} | ||
|
||
func (d *defaultClientFactory) close() error { | ||
return nil | ||
} | ||
|
||
// poolClientFactory creates one PG connection per database, keeping a pool of connections | ||
type poolClientFactory struct { | ||
sync.Mutex | ||
baseConfig postgreSQLConfig | ||
poolConfig *ConnectionPool | ||
pool map[string]*sql.DB | ||
closed bool | ||
} | ||
|
||
func newPoolClientFactory(cfg *Config) *poolClientFactory { | ||
poolCfg := cfg.ConnectionPool | ||
return &poolClientFactory{ | ||
baseConfig: postgreSQLConfig{ | ||
username: cfg.Username, | ||
password: string(cfg.Password), | ||
address: cfg.AddrConfig, | ||
tls: cfg.TLSClientSetting, | ||
}, | ||
poolConfig: &poolCfg, | ||
pool: make(map[string]*sql.DB), | ||
closed: false, | ||
} | ||
} | ||
|
||
func (p *poolClientFactory) getClient(database string) (client, error) { | ||
p.Lock() | ||
defer p.Unlock() | ||
db, ok := p.pool[database] | ||
if !ok { | ||
var err error | ||
db, err = getDB(p.baseConfig, database) | ||
p.setPoolSettings(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.pool[database] = db | ||
} | ||
return &postgreSQLClient{client: db, closeFn: nil}, nil | ||
} | ||
|
||
func (p *poolClientFactory) close() error { | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
if p.closed { | ||
return nil | ||
} | ||
|
||
if p.pool != nil { | ||
var err error | ||
for _, db := range p.pool { | ||
if closeErr := db.Close(); closeErr != nil { | ||
err = multierr.Append(err, closeErr) | ||
} | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
p.closed = true | ||
return nil | ||
} | ||
|
||
func (p *poolClientFactory) setPoolSettings(db *sql.DB) { | ||
if p.poolConfig == nil { | ||
return | ||
} | ||
if p.poolConfig.MaxIdleTime != nil { | ||
db.SetConnMaxIdleTime(*p.poolConfig.MaxIdleTime) | ||
} | ||
if p.poolConfig.MaxLifetime != nil { | ||
db.SetConnMaxLifetime(*p.poolConfig.MaxLifetime) | ||
} | ||
if p.poolConfig.MaxIdle != nil { | ||
db.SetMaxIdleConns(*p.poolConfig.MaxIdle) | ||
} | ||
if p.poolConfig.MaxOpen != nil { | ||
db.SetMaxOpenConns(*p.poolConfig.MaxOpen) | ||
} | ||
} | ||
|
||
func getDB(cfg postgreSQLConfig, database string) (*sql.DB, error) { | ||
if database != "" { | ||
cfg.database = database | ||
} | ||
connectionString, err := cfg.ConnectionString() | ||
if err != nil { | ||
return nil, err | ||
} | ||
conn, err := pq.NewConnector(connectionString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return sql.OpenDB(conn), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.