Skip to content

Commit

Permalink
database cleanup for metrics on stop of anklet
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Jan 7, 2025
1 parent 793c994 commit b5ad142
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/mem"
"github.com/veertuinc/anklet/internal/config"
"github.com/veertuinc/anklet/internal/database"
"github.com/veertuinc/anklet/internal/logging"
)

// Server defines the structure for the API server
Expand Down Expand Up @@ -754,3 +756,15 @@ func GetMetricsDataFromContext(ctx context.Context) (*MetricsDataLock, error) {
}
return metricsData, nil
}

func Cleanup(ctx context.Context, logger *slog.Logger, owner string, name string) {
databaseContainer, err := database.GetDatabaseFromContext(ctx)
if err != nil {
logger.ErrorContext(ctx, "error getting database client from context", "error", err.Error())
}
result := databaseContainer.Client.Del(context.Background(), "anklet/metrics/"+owner+"/"+name)
if result.Err() != nil {
logger.ErrorContext(ctx, "error deleting metrics data from Redis", "error", result.Err().Error())
}
logging.DevContext(ctx, "successfully deleted metrics data from Redis, key: anklet/metrics/"+owner+"/"+name)
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ func worker(
return
}
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("database"), databaseClient)
// cleanup metrics data when the plugin is stopped (otherwise it's orphaned in the aggregator)
if index == 0 { // only cleanup the first plugin's metrics data (they're aggregated by the first plugin's name)
defer metrics.Cleanup(pluginCtx, pluginLogger, plugin.Owner, plugin.Name)
}
logging.DevContext(pluginCtx, "connected to database")
}

Expand Down

0 comments on commit b5ad142

Please sign in to comment.