diff --git a/clients/database/request_metric.go b/clients/database/request_metric.go index 59c7d61..38c9fce 100644 --- a/clients/database/request_metric.go +++ b/clients/database/request_metric.go @@ -109,3 +109,12 @@ WHERE parent.relname='%s' order by child.oid desc limit 1;`, ProxiedRequestMetri return lastCreatedAttachedPartitionName, nil } + +// DeleteProxiedRequestMetricsOlderThanNDays deletes +// all proxied request metrics older than the specified +// days, returning error (if any) +func DeleteProxiedRequestMetricsOlderThanNDays(ctx context.Context, db *bun.DB, n int64) error { + _, err := db.NewDelete().Model((*ProxiedRequestMetric)(nil)).Where(fmt.Sprintf("request_time < now() - interval '%d' day", n)).Exec(ctx) + + return err +} diff --git a/main.go b/main.go index c61b1b4..a213682 100644 --- a/main.go +++ b/main.go @@ -95,11 +95,17 @@ func startMetricCompactionRoutine(serviceConfig config.Config, service service.P } func startMetricPruningRoutine(serviceConfig config.Config, service service.ProxyService, serviceLogger logging.ServiceLogger) <-chan error { + if !serviceConfig.MetricPruningEnabled { + serviceLogger.Info().Msg("skipping starting metric pruning routine since it is disabled via config") + return make(<-chan error) + } + metricPruningRoutineConfig := routines.MetricPruningRoutineConfig{ - Interval: serviceConfig.MetricPruningRoutineInterval, - StartDelay: serviceConfig.MetricPartitioningRoutineDelayFirstRun, - Database: service.Database, - Logger: serviceLogger, + Interval: serviceConfig.MetricPruningRoutineInterval, + StartDelay: serviceConfig.MetricPartitioningRoutineDelayFirstRun, + MaxRequestMetricsHistoryDays: int64(serviceConfig.MetricPruningMaxRequestMetricsHistoryDays), + Database: service.Database, + Logger: serviceLogger, } metricPruningRoutine, err := routines.NewMetricPruningRoutine(metricPruningRoutineConfig) diff --git a/routines/metric_pruning.go b/routines/metric_pruning.go index ec67360..6586c7e 100644 --- a/routines/metric_pruning.go +++ b/routines/metric_pruning.go @@ -4,6 +4,7 @@ package routines import ( + "context" "fmt" "time" @@ -15,19 +16,21 @@ import ( // MetricPruningRoutineConfig wraps values used // for creating a new metric Pruning routine type MetricPruningRoutineConfig struct { - Interval time.Duration - StartDelay time.Duration - Database *database.PostgresClient - Logger logging.ServiceLogger + Interval time.Duration + StartDelay time.Duration + MaxRequestMetricsHistoryDays int64 + Database *database.PostgresClient + Logger logging.ServiceLogger } // MetricPruningRoutine can be used to // run a background routine on a configurable interval // to aggregate and prune historical request metrics type MetricPruningRoutine struct { - id string - interval time.Duration - startDelay time.Duration + id string + interval time.Duration + startDelay time.Duration + maxRequestMetricsHistoryDays int64 *database.PostgresClient logging.ServiceLogger } @@ -46,6 +49,8 @@ func (mpr *MetricPruningRoutine) Run() (<-chan error, error) { go func() { for tick := range timer { mpr.Trace().Msg(fmt.Sprintf("%s tick at %+v", mpr.id, tick)) + + database.DeleteProxiedRequestMetricsOlderThanNDays(context.Background(), mpr.DB, mpr.maxRequestMetricsHistoryDays) } }() @@ -56,10 +61,11 @@ func (mpr *MetricPruningRoutine) Run() (<-chan error, error) { // using the provided config, returning the routine and error (if any) func NewMetricPruningRoutine(config MetricPruningRoutineConfig) (*MetricPruningRoutine, error) { return &MetricPruningRoutine{ - id: uuid.New().String(), - interval: config.Interval, - startDelay: config.StartDelay, - PostgresClient: config.Database, - ServiceLogger: config.Logger, + id: uuid.New().String(), + interval: config.Interval, + startDelay: config.StartDelay, + maxRequestMetricsHistoryDays: config.MaxRequestMetricsHistoryDays, + PostgresClient: config.Database, + ServiceLogger: config.Logger, }, nil }