Skip to content

Commit

Permalink
[receiver/postgresql] Use postgres database when listing (#13931)
Browse files Browse the repository at this point in the history
* update postgresqlreceiver to use postgres database for listing
  • Loading branch information
schmikei committed Sep 7, 2022
1 parent f578742 commit 7ae3abe
Show file tree
Hide file tree
Showing 8 changed files with 718 additions and 730 deletions.
6 changes: 4 additions & 2 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func sslConnectionString(tls configtls.TLSClientSetting) string {
}

func newPostgreSQLClient(conf postgreSQLConfig) (*postgreSQLClient, error) {
dbField := ""
// postgres will assume the supplied user as the database name if none is provided,
// so we must specify a databse name even when we are just collecting the list of databases.
dbField := "dbname=postgres"
if conf.database != "" {
dbField = fmt.Sprintf("dbname=%s ", conf.database)
}
Expand All @@ -118,7 +120,7 @@ func newPostgreSQLClient(conf postgreSQLConfig) (*postgreSQLClient, error) {
host = fmt.Sprintf("/%s", host)
}

connStr := fmt.Sprintf("port=%s host=%s user=%s password=%s %s%s", port, host, conf.username, conf.password, dbField, sslConnectionString(conf.tls))
connStr := fmt.Sprintf("port=%s host=%s user=%s password=%s %s %s", port, host, conf.username, conf.password, dbField, sslConnectionString(conf.tls))

conn, err := pq.NewConnector(connStr)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions receiver/postgresqlreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestPostgreSQLIntegration(t *testing.T) {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Endpoint = net.JoinHostPort(hostname, "15432")
cfg.Databases = []string{"otel"}
cfg.Username = "otel"
cfg.Password = "otel"
cfg.Username = "otelu"
cfg.Password = "otelp"
cfg.Insecure = true
return cfg
},
Expand All @@ -71,8 +71,8 @@ func TestPostgreSQLIntegration(t *testing.T) {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Endpoint = net.JoinHostPort(hostname, "15432")
cfg.Databases = []string{"otel", "otel2"}
cfg.Username = "otel"
cfg.Password = "otel"
cfg.Username = "otelu"
cfg.Password = "otelp"
cfg.Insecure = true
return cfg
},
Expand All @@ -85,8 +85,8 @@ func TestPostgreSQLIntegration(t *testing.T) {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Endpoint = net.JoinHostPort(hostname, "15432")
cfg.Databases = []string{}
cfg.Username = "otel"
cfg.Password = "otel"
cfg.Username = "otelu"
cfg.Password = "otelp"
cfg.Insecure = true
return cfg
},
Expand All @@ -103,8 +103,8 @@ func TestPostgreSQLIntegration(t *testing.T) {
cfg := f.CreateDefaultConfig().(*Config)
cfg.Endpoint = net.JoinHostPort(hostname, "15432")
cfg.Databases = []string{}
cfg.Username = "otel"
cfg.Password = "otel"
cfg.Username = "otelu"
cfg.Password = "otelp"
cfg.Insecure = true
return cfg
},
Expand Down
474 changes: 237 additions & 237 deletions receiver/postgresqlreceiver/testdata/integration/expected_all_db.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

421 changes: 203 additions & 218 deletions receiver/postgresqlreceiver/testdata/integration/expected_multi_db.json

Large diffs are not rendered by default.

231 changes: 108 additions & 123 deletions receiver/postgresqlreceiver/testdata/integration/expected_single_db.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions receiver/postgresqlreceiver/testdata/integration/init.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE USER otel WITH PASSWORD 'otel';
GRANT SELECT ON pg_stat_database TO otel;
CREATE USER otelu WITH PASSWORD 'otelp';
GRANT SELECT ON pg_stat_database TO otelu;

CREATE TABLE table1 (
id serial PRIMARY KEY
Expand Down
16 changes: 16 additions & 0 deletions unreleased/postgres-list-postgres-user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# 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: Uses the postgres databasename when retrieving database inventory information

# One or more tracking issues related to the change
issues: [13641]

# (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:

0 comments on commit 7ae3abe

Please sign in to comment.