Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions agents/postgres/pgstatmonitor/pgstatmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Params struct {
}

type pgStatMonitorVersion int
type pgStatMonitorPrerelease string

const (
pgStatMonitorVersion06 pgStatMonitorVersion = iota
Expand Down Expand Up @@ -161,7 +162,7 @@ func getPGVersion(q *reform.Querier) (pgVersion float64, err error) {
return strconv.ParseFloat(v, 64)
}

func getPGMonitorVersion(q *reform.Querier) (pgStatMonitorVersion, string, error) {
func getPGMonitorVersion(q *reform.Querier) (pgStatMonitorVersion, pgStatMonitorPrerelease, error) {
var result string
err := q.QueryRow(fmt.Sprintf("SELECT /* %s */ pg_stat_monitor_version()", queryTag)).Scan(&result)
if err != nil {
Expand Down Expand Up @@ -195,7 +196,9 @@ func getPGMonitorVersion(q *reform.Querier) (pgStatMonitorVersion, string, error
version = pgStatMonitorVersion08
}

return version, pgsmVersion.Prerelease(), nil
prerelease := pgsmVersion.Prerelease()

return version, pgStatMonitorPrerelease(prerelease), nil
}

// Run extracts stats data and sends it to the channel until ctx is canceled.
Expand Down
19 changes: 12 additions & 7 deletions agents/postgres/pgstatmonitor/pgstatmonitor_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type field struct {
pointer interface{}
}

func NewPgStatMonitorStructs(v pgStatMonitorVersion) (*pgStatMonitor, reform.View) {
func NewPgStatMonitorStructs(v pgStatMonitorVersion, p pgStatMonitorPrerelease) (*pgStatMonitor, reform.View) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
Function 'NewPgStatMonitorStructs' is too long (114 > 60) (funlen)

s := &pgStatMonitor{}
fields := []field{
{info: parse.FieldInfo{Name: "Bucket", Type: "int64", Column: "bucket"}, pointer: &s.Bucket},
Expand Down Expand Up @@ -136,8 +136,7 @@ func NewPgStatMonitorStructs(v pgStatMonitorVersion) (*pgStatMonitor, reform.Vie
fields = append(fields,
field{info: parse.FieldInfo{Name: "Relations", Type: "pq.StringArray", Column: "tables_names"}, pointer: &s.Relations},
field{info: parse.FieldInfo{Name: "DBID", Type: "int64", Column: "dbid"}, pointer: &s.DBID},
field{info: parse.FieldInfo{Name: "UserID", Type: "int64", Column: "userid"}, pointer: &s.UserID},
field{info: parse.FieldInfo{Name: "BucketStartTime", Type: "time.Time", Column: "bucket_start_time"}, pointer: &s.BucketStartTime})
field{info: parse.FieldInfo{Name: "UserID", Type: "int64", Column: "userid"}, pointer: &s.UserID})
}
if v <= pgStatMonitorVersion08 {
fields = append(fields,
Expand All @@ -147,8 +146,7 @@ func NewPgStatMonitorStructs(v pgStatMonitorVersion) (*pgStatMonitor, reform.Vie
fields = append(fields,
field{info: parse.FieldInfo{Name: "Relations", Type: "pq.StringArray", Column: "relations"}, pointer: &s.Relations},
field{info: parse.FieldInfo{Name: "DatName", Type: "string", Column: "datname"}, pointer: &s.DatName},
field{info: parse.FieldInfo{Name: "UserName", Type: "string", Column: "userid"}, pointer: &s.UserName},
field{info: parse.FieldInfo{Name: "BucketStartTimeString", Type: "string", Column: "bucket_start_time"}, pointer: &s.BucketStartTimeString})
field{info: parse.FieldInfo{Name: "UserName", Type: "string", Column: "userid"}, pointer: &s.UserName})
}
if v == pgStatMonitorVersion09 {
fields = append(fields,
Expand All @@ -175,7 +173,6 @@ func NewPgStatMonitorStructs(v pgStatMonitorVersion) (*pgStatMonitor, reform.Vie
field{info: parse.FieldInfo{Name: "WalFpi", Type: "int64", Column: "wal_fpi"}, pointer: &s.WalFpi},
field{info: parse.FieldInfo{Name: "WalBytes", Type: "int64", Column: "wal_bytes"}, pointer: &s.WalBytes})
}

if v <= pgStatMonitorVersion10PG12 {
fields = append(fields,
field{info: parse.FieldInfo{Name: "TotalTime", Type: "float64", Column: "total_time"}, pointer: &s.TotalTime},
Expand All @@ -197,6 +194,13 @@ func NewPgStatMonitorStructs(v pgStatMonitorVersion) (*pgStatMonitor, reform.Vie
field{info: parse.FieldInfo{Name: "PlanMaxTime", Type: "float64", Column: "max_plan_time"}, pointer: &s.PlanMaxTime},
field{info: parse.FieldInfo{Name: "PlanMeanTime", Type: "float64", Column: "mean_plan_time"}, pointer: &s.PlanMeanTime})
}
if v >= pgStatMonitorVersion08 && v <= pgStatMonitorVersion10PG14 && p != "" {
fields = append(fields,
field{info: parse.FieldInfo{Name: "BucketStartTimeString", Type: "string", Column: "bucket_start_time"}, pointer: &s.BucketStartTimeString})
} else {
fields = append(fields,
field{info: parse.FieldInfo{Name: "BucketStartTime", Type: "time.Time", Column: "bucket_start_time"}, pointer: &s.BucketStartTime})
}

s.pointers = make([]interface{}, len(fields))
pgStatMonitorDefaultView := &pgStatMonitorAllViewType{
Expand Down Expand Up @@ -225,6 +229,7 @@ type pgStatMonitorAllViewType struct {
z []interface{}
c []string
v pgStatMonitorVersion
p pgStatMonitorPrerelease
}

// Schema returns a schema name in SQL database ("").
Expand All @@ -244,7 +249,7 @@ func (v *pgStatMonitorAllViewType) Columns() []string {

// NewStruct makes a new struct for that view or table.
func (v *pgStatMonitorAllViewType) NewStruct() reform.Struct {
str, _ := NewPgStatMonitorStructs(v.v)
str, _ := NewPgStatMonitorStructs(v.v, v.p)
return str
}

Expand Down
4 changes: 2 additions & 2 deletions agents/postgres/pgstatmonitor/pgstatmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ func TestPGStatMonitorSchema(t *testing.T) {
assert.NoError(t, err)
}()

version, _, err := getPGMonitorVersion(db.Querier)
version, prerelease, err := getPGMonitorVersion(db.Querier)
assert.NoError(t, err)

_, view := NewPgStatMonitorStructs(version)
_, view := NewPgStatMonitorStructs(version, prerelease)
structs, err := db.SelectAllFrom(view, "")
require.NoError(t, err)
tests.LogTable(t, structs)
Expand Down
12 changes: 7 additions & 5 deletions agents/postgres/pgstatmonitor/stat_monitor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (ssc *statMonitorCache) getStatMonitorExtended(ctx context.Context, q *refo
}
ssc.l.Infof("pg version = %d", pgMonitorVersion)

row, view := NewPgStatMonitorStructs(pgMonitorVersion)
row, view := NewPgStatMonitorStructs(pgMonitorVersion, prerelease)
conditions := "WHERE queryid IS NOT NULL AND query IS NOT NULL"
if pgMonitorVersion >= pgStatMonitorVersion09 && pgMonitorVersion <= pgStatMonitorVersion10PG14 && prerelease != "" {
// only pg_stat_monitor 0.9.0, 1.0.0-beta-2, 1.0.0-rc.1, 1.0.0-rc.2 supports state_code. It tells what is the query's current state.
Expand Down Expand Up @@ -121,10 +121,12 @@ func (ssc *statMonitorCache) getStatMonitorExtended(ctx context.Context, q *refo
c.Database = databases[row.DBID]
c.Username = usernames[row.UserID]
default:
row.BucketStartTime, e = time.Parse("2006-01-02 15:04:05", row.BucketStartTimeString)
if e != nil {
err = e
break
if pgMonitorVersion >= pgStatMonitorVersion08 && pgMonitorVersion <= pgStatMonitorVersion10PG14 && prerelease != "" {
row.BucketStartTime, e = time.Parse("2006-01-02 15:04:05", row.BucketStartTimeString)
if e != nil {
err = e
break
}
}
c.pgStatMonitor = *row
c.Database = row.DatName
Expand Down