commit
4673a62579
39
app/app.go
39
app/app.go
@ -8,6 +8,7 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -15,8 +16,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 (
|
||||||
@ -54,11 +55,12 @@ type ComponentStatable interface {
|
|||||||
// App is the central part of the application
|
// App is the central part of the application
|
||||||
// It contains and manages all components
|
// It contains and manages all components
|
||||||
type App struct {
|
type App struct {
|
||||||
components []Component
|
components []Component
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
startStat Stat
|
startStat Stat
|
||||||
stopStat Stat
|
stopStat Stat
|
||||||
deviceState int
|
deviceState int
|
||||||
|
anySyncVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns app name
|
// Name returns app name
|
||||||
@ -66,6 +68,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 +263,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() {
|
||||||
@ -311,3 +317,20 @@ func (app *App) SetDeviceState(state int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var onceVersion sync.Once
|
||||||
|
|
||||||
|
func (app *App) AnySyncVersion() string {
|
||||||
|
onceVersion.Do(func() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@ -35,6 +35,10 @@ func Identity(val string) zap.Field {
|
|||||||
return zap.String("identity", val)
|
return zap.String("identity", val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func App(app string) zap.Field {
|
||||||
|
return zap.String("app", app)
|
||||||
|
}
|
||||||
|
|
||||||
func FileId(fileId string) zap.Field {
|
func FileId(fileId string) zap.Field {
|
||||||
return zap.String("fileId", fileId)
|
return zap.String("fileId", fileId)
|
||||||
}
|
}
|
||||||
@ -57,5 +61,5 @@ func (m *metric) RequestLog(ctx context.Context, rpc string, fields ...zap.Field
|
|||||||
if ak != nil {
|
if ak != nil {
|
||||||
acc = ak.Account()
|
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))...)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,11 @@ package metric
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/anytypeio/any-sync/app/logger"
|
"github.com/anytypeio/any-sync/app/logger"
|
||||||
|
"go.uber.org/zap"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLog(t *testing.T) {
|
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")
|
m.RequestLog(context.Background(), "rpc")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,12 +32,16 @@ type metric struct {
|
|||||||
registry *prometheus.Registry
|
registry *prometheus.Registry
|
||||||
rpcLog logger.CtxLogger
|
rpcLog logger.CtxLogger
|
||||||
config Config
|
config Config
|
||||||
|
a *app.App
|
||||||
|
appField zap.Field
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
|
m.appField = App(a.Version())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +56,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{}))
|
||||||
|
|||||||
30
metric/version.go
Normal file
30
metric/version.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package metric
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/anytypeio/any-sync/app"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
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": a.AnySyncVersion(),
|
||||||
|
"app_name": a.AppName(),
|
||||||
|
"app_version": a.Version(),
|
||||||
|
},
|
||||||
|
), prometheus.GaugeValue, 1)}
|
||||||
|
}
|
||||||
|
|
||||||
|
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…
x
Reference in New Issue
Block a user