Skip to content
Snippets Groups Projects
Commit 40c1db6c authored by Oliver Herms's avatar Oliver Herms
Browse files

Refactor to use prometheus GaugeVector

parent e9d57678
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,6 @@ func main() { ...@@ -46,7 +46,6 @@ func main() {
} }
s := risserver.NewServer(b) s := risserver.NewServer(b)
prometheus.MustRegister(s)
unaryInterceptors := []grpc.UnaryServerInterceptor{} unaryInterceptors := []grpc.UnaryServerInterceptor{}
streamInterceptors := []grpc.StreamServerInterceptor{} streamInterceptors := []grpc.StreamServerInterceptor{}
srv, err := servicewrapper.New( srv, err := servicewrapper.New(
......
...@@ -3,7 +3,6 @@ package risserver ...@@ -3,7 +3,6 @@ package risserver
import ( import (
"context" "context"
"fmt" "fmt"
"sync"
"github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/server" "github.com/bio-routing/bio-rd/protocols/bgp/server"
...@@ -20,88 +19,36 @@ import ( ...@@ -20,88 +19,36 @@ import (
routeapi "github.com/bio-routing/bio-rd/route/api" routeapi "github.com/bio-routing/bio-rd/route/api"
) )
const (
prefix = "bio_ris_"
)
var ( var (
risObserveFIBClientsDesc *prometheus.Desc risObserveFIBClients *prometheus.GaugeVec
) )
func init() { func init() {
labels := []string{"router", "vrf", "afisafi"} risObserveFIBClients = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
risObserveFIBClientsDesc = prometheus.NewDesc(prefix+"observe_fib_clients", "number of observe FIB clients per router/vrf/afisafi", labels, nil) Namespace: "bio",
Subsystem: "ris",
Name: "observe_fib_clients",
Help: "number of observe FIB clients per router/vrf/afisafi",
},
[]string{
"router",
"vrf",
"afisafi",
},
)
prometheus.MustRegister(risObserveFIBClients)
} }
// Server represents an RoutingInformationService server // Server represents an RoutingInformationService server
type Server struct { type Server struct {
bmp *server.BMPServer bmp *server.BMPServer
observeClients map[observeRIBRequest]uint64
observeClientsMu sync.Mutex
}
type observeRIBRequest struct {
router string
vrfID uint64
afisafi pb.ObserveRIBRequest_AFISAFI
} }
// NewServer creates a new server // NewServer creates a new server
func NewServer(b *server.BMPServer) *Server { func NewServer(b *server.BMPServer) *Server {
return &Server{ return &Server{
bmp: b, bmp: b,
observeClients: make(map[observeRIBRequest]uint64),
}
}
func convertRIBRequest(r *pb.ObserveRIBRequest) observeRIBRequest {
return observeRIBRequest{
router: r.Router,
vrfID: r.VrfId,
afisafi: r.Afisafi,
}
}
func (s *Server) addObserveRIBRequest(req *pb.ObserveRIBRequest) {
r := convertRIBRequest(req)
s.observeClientsMu.Lock()
defer s.observeClientsMu.Unlock()
if _, found := s.observeClients[r]; found {
s.observeClients[r]++
return
}
s.observeClients[r] = 1
}
func (s *Server) delObserveRIBRequest(req *pb.ObserveRIBRequest) {
r := convertRIBRequest(req)
s.observeClientsMu.Lock()
defer s.observeClientsMu.Unlock()
if _, found := s.observeClients[r]; found {
s.observeClients[r]--
return
}
}
// Describe describes metrics of this server
func (s *Server) Describe(ch chan<- *prometheus.Desc) {
ch <- risObserveFIBClientsDesc
}
// Collect collects metrics of this server
func (s *Server) Collect(ch chan<- prometheus.Metric) {
s.observeClientsMu.Lock()
defer s.observeClientsMu.Unlock()
for k, v := range s.observeClients {
l := []string{k.router, fmt.Sprintf("%d", k.vrfID), fmt.Sprintf("%d", k.afisafi)}
ch <- prometheus.MustNewConstMetric(risObserveFIBClientsDesc, prometheus.GaugeValue, float64(v), l...)
} }
} }
...@@ -207,8 +154,8 @@ func (s *Server) ObserveRIB(req *pb.ObserveRIBRequest, stream pb.RoutingInformat ...@@ -207,8 +154,8 @@ func (s *Server) ObserveRIB(req *pb.ObserveRIBRequest, stream pb.RoutingInformat
return err return err
} }
s.addObserveRIBRequest(req) risObserveFIBClients.WithLabelValues(req.Router, fmt.Sprintf("%d", req.VrfId), fmt.Sprintf("%d", req.Afisafi)).Inc()
defer s.delObserveRIBRequest(req) defer risObserveFIBClients.WithLabelValues(req.Router, fmt.Sprintf("%d", req.VrfId), fmt.Sprintf("%d", req.Afisafi)).Dec()
fifo := newUpdateFIFO() fifo := newUpdateFIFO()
rc := newRIBClient(fifo) rc := newRIBClient(fifo)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment