Skip to content

Commit

Permalink
chore: add slow diff log
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Aug 28, 2024
1 parent eab25a2 commit f473d23
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
11 changes: 11 additions & 0 deletions db/models/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ type ConfigItem struct {
ProbableParents []string `gorm:"-" json:"-"`
}

func (ci ConfigItem) Label() string {
if len(ci.ExternalID) == 0 {
return fmt.Sprintf("%s/%s id=%s ", lo.FromPtr(ci.Type), lo.FromPtr(ci.Name), ci.ID)
}
if ci.ID == ci.ExternalID[0] {
return fmt.Sprintf("%s/%s id=%s", lo.FromPtr(ci.Type), lo.FromPtr(ci.Name), ci.ID)
}

return fmt.Sprintf("%s/%s id=%s external=%s", lo.FromPtr(ci.Type), lo.FromPtr(ci.Name), ci.ID, ci.ExternalID[0])
}

func (ci ConfigItem) String() string {
if len(ci.ExternalID) == 0 {
return fmt.Sprintf("id=%s type=%s name=%s ", ci.ID, lo.FromPtr(ci.Type), lo.FromPtr(ci.Name))
Expand Down
35 changes: 35 additions & 0 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
jsonpatch "github.com/evanphx/json-patch"
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/text"
"github.com/flanksource/commons/timer"
cUtils "github.com/flanksource/commons/utils"
"github.com/flanksource/config-db/api"
v1 "github.com/flanksource/config-db/api/v1"
Expand Down Expand Up @@ -674,11 +676,44 @@ func generateConfigChange(ctx api.ScrapeContext, newConf, prev models.ConfigItem
}
}

var _timer *timer.MemoryTimer
if ctx.Properties().On(false, "scraper.log.items") {
ctx.Logger.V(5).Infof("generating diff for %s", newConf.Label())
}
if ctx.Logger.IsLevelEnabled(4) && len(*newConf.Config) > ctx.Properties().Int("scraper.diff.timer.minSize", 1024*20) {
_timer = lo.ToPtr(timer.NewMemoryTimer())
}

start := time.Now()

diff, err := GenerateDiff(ctx.Context, *newConf.Config, *prev.Config)
if err != nil {
return nil, fmt.Errorf("failed to generate diff: %w", err)
}

duration := time.Since(start)

ctx.Histogram("scraper_diff_duration",
[]float64{1, 10, 100, 1000, 10000}, "scraper", ctx.ScrapeConfig().GetPersistedID().String()).
Record(time.Duration(duration.Milliseconds()))

msg := fmt.Sprintf("generated in %dms for %s size=%s diff=%s",
duration.Milliseconds(),
newConf.Label(),
text.HumanizeBytes(len(*newConf.Config)),
text.HumanizeBytes(len(diff)),
)
if _timer != nil {
msg += " " + _timer.End()
}
if duration > 500*time.Millisecond {
ctx.Logger.Warnf("SLOW DIFF >= %s", msg)
} else if duration > 50*time.Millisecond {
ctx.Logger.Infof("SLOW DIFF >= %s", msg)
} else if ctx.Properties().On(false, "scraper.log.items") {
ctx.Logger.V(4).Infof(msg)
}

if diff == "" {
return nil, nil
}
Expand Down
12 changes: 9 additions & 3 deletions scrapers/kubernetes/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func (t *SharedInformerManager) Register(ctx api.ScrapeContext, watchResource v1
return
}

ctx.Logger.V(3).Infof("added: %s %s %s", u.GetUID(), u.GetKind(), u.GetName())
if ctx.Properties().On(false, "scraper.log.items") {
ctx.Logger.V(4).Infof("added: %s %s %s", u.GetUID(), u.GetKind(), u.GetName())
}
buffer <- u
},
UpdateFunc: func(oldObj any, newObj any) {
Expand All @@ -71,7 +73,9 @@ func (t *SharedInformerManager) Register(ctx api.ScrapeContext, watchResource v1
return
}

ctx.Logger.V(3).Infof("updated: %s %s %s", u.GetUID(), u.GetKind(), u.GetName())
if ctx.Properties().On(false, "scraper.log.items") {
ctx.Logger.V(3).Infof("updated: %s %s %s", u.GetUID(), u.GetKind(), u.GetName())
}
buffer <- u
},
DeleteFunc: func(obj any) {
Expand All @@ -81,7 +85,9 @@ func (t *SharedInformerManager) Register(ctx api.ScrapeContext, watchResource v1
return
}

ctx.Logger.V(3).Infof("deleted: %s %s %s", u.GetUID(), u.GetKind(), u.GetName())
if ctx.Properties().On(false, "scraper.log.items") {
ctx.Logger.V(3).Infof("deleted: %s %s %s", u.GetUID(), u.GetKind(), u.GetName())
}
deleteBuffer <- string(u.GetUID())
},
})
Expand Down

0 comments on commit f473d23

Please sign in to comment.