Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: add metrics for BGP peer state and number of advertised routes #2806

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions internal/pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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...,
)
}
})
Expand Down
Loading