Postgres: Config diagnostics / logging update (#105666)

* config diagnostics update

* update tests

* fix typo
This commit is contained in:
Sriram
2025-05-22 08:27:36 +01:00
committed by GitHub
parent d692bce405
commit a8e87fc674
6 changed files with 18 additions and 17 deletions

View File

@ -176,7 +176,7 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
var port int
if strings.HasPrefix(dsInfo.URL, "/") {
host = dsInfo.URL
logger.Debug("Generating connection string with Unix socket specifier", "socket", host)
logger.Debug("Generating connection string with Unix socket specifier", "address", dsInfo.URL)
} else {
index := strings.LastIndex(dsInfo.URL, ":")
v6Index := strings.Index(dsInfo.URL, "]")
@ -187,12 +187,12 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
var err error
port, err = strconv.Atoi(sp[1])
if err != nil {
return "", fmt.Errorf("%w %q: %w", sqleng.ErrInvalidPortSpecified, sp[1], err)
logger.Debug("Error parsing the IPv4 address", "address", dsInfo.URL)
return "", sqleng.ErrParsingPostgresURL
}
logger.Debug("Generating connection string with network host/port pair", "host", host, "port", port)
logger.Debug("Generating IPv4 connection string with network host/port pair", "host", host, "port", port, "address", dsInfo.URL)
} else {
logger.Debug("Generating connection string with network host", "host", host)
logger.Debug("Generating IPv4 connection string with network host", "host", host, "address", dsInfo.URL)
}
} else {
if index == v6Index+1 {
@ -200,13 +200,13 @@ func (s *Service) generateConnectionString(dsInfo sqleng.DataSourceInfo) (string
var err error
port, err = strconv.Atoi(dsInfo.URL[index+1:])
if err != nil {
return "", fmt.Errorf("%w %q: %w", sqleng.ErrInvalidPortSpecified, dsInfo.URL[index+1:], err)
logger.Debug("Error parsing the IPv6 address", "address", dsInfo.URL)
return "", sqleng.ErrParsingPostgresURL
}
logger.Debug("Generating ipv6 connection string with network host/port pair", "host", host, "port", port)
logger.Debug("Generating IPv6 connection string with network host/port pair", "host", host, "port", port, "address", dsInfo.URL)
} else {
host = dsInfo.URL[1 : len(dsInfo.URL)-1]
logger.Debug("Generating ipv6 connection string with network host", "host", host)
logger.Debug("Generating IPv6 connection string with network host", "host", host, "address", dsInfo.URL)
}
}
}

View File

@ -99,7 +99,7 @@ func TestIntegrationGenerateConnectionString(t *testing.T) {
user: "user",
database: "database",
tlsSettings: tlsSettings{},
expErr: "invalid port in host specifier",
expErr: "error parsing postgres url",
},
{
desc: "Password with single quote and backslash",

View File

@ -3,5 +3,6 @@ package sqleng
import "errors"
var (
ErrInvalidPortSpecified error = errors.New("invalid port in host specifier")
ErrParsingPostgresURL error = errors.New("error parsing postgres url")
ErrCertFileNotExist error = errors.New("certificate file doesn't exist")
)

View File

@ -81,8 +81,8 @@ func ErrToHealthCheckResult(err error) (*backend.CheckHealthResult, error) {
details["verboseMessage"] = pqErr.Message
}
}
if errors.Is(err, ErrInvalidPortSpecified) {
res.Message = fmt.Sprintf("Connection string error: %s", ErrInvalidPortSpecified.Error())
if errors.Is(err, ErrParsingPostgresURL) {
res.Message = fmt.Sprintf("Connection string error: %s", ErrParsingPostgresURL.Error())
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
details["verboseMessage"] = unwrappedErr.Error()
}

View File

@ -51,11 +51,11 @@ func TestErrToHealthCheckResult(t *testing.T) {
},
{
name: "invalid port specifier error",
err: fmt.Errorf("%w %q: %w", ErrInvalidPortSpecified, `"foo.bar.co"`, errors.New(`strconv.Atoi: parsing "foo.bar.co": invalid syntax`)),
err: fmt.Errorf("%w %q: %w", ErrParsingPostgresURL, `"foo.bar.co"`, errors.New(`strconv.Atoi: parsing "foo.bar.co": invalid syntax`)),
want: &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "Connection string error: invalid port in host specifier",
JSONDetails: []byte(`{"errorDetailsLink":"https://grafana.com/docs/grafana/latest/datasources/postgres","verboseMessage":"invalid port in host specifier \"\\\"foo.bar.co\\\"\": strconv.Atoi: parsing \"foo.bar.co\": invalid syntax"}`),
Message: "Connection string error: error parsing postgres url",
JSONDetails: []byte(`{"errorDetailsLink":"https://grafana.com/docs/grafana/latest/datasources/postgres","verboseMessage":"error parsing postgres url \"\\\"foo.bar.co\\\"\": strconv.Atoi: parsing \"foo.bar.co\": invalid syntax"}`),
},
},
}

View File

@ -229,7 +229,7 @@ func validateCertFilePaths(rootCert, clientCert, clientKey string) error {
return err
}
if !exists {
return fmt.Errorf("certificate file %q doesn't exist", fpath)
return sqleng.ErrCertFileNotExist
}
}
return nil