Skip to content

Commit

Permalink
Expose database server_lifetime metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbub committed Oct 10, 2024
1 parent 46e9027 commit 12d8a3d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
16 changes: 16 additions & 0 deletions internal/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ func buildMetrics(cfg config.Config) []metric {
return results
},
},
{
enabled: cfg.ExportDatabases,
name: fqName(SubsystemDatabases, "server_lifetime"),
help: "The maximum lifetime of a server connection for this database.",
labels: []string{"name", "pool_mode"},
valType: prometheus.GaugeValue,
eval: func(res *storeResult) (results []metricResult) {
for _, database := range res.databases {
results = append(results, metricResult{
labels: []string{database.Name, database.PoolMode},
value: float64(database.ServerLifetime),
})
}
return results
},
},
{
enabled: cfg.ExportLists,
name: fqName(SubsystemLists, "items"),
Expand Down
1 change: 1 addition & 0 deletions internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Database struct {
CurrentConnections int64
Paused int64
Disabled int64
ServerLifetime int64
}

// List represents list row.
Expand Down
9 changes: 5 additions & 4 deletions internal/sqlstore/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *Store) GetStats(ctx context.Context) ([]domain.Stat, error) {
var stats []domain.Stat

for rows.Next() {
dest := make([]interface{}, 0, len(columns))
dest := make([]any, 0, len(columns))

for _, column := range columns {
switch column {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s *Store) GetPools(ctx context.Context) ([]domain.Pool, error) {
var pools []pool

for rows.Next() {
dest := make([]interface{}, 0, len(columns))
dest := make([]any, 0, len(columns))

for _, column := range columns {
switch column {
Expand Down Expand Up @@ -237,7 +237,7 @@ func (s *Store) GetDatabases(ctx context.Context) ([]domain.Database, error) {
var databases []database

for rows.Next() {
dest := make([]interface{}, 0, len(columns))
dest := make([]any, 0, len(columns))

for _, column := range columns {
switch column {
Expand Down Expand Up @@ -300,6 +300,7 @@ func (s *Store) GetDatabases(ctx context.Context) ([]domain.Database, error) {
CurrentConnections: row.CurrentConnections,
Paused: row.Paused,
Disabled: row.Disabled,
ServerLifetime: row.ServerLifetime,
})
}

Expand All @@ -323,7 +324,7 @@ func (s *Store) GetLists(ctx context.Context) ([]domain.List, error) {
var lists []domain.List

for rows.Next() {
dest := make([]interface{}, 0, len(columns))
dest := make([]any, 0, len(columns))

for _, column := range columns {
switch column {
Expand Down
46 changes: 26 additions & 20 deletions internal/sqlstore/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ func TestGetStats(t *testing.T) {

st := New(db)

data := map[string]interface{}{
"database": "pgbouncer",
"total_xact_count": 1,
"total_query_count": 2,
"total_received": 3,
"total_sent": 4,
"total_xact_time": 5,
"total_query_time": 6,
"total_wait_time": 7,
"avg_xact_count": 8,
"avg_query_count": 9,
"avg_recv": 10,
"avg_sent": 11,
"avg_xact_time": 12,
"avg_query_time": 13,
"avg_wait_time": 14,
data := map[string]any{
"database": "pgbouncer",
"total_xact_count": 1,
"total_query_count": 2,
"total_received": 3,
"total_sent": 4,
"total_xact_time": 5,
"total_query_time": 6,
"total_wait_time": 7,
"total_server_assignment_count": 8,
"avg_xact_count": 9,
"avg_query_count": 10,
"avg_recv": 11,
"avg_sent": 12,
"avg_xact_time": 13,
"avg_query_time": 14,
"avg_wait_time": 15,
"avg_server_assignment_count": 16,
}

mock.ExpectQuery("SHOW STATS").WillReturnRows(mapToRows(data))
Expand All @@ -51,13 +53,15 @@ func TestGetStats(t *testing.T) {
require.Equal(t, int64(data["total_xact_time"].(int)), stat.TotalXactTime)
require.Equal(t, int64(data["total_query_time"].(int)), stat.TotalQueryTime)
require.Equal(t, int64(data["total_wait_time"].(int)), stat.TotalWaitTime)
require.Equal(t, int64(data["total_server_assignment_count"].(int)), stat.TotalServerAssignmentCount)
require.Equal(t, int64(data["avg_xact_count"].(int)), stat.AverageXactCount)
require.Equal(t, int64(data["avg_query_count"].(int)), stat.AverageQueryCount)
require.Equal(t, int64(data["avg_recv"].(int)), stat.AverageReceived)
require.Equal(t, int64(data["avg_sent"].(int)), stat.AverageSent)
require.Equal(t, int64(data["avg_xact_time"].(int)), stat.AverageXactTime)
require.Equal(t, int64(data["avg_query_time"].(int)), stat.AverageQueryTime)
require.Equal(t, int64(data["avg_wait_time"].(int)), stat.AverageWaitTime)
require.Equal(t, int64(data["avg_server_assignment_count"].(int)), stat.AverageServerAssignmentCount)
}

func TestGetPools(t *testing.T) {
Expand All @@ -69,7 +73,7 @@ func TestGetPools(t *testing.T) {

st := New(db)

data := map[string]interface{}{
data := map[string]any{
"database": "pgbouncer",
"user": "myuser",
"cl_active": 1,
Expand Down Expand Up @@ -114,7 +118,7 @@ func TestGetDatabases(t *testing.T) {

st := New(db)

data := map[string]interface{}{
data := map[string]any{
"database": "pgbouncer",
"name": "myname",
"host": "localhost",
Expand All @@ -127,6 +131,7 @@ func TestGetDatabases(t *testing.T) {
"current_connections": 8,
"paused": 9,
"disabled": 10,
"server_lifetime": 11,
}

mock.ExpectQuery("SHOW DATABASES").WillReturnRows(mapToRows(data))
Expand All @@ -148,6 +153,7 @@ func TestGetDatabases(t *testing.T) {
require.Equal(t, int64(data["current_connections"].(int)), database.CurrentConnections)
require.Equal(t, int64(data["paused"].(int)), database.Paused)
require.Equal(t, int64(data["disabled"].(int)), database.Disabled)
require.Equal(t, int64(data["server_lifetime"].(int)), database.ServerLifetime)
}

func TestGetLists(t *testing.T) {
Expand All @@ -159,7 +165,7 @@ func TestGetLists(t *testing.T) {

st := New(db)

data := map[string]interface{}{
data := map[string]any{
"list": "mylist",
"items": 6,
}
Expand All @@ -175,7 +181,7 @@ func TestGetLists(t *testing.T) {
require.Equal(t, int64(data["items"].(int)), list.Items)
}

func mapToRows(data map[string]interface{}) *sqlmock.Rows {
func mapToRows(data map[string]any) *sqlmock.Rows {
columns := make([]string, 0, len(data))
values := make([]driver.Value, 0, len(data))
for k, v := range data {
Expand Down

0 comments on commit 12d8a3d

Please sign in to comment.