From bd1f84b6a4ab4b8927c35b66ae62ebb68b155af0 Mon Sep 17 00:00:00 2001 From: Mikhail Iudin Date: Wed, 27 Nov 2024 19:29:14 +0100 Subject: [PATCH] GO-3508 Add dependencies tracing --- app/app.go | 25 +++++++++++++++++-------- app/apptrace.go | 8 ++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 app/apptrace.go diff --git a/app/app.go b/app/app.go index 0269be7d..29844b6e 100644 --- a/app/app.go +++ b/app/app.go @@ -57,14 +57,15 @@ type ComponentStatable interface { // App is the central part of the application // It contains and manages all components type App struct { - parent *App - components []Component - mu sync.RWMutex - startStat Stat - stopStat Stat - deviceState int - versionName string - anySyncVersion string + parent *App + components []Component + mu sync.RWMutex + startStat Stat + stopStat Stat + deviceState int + versionName string + anySyncVersion string + componentListener func(comp Component) } // Name returns app name @@ -170,6 +171,7 @@ func (app *App) Component(name string) Component { // MustComponent is like Component, but it will panic if service wasn't found func (app *App) MustComponent(name string) Component { s := app.Component(name) + app.onComponent(s) if s == nil { panic(fmt.Errorf("component '%s' not registered", name)) } @@ -184,6 +186,7 @@ func MustComponent[i any](app *App) i { for current != nil { for _, s := range current.components { if v, ok := s.(i); ok { + app.onComponent(s) return v } } @@ -384,3 +387,9 @@ func (app *App) AnySyncVersion() string { }) return app.anySyncVersion } + +func (app *App) onComponent(s Component) { + if app.componentListener != nil { + app.componentListener(s) + } +} diff --git a/app/apptrace.go b/app/apptrace.go new file mode 100644 index 00000000..1f75144a --- /dev/null +++ b/app/apptrace.go @@ -0,0 +1,8 @@ +//go:build appdebug +// +build appdebug + +package app + +func (app *App) SetOnComponentListener(listener func(comp Component)) { + app.componentListener = listener +}