diff --git a/internal/pkg/metrics/metrics.go b/internal/pkg/metrics/metrics.go index 538f17a89..efcfbb07f 100644 --- a/internal/pkg/metrics/metrics.go +++ b/internal/pkg/metrics/metrics.go @@ -15,8 +15,9 @@ type bgpCollector struct { } var ( - peerLabels = []string{"peer"} - rfLabels = []string{"peer", "route_family"} + peerLabels = []string{"peer"} + peerStateLabels = []string{"peer", "session_state", "admin_state"} + rfLabels = []string{"peer", "route_family"} bgpReceivedUpdateTotalDesc = prometheus.NewDesc("bgp_received_update_total", "Number of received BGP UPDATE messages from peer", peerLabels, nil) bgpReceivedNotificationTotalDesc = prometheus.NewDesc("bgp_received_notification_total", "Number of received BGP NOTIFICATION messages from peer", peerLabels, nil) @@ -38,6 +39,8 @@ var ( bgpSentDiscardedTotalDesc = prometheus.NewDesc("bgp_sent_discarded_total", "Number of discarded BGP messages from peer", peerLabels, nil) bgpSentMessageTotalDesc = prometheus.NewDesc("bgp_sent_message_total", "Number of sent BGP messages from peer", peerLabels, nil) + bgpPeerStateDesc = prometheus.NewDesc("bgp_peer_state", "State of the BGP session with peer", peerStateLabels, nil) + bgpRoutesReceivedDesc = prometheus.NewDesc( "bgp_routes_received", "Number of routes received from peer", @@ -48,6 +51,11 @@ var ( "Number of routes accepted from peer", rfLabels, nil, ) + bgpRoutesAdvertisedDesc = prometheus.NewDesc( + "bgp_routes_advertised", + "Number of routes advertised to peer", + rfLabels, nil, + ) ) func NewBgpCollector(server *server.BgpServer) prometheus.Collector { @@ -75,15 +83,20 @@ func (c *bgpCollector) Describe(out chan<- *prometheus.Desc) { out <- bgpSentDiscardedTotalDesc out <- bgpSentMessageTotalDesc + out <- bgpPeerStateDesc + out <- bgpRoutesReceivedDesc out <- bgpRoutesAcceptedDesc + out <- bgpRoutesAdvertisedDesc } func (c *bgpCollector) Collect(out chan<- prometheus.Metric) { - req := &api.ListPeerRequest{EnableAdvertised: false} + req := &api.ListPeerRequest{EnableAdvertised: true} err := c.server.ListPeer(context.Background(), req, func(p *api.Peer) { - peerAddr := p.GetConf().GetNeighborAddress() - msg := p.GetState().GetMessages() + peerState := p.GetState() + peerAddr := peerState.GetNeighborAddress() + msg := peerState.GetMessages() + send := func(desc *prometheus.Desc, cnt uint64) { out <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(cnt), peerAddr) } @@ -108,25 +121,42 @@ func (c *bgpCollector) Collect(out chan<- prometheus.Metric) { send(bgpSentDiscardedTotalDesc, msg.Sent.Discarded) send(bgpSentMessageTotalDesc, msg.Sent.Total) + out <- prometheus.MustNewConstMetric( + bgpPeerStateDesc, + prometheus.GaugeValue, + 1.0, + peerAddr, + peerState.GetSessionState().String(), + peerState.GetAdminState().String(), + ) + for _, afiSafi := range p.GetAfiSafis() { if !afiSafi.GetConfig().GetEnabled() { continue } + afiState := afiSafi.GetState() family := bgp.AfiSafiToRouteFamily( - uint16(afiSafi.GetState().GetFamily().GetAfi()), - uint8(afiSafi.GetState().GetFamily().GetSafi()), + uint16(afiState.GetFamily().GetAfi()), + uint8(afiState.GetFamily().GetSafi()), ).String() + labelValues := []string{peerAddr, family} out <- prometheus.MustNewConstMetric( bgpRoutesReceivedDesc, prometheus.GaugeValue, - float64(afiSafi.GetState().GetReceived()), - peerAddr, family, + float64(afiState.GetReceived()), + labelValues..., ) out <- prometheus.MustNewConstMetric( bgpRoutesAcceptedDesc, prometheus.GaugeValue, - float64(afiSafi.GetState().GetAccepted()), - peerAddr, family, + float64(afiState.GetAccepted()), + labelValues..., + ) + out <- prometheus.MustNewConstMetric( + bgpRoutesAdvertisedDesc, + prometheus.GaugeValue, + float64(afiState.GetAdvertised()), + labelValues..., ) } })