Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Add 'PostgresServiceConfig.LocalService' which defines service locality.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Jun 8, 2021
1 parent ebed2e3 commit 4a44408
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Config struct {

// PostgresServiceConfig defines Postgres-specific stuff required during collecting Postgres metrics.
type PostgresServiceConfig struct {
// LocalService defines service is running on the local host.
LocalService bool
// BlockSize defines size of data block Postgres operates.
BlockSize uint64
// WalSegmentSize defines size of WAL segment Postgres operates.
Expand Down Expand Up @@ -57,6 +59,9 @@ func NewPostgresServiceConfig(connStr string) (PostgresServiceConfig, error) {
return config, err
}

// Determine is service running locally.
config.LocalService = isAddressLocal(pgconfig.Host)

conn, err := store.NewWithConfig(pgconfig)
if err != nil {
return config, err
Expand Down Expand Up @@ -141,6 +146,14 @@ func NewPostgresServiceConfig(connStr string) (PostgresServiceConfig, error) {
return config, nil
}

// isAddressLocal return true if passed address is local, and return false otherwise.
func isAddressLocal(addr string) bool {
if strings.HasPrefix(addr, "/") || strings.HasPrefix(addr, "127.") || addr == "localhost" {
return true
}
return false
}

// discoverPgStatStatements discovers pg_stat_statements, what database and schema it is installed.
func discoverPgStatStatements(connStr string) (bool, string, string, error) {
pgconfig, err := pgx.ParseConfig(connStr)
Expand Down
18 changes: 18 additions & 0 deletions internal/collector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ func TestNewPostgresServiceConfig(t *testing.T) {
}
}

func Test_isAddressLocal(t *testing.T) {
testcases := []struct {
addr string
want bool
}{
{addr: "127.0.0.1", want: true},
{addr: "127.1.2.3", want: true},
{addr: "/var/run/postgresql", want: true},
{addr: "localhost", want: true},
{addr: "", want: false},
{addr: "1.2.3.4", want: false},
}

for _, tc := range testcases {
assert.Equal(t, tc.want, isAddressLocal(tc.addr))
}
}

func Test_discoverPgStatStatements(t *testing.T) {
testcases := []struct {
valid bool
Expand Down

0 comments on commit 4a44408

Please sign in to comment.