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 == "" {
subsystem = "cache"
}
if reg == nil {
return nil
}
return func(cache *oCache) {
cache.metrics = &metrics{
hit: prometheus.NewCounter(prometheus.CounterOpts{

View File

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

View File

@ -6,8 +6,10 @@ import (
"github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/app/logger"
"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/peer"
"github.com/prometheus/client_golang/prometheus"
"math/rand"
"time"
)
@ -47,6 +49,10 @@ type pool struct {
func (p *pool) Init(a *app.App) (err error) {
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(
func(ctx context.Context, id string) (value ocache.Object, err error) {
return p.dialer.Dial(ctx, id)
@ -54,6 +60,7 @@ func (p *pool) Init(a *app.App) (err error) {
ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute),
ocache.WithTTL(time.Minute*5),
ocache.WithPrometheus(reg, "netpool", "cache"),
)
return nil
}