net pool metrics

This commit is contained in:
Sergey Cherepanov 2023-01-12 13:53:32 +03:00 committed by Mikhail Iudin
parent 332d8bb373
commit 0317a4fbe2
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 13 additions and 1 deletions

View File

@ -6,6 +6,9 @@ func WithPrometheus(reg *prometheus.Registry, namespace, subsystem string) Optio
if subsystem == "" { if subsystem == "" {
subsystem = "cache" subsystem = "cache"
} }
if reg == nil {
return nil
}
return func(cache *oCache) { return func(cache *oCache) {
cache.metrics = &metrics{ cache.metrics = &metrics{
hit: prometheus.NewCounter(prometheus.CounterOpts{ hit: prometheus.NewCounter(prometheus.CounterOpts{

View File

@ -61,7 +61,9 @@ func New(loadFunc LoadFunc, opts ...Option) OCache {
log: log.Sugar(), log: log.Sugar(),
} }
for _, o := range opts { for _, o := range opts {
o(c) if o != nil {
o(c)
}
} }
if c.ttl != 0 && c.gc != 0 { if c.ttl != 0 && c.gc != 0 {
go c.ticker() go c.ticker()

View File

@ -6,8 +6,10 @@ import (
"github.com/anytypeio/any-sync/app" "github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/app/logger" "github.com/anytypeio/any-sync/app/logger"
"github.com/anytypeio/any-sync/app/ocache" "github.com/anytypeio/any-sync/app/ocache"
"github.com/anytypeio/any-sync/metric"
"github.com/anytypeio/any-sync/net/dialer" "github.com/anytypeio/any-sync/net/dialer"
"github.com/anytypeio/any-sync/net/peer" "github.com/anytypeio/any-sync/net/peer"
"github.com/prometheus/client_golang/prometheus"
"math/rand" "math/rand"
"time" "time"
) )
@ -47,6 +49,10 @@ type pool struct {
func (p *pool) Init(a *app.App) (err error) { func (p *pool) Init(a *app.App) (err error) {
p.dialer = a.MustComponent(dialer.CName).(dialer.Dialer) p.dialer = a.MustComponent(dialer.CName).(dialer.Dialer)
var reg *prometheus.Registry
if m := a.Component(metric.CName); m != nil {
reg = m.(metric.Metric).Registry()
}
p.cache = ocache.New( p.cache = ocache.New(
func(ctx context.Context, id string) (value ocache.Object, err error) { func(ctx context.Context, id string) (value ocache.Object, err error) {
return p.dialer.Dial(ctx, id) return p.dialer.Dial(ctx, id)
@ -54,6 +60,7 @@ func (p *pool) Init(a *app.App) (err error) {
ocache.WithLogger(log.Sugar()), ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute), ocache.WithGCPeriod(time.Minute),
ocache.WithTTL(time.Minute*5), ocache.WithTTL(time.Minute*5),
ocache.WithPrometheus(reg, "netpool", "cache"),
) )
return nil return nil
} }