Skip to content

Commit

Permalink
fix: duty context middleware and add span for deleting stale configs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Sep 18, 2024
1 parent 0292971 commit 0d94428
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func run(cmd *cobra.Command, args []string) error {
utilruntime.Must(configsv1.AddToScheme(scheme))

// Start the server
go serve(args)
go serve(dutyCtx, args)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Expand Down
16 changes: 7 additions & 9 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,24 @@ var Serve = &cobra.Command{
return fmt.Errorf("failed to initialize change fingerprint cache: %w", err)
}

serve(args)
serve(dutyCtx, args)
return nil
},
}

func serve(configFiles []string) {
func serve(ctx dutyContext.Context, configFiles []string) {
e := echo.New()

dutyEcho.AddDebugHandlers(ctx, e, func(next echo.HandlerFunc) echo.HandlerFunc { return next })
e.Use(otelecho.Middleware("config-db", otelecho.WithSkipper(telemetryURLSkipper)))

e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := api.DefaultContext.Wrap(c.Request().Context())
c.SetRequest(c.Request().WithContext(ctx))
c.SetRequest(c.Request().WithContext(ctx.Wrap(c.Request().Context())))
return next(c)
}
})

dutyEcho.AddDebugHandlers(api.DefaultContext.DutyContext(), e, func(next echo.HandlerFunc) echo.HandlerFunc { return next })
e.Use(otelecho.Middleware("config-db", otelecho.WithSkipper(telemetryURLSkipper)))

if logger.IsTraceEnabled() {
echoLogConfig := middleware.DefaultLoggerConfig
echoLogConfig.Skipper = telemetryURLSkipper
Expand Down Expand Up @@ -104,7 +103,7 @@ func serve(configFiles []string) {

go startScraperCron(configFiles)

go jobs.ScheduleJobs(api.DefaultContext.DutyContext())
go jobs.ScheduleJobs(ctx)
shutdown.AddHook(jobs.Stop)

shutdown.AddHook(func() {
Expand All @@ -121,7 +120,6 @@ func serve(configFiles []string) {
if err := e.Start(fmt.Sprintf(":%d", httpPort)); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal(err)
}

}

func startScraperCron(configFiles []string) {
Expand Down
2 changes: 1 addition & 1 deletion db/config_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"gorm.io/gorm"
)

func FindScraper(ctx api.ScrapeContext, id string) (*models.ConfigScraper, error) {
func FindScraper(ctx context.Context, id string) (*models.ConfigScraper, error) {
var configScraper models.ConfigScraper
if err := ctx.DB().Where("id = ?", id).First(&configScraper).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand Down
11 changes: 11 additions & 0 deletions scrapers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/flanksource/config-db/api"
v1 "github.com/flanksource/config-db/api/v1"
"github.com/flanksource/config-db/db"
"go.opentelemetry.io/otel/attribute"
)

type contextKey string
Expand Down Expand Up @@ -56,8 +57,18 @@ func RunScraper(ctx api.ScrapeContext) (*ScrapeOutput, error) {
}

func UpdateStaleConfigItems(ctx api.ScrapeContext, results v1.ScrapeResults) error {
basectx, span := ctx.StartSpan("UpdateStaleConfigItems")
defer span.End()

ctx.Context = basectx

persistedID := ctx.ScrapeConfig().GetPersistedID()
if persistedID != nil {
ctx.GetSpan().SetAttributes(
attribute.Int("scrape.results", len(results)),
attribute.Bool("scrape.hasError", v1.ScrapeResults(results).HasErr()),
)

// If error in any of the scrape results, don't delete old items
if len(results) > 0 && !v1.ScrapeResults(results).HasErr() {
if err := DeleteStaleConfigItems(ctx, *persistedID); err != nil {
Expand Down
10 changes: 7 additions & 3 deletions scrapers/run_now.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"github.com/flanksource/config-db/api"
v1 "github.com/flanksource/config-db/api/v1"
"github.com/flanksource/config-db/db"
"github.com/flanksource/duty/context"
"github.com/labstack/echo/v4"
)

func RunNowHandler(c echo.Context) error {
id := c.Param("id")

scraper, err := db.FindScraper(api.DefaultContext, id)
baseCtx := c.Request().Context()
ctx := baseCtx.(context.Context)

scraper, err := db.FindScraper(ctx, id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) // could mean server errors as well, but there's no trivial way to find out...
}
Expand All @@ -27,8 +31,8 @@ func RunNowHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to transform config scraper model", err)
}

ctx := api.DefaultContext.WithScrapeConfig(&configScraper)
j := newScraperJob(ctx)
scrapeCtx := api.NewScrapeContext(ctx).WithScrapeConfig(&configScraper)
j := newScraperJob(scrapeCtx)
j.Run()

return c.JSON(http.StatusOK, j.LastJob.Details)
Expand Down

0 comments on commit 0d94428

Please sign in to comment.