diff --git a/cmd/pgscv.go b/cmd/pgscv.go index 737c19e..13fbf0c 100644 --- a/cmd/pgscv.go +++ b/cmd/pgscv.go @@ -1,3 +1,4 @@ +// Package main is a pgSCV main package package main import ( diff --git a/internal/collector/collector.go b/internal/collector/collector.go index f5a6738..a5c87c8 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/collector_common.go b/internal/collector/collector_common.go index fc2c19e..2e57fa3 100644 --- a/internal/collector/collector_common.go +++ b/internal/collector/collector_common.go @@ -1,16 +1,18 @@ +// Package collector is a pgSCV collectors package collector import ( "database/sql" - "github.com/jackc/pgx/v4" + "regexp" + "strconv" + "strings" + "github.com/cherts/pgscv/internal/filter" "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" + "github.com/jackc/pgx/v4" "github.com/prometheus/client_golang/prometheus" - "regexp" - "strconv" - "strings" ) // labels is a local wrapper over prometheus.Labels which is a simple map[string]string. diff --git a/internal/collector/config.go b/internal/collector/config.go index 9345077..28c0412 100644 --- a/internal/collector/config.go +++ b/internal/collector/config.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/filesystem_common.go b/internal/collector/filesystem_common.go index 756c1a8..98c7045 100644 --- a/internal/collector/filesystem_common.go +++ b/internal/collector/filesystem_common.go @@ -1,12 +1,14 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "fmt" - "github.com/cherts/pgscv/internal/log" "io" "os" "strings" + + "github.com/cherts/pgscv/internal/log" ) // mount describes properties of mounted filesystems diff --git a/internal/collector/linux_cpu.go b/internal/collector/linux_cpu.go index 368747b..d78844e 100644 --- a/internal/collector/linux_cpu.go +++ b/internal/collector/linux_cpu.go @@ -1,18 +1,20 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "bytes" "fmt" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "os/exec" "path/filepath" "strconv" "strings" + + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) type cpuCollector struct { diff --git a/internal/collector/linux_diskstats.go b/internal/collector/linux_diskstats.go index 92ba52c..18a2d77 100644 --- a/internal/collector/linux_diskstats.go +++ b/internal/collector/linux_diskstats.go @@ -1,19 +1,21 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "bytes" "fmt" - "github.com/cherts/pgscv/internal/filter" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "path/filepath" "regexp" "strconv" "strings" + + "github.com/cherts/pgscv/internal/filter" + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) const ( diff --git a/internal/collector/linux_filesystem.go b/internal/collector/linux_filesystem.go index e5ad0b9..04371ef 100644 --- a/internal/collector/linux_filesystem.go +++ b/internal/collector/linux_filesystem.go @@ -1,16 +1,18 @@ +// Package collector is a pgSCV collectors package collector import ( "errors" "fmt" - "github.com/cherts/pgscv/internal/filter" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "syscall" "time" + + "github.com/cherts/pgscv/internal/filter" + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) var ( diff --git a/internal/collector/linux_load_average.go b/internal/collector/linux_load_average.go index 27e0754..2986e70 100644 --- a/internal/collector/linux_load_average.go +++ b/internal/collector/linux_load_average.go @@ -1,13 +1,15 @@ +// Package collector is a pgSCV collectors package collector import ( "fmt" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "os" "strconv" "strings" + + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) type loadaverageCollector struct { diff --git a/internal/collector/linux_memory.go b/internal/collector/linux_memory.go index 45bf55d..9b4ac36 100644 --- a/internal/collector/linux_memory.go +++ b/internal/collector/linux_memory.go @@ -1,17 +1,19 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "fmt" - "github.com/cherts/pgscv/internal/filter" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "regexp" "strconv" "strings" + + "github.com/cherts/pgscv/internal/filter" + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) type meminfoCollector struct { diff --git a/internal/collector/linux_netdev.go b/internal/collector/linux_netdev.go index 2f4116e..25e48c7 100644 --- a/internal/collector/linux_netdev.go +++ b/internal/collector/linux_netdev.go @@ -1,16 +1,18 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "fmt" - "github.com/cherts/pgscv/internal/filter" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "strconv" "strings" + + "github.com/cherts/pgscv/internal/filter" + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) type netdevCollector struct { diff --git a/internal/collector/linux_network.go b/internal/collector/linux_network.go index 5779699..fc82787 100644 --- a/internal/collector/linux_network.go +++ b/internal/collector/linux_network.go @@ -1,12 +1,14 @@ +// Package collector is a pgSCV collectors package collector import ( "fmt" + "net" + "strings" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/prometheus/client_golang/prometheus" - "net" - "strings" ) type networkCollector struct { @@ -14,6 +16,7 @@ type networkCollector struct { publicAddresses typedDesc } +// NewNetworkCollector returns a new Collector exposing network interfaces addresses. func NewNetworkCollector(constLabels labels, settings model.CollectorSettings) (Collector, error) { return &networkCollector{ publicAddresses: newBuiltinTypedDesc( diff --git a/internal/collector/linux_sysconfig.go b/internal/collector/linux_sysconfig.go index b26738c..64e2a59 100644 --- a/internal/collector/linux_sysconfig.go +++ b/internal/collector/linux_sysconfig.go @@ -1,12 +1,10 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "bytes" "fmt" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "path" @@ -14,6 +12,10 @@ import ( "regexp" "strconv" "strings" + + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) type systemCollector struct { @@ -27,7 +29,7 @@ type systemCollector struct { btime typedDesc } -// NewSystemCollector returns a new Collector exposing system-wide stats. +// NewSysconfigCollector returns a new Collector exposing system-wide stats. func NewSysconfigCollector(constLabels labels, settings model.CollectorSettings) (Collector, error) { return &systemCollector{ sysctlList: []string{ diff --git a/internal/collector/linux_sysinfo.go b/internal/collector/linux_sysinfo.go index 4ba2e2a..80b7c8d 100644 --- a/internal/collector/linux_sysinfo.go +++ b/internal/collector/linux_sysinfo.go @@ -1,15 +1,17 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "fmt" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/prometheus/client_golang/prometheus" "io" "os" "path/filepath" "strings" + + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/prometheus/client_golang/prometheus" ) type sysinfoCollector struct { diff --git a/internal/collector/patroni_common.go b/internal/collector/patroni_common.go index 9795355..1d47deb 100644 --- a/internal/collector/patroni_common.go +++ b/internal/collector/patroni_common.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( @@ -222,13 +223,14 @@ func NewPatroniCommonCollector(constLabels labels, settings model.CollectorSetti }, nil } +// Update method collects statistics, parse it and produces metrics that are sent to Prometheus. func (c *patroniCommonCollector) Update(config Config, ch chan<- prometheus.Metric) error { if strings.HasPrefix(config.BaseURL, "https://") { c.client.EnableTLSInsecure() } // Check liveness. - err := requestApiLiveness(c.client, config.BaseURL) + err := requestAPILiveness(c.client, config.BaseURL) if err != nil { ch <- c.up.newConstMetric(0) return err @@ -237,7 +239,7 @@ func (c *patroniCommonCollector) Update(config Config, ch chan<- prometheus.Metr ch <- c.up.newConstMetric(1) // Request general info. - respInfo, err := requestApiPatroni(c.client, config.BaseURL) + respInfo, err := requestAPIPatroni(c.client, config.BaseURL) if err != nil { return err } @@ -273,7 +275,7 @@ func (c *patroniCommonCollector) Update(config Config, ch chan<- prometheus.Metr ch <- c.syncStandby.newConstMetric(info.syncStandby, info.scope) // Request and parse config. - respConfig, err := requestApiPatroniConfig(c.client, config.BaseURL) + respConfig, err := requestAPIPatroniConfig(c.client, config.BaseURL) if err != nil { return err } @@ -288,7 +290,7 @@ func (c *patroniCommonCollector) Update(config Config, ch chan<- prometheus.Metr } // Request and parse history. - respHist, err := requestApiHistory(c.client, config.BaseURL) + respHist, err := requestAPIHistory(c.client, config.BaseURL) if err != nil { return err } @@ -301,8 +303,8 @@ func (c *patroniCommonCollector) Update(config Config, ch chan<- prometheus.Metr return nil } -// requestApiLiveness requests to /liveness endpoint of API and returns error if failed. -func requestApiLiveness(c *http.Client, baseurl string) error { +// requestAPILiveness requests to /liveness endpoint of API and returns error if failed. +func requestAPILiveness(c *http.Client, baseurl string) error { _, err := c.Get(baseurl + "/liveness") if err != nil { return err @@ -371,13 +373,13 @@ type patroniInfo struct { syncStandby float64 } -// apiPatroniResponse implements API response returned by '/config' endpoint. +// apiPatroniConfigResponse implements API response returned by '/config' endpoint. type apiPatroniConfigResponse struct { FailSafeMode bool `json:"failsafe_mode"` LoopWait int `json:"loop_wait"` MaxLagOnFailover int `json:"maximum_lag_on_failover"` RetryTimeout int `json:"retry_timeout"` - Ttl int `json:"ttl"` + TTL int `json:"ttl"` } // patroniConfigInfo implements metrics values extracted from the response of '/config' endpoint. @@ -389,8 +391,8 @@ type patroniConfigInfo struct { ttl float64 } -// requestPatroniConfigInfo requests to /config endpoint of API and returns parsed response. -func requestApiPatroniConfig(c *http.Client, baseurl string) (*apiPatroniConfigResponse, error) { +// requestAPIPatroniConfig requests to /config endpoint of API and returns parsed response. +func requestAPIPatroniConfig(c *http.Client, baseurl string) (*apiPatroniConfigResponse, error) { resp, err := c.Get(baseurl + "/config") if err != nil { return nil, err @@ -429,12 +431,12 @@ func parsePatroniConfigResponse(resp *apiPatroniConfigResponse) (*patroniConfigI loopWait: float64(resp.LoopWait), maximumLagOnFailover: float64(resp.MaxLagOnFailover), retryTimeout: float64(resp.RetryTimeout), - ttl: float64(resp.Ttl), + ttl: float64(resp.TTL), }, nil } -// requestPatroniInfo requests to /patroni endpoint of API and returns parsed response. -func requestApiPatroni(c *http.Client, baseurl string) (*apiPatroniResponse, error) { +// requestAPIPatroni requests to /patroni endpoint of API and returns parsed response. +func requestAPIPatroni(c *http.Client, baseurl string) (*apiPatroniResponse, error) { resp, err := c.Get(baseurl + "/patroni") if err != nil { return nil, err @@ -580,8 +582,8 @@ type patroniHistory struct { lastTimelineChangeUnix float64 } -// requestApiHistory requests /history endpoint of API and returns parsed response. -func requestApiHistory(c *http.Client, baseurl string) (apiHistoryResponse, error) { +// requestAPIHistory requests /history endpoint of API and returns parsed response. +func requestAPIHistory(c *http.Client, baseurl string) (apiHistoryResponse, error) { resp, err := c.Get(baseurl + "/history") if err != nil { return nil, err diff --git a/internal/collector/patroni_common_test.go b/internal/collector/patroni_common_test.go index 8e179f1..492ebe4 100644 --- a/internal/collector/patroni_common_test.go +++ b/internal/collector/patroni_common_test.go @@ -8,22 +8,22 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_requestApiLiveness(t *testing.T) { +func Test_requestAPILivenesss(t *testing.T) { ts := http.TestServer(t, http.StatusOK, "") defer ts.Close() c := http.NewClient(http.ClientConfig{}) - err := requestApiLiveness(c, ts.URL) + err := requestAPILiveness(c, ts.URL) assert.NoError(t, err) // Test errors - err = requestApiLiveness(c, "http://[") + err = requestAPILiveness(c, "http://[") assert.Error(t, err) fmt.Println(err) } -func Test_requestApiPatroni(t *testing.T) { +func Test_requestAPIPatroni(t *testing.T) { testcases := []struct { name string response string @@ -66,7 +66,7 @@ func Test_requestApiPatroni(t *testing.T) { c := http.NewClient(http.ClientConfig{}) - got, err := requestApiPatroni(c, ts.URL) + got, err := requestAPIPatroni(c, ts.URL) assert.NoError(t, err) assert.Equal(t, tc.want, got) }) @@ -75,7 +75,7 @@ func Test_requestApiPatroni(t *testing.T) { // Test errors t.Run("invalid url", func(t *testing.T) { c := http.NewClient(http.ClientConfig{}) - _, err := requestApiPatroni(c, "http://127.0.0.1:30080/invalid") + _, err := requestAPIPatroni(c, "http://127.0.0.1:30080/invalid") assert.Error(t, err) }) } @@ -148,7 +148,7 @@ func Test_parsePatroniResponse(t *testing.T) { } } -func Test_requestApiHistory(t *testing.T) { +func Test_requestAPIHistory(t *testing.T) { ts := http.TestServer(t, http.StatusOK, `[[1, 1234, "no recovery target specified", "2021-06-30T00:00:00.123456+00:00"],[2, 2345, "no recovery target specified", "2021-06-30T10:00:00+00:00"]]`, ) @@ -156,7 +156,7 @@ func Test_requestApiHistory(t *testing.T) { c := http.NewClient(http.ClientConfig{}) - got, err := requestApiHistory(c, ts.URL) + got, err := requestAPIHistory(c, ts.URL) assert.NoError(t, err) assert.EqualValues(t, apiHistoryResponse{ {float64(1), float64(1234), "no recovery target specified", "2021-06-30T00:00:00.123456+00:00"}, @@ -164,7 +164,7 @@ func Test_requestApiHistory(t *testing.T) { }, got) // Test errors - _, err = requestApiHistory(c, "http://127.0.0.1:30080/invalid") + _, err = requestAPIHistory(c, "http://127.0.0.1:30080/invalid") assert.Error(t, err) } diff --git a/internal/collector/pgbouncer_pools.go b/internal/collector/pgbouncer_pools.go index b4bfaf7..416780f 100644 --- a/internal/collector/pgbouncer_pools.go +++ b/internal/collector/pgbouncer_pools.go @@ -1,12 +1,14 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "strings" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" - "strings" ) const ( diff --git a/internal/collector/pgbouncer_settings.go b/internal/collector/pgbouncer_settings.go index 45e8354..7e8ac18 100644 --- a/internal/collector/pgbouncer_settings.go +++ b/internal/collector/pgbouncer_settings.go @@ -1,19 +1,21 @@ +// Package collector is a pgSCV collectors package collector import ( "bufio" "context" "fmt" - "github.com/jackc/pgx/v4" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/cherts/pgscv/internal/store" - "github.com/prometheus/client_golang/prometheus" "os" "path/filepath" "regexp" "strconv" "strings" + + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/cherts/pgscv/internal/store" + "github.com/jackc/pgx/v4" + "github.com/prometheus/client_golang/prometheus" ) const ( @@ -89,12 +91,12 @@ func (c *pgbouncerSettingsCollector) Update(config Config, ch chan<- prometheus. settings := parsePgbouncerSettings(res) for k, v := range settings { + f, err := strconv.ParseFloat(v, 64) // If value could be converted to numeric, send it as value. For string values use "1". - if f, err := strconv.ParseFloat(v, 64); err != nil { + if err != nil { ch <- c.settings.newConstMetric(1, k, v) - } else { - ch <- c.settings.newConstMetric(f, k, v) } + ch <- c.settings.newConstMetric(f, k, v) } if conffile, ok := settings["conffile"]; ok { @@ -112,12 +114,12 @@ func (c *pgbouncerSettingsCollector) Update(config Config, ch chan<- prometheus. for _, p := range dbSettings { ch <- c.dbSettings.newConstMetric(1, p.name, p.mode, p.size) - if f, err := strconv.ParseFloat(p.size, 64); err != nil { + f, err := strconv.ParseFloat(p.size, 64) + if err != nil { log.Warnf("invalid input, parse '%s' failed: %s; skip", p.size, err) continue - } else { - ch <- c.poolSize.newConstMetric(f, p.name) } + ch <- c.poolSize.newConstMetric(f, p.name) } } diff --git a/internal/collector/pgbouncer_stats.go b/internal/collector/pgbouncer_stats.go index 1796f6b..5271283 100644 --- a/internal/collector/pgbouncer_stats.go +++ b/internal/collector/pgbouncer_stats.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const pgbouncerStatsQuery = "SHOW STATS" diff --git a/internal/collector/pgscv_services.go b/internal/collector/pgscv_services.go index c8d84ed..592b722 100644 --- a/internal/collector/pgscv_services.go +++ b/internal/collector/pgscv_services.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_activity.go b/internal/collector/postgres_activity.go index e30fd52..a7a34d7 100644 --- a/internal/collector/postgres_activity.go +++ b/internal/collector/postgres_activity.go @@ -1,14 +1,16 @@ +// Package collector is a pgSCV collectors package collector import ( "context" + "regexp" + "strconv" + "strings" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "regexp" - "strconv" - "strings" ) const ( @@ -82,8 +84,8 @@ type postgresActivityCollector struct { // NewPostgresActivityCollector returns a new Collector exposing postgres activity stats. // For details see: -// 1. https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW -// 2. https://www.postgresql.org/docs/current/view-pg-prepared-xacts.html +// 1. https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW +// 2. https://www.postgresql.org/docs/current/view-pg-prepared-xacts.html func NewPostgresActivityCollector(constLabels labels, settings model.CollectorSettings) (Collector, error) { return &postgresActivityCollector{ up: newBuiltinTypedDesc( diff --git a/internal/collector/postgres_archiver.go b/internal/collector/postgres_archiver.go index 22e6612..d9b177c 100644 --- a/internal/collector/postgres_archiver.go +++ b/internal/collector/postgres_archiver.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const walArchivingQuery = "SELECT archived_count, failed_count, " + diff --git a/internal/collector/postgres_bgwriter.go b/internal/collector/postgres_bgwriter.go index 11653bb..f4feb04 100644 --- a/internal/collector/postgres_bgwriter.go +++ b/internal/collector/postgres_bgwriter.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_common.go b/internal/collector/postgres_common.go index 8c4e8b3..b73976e 100644 --- a/internal/collector/postgres_common.go +++ b/internal/collector/postgres_common.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( @@ -10,6 +11,7 @@ import ( "github.com/cherts/pgscv/internal/store" ) +// Postgres server versions const ( // Postgres server versions numeric representations. PostgresV95 = 90500 @@ -23,7 +25,7 @@ const ( PostgresV16 = 160000 PostgresV17 = 170000 - // Minimal required version is 9.5. + // Minimal required version is 9.5 PostgresVMinNum = PostgresV95 PostgresVMinStr = "9.5" ) diff --git a/internal/collector/postgres_conflicts.go b/internal/collector/postgres_conflicts.go index 193c9e0..d74d785 100644 --- a/internal/collector/postgres_conflicts.go +++ b/internal/collector/postgres_conflicts.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const ( diff --git a/internal/collector/postgres_custom.go b/internal/collector/postgres_custom.go index 92349d3..d9d596f 100644 --- a/internal/collector/postgres_custom.go +++ b/internal/collector/postgres_custom.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_database.go b/internal/collector/postgres_database.go index c9523fc..ec725c1 100644 --- a/internal/collector/postgres_database.go +++ b/internal/collector/postgres_database.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const ( diff --git a/internal/collector/postgres_functions.go b/internal/collector/postgres_functions.go index 1216c15..54fb21e 100644 --- a/internal/collector/postgres_functions.go +++ b/internal/collector/postgres_functions.go @@ -1,13 +1,15 @@ +// Package collector is a pgSCV collectors package collector import ( - "github.com/jackc/pgx/v4" + "strconv" + "strings" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" + "github.com/jackc/pgx/v4" "github.com/prometheus/client_golang/prometheus" - "strconv" - "strings" ) const postgresFunctionsQuery = "SELECT current_database() AS database, schemaname AS schema, funcname AS function, calls, total_time, self_time FROM pg_stat_user_functions" diff --git a/internal/collector/postgres_indexes.go b/internal/collector/postgres_indexes.go index 255e8c3..399d756 100644 --- a/internal/collector/postgres_indexes.go +++ b/internal/collector/postgres_indexes.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_locks.go b/internal/collector/postgres_locks.go index e5ff9dc..370cdbb 100644 --- a/internal/collector/postgres_locks.go +++ b/internal/collector/postgres_locks.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const ( diff --git a/internal/collector/postgres_logs.go b/internal/collector/postgres_logs.go index 06cabe7..e883742 100644 --- a/internal/collector/postgres_logs.go +++ b/internal/collector/postgres_logs.go @@ -1,16 +1,18 @@ +// Package collector is a pgSCV collectors package collector import ( "context" + "io" + "regexp" + "strings" + "sync" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/nxadm/tail" "github.com/prometheus/client_golang/prometheus" - "io" - "regexp" - "strings" - "sync" ) // Current implementation has an issue described here: https://github.com/nxadm/tail/issues/18. diff --git a/internal/collector/postgres_replication.go b/internal/collector/postgres_replication.go index 606efa5..a459fb0 100644 --- a/internal/collector/postgres_replication.go +++ b/internal/collector/postgres_replication.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const ( diff --git a/internal/collector/postgres_replication_slots.go b/internal/collector/postgres_replication_slots.go index 409fc51..f19fa9f 100644 --- a/internal/collector/postgres_replication_slots.go +++ b/internal/collector/postgres_replication_slots.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_schema.go b/internal/collector/postgres_schema.go index 8108fbc..ce04216 100644 --- a/internal/collector/postgres_schema.go +++ b/internal/collector/postgres_schema.go @@ -1,13 +1,15 @@ +// Package collector is a pgSCV collectors package collector import ( - "github.com/jackc/pgx/v4" + "strings" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" + "github.com/jackc/pgx/v4" "github.com/prometheus/client_golang/prometheus" "golang.org/x/net/context" - "strings" ) // postgresSchemaCollector defines metric descriptors and stats store. @@ -21,7 +23,7 @@ type postgresSchemaCollector struct { difftypefkey typedDesc } -// NewPostgresSchemaCollector returns a new Collector exposing postgres schema stats. Stats are based on different +// NewPostgresSchemasCollector returns a new Collector exposing postgres schema stats. Stats are based on different // sources inside system catalog. func NewPostgresSchemasCollector(constLabels labels, settings model.CollectorSettings) (Collector, error) { return &postgresSchemaCollector{ @@ -160,7 +162,7 @@ func collectSystemCatalogSize(conn *store.DB, ch chan<- prometheus.Metric, desc // getSystemCatalogSize returns size of system catalog in bytes. func getSystemCatalogSize(conn *store.DB) (float64, error) { var query = `SELECT sum(pg_total_relation_size(relname::regclass)) AS bytes FROM pg_stat_sys_tables WHERE schemaname = 'pg_catalog'` - var size int64 = 0 + var size int64 if err := conn.Conn().QueryRow(context.Background(), query).Scan(&size); err != nil { return 0, err } diff --git a/internal/collector/postgres_settings.go b/internal/collector/postgres_settings.go index cb0b863..0e83a93 100644 --- a/internal/collector/postgres_settings.go +++ b/internal/collector/postgres_settings.go @@ -1,15 +1,17 @@ +// Package collector is a pgSCV collectors package collector import ( "fmt" - "github.com/cherts/pgscv/internal/log" - "github.com/cherts/pgscv/internal/model" - "github.com/cherts/pgscv/internal/store" - "github.com/prometheus/client_golang/prometheus" "os" "regexp" "strconv" "strings" + + "github.com/cherts/pgscv/internal/log" + "github.com/cherts/pgscv/internal/model" + "github.com/cherts/pgscv/internal/store" + "github.com/prometheus/client_golang/prometheus" ) // postgresSettingsCollector defines metric descriptors and stats store. diff --git a/internal/collector/postgres_statements.go b/internal/collector/postgres_statements.go index cc2fdf7..1745b87 100644 --- a/internal/collector/postgres_statements.go +++ b/internal/collector/postgres_statements.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( @@ -387,7 +388,7 @@ func (c *postgresStatementsCollector) Update(config Config, ch chan<- prometheus return nil } -// postgresStatementsStat represents stats values for single statement based on pg_stat_statements. +// postgresStatementStat represents stats values for single statement based on pg_stat_statements. type postgresStatementStat struct { database string user string @@ -521,26 +522,25 @@ func parsePostgresStatementsStats(r *model.PGResult, labelNames []string) map[st // selectStatementsQuery returns suitable statements query depending on passed version. func selectStatementsQuery(version int, schema string, notrackmode bool, topK int) string { - var query_columm string + var queryColumm string if notrackmode { - query_columm = "null" + queryColumm = "null" } else { - query_columm = "p.query" + queryColumm = "p.query" } if version < PostgresV13 { if topK > 0 { - return fmt.Sprintf(postgresStatementsQuery12TopK, query_columm, schema) + return fmt.Sprintf(postgresStatementsQuery12TopK, queryColumm, schema) } - return fmt.Sprintf(postgresStatementsQuery12, query_columm, schema) + return fmt.Sprintf(postgresStatementsQuery12, queryColumm, schema) } else if version > PostgresV12 && version < PostgresV17 { if topK > 0 { - return fmt.Sprintf(postgresStatementsQuery16TopK, query_columm, schema) + return fmt.Sprintf(postgresStatementsQuery16TopK, queryColumm, schema) } - return fmt.Sprintf(postgresStatementsQuery16, query_columm, schema) - } else { - if topK > 0 { - return fmt.Sprintf(postgresStatementsQueryLatestTopK, query_columm, schema) - } - return fmt.Sprintf(postgresStatementsQueryLatest, query_columm, schema) + return fmt.Sprintf(postgresStatementsQuery16, queryColumm, schema) + } + if topK > 0 { + return fmt.Sprintf(postgresStatementsQueryLatestTopK, queryColumm, schema) } + return fmt.Sprintf(postgresStatementsQueryLatest, queryColumm, schema) } diff --git a/internal/collector/postgres_storage.go b/internal/collector/postgres_storage.go index 29ad4f7..507e397 100644 --- a/internal/collector/postgres_storage.go +++ b/internal/collector/postgres_storage.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_tables.go b/internal/collector/postgres_tables.go index 684f073..b3e678e 100644 --- a/internal/collector/postgres_tables.go +++ b/internal/collector/postgres_tables.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/collector/postgres_wal.go b/internal/collector/postgres_wal.go index 0db7458..426ffb5 100644 --- a/internal/collector/postgres_wal.go +++ b/internal/collector/postgres_wal.go @@ -1,11 +1,13 @@ +// Package collector is a pgSCV collectors package collector import ( + "strconv" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/cherts/pgscv/internal/store" "github.com/prometheus/client_golang/prometheus" - "strconv" ) const ( diff --git a/internal/collector/strings.go b/internal/collector/strings.go index 80cda21..b30e23f 100644 --- a/internal/collector/strings.go +++ b/internal/collector/strings.go @@ -1,3 +1,4 @@ +// Package collector is a pgSCV collectors package collector import ( diff --git a/internal/filter/filter.go b/internal/filter/filter.go index 0b3a070..a4fa28c 100644 --- a/internal/filter/filter.go +++ b/internal/filter/filter.go @@ -1,8 +1,10 @@ +// Package filter is a pgSCV filter package filter import ( - "github.com/cherts/pgscv/internal/log" "regexp" + + "github.com/cherts/pgscv/internal/log" ) // Filter describes settings for filtering stats values for metrics. diff --git a/internal/http/http_client.go b/internal/http/http_client.go index 780f1e4..2bf63b3 100644 --- a/internal/http/http_client.go +++ b/internal/http/http_client.go @@ -1,3 +1,4 @@ +// Package http is a pgSCV http helper package http import ( @@ -6,11 +7,16 @@ import ( "time" ) +// Status code const ( - StatusOK = http.StatusOK // 200 - StatusBadRequest = http.StatusBadRequest // 400 - StatusUnauthorized = http.StatusUnauthorized // 401 - StatusNotFound = http.StatusNotFound // 404 + // Code 200 + StatusOK = http.StatusOK + // Code 400 + StatusBadRequest = http.StatusBadRequest + // Code 401 + StatusUnauthorized = http.StatusUnauthorized + // Code 404 + StatusNotFound = http.StatusNotFound ) // Client defines local wrapper on standard http.Client. diff --git a/internal/http/http_server.go b/internal/http/http_server.go index d72d8be..f51d53c 100644 --- a/internal/http/http_server.go +++ b/internal/http/http_server.go @@ -1,3 +1,4 @@ +// Package http is a pgSCV http helper package http import ( diff --git a/internal/http/http_server_test.go b/internal/http/http_server_test.go index 3a34871..c7b4fa8 100644 --- a/internal/http/http_server_test.go +++ b/internal/http/http_server_test.go @@ -16,11 +16,11 @@ func TestAuthConfig_Validate(t *testing.T) { valid bool cfg AuthConfig wantAuth bool - wantTls bool + wantTLS bool }{ - {valid: true, cfg: AuthConfig{}, wantAuth: false, wantTls: false}, - {valid: true, cfg: AuthConfig{Username: "user", Password: "pass"}, wantAuth: true, wantTls: false}, - {valid: true, cfg: AuthConfig{Keyfile: "key", Certfile: "cert"}, wantAuth: false, wantTls: true}, + {valid: true, cfg: AuthConfig{}, wantAuth: false, wantTLS: false}, + {valid: true, cfg: AuthConfig{Username: "user", Password: "pass"}, wantAuth: true, wantTLS: false}, + {valid: true, cfg: AuthConfig{Keyfile: "key", Certfile: "cert"}, wantAuth: false, wantTLS: true}, {valid: false, cfg: AuthConfig{Username: "user", Password: ""}}, {valid: false, cfg: AuthConfig{Username: "", Password: "pass"}}, {valid: false, cfg: AuthConfig{Keyfile: "key", Certfile: ""}}, @@ -32,7 +32,7 @@ func TestAuthConfig_Validate(t *testing.T) { if tc.valid { assert.NoError(t, err) assert.Equal(t, tc.wantAuth, auth) - assert.Equal(t, tc.wantTls, tls) + assert.Equal(t, tc.wantTLS, tls) } else { assert.Error(t, err) } diff --git a/internal/http/testing.go b/internal/http/testing.go index 2e18c8c..86eee68 100644 --- a/internal/http/testing.go +++ b/internal/http/testing.go @@ -2,12 +2,14 @@ package http import ( "fmt" - "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" + + "github.com/stretchr/testify/assert" ) +// TestServer create http test server func TestServer(t *testing.T, code int, response string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { if code == http.StatusOK { @@ -23,6 +25,7 @@ func TestServer(t *testing.T, code int, response string) *httptest.Server { })) } +// TestFileServer create http test server func TestFileServer(_ *testing.T, dir string) *httptest.Server { return httptest.NewServer(http.FileServer(http.Dir(dir))) } diff --git a/internal/log/log.go b/internal/log/log.go index 9b259eb..a01713a 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -1,9 +1,11 @@ +// Package log is a pgSCV logger package log import ( "fmt" - "github.com/rs/zerolog" "os" + + "github.com/rs/zerolog" ) // Logger is the global logger with predefined settings @@ -28,6 +30,7 @@ func SetLevel(level string) { } } +// New create logger func New() zerolog.Logger { var logger = Logger return logger diff --git a/internal/model/model.go b/internal/model/model.go index e04349d..a51d57c 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -1,3 +1,4 @@ +// Package model is a pgSCV model package model import ( diff --git a/internal/pgscv/config.go b/internal/pgscv/config.go index 015bc3a..36b1f43 100644 --- a/internal/pgscv/config.go +++ b/internal/pgscv/config.go @@ -1,3 +1,4 @@ +// Package pgscv is a pgSCV main helper package pgscv import ( @@ -141,7 +142,7 @@ func mergeServicesConnsSettings(dest, src service.ConnsSettings) service.ConnsSe return dest } -// Read real config file path +// RealPath read real config file path func RealPath(filePath string) (string, error) { log.Infoln("reading file information ", filePath) fileInfo, err := os.Lstat(filepath.Clean(filePath)) @@ -165,9 +166,8 @@ func RealPath(filePath string) (string, error) { } else if fileInfo.Mode().IsDir() { log.Debugln("is directory") return filePath, err - } else { - return filePath, err } + return filePath, err } // Validate checks configuration for stupid values and set defaults @@ -432,6 +432,7 @@ func newConfigFromEnv() (*Config, error) { return config, nil } +// toBool string to bool func toBool(s string) bool { switch s { case "y", "yes", "Yes", "YES", "t", "true", "True", "TRUE", "1", "on": diff --git a/internal/pgscv/config_test.go b/internal/pgscv/config_test.go index c7ce928..659bdf8 100644 --- a/internal/pgscv/config_test.go +++ b/internal/pgscv/config_test.go @@ -1,3 +1,4 @@ +// Package pgscv is a pgSCV helper package pgscv import ( diff --git a/internal/pgscv/pgscv.go b/internal/pgscv/pgscv.go index a8d9c41..ed3d227 100644 --- a/internal/pgscv/pgscv.go +++ b/internal/pgscv/pgscv.go @@ -1,3 +1,4 @@ +// Package pgscv is a pgSCV main helper package pgscv import ( diff --git a/internal/service/config.go b/internal/service/config.go index 131bbc9..a624196 100644 --- a/internal/service/config.go +++ b/internal/service/config.go @@ -1,3 +1,4 @@ +// Package service is a pgSCV service helper package service import ( diff --git a/internal/service/config_test.go b/internal/service/config_test.go index 4bebfa0..4c802b2 100644 --- a/internal/service/config_test.go +++ b/internal/service/config_test.go @@ -36,17 +36,17 @@ func Test_parseDSNEnv(t *testing.T) { valid bool prefix string key string - wantId string + wantID string wantType string }{ - {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN", wantId: "postgres", wantType: "postgres"}, - {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN_POSTGRES_123", wantId: "POSTGRES_123", wantType: "postgres"}, - {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN1", wantId: "1", wantType: "postgres"}, - {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN_POSTGRES_5432", wantId: "POSTGRES_5432", wantType: "postgres"}, - {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN", wantId: "pgbouncer", wantType: "pgbouncer"}, - {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN_PGBOUNCER_123", wantId: "PGBOUNCER_123", wantType: "pgbouncer"}, - {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN1", wantId: "1", wantType: "pgbouncer"}, - {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN_PGBOUNCER_6432", wantId: "PGBOUNCER_6432", wantType: "pgbouncer"}, + {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN", wantID: "postgres", wantType: "postgres"}, + {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN_POSTGRES_123", wantID: "POSTGRES_123", wantType: "postgres"}, + {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN1", wantID: "1", wantType: "postgres"}, + {valid: true, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN_POSTGRES_5432", wantID: "POSTGRES_5432", wantType: "postgres"}, + {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN", wantID: "pgbouncer", wantType: "pgbouncer"}, + {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN_PGBOUNCER_123", wantID: "PGBOUNCER_123", wantType: "pgbouncer"}, + {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN1", wantID: "1", wantType: "pgbouncer"}, + {valid: true, prefix: "PGBOUNCER_DSN", key: "PGBOUNCER_DSN_PGBOUNCER_6432", wantID: "PGBOUNCER_6432", wantType: "pgbouncer"}, {valid: false, prefix: "POSTGRES_DSN", key: "POSTGRES_DSN_"}, {valid: false, prefix: "POSTGRES_DSN", key: "INVALID"}, {valid: false, prefix: "INVALID", key: "INVALID"}, @@ -56,7 +56,7 @@ func Test_parseDSNEnv(t *testing.T) { gotID, gotCS, err := parseDSNEnv(tc.prefix, tc.key, "conninfo") if tc.valid { assert.NoError(t, err) - assert.Equal(t, tc.wantId, gotID) + assert.Equal(t, tc.wantID, gotID) assert.Equal(t, ConnSetting{ServiceType: tc.wantType, Conninfo: "conninfo"}, gotCS) } else { assert.Error(t, err) @@ -69,12 +69,12 @@ func Test_parseURLEnv(t *testing.T) { valid bool prefix string key string - wantId string + wantID string wantType string }{ - {valid: true, prefix: "PATRONI_URL", key: "PATRONI_URL", wantId: "patroni", wantType: "patroni"}, - {valid: true, prefix: "PATRONI_URL", key: "PATRONI_URL1", wantId: "1", wantType: "patroni"}, - {valid: true, prefix: "PATRONI_URL", key: "PATRONI_URL_PATRONI_123", wantId: "PATRONI_123", wantType: "patroni"}, + {valid: true, prefix: "PATRONI_URL", key: "PATRONI_URL", wantID: "patroni", wantType: "patroni"}, + {valid: true, prefix: "PATRONI_URL", key: "PATRONI_URL1", wantID: "1", wantType: "patroni"}, + {valid: true, prefix: "PATRONI_URL", key: "PATRONI_URL_PATRONI_123", wantID: "PATRONI_123", wantType: "patroni"}, // {valid: false, prefix: "PATRONI_URL", key: "PATRONI_URL_"}, {valid: false, prefix: "PATRONI_URL", key: "INVALID"}, @@ -85,7 +85,7 @@ func Test_parseURLEnv(t *testing.T) { gotID, gotCS, err := parseURLEnv(tc.prefix, tc.key, "baseurl") if tc.valid { assert.NoError(t, err) - assert.Equal(t, tc.wantId, gotID) + assert.Equal(t, tc.wantID, gotID) assert.Equal(t, ConnSetting{ServiceType: tc.wantType, BaseURL: "baseurl"}, gotCS) } else { assert.Error(t, err) diff --git a/internal/service/service.go b/internal/service/service.go index 8caedf3..6f31e32 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,3 +1,4 @@ +// Package service is a pgSCV service helper package service import ( diff --git a/internal/store/store.go b/internal/store/store.go index 7b117db..78c7db0 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -1,9 +1,11 @@ +// Package store is a pgSCV database helper package store import ( "context" "database/sql" "fmt" + "github.com/cherts/pgscv/internal/log" "github.com/cherts/pgscv/internal/model" "github.com/jackc/pgx/v4" diff --git a/internal/store/testing.go b/internal/store/testing.go index 78361e1..46603ee 100644 --- a/internal/store/testing.go +++ b/internal/store/testing.go @@ -1,19 +1,25 @@ package store import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) +// TestPostgresConnStr PostgreSQL connection string const TestPostgresConnStr = "host=127.0.0.1 port=5432 user=pgscv dbname=pgscv_fixtures sslmode=disable" + +// TestPgbouncerConnStr Pgbouncer connection string const TestPgbouncerConnStr = "host=127.0.0.1 port=6432 user=pgscv dbname=pgbouncer sslmode=disable password=pgscv" +// NewTest create PostgreSQL test func NewTest(t *testing.T) *DB { db, err := New(TestPostgresConnStr) assert.NoError(t, err) return db } +// NewTestPgbouncer create Pgbouncer test func NewTestPgbouncer(t *testing.T) *DB { db, err := New(TestPgbouncerConnStr) assert.NoError(t, err)