postgres: simplify proxy code (#80121)

This commit is contained in:
Gábor Farkas
2024-01-17 09:20:27 +01:00
committed by GitHub
parent e062fa1d7e
commit 2bf56b12cf
3 changed files with 21 additions and 89 deletions

View File

@ -16,6 +16,7 @@ import (
sdkproxy "github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/lib/pq"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana/pkg/setting"
@ -99,15 +100,22 @@ func (s *Service) newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFacto
logger.Debug("GetEngine", "connection", cnnstr)
}
driverName := "postgres"
// register a proxy driver if the secure socks proxy is enabled
connector, err := pq.NewConnector(cnnstr)
if err != nil {
logger.Error("postgres connector creation failed", "error", err)
return nil, fmt.Errorf("postgres connector creation failed")
}
// use the proxy-dialer if the secure socks proxy is enabled
proxyOpts := proxyutil.GetSQLProxyOptions(cfg.SecureSocksDSProxy, dsInfo)
if sdkproxy.New(proxyOpts).SecureSocksProxyEnabled() {
driverName, err = createPostgresProxyDriver(cnnstr, proxyOpts)
dialer, err := newPostgresProxyDialer(proxyOpts)
if err != nil {
logger.Error("postgres proxy creation failed", "error", err)
return nil, fmt.Errorf("postgres proxy creation failed")
}
// update the postgres dialer with the proxy dialer
connector.Dialer(dialer)
}
config := sqleng.DataPluginConfiguration{
@ -118,10 +126,7 @@ func (s *Service) newInstanceSettings(cfg *setting.Cfg) datasource.InstanceFacto
queryResultTransformer := postgresQueryResultTransformer{}
db, err := sql.Open(driverName, cnnstr)
if err != nil {
return nil, err
}
db := sql.OpenDB(connector)
db.SetMaxOpenConns(config.DSInfo.JsonData.MaxOpenConns)
db.SetMaxIdleConns(config.DSInfo.JsonData.MaxIdleConns)