Skip to content

Commit

Permalink
implement metric pruning routine logic
Browse files Browse the repository at this point in the history
  • Loading branch information
galxy25 committed Oct 27, 2023
1 parent cbb9e50 commit a6101b2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
9 changes: 9 additions & 0 deletions clients/database/request_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 18 additions & 12 deletions routines/metric_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package routines

import (
"context"
"fmt"
"time"

Expand All @@ -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
}
Expand All @@ -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)
}
}()

Expand All @@ -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
}

0 comments on commit a6101b2

Please sign in to comment.