-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce db stats collector (#648)
- Loading branch information
Showing
17 changed files
with
698 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package stats | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const defaultPauseDur = 10 * time.Second | ||
|
||
type gaugeTagsFunc = func(key string, tags Tags, val uint64) | ||
|
||
type Collector interface { | ||
Collect(gaugeTagsFunc) | ||
Zero(gaugeTagsFunc) | ||
ID() string | ||
} | ||
|
||
type aggregatedCollector struct { | ||
c map[string]Collector | ||
PauseDur time.Duration | ||
gaugeFunc gaugeTagsFunc | ||
mu sync.Mutex | ||
} | ||
|
||
func (p *aggregatedCollector) Add(c Collector) error { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
if p.c == nil { | ||
p.c = make(map[string]Collector) | ||
} | ||
|
||
if _, ok := p.c[c.ID()]; ok { | ||
return fmt.Errorf("collector with ID %s already register", c.ID()) | ||
} | ||
|
||
p.c[c.ID()] = c | ||
return nil | ||
} | ||
|
||
func (p *aggregatedCollector) Run(ctx context.Context) { | ||
defer p.allZero() | ||
p.allCollect() | ||
|
||
if p.PauseDur <= 0 { | ||
p.PauseDur = defaultPauseDur | ||
} | ||
|
||
tick := time.NewTicker(p.PauseDur) | ||
defer tick.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-tick.C: | ||
p.allCollect() | ||
} | ||
} | ||
} | ||
|
||
func (p *aggregatedCollector) allCollect() { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
for _, c := range p.c { | ||
c.Collect(p.gaugeFunc) | ||
} | ||
} | ||
|
||
func (p *aggregatedCollector) allZero() { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
for _, c := range p.c { | ||
c.Zero(p.gaugeFunc) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package collectors | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/stats" | ||
) | ||
|
||
const ( | ||
uniqName = "database_sql_%s" | ||
) | ||
|
||
type SQLDBStats struct { | ||
name string | ||
db *sql.DB | ||
} | ||
|
||
func NewDatabaseSQLStats(name string, db *sql.DB) *SQLDBStats { | ||
return &SQLDBStats{ | ||
name: name, | ||
db: db, | ||
} | ||
} | ||
|
||
func (s *SQLDBStats) Collect(gaugeFunc func(key string, tag stats.Tags, val uint64)) { | ||
dbStats := s.db.Stats() | ||
tags := stats.Tags{"name": s.name} | ||
|
||
gaugeFunc("sql_db_max_open_connections", tags, uint64(dbStats.MaxOpenConnections)) | ||
gaugeFunc("sql_db_open_connections", tags, uint64(dbStats.OpenConnections)) | ||
gaugeFunc("sql_db_in_use_connections", tags, uint64(dbStats.InUse)) | ||
gaugeFunc("sql_db_idle_connections", tags, uint64(dbStats.Idle)) | ||
|
||
gaugeFunc("sql_db_wait_count_total", tags, uint64(dbStats.WaitCount)) | ||
gaugeFunc("sql_db_wait_duration_seconds_total", tags, uint64(dbStats.WaitDuration.Seconds())) | ||
|
||
gaugeFunc("sql_db_max_idle_closed_total", tags, uint64(dbStats.MaxIdleClosed)) | ||
gaugeFunc("sql_db_max_idle_time_closed_total", tags, uint64(dbStats.MaxIdleTimeClosed)) | ||
gaugeFunc("sql_db_max_lifetime_closed_total", tags, uint64(dbStats.MaxLifetimeClosed)) | ||
} | ||
|
||
func (s *SQLDBStats) Zero(gaugeFunc func(key string, tag stats.Tags, val uint64)) { | ||
tags := stats.Tags{"name": s.name} | ||
|
||
gaugeFunc("sql_db_max_open_connections", tags, 0) | ||
|
||
gaugeFunc("sql_db_open_connections", tags, 0) | ||
gaugeFunc("sql_db_in_use_connections", tags, 0) | ||
gaugeFunc("sql_db_idle_connections", tags, 0) | ||
|
||
gaugeFunc("sql_db_wait_count_total", tags, 0) | ||
gaugeFunc("sql_db_wait_duration_seconds_total", tags, 0) | ||
|
||
gaugeFunc("sql_db_max_idle_closed_total", tags, 0) | ||
gaugeFunc("sql_db_max_idle_time_closed_total", tags, 0) | ||
gaugeFunc("sql_db_max_lifetime_closed_total", tags, 0) | ||
} | ||
|
||
func (s *SQLDBStats) ID() string { | ||
return fmt.Sprintf(uniqName, s.name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package collectors_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/stats" | ||
"github.com/rudderlabs/rudder-go-kit/stats/collectors" | ||
"github.com/rudderlabs/rudder-go-kit/stats/memstats" | ||
) | ||
|
||
func TestSQLDatabase(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
db.SetMaxOpenConns(5) | ||
|
||
m, err := memstats.New() | ||
require.NoError(t, err) | ||
|
||
testName := "test_sqlite" | ||
s := collectors.NewDatabaseSQLStats(testName, db) | ||
|
||
err = m.RegisterCollector(s) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, []memstats.Metric{ | ||
{ | ||
Name: "sql_db_idle_connections", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 1, | ||
}, | ||
{ | ||
Name: "sql_db_in_use_connections", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 0, | ||
}, | ||
{ | ||
Name: "sql_db_max_idle_closed_total", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 0, | ||
}, | ||
{ | ||
Name: "sql_db_max_idle_time_closed_total", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 0, | ||
}, | ||
{ | ||
Name: "sql_db_max_lifetime_closed_total", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 0, | ||
}, | ||
{ | ||
Name: "sql_db_max_open_connections", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 5, | ||
}, | ||
{ | ||
Name: "sql_db_open_connections", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 1, | ||
}, | ||
{ | ||
Name: "sql_db_wait_count_total", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 0, | ||
}, | ||
{ | ||
Name: "sql_db_wait_duration_seconds_total", | ||
Tags: stats.Tags{"name": testName}, | ||
Value: 0, | ||
}, | ||
}, m.GetAll()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package collectors | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/stats" | ||
) | ||
|
||
const ( | ||
statsUniqName = "static_%s_%s" | ||
) | ||
|
||
type staticStats struct { | ||
tags stats.Tags | ||
key string | ||
value uint64 | ||
} | ||
|
||
// NewStaticMetric allows to capture a gauge metric that does not change during the lifetime of the application. | ||
// Can be useful for capturing configuration values or application version. | ||
func NewStaticMetric(key string, tags stats.Tags, value uint64) *staticStats { | ||
return &staticStats{ | ||
tags: tags, | ||
key: key, | ||
value: value, | ||
} | ||
} | ||
|
||
func (s *staticStats) Collect(gaugeFunc func(key string, tag stats.Tags, val uint64)) { | ||
gaugeFunc(s.key, s.tags, s.value) | ||
} | ||
|
||
func (s *staticStats) Zero(gaugeFunc func(key string, tag stats.Tags, val uint64)) { | ||
gaugeFunc(s.key, s.tags, 0) | ||
} | ||
|
||
func (s *staticStats) ID() string { | ||
return fmt.Sprintf(statsUniqName, s.key, s.tags.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package collectors_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/stats" | ||
"github.com/rudderlabs/rudder-go-kit/stats/collectors" | ||
"github.com/rudderlabs/rudder-go-kit/stats/memstats" | ||
) | ||
|
||
func TestStatic(t *testing.T) { | ||
testName := "test_sqlite" | ||
s := collectors.NewStaticMetric(testName, stats.Tags{ | ||
"foo": "bar", | ||
}, 2) | ||
|
||
m, err := memstats.New() | ||
require.NoError(t, err) | ||
|
||
err = m.RegisterCollector(s) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, []memstats.Metric{ | ||
{ | ||
Name: testName, | ||
Tags: stats.Tags{"foo": "bar"}, | ||
Value: 2, | ||
}, | ||
}, m.GetAll()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.