From 0d72b3b6716216a76477c91996638f4f8ca084cc Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Thu, 18 May 2023 13:21:01 +0200 Subject: [PATCH] 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 +}