From 1c9010a8a1ee60a275dd71dc9ea955814b7a158c Mon Sep 17 00:00:00 2001 From: Vladyslav Budichenko Date: Mon, 16 Sep 2024 14:43:35 -0400 Subject: [PATCH] feat: fix comments for review --- clients/database/empty/database.go | 41 ---------------- clients/database/noop/database.go | 41 ++++++++++++++++ clients/database/postgres/migrate.go | 1 - main.go | 71 ++++++++++++---------------- main_test.go | 8 ++-- routines/metric_pruning.go | 6 +-- service/service.go | 4 +- 7 files changed, 81 insertions(+), 91 deletions(-) delete mode 100644 clients/database/empty/database.go create mode 100644 clients/database/noop/database.go diff --git a/clients/database/empty/database.go b/clients/database/empty/database.go deleted file mode 100644 index a60b799..0000000 --- a/clients/database/empty/database.go +++ /dev/null @@ -1,41 +0,0 @@ -package empty - -import ( - "context" - "github.com/kava-labs/kava-proxy-service/clients/database" -) - -// Empty is a database client that does nothing -type Empty struct{} - -func New() *Empty { - return &Empty{} -} - -func (e *Empty) SaveProxiedRequestMetric(ctx context.Context, metric *database.ProxiedRequestMetric) error { - return nil -} - -func (e *Empty) ListProxiedRequestMetricsWithPagination(ctx context.Context, cursor int64, limit int) ([]*database.ProxiedRequestMetric, int64, error) { - return []*database.ProxiedRequestMetric{}, 0, nil -} - -func (e *Empty) CountAttachedProxiedRequestMetricPartitions(ctx context.Context) (int64, error) { - return 0, nil -} - -func (e *Empty) GetLastCreatedAttachedProxiedRequestMetricsPartitionName(ctx context.Context) (string, error) { - return "", nil -} - -func (e *Empty) DeleteProxiedRequestMetricsOlderThanNDays(ctx context.Context, n int64) error { - return nil -} - -func (e *Empty) HealthCheck() error { - return nil -} - -func (e *Empty) Partition(prefillPeriodDays int) error { - return nil -} diff --git a/clients/database/noop/database.go b/clients/database/noop/database.go new file mode 100644 index 0000000..fa133e8 --- /dev/null +++ b/clients/database/noop/database.go @@ -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 +} diff --git a/clients/database/postgres/migrate.go b/clients/database/postgres/migrate.go index 32c30cf..20fa0b3 100644 --- a/clients/database/postgres/migrate.go +++ b/clients/database/postgres/migrate.go @@ -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) diff --git a/main.go b/main.go index 78ac832..ba5a95c 100644 --- a/main.go +++ b/main.go @@ -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, @@ -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, @@ -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) @@ -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() diff --git a/main_test.go b/main_test.go index c6f58a6..1e8ae2a 100644 --- a/main_test.go +++ b/main_test.go @@ -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) @@ -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] diff --git a/routines/metric_pruning.go b/routines/metric_pruning.go index 5110ea8..95812fd 100644 --- a/routines/metric_pruning.go +++ b/routines/metric_pruning.go @@ -31,7 +31,7 @@ type MetricPruningRoutine struct { interval time.Duration startDelay time.Duration maxRequestMetricsHistoryDays int64 - Database database.MetricsDatabase + db database.MetricsDatabase logging.ServiceLogger } @@ -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) } }() @@ -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 } diff --git a/service/service.go b/service/service.go index 653fcf3..133a5b0 100644 --- a/service/service.go +++ b/service/service.go @@ -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" @@ -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