diff --git a/app/app.go b/app/app.go index a83e0157..e9e69391 100644 --- a/app/app.go +++ b/app/app.go @@ -8,6 +8,7 @@ import ( "go.uber.org/zap" "os" "runtime" + "runtime/debug" "strings" "sync" "time" @@ -54,11 +55,12 @@ type ComponentStatable interface { // App is the central part of the application // It contains and manages all components type App struct { - components []Component - mu sync.RWMutex - startStat Stat - stopStat Stat - deviceState int + components []Component + mu sync.RWMutex + startStat Stat + stopStat Stat + deviceState int + anySyncVersion string } // Name returns app name @@ -315,3 +317,21 @@ func (app *App) SetDeviceState(state int) { } } } + +var onceVersion sync.Once + +func (app *App) AnySyncVersion() string { + onceVersion.Do(func() { + fmt.Println("111") + info, ok := debug.ReadBuildInfo() + if ok { + for _, mod := range info.Deps { + if mod.Path == "github.com/anytypeio/any-sync" { + app.anySyncVersion = mod.Version + break + } + } + } + }) + return app.anySyncVersion +} diff --git a/metric/version.go b/metric/version.go index 614552d1..19a93a12 100644 --- a/metric/version.go +++ b/metric/version.go @@ -3,7 +3,6 @@ package metric import ( "github.com/anytypeio/any-sync/app" "github.com/prometheus/client_golang/prometheus" - "runtime/debug" ) func newVersionsCollector(a *app.App) prometheus.Collector { @@ -11,25 +10,13 @@ func newVersionsCollector(a *app.App) prometheus.Collector { "anysync_versions", "Build information about the main Go module.", nil, prometheus.Labels{ - "anysync_version": anySyncVerion(), + "anysync_version": a.AnySyncVersion(), "app_name": a.AppName(), "app_version": a.Version(), }, ), prometheus.GaugeValue, 1)} } -func anySyncVerion() string { - info, ok := debug.ReadBuildInfo() - if ok { - for _, mod := range info.Deps { - if mod.Path == "github.com/anytypeio/any-sync" { - return mod.Version - } - } - } - return "" -} - type versionCollector struct { ver prometheus.Metric }