move rpc prometheus config to metric service

This commit is contained in:
Sergey Cherepanov 2023-04-21 17:00:35 +02:00 committed by Mikhail Iudin
parent 01abaf6f81
commit 6ed21a94d3
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 36 additions and 21 deletions

View File

@ -6,12 +6,12 @@ import (
"time" "time"
) )
type PrometheusDRPC struct { type prometheusDRPC struct {
drpc.Handler drpc.Handler
SummaryVec *prometheus.SummaryVec SummaryVec *prometheus.SummaryVec
} }
func (ph *PrometheusDRPC) HandleRPC(stream drpc.Stream, rpc string) (err error) { func (ph *prometheusDRPC) HandleRPC(stream drpc.Stream, rpc string) (err error) {
st := time.Now() st := time.Now()
defer func() { defer func() {
ph.SummaryVec.WithLabelValues(rpc).Observe(time.Since(st).Seconds()) ph.SummaryVec.WithLabelValues(rpc).Observe(time.Since(st).Seconds())

View File

@ -3,32 +3,40 @@ package metric
import ( import (
"context" "context"
"github.com/anytypeio/any-sync/app" "github.com/anytypeio/any-sync/app"
"github.com/anytypeio/any-sync/app/logger"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"net/http" "net/http"
"storj.io/drpc"
"time" "time"
) )
const CName = "common.metric" const CName = "common.metric"
var log = logger.NewNamed(CName)
func New() Metric { func New() Metric {
return new(metric) return new(metric)
} }
type Metric interface { type Metric interface {
Registry() *prometheus.Registry Registry() *prometheus.Registry
WrapDRPCHandler(h drpc.Handler) drpc.Handler
app.ComponentRunnable app.ComponentRunnable
} }
type metric struct { type metric struct {
registry *prometheus.Registry registry *prometheus.Registry
rpcLog logger.CtxLogger
config Config config Config
} }
func (m *metric) Init(a *app.App) (err error) { func (m *metric) Init(a *app.App) (err error) {
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")
return nil return nil
} }
@ -61,6 +69,31 @@ func (m *metric) Registry() *prometheus.Registry {
return m.registry return m.registry
} }
func (m *metric) WrapDRPCHandler(h drpc.Handler) drpc.Handler {
if m == nil {
return h
}
histVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "drpc",
Subsystem: "server",
Name: "duration_seconds",
Objectives: map[float64]float64{
0.5: 0.5,
0.85: 0.01,
0.95: 0.0005,
0.99: 0.0001,
},
}, []string{"rpc"})
if err := m.Registry().Register(histVec); err != nil {
log.Warn("can't register prometheus drpc metric", zap.Error(err))
return h
}
return &prometheusDRPC{
Handler: h,
SummaryVec: histVec,
}
}
func (m *metric) Close(ctx context.Context) (err error) { func (m *metric) Close(ctx context.Context) (err error) {
return return
} }

View File

@ -8,7 +8,6 @@ import (
anyNet "github.com/anytypeio/any-sync/net" anyNet "github.com/anytypeio/any-sync/net"
"github.com/anytypeio/any-sync/net/secureservice" "github.com/anytypeio/any-sync/net/secureservice"
"github.com/libp2p/go-libp2p/core/sec" "github.com/libp2p/go-libp2p/core/sec"
"github.com/prometheus/client_golang/prometheus"
"net" "net"
"storj.io/drpc" "storj.io/drpc"
"time" "time"
@ -46,29 +45,12 @@ func (s *drpcServer) Name() (name string) {
} }
func (s *drpcServer) Run(ctx context.Context) (err error) { func (s *drpcServer) Run(ctx context.Context) (err error) {
histVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "drpc",
Subsystem: "server",
Name: "duration_seconds",
Objectives: map[float64]float64{
0.5: 0.5,
0.85: 0.01,
0.95: 0.0005,
0.99: 0.0001,
},
}, []string{"rpc"})
if err = s.metric.Registry().Register(histVec); err != nil {
return
}
params := Params{ params := Params{
BufferSizeMb: s.config.Stream.MaxMsgSizeMb, BufferSizeMb: s.config.Stream.MaxMsgSizeMb,
TimeoutMillis: s.config.Stream.TimeoutMilliseconds, TimeoutMillis: s.config.Stream.TimeoutMilliseconds,
ListenAddrs: s.config.Server.ListenAddrs, ListenAddrs: s.config.Server.ListenAddrs,
Wrapper: func(handler drpc.Handler) drpc.Handler { Wrapper: func(handler drpc.Handler) drpc.Handler {
return &metric.PrometheusDRPC{ return s.metric.WrapDRPCHandler(handler)
Handler: handler,
SummaryVec: histVec,
}
}, },
Handshake: func(conn net.Conn) (cCtx context.Context, sc sec.SecureConn, err error) { Handshake: func(conn net.Conn) (cCtx context.Context, sc sec.SecureConn, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)