mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 12:22:15 +08:00
postgres: simplify proxy code (#80121)
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user