Skip to content

Commit

Permalink
feat: fix comments for review
Browse files Browse the repository at this point in the history
  • Loading branch information
boodyvo committed Sep 16, 2024
1 parent e5de205 commit 1c9010a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 91 deletions.
41 changes: 0 additions & 41 deletions clients/database/empty/database.go

This file was deleted.

41 changes: 41 additions & 0 deletions clients/database/noop/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package noop

import (
"context"
"github.com/kava-labs/kava-proxy-service/clients/database"
)

// Noop is a database client that does nothing
type Noop struct{}

func New() *Noop {
return &Noop{}
}

func (e *Noop) SaveProxiedRequestMetric(ctx context.Context, metric *database.ProxiedRequestMetric) error {
return nil
}

func (e *Noop) ListProxiedRequestMetricsWithPagination(ctx context.Context, cursor int64, limit int) ([]*database.ProxiedRequestMetric, int64, error) {
return []*database.ProxiedRequestMetric{}, 0, nil
}

func (e *Noop) CountAttachedProxiedRequestMetricPartitions(ctx context.Context) (int64, error) {
return 0, nil
}

func (e *Noop) GetLastCreatedAttachedProxiedRequestMetricsPartitionName(ctx context.Context) (string, error) {
return "", nil
}

func (e *Noop) DeleteProxiedRequestMetricsOlderThanNDays(ctx context.Context, n int64) error {
return nil
}

func (e *Noop) HealthCheck() error {
return nil
}

func (e *Noop) Partition(prefillPeriodDays int) error {
return nil
}
1 change: 0 additions & 1 deletion clients/database/postgres/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
// that haven't been run on the database being used by the proxy service
// returning error (if any) and a list of migrations that have been
// run and any that were not
// If db is nil, returns empty slice and nil error, as there is no database to migrate.
func (c *Client) Migrate(ctx context.Context, migrations migrate.Migrations, logger *logging.ServiceLogger) (*migrate.MigrationSlice, error) {
// set up migration config
migrator := migrate.NewMigrator(c.db, &migrations)
Expand Down
71 changes: 31 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ func init() {
}

func startMetricPartitioningRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error {
if !serviceConfig.MetricDatabaseEnabled {
serviceLogger.Info().Msg("skipping starting metric partitioning routine since it is disabled via config")

return nil
}

metricPartitioningRoutineConfig := routines.MetricPartitioningRoutineConfig{
Interval: serviceConfig.MetricPartitioningRoutineInterval,
DelayFirstRun: serviceConfig.MetricPartitioningRoutineDelayFirstRun,
Expand Down Expand Up @@ -73,12 +67,6 @@ func startMetricPartitioningRoutine(serviceConfig config.Config, service service
}

func startMetricCompactionRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error {
if !serviceConfig.MetricDatabaseEnabled {
serviceLogger.Info().Msg("skipping starting metric compaction routine since it is disabled via config")

return nil
}

metricCompactionRoutineConfig := routines.MetricCompactionRoutineConfig{
Interval: serviceConfig.MetricCompactionRoutineInterval,
Database: service.Database,
Expand Down Expand Up @@ -107,7 +95,7 @@ func startMetricCompactionRoutine(serviceConfig config.Config, service service.P
}

func startMetricPruningRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error {
if !serviceConfig.MetricPruningEnabled || !serviceConfig.MetricDatabaseEnabled {
if !serviceConfig.MetricPruningEnabled {
serviceLogger.Info().Msg("skipping starting metric pruning routine since it is disabled via config")

return make(<-chan error)
Expand Down Expand Up @@ -154,33 +142,36 @@ func main() {
serviceLogger.Panic().Msg(fmt.Sprintf("%v", errors.Unwrap(err)))
}

// configure and run background routines
// metric partitioning routine
go func() {
metricPartitioningErrs := startMetricPartitioningRoutine(serviceConfig, service, serviceLogger)

for routineErr := range metricPartitioningErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric partitioning routine encountered error %s", routineErr))
}
}()

// metric compaction routine
go func() {
metricCompactionErrs := startMetricCompactionRoutine(serviceConfig, service, serviceLogger)

for routineErr := range metricCompactionErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric compaction routine encountered error %s", routineErr))
}
}()

// metric pruning routine
go func() {
metricPruningErrs := startMetricPruningRoutine(serviceConfig, service, serviceLogger)

for routineErr := range metricPruningErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric pruning routine encountered error %s", routineErr))
}
}()
// if we use metrics with database, we run some background routines
if !serviceConfig.MetricDatabaseEnabled {
// configure and run background routines
// metric partitioning routine
go func() {
metricPartitioningErrs := startMetricPartitioningRoutine(serviceConfig, service, serviceLogger)

for routineErr := range metricPartitioningErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric partitioning routine encountered error %s", routineErr))
}
}()

// metric compaction routine
go func() {
metricCompactionErrs := startMetricCompactionRoutine(serviceConfig, service, serviceLogger)

for routineErr := range metricCompactionErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric compaction routine encountered error %s", routineErr))
}
}()

// metric pruning routine
go func() {
metricPruningErrs := startMetricPruningRoutine(serviceConfig, service, serviceLogger)

for routineErr := range metricPruningErrs {
serviceLogger.Error().Msg(fmt.Sprintf("metric pruning routine encountered error %s", routineErr))
}
}()
}

// run the proxy service
finalErr := service.Run()
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func TestE2ETestProxyProxiesForMultipleHosts(t *testing.T) {
}

func TestE2ETestProxyCreatesRequestMetricForEachRequest(t *testing.T) {
if shouldSkipMetrics() {
t.Skip("metrics are disabled")
}

testEthMethodName := "eth_getBlockByNumber"
// create api and database clients
client, err := ethclient.Dial(proxyServiceURL)
Expand All @@ -214,10 +218,6 @@ func TestE2ETestProxyCreatesRequestMetricForEachRequest(t *testing.T) {

require.NoError(t, err)

if shouldSkipMetrics() {
return
}

requestMetricsDuringRequestWindow := waitForMetricsInWindow(t, 1, databaseClient, startTime, []string{testEthMethodName})

requestMetricDuringRequestWindow := requestMetricsDuringRequestWindow[0]
Expand Down
6 changes: 3 additions & 3 deletions routines/metric_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MetricPruningRoutine struct {
interval time.Duration
startDelay time.Duration
maxRequestMetricsHistoryDays int64
Database database.MetricsDatabase
db database.MetricsDatabase
logging.ServiceLogger
}

Expand All @@ -50,7 +50,7 @@ func (mpr *MetricPruningRoutine) Run() (<-chan error, error) {
for tick := range timer {
mpr.Trace().Msg(fmt.Sprintf("%s tick at %+v", mpr.id, tick))

mpr.Database.DeleteProxiedRequestMetricsOlderThanNDays(context.Background(), mpr.maxRequestMetricsHistoryDays)
mpr.db.DeleteProxiedRequestMetricsOlderThanNDays(context.Background(), mpr.maxRequestMetricsHistoryDays)
}
}()

Expand All @@ -65,7 +65,7 @@ func NewMetricPruningRoutine(config MetricPruningRoutineConfig) (*MetricPruningR
interval: config.Interval,
startDelay: config.StartDelay,
maxRequestMetricsHistoryDays: config.MaxRequestMetricsHistoryDays,
Database: config.Database,
db: config.Database,
ServiceLogger: config.Logger,
}, nil
}
4 changes: 2 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"context"
"fmt"
"github.com/kava-labs/kava-proxy-service/clients/database"
"github.com/kava-labs/kava-proxy-service/clients/database/empty"
"github.com/kava-labs/kava-proxy-service/clients/database/noop"
"github.com/kava-labs/kava-proxy-service/clients/database/postgres"
"github.com/kava-labs/kava-proxy-service/clients/database/postgres/migrations"
"net/http"
Expand Down Expand Up @@ -46,7 +46,7 @@ func New(ctx context.Context, config config.Config, serviceLogger *logging.Servi
return ProxyService{}, err
}
} else {
db = empty.New()
db = noop.New()
}

// create evm api client
Expand Down

0 comments on commit 1c9010a

Please sign in to comment.