Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lvrach committed Sep 26, 2024
1 parent 84fde08 commit 9ce2212
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
5 changes: 3 additions & 2 deletions stats/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"
)

const defaultPauseDur = 10 * time.Second

type gaugeTagsFunc = func(key string, tags Tags, val uint64)

type Collector interface {
Expand Down Expand Up @@ -43,8 +45,7 @@ func (p *aggregatedCollector) Run(ctx context.Context) {
p.allCollect()

if p.PauseDur <= 0 {
p.PauseDur = 10 * time.Second
return
p.PauseDur = defaultPauseDur
}

tick := time.NewTicker(p.PauseDur)
Expand Down
12 changes: 6 additions & 6 deletions stats/collectors/sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ const (
uniqName = "database_sql_%s"
)

type sqlDBStats struct {
type SQLDBStats struct {
name string
db *sql.DB
}

func NewDatabaseSQLStats(name string, db *sql.DB) *sqlDBStats {
return &sqlDBStats{
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)) {
func (s *SQLDBStats) Collect(gaugeFunc func(key string, tag stats.Tags, val uint64)) {
dbStats := s.db.Stats()
tags := stats.Tags{"name": s.name}

Expand All @@ -40,7 +40,7 @@ func (s *sqlDBStats) Collect(gaugeFunc func(key string, tag stats.Tags, val uint
gaugeFunc("sql_db_max_lifetime_closed_total", tags, uint64(dbStats.MaxLifetimeClosed))
}

func (s *sqlDBStats) Zero(gaugeFunc func(key string, tag stats.Tags, val uint64)) {
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)
Expand All @@ -57,6 +57,6 @@ func (s *sqlDBStats) Zero(gaugeFunc func(key string, tag stats.Tags, val uint64)
gaugeFunc("sql_db_max_lifetime_closed_total", tags, 0)
}

func (s *sqlDBStats) ID() string {
func (s *SQLDBStats) ID() string {
return fmt.Sprintf(uniqName, s.name)
}
43 changes: 20 additions & 23 deletions stats/otel_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

promClient "github.com/prometheus/client_model/go"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"

Expand All @@ -25,20 +26,16 @@ import (
"github.com/rudderlabs/rudder-go-kit/testhelper/docker"
)

func ptr[T any](v T) *T {
return &v
}

const (
metricsPort = "8889"
)

var globalDefaultAttrs = []*promClient.LabelPair{
{Name: ptr("instanceName"), Value: ptr("my-instance-id")},
{Name: ptr("service_version"), Value: ptr("v1.2.3")},
{Name: ptr("telemetry_sdk_language"), Value: ptr("go")},
{Name: ptr("telemetry_sdk_name"), Value: ptr("opentelemetry")},
{Name: ptr("telemetry_sdk_version"), Value: ptr(otel.Version())},
{Name: lo.ToPtr("instanceName"), Value: lo.ToPtr("my-instance-id")},
{Name: lo.ToPtr("service_version"), Value: lo.ToPtr("v1.2.3")},
{Name: lo.ToPtr("telemetry_sdk_language"), Value: lo.ToPtr("go")},
{Name: lo.ToPtr("telemetry_sdk_name"), Value: lo.ToPtr("opentelemetry")},
{Name: lo.ToPtr("telemetry_sdk_version"), Value: lo.ToPtr(otel.Version())},
}

func TestOTelPeriodicStats(t *testing.T) {
Expand Down Expand Up @@ -107,14 +104,14 @@ func TestOTelPeriodicStats(t *testing.T) {
for _, exp := range expected {
metricName := strings.ReplaceAll(exp.name, ".", "_")
require.EqualValues(t, &metricName, metrics[metricName].Name)
require.EqualValues(t, ptr(promClient.MetricType_GAUGE), metrics[metricName].Type)
require.EqualValues(t, lo.ToPtr(promClient.MetricType_GAUGE), metrics[metricName].Type)
require.Len(t, metrics[metricName].Metric, 1)

expectedLabels := append(globalDefaultAttrs,
// the label1=value1 is coming from the otel-collector-config.yaml (see const_labels)
&promClient.LabelPair{Name: ptr("label1"), Value: ptr("value1")},
&promClient.LabelPair{Name: ptr("job"), Value: ptr("TestOTelPeriodicStats")},
&promClient.LabelPair{Name: ptr("service_name"), Value: ptr("TestOTelPeriodicStats")},
&promClient.LabelPair{Name: lo.ToPtr("label1"), Value: lo.ToPtr("value1")},
&promClient.LabelPair{Name: lo.ToPtr("job"), Value: lo.ToPtr("TestOTelPeriodicStats")},
&promClient.LabelPair{Name: lo.ToPtr("service_name"), Value: lo.ToPtr("TestOTelPeriodicStats")},
)
if exp.tags != nil {
expectedLabels = append(expectedLabels, exp.tags...)
Expand All @@ -136,7 +133,7 @@ func TestOTelPeriodicStats(t *testing.T) {
runTest(t,
[]expectation{
{name: "a_custom_metric", tags: []*promClient.LabelPair{
{Name: ptr("foo"), Value: ptr("bar")},
{Name: lo.ToPtr("foo"), Value: lo.ToPtr("bar")},
}},
},
collectors.NewStaticMetric("a_custom_metric", stats.Tags{"foo": "bar"}, 1),
Expand All @@ -163,31 +160,31 @@ func TestOTelPeriodicStats(t *testing.T) {
runTest(t,
[]expectation{
{name: "sql_db_max_open_connections", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_open_connections", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_in_use_connections", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_idle_connections", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_wait_count_total", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_wait_duration_seconds_total", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_max_idle_closed_total", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_max_idle_time_closed_total", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
{name: "sql_db_max_lifetime_closed_total", tags: []*promClient.LabelPair{
{Name: ptr("name"), Value: ptr("test")},
{Name: lo.ToPtr("name"), Value: lo.ToPtr("test")},
}},
},
collectors.NewDatabaseSQLStats("test", db),
Expand Down
2 changes: 1 addition & 1 deletion stats/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func TestStatsdRegisterCollector(t *testing.T) {
receivedMu.RLock()
defer receivedMu.RUnlock()

t.Logf("%s != %s", received, expected)
t.Logf("received: %s \n!=\n expected: %s", received, expected)
}()

require.Eventually(t, func() bool {
Expand Down

0 comments on commit 9ce2212

Please sign in to comment.