github.com/osrg/gobgp/v3@v3.30.0/internal/pkg/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  
     8  	api "github.com/osrg/gobgp/v3/api"
     9  	"github.com/osrg/gobgp/v3/pkg/packet/bgp"
    10  	"github.com/osrg/gobgp/v3/pkg/server"
    11  )
    12  
    13  type bgpCollector struct {
    14  	server *server.BgpServer
    15  }
    16  
    17  var (
    18  	peerLabels      = []string{"peer"}
    19  	peerStateLabels = []string{"peer", "session_state", "admin_state"}
    20  	rfLabels        = []string{"peer", "route_family"}
    21  
    22  	bgpReceivedUpdateTotalDesc         = prometheus.NewDesc("bgp_received_update_total", "Number of received BGP UPDATE messages from peer", peerLabels, nil)
    23  	bgpReceivedNotificationTotalDesc   = prometheus.NewDesc("bgp_received_notification_total", "Number of received BGP NOTIFICATION messages from peer", peerLabels, nil)
    24  	bgpReceivedOpenTotalDesc           = prometheus.NewDesc("bgp_received_open_total", "Number of received BGP OPEN messages from peer", peerLabels, nil)
    25  	bgpReceivedRefreshTotalDesc        = prometheus.NewDesc("bgp_received_refresh_total", "Number of received BGP REFRESH messages from peer", peerLabels, nil)
    26  	bgpReceivedKeepaliveTotalDesc      = prometheus.NewDesc("bgp_received_keepalive_total", "Number of received BGP KEEPALIVE messages from peer", peerLabels, nil)
    27  	bgpReceivedWithdrawUpdateTotalDesc = prometheus.NewDesc("bgp_received_withdraw_update_total", "Number of received BGP WITHDRAW-UPDATE messages from peer", peerLabels, nil)
    28  	bgpReceivedWithdrawPrefixTotalDesc = prometheus.NewDesc("bgp_received_withdraw_prefix_total", "Number of received BGP WITHDRAW-PREFIX messages from peer", peerLabels, nil)
    29  	bgpReceivedDiscardedTotalDesc      = prometheus.NewDesc("bgp_received_discarded_total", "Number of discarded BGP messages from peer", peerLabels, nil)
    30  	bgpReceivedMessageTotalDesc        = prometheus.NewDesc("bgp_received_message_total", "Number of received BGP messages from peer", peerLabels, nil)
    31  
    32  	bgpSentUpdateTotalDesc         = prometheus.NewDesc("bgp_sent_update_total", "Number of sent BGP UPDATE messages from peer", peerLabels, nil)
    33  	bgpSentNotificationTotalDesc   = prometheus.NewDesc("bgp_sent_notification_total", "Number of sent BGP NOTIFICATION messages from peer", peerLabels, nil)
    34  	bgpSentOpenTotalDesc           = prometheus.NewDesc("bgp_sent_open_total", "Number of sent BGP OPEN messages from peer", peerLabels, nil)
    35  	bgpSentRefreshTotalDesc        = prometheus.NewDesc("bgp_sent_refresh_total", "Number of sent BGP REFRESH messages from peer", peerLabels, nil)
    36  	bgpSentKeepaliveTotalDesc      = prometheus.NewDesc("bgp_sent_keepalive_total", "Number of sent BGP KEEPALIVE messages from peer", peerLabels, nil)
    37  	bgpSentWithdrawUpdateTotalDesc = prometheus.NewDesc("bgp_sent_withdraw_update_total", "Number of sent BGP WITHDRAW-UPDATE messages from peer", peerLabels, nil)
    38  	bgpSentWithdrawPrefixTotalDesc = prometheus.NewDesc("bgp_sent_withdraw_prefix_total", "Number of sent BGP WITHDRAW-PREFIX messages from peer", peerLabels, nil)
    39  	bgpSentDiscardedTotalDesc      = prometheus.NewDesc("bgp_sent_discarded_total", "Number of discarded BGP messages from peer", peerLabels, nil)
    40  	bgpSentMessageTotalDesc        = prometheus.NewDesc("bgp_sent_message_total", "Number of sent BGP messages from peer", peerLabels, nil)
    41  
    42  	bgpPeerStateDesc = prometheus.NewDesc("bgp_peer_state", "State of the BGP session with peer", peerStateLabels, nil)
    43  
    44  	bgpRoutesReceivedDesc = prometheus.NewDesc(
    45  		"bgp_routes_received",
    46  		"Number of routes received from peer",
    47  		rfLabels, nil,
    48  	)
    49  	bgpRoutesAcceptedDesc = prometheus.NewDesc(
    50  		"bgp_routes_accepted",
    51  		"Number of routes accepted from peer",
    52  		rfLabels, nil,
    53  	)
    54  	bgpRoutesAdvertisedDesc = prometheus.NewDesc(
    55  		"bgp_routes_advertised",
    56  		"Number of routes advertised to peer",
    57  		rfLabels, nil,
    58  	)
    59  )
    60  
    61  func NewBgpCollector(server *server.BgpServer) prometheus.Collector {
    62  	return &bgpCollector{server: server}
    63  }
    64  
    65  func (c *bgpCollector) Describe(out chan<- *prometheus.Desc) {
    66  	out <- bgpReceivedUpdateTotalDesc
    67  	out <- bgpReceivedNotificationTotalDesc
    68  	out <- bgpReceivedOpenTotalDesc
    69  	out <- bgpReceivedRefreshTotalDesc
    70  	out <- bgpReceivedKeepaliveTotalDesc
    71  	out <- bgpReceivedWithdrawUpdateTotalDesc
    72  	out <- bgpReceivedWithdrawPrefixTotalDesc
    73  	out <- bgpReceivedDiscardedTotalDesc
    74  	out <- bgpReceivedMessageTotalDesc
    75  
    76  	out <- bgpSentUpdateTotalDesc
    77  	out <- bgpSentNotificationTotalDesc
    78  	out <- bgpSentOpenTotalDesc
    79  	out <- bgpSentRefreshTotalDesc
    80  	out <- bgpSentKeepaliveTotalDesc
    81  	out <- bgpSentWithdrawUpdateTotalDesc
    82  	out <- bgpSentWithdrawPrefixTotalDesc
    83  	out <- bgpSentDiscardedTotalDesc
    84  	out <- bgpSentMessageTotalDesc
    85  
    86  	out <- bgpPeerStateDesc
    87  
    88  	out <- bgpRoutesReceivedDesc
    89  	out <- bgpRoutesAcceptedDesc
    90  	out <- bgpRoutesAdvertisedDesc
    91  }
    92  
    93  func (c *bgpCollector) Collect(out chan<- prometheus.Metric) {
    94  	req := &api.ListPeerRequest{EnableAdvertised: true}
    95  	err := c.server.ListPeer(context.Background(), req, func(p *api.Peer) {
    96  		peerState := p.GetState()
    97  		peerAddr := peerState.GetNeighborAddress()
    98  		msg := peerState.GetMessages()
    99  
   100  		send := func(desc *prometheus.Desc, cnt uint64) {
   101  			out <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(cnt), peerAddr)
   102  		}
   103  
   104  		send(bgpReceivedUpdateTotalDesc, msg.Received.Update)
   105  		send(bgpReceivedNotificationTotalDesc, msg.Received.Notification)
   106  		send(bgpReceivedOpenTotalDesc, msg.Received.Open)
   107  		send(bgpReceivedRefreshTotalDesc, msg.Received.Refresh)
   108  		send(bgpReceivedKeepaliveTotalDesc, msg.Received.Keepalive)
   109  		send(bgpReceivedWithdrawUpdateTotalDesc, uint64(msg.Received.WithdrawUpdate))
   110  		send(bgpReceivedWithdrawPrefixTotalDesc, uint64(msg.Received.WithdrawPrefix))
   111  		send(bgpReceivedDiscardedTotalDesc, msg.Received.Discarded)
   112  		send(bgpReceivedMessageTotalDesc, msg.Received.Total)
   113  
   114  		send(bgpSentUpdateTotalDesc, msg.Sent.Update)
   115  		send(bgpSentNotificationTotalDesc, msg.Sent.Notification)
   116  		send(bgpSentOpenTotalDesc, msg.Sent.Open)
   117  		send(bgpSentRefreshTotalDesc, msg.Sent.Refresh)
   118  		send(bgpSentKeepaliveTotalDesc, msg.Sent.Keepalive)
   119  		send(bgpSentWithdrawUpdateTotalDesc, uint64(msg.Sent.WithdrawUpdate))
   120  		send(bgpSentWithdrawPrefixTotalDesc, uint64(msg.Sent.WithdrawPrefix))
   121  		send(bgpSentDiscardedTotalDesc, msg.Sent.Discarded)
   122  		send(bgpSentMessageTotalDesc, msg.Sent.Total)
   123  
   124  		out <- prometheus.MustNewConstMetric(
   125  			bgpPeerStateDesc,
   126  			prometheus.GaugeValue,
   127  			1.0,
   128  			peerAddr,
   129  			peerState.GetSessionState().String(),
   130  			peerState.GetAdminState().String(),
   131  		)
   132  
   133  		for _, afiSafi := range p.GetAfiSafis() {
   134  			if !afiSafi.GetConfig().GetEnabled() {
   135  				continue
   136  			}
   137  			afiState := afiSafi.GetState()
   138  			family := bgp.AfiSafiToRouteFamily(
   139  				uint16(afiState.GetFamily().GetAfi()),
   140  				uint8(afiState.GetFamily().GetSafi()),
   141  			).String()
   142  			labelValues := []string{peerAddr, family}
   143  			out <- prometheus.MustNewConstMetric(
   144  				bgpRoutesReceivedDesc,
   145  				prometheus.GaugeValue,
   146  				float64(afiState.GetReceived()),
   147  				labelValues...,
   148  			)
   149  			out <- prometheus.MustNewConstMetric(
   150  				bgpRoutesAcceptedDesc,
   151  				prometheus.GaugeValue,
   152  				float64(afiState.GetAccepted()),
   153  				labelValues...,
   154  			)
   155  			out <- prometheus.MustNewConstMetric(
   156  				bgpRoutesAdvertisedDesc,
   157  				prometheus.GaugeValue,
   158  				float64(afiState.GetAdvertised()),
   159  				labelValues...,
   160  			)
   161  		}
   162  	})
   163  	if err != nil {
   164  		out <- prometheus.NewInvalidMetric(prometheus.NewDesc("error", "error during metric collection", nil, nil), err)
   165  	}
   166  }