mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 14:07:02 +09:00
app.AppName + prometheus versions metric
This commit is contained in:
parent
b141dcdb47
commit
0d72b3b671
3 changed files with 55 additions and 3 deletions
10
app/app.go
10
app/app.go
|
@ -15,8 +15,8 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// values of this vars will be defined while compilation
|
// values of this vars will be defined while compilation
|
||||||
GitCommit, GitBranch, GitState, GitSummary, BuildDate string
|
AppName, GitCommit, GitBranch, GitState, GitSummary, BuildDate string
|
||||||
name string
|
name string
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -66,6 +66,10 @@ func (app *App) Name() string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *App) AppName() string {
|
||||||
|
return AppName
|
||||||
|
}
|
||||||
|
|
||||||
// Version return app version
|
// Version return app version
|
||||||
func (app *App) Version() string {
|
func (app *App) Version() string {
|
||||||
return GitSummary
|
return GitSummary
|
||||||
|
@ -257,7 +261,7 @@ func (app *App) Close(ctx context.Context) error {
|
||||||
case <-time.After(StopWarningAfter):
|
case <-time.After(StopWarningAfter):
|
||||||
statLogger(app.stopStat, log).
|
statLogger(app.stopStat, log).
|
||||||
With(zap.String("in_progress", currentComponentStopping)).
|
With(zap.String("in_progress", currentComponentStopping)).
|
||||||
Warn("components close in progress")
|
Warn("components close in progress")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
|
|
|
@ -32,9 +32,11 @@ type metric struct {
|
||||||
registry *prometheus.Registry
|
registry *prometheus.Registry
|
||||||
rpcLog logger.CtxLogger
|
rpcLog logger.CtxLogger
|
||||||
config Config
|
config Config
|
||||||
|
a *app.App
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metric) Init(a *app.App) (err error) {
|
func (m *metric) Init(a *app.App) (err error) {
|
||||||
|
m.a = a
|
||||||
m.registry = prometheus.NewRegistry()
|
m.registry = prometheus.NewRegistry()
|
||||||
m.config = a.MustComponent("config").(configSource).GetMetric()
|
m.config = a.MustComponent("config").(configSource).GetMetric()
|
||||||
m.rpcLog = logger.NewNamed("rpcLog")
|
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 {
|
if err = m.registry.Register(collectors.NewGoCollector()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err = m.registry.Register(newVersionsCollector(m.a)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if m.config.Addr != "" {
|
if m.config.Addr != "" {
|
||||||
var errCh = make(chan error)
|
var errCh = make(chan error)
|
||||||
http.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{}))
|
http.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{}))
|
||||||
|
|
43
metric/version.go
Normal file
43
metric/version.go
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue