fix(inputs.postgresql_extensible): Restore outputaddress behavior (#13972)

This commit is contained in:
Joshua Powers 2023-09-26 08:19:23 -06:00 committed by GitHub
parent 3b00b1da95
commit ebb20bfa4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 9 deletions

View File

@ -175,7 +175,12 @@ func (p *Service) SanitizedAddress() (sanitizedAddress string, err error) {
}
// GetConnectDatabase utility function for getting the database to which the connection was made
// If the user set the output address use that before parsing anything else.
func (p *Service) GetConnectDatabase(connectionString string) (string, error) {
if p.OutputAddress != "" {
return p.OutputAddress, nil
}
connConfig, err := pgx.ParseConfig(connectionString)
if err != nil {
return "", fmt.Errorf("connection string parsing failed: %w", err)

View File

@ -294,21 +294,48 @@ func TestPostgresqlIgnoresUnwantedColumnsIntegration(t *testing.T) {
func TestAccRow(t *testing.T) {
p := Postgresql{
Log: testutil.Logger{},
Service: postgresql.Service{
OutputAddress: "server",
},
}
var acc testutil.Accumulator
columns := []string{"datname", "cat"}
testRows := []fakeRow{
{fields: []interface{}{1, "gato"}},
{fields: []interface{}{nil, "gato"}},
{fields: []interface{}{"name", "gato"}},
tests := []struct {
fields fakeRow
dbName string
server string
}{
{
fields: fakeRow{
fields: []interface{}{1, "gato"},
},
dbName: "server",
server: "server",
},
{
fields: fakeRow{
fields: []interface{}{nil, "gato"},
},
dbName: "server",
server: "server",
},
{
fields: fakeRow{
fields: []interface{}{"name", "gato"},
},
dbName: "name",
server: "server",
},
}
for i := range testRows {
err := p.accRow("pgTEST", testRows[i], &acc, columns)
if err != nil {
t.Fatalf("Scan failed: %s", err)
}
for _, tt := range tests {
require.NoError(t, p.accRow("pgTEST", tt.fields, &acc, columns))
require.Equal(t, 1, len(acc.Metrics))
metric := acc.Metrics[0]
require.Equal(t, tt.dbName, metric.Tags["db"])
require.Equal(t, tt.server, metric.Tags["server"])
acc.ClearMetrics()
}
}