From 6c98cfbf57eb250b033aeac1609ba7ca6b71e20c Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Thu, 18 May 2023 13:21:01 +0200 Subject: [PATCH 1/5] app.AppName + prometheus versions metric --- app/app.go | 10 +++++++--- metric/metric.go | 5 +++++ metric/version.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 metric/version.go diff --git a/app/app.go b/app/app.go index 84682f2c..a83e0157 100644 --- a/app/app.go +++ b/app/app.go @@ -15,8 +15,8 @@ import ( var ( // values of this vars will be defined while compilation - GitCommit, GitBranch, GitState, GitSummary, BuildDate string - name string + AppName, GitCommit, GitBranch, GitState, GitSummary, BuildDate string + name string ) var ( @@ -66,6 +66,10 @@ func (app *App) Name() string { return name } +func (app *App) AppName() string { + return AppName +} + // Version return app version func (app *App) Version() string { return GitSummary @@ -257,7 +261,7 @@ func (app *App) Close(ctx context.Context) error { case <-time.After(StopWarningAfter): statLogger(app.stopStat, log). With(zap.String("in_progress", currentComponentStopping)). - Warn("components close in progress") + Warn("components close in progress") } }() go func() { diff --git a/metric/metric.go b/metric/metric.go index d5319982..fedff6bd 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -32,9 +32,11 @@ type metric struct { registry *prometheus.Registry rpcLog logger.CtxLogger config Config + a *app.App } func (m *metric) Init(a *app.App) (err error) { + m.a = a m.registry = prometheus.NewRegistry() m.config = a.MustComponent("config").(configSource).GetMetric() m.rpcLog = logger.NewNamed("rpcLog") @@ -52,6 +54,9 @@ func (m *metric) Run(ctx context.Context) (err error) { if err = m.registry.Register(collectors.NewGoCollector()); err != nil { return err } + if err = m.registry.Register(newVersionsCollector(m.a)); err != nil { + return + } if m.config.Addr != "" { var errCh = make(chan error) http.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})) diff --git a/metric/version.go b/metric/version.go new file mode 100644 index 00000000..614552d1 --- /dev/null +++ b/metric/version.go @@ -0,0 +1,43 @@ +package metric + +import ( + "github.com/anytypeio/any-sync/app" + "github.com/prometheus/client_golang/prometheus" + "runtime/debug" +) + +func newVersionsCollector(a *app.App) prometheus.Collector { + return &versionCollector{prometheus.MustNewConstMetric(prometheus.NewDesc( + "anysync_versions", + "Build information about the main Go module.", + nil, prometheus.Labels{ + "anysync_version": anySyncVerion(), + "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 +} + +func (v *versionCollector) Describe(descs chan<- *prometheus.Desc) { + descs <- v.ver.Desc() +} + +func (v *versionCollector) Collect(metrics chan<- prometheus.Metric) { + metrics <- v.ver +} From b22ddb612a0ce9a24282cae97336cbbe9ec178ee Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Thu, 18 May 2023 13:31:53 +0200 Subject: [PATCH 2/5] app.AnySyncVersion --- app/app.go | 30 +++++++++++++++++++++++++----- metric/version.go | 15 +-------------- 2 files changed, 26 insertions(+), 19 deletions(-) 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 } From 190d48b430e965e50f37e1dd2a48f7425c5787da Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Thu, 18 May 2023 13:35:49 +0200 Subject: [PATCH 3/5] write app version to rpc log --- metric/log.go | 6 +++++- metric/metric.go | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/metric/log.go b/metric/log.go index 7cc34e66..edb63249 100644 --- a/metric/log.go +++ b/metric/log.go @@ -35,6 +35,10 @@ func Identity(val string) zap.Field { return zap.String("identity", val) } +func App(app string) zap.Field { + return zap.String("app", app) +} + func FileId(fileId string) zap.Field { return zap.String("fileId", fileId) } @@ -57,5 +61,5 @@ func (m *metric) RequestLog(ctx context.Context, rpc string, fields ...zap.Field if ak != nil { acc = ak.Account() } - m.rpcLog.Info("", append(fields, PeerId(peerId), Identity(acc), Method(rpc))...) + m.rpcLog.Info("", append(fields, m.appField, PeerId(peerId), Identity(acc), Method(rpc))...) } diff --git a/metric/metric.go b/metric/metric.go index fedff6bd..aeac0550 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -33,6 +33,7 @@ type metric struct { rpcLog logger.CtxLogger config Config a *app.App + appField zap.Field } func (m *metric) Init(a *app.App) (err error) { @@ -40,6 +41,7 @@ func (m *metric) Init(a *app.App) (err error) { m.registry = prometheus.NewRegistry() m.config = a.MustComponent("config").(configSource).GetMetric() m.rpcLog = logger.NewNamed("rpcLog") + m.appField = App(a.Version()) return nil } From 014d8d72dfc29796598b258cda3e1509a023fa8d Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Fri, 19 May 2023 22:18:39 +0200 Subject: [PATCH 4/5] remove debug --- app/app.go | 1 - 1 file changed, 1 deletion(-) diff --git a/app/app.go b/app/app.go index e9e69391..80d5b1fe 100644 --- a/app/app.go +++ b/app/app.go @@ -322,7 +322,6 @@ 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 { From be29456a29ddd2e889f11e3cb32a1df761ac3f8e Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Fri, 19 May 2023 22:21:12 +0200 Subject: [PATCH 5/5] fix test --- metric/log_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metric/log_test.go b/metric/log_test.go index 50835795..e22a7112 100644 --- a/metric/log_test.go +++ b/metric/log_test.go @@ -3,10 +3,11 @@ package metric import ( "context" "github.com/anytypeio/any-sync/app/logger" + "go.uber.org/zap" "testing" ) func TestLog(t *testing.T) { - m := &metric{rpcLog: logger.NewNamed("rpcLog")} + m := &metric{rpcLog: logger.NewNamed("rpcLog"), appField: zap.String("appName", "test")} m.RequestLog(context.Background(), "rpc") }