1
0
mirror of https://github.com/bgp/stayrtr.git synced 2024-05-06 15:54:54 +00:00

Track objects that differ for a longer period separately

* Track when the VRP was first seen for each VRP
  * Add a metric which tracks how many objects that were first seen
    [duration] ago are not present in the other endpoint.
    * Use current naming convention for the metric to be consistent (i.e. no
      prefix with application name).
This commit is contained in:
Ties de Kock
2021-06-21 15:53:50 +02:00
committed by Job Snijders
parent 36d242c3ac
commit a29da795ac

View File

@ -13,6 +13,7 @@ import (
"net/url"
"os"
"runtime"
"strconv"
"sync"
"time"
@ -90,6 +91,13 @@ var (
},
[]string{"server", "url", "type"},
)
VRPDifferenceForDuration = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "vrp_diff",
Help: "Number of VRPS in [lhs_url] that are not in [rhs_url] that were first seen [visibility_seconds] ago in lhs.",
},
[]string{"lhs_url", "rhs_url", "visibility_seconds"},
)
RTRState = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "rtr_state",
@ -124,10 +132,13 @@ var (
1: "primary",
2: "secondary",
}
VisibilityThresholds = []int64{0, 56, 256, 596, 851, 1024, 1706, 3411}
)
func init() {
prometheus.MustRegister(VRPCount)
prometheus.MustRegister(VRPDifferenceForDuration)
prometheus.MustRegister(RTRState)
prometheus.MustRegister(RTRSerial)
prometheus.MustRegister(RTRSession)
@ -279,6 +290,7 @@ func (c *Client) Start(id int, ch chan int) {
}
c.lastUpdate = time.Now().UTC()
tCurrentUpdate := time.Now().UTC().Unix()
tmpVrpMap := make(map[string]*VRPJsonSimple)
for _, vrp := range decoded.Data {
@ -296,10 +308,17 @@ func (c *Client) Start(id int, ch chan int) {
maxlen := vrp.GetMaxLen()
key := fmt.Sprintf("%s-%d-%d", prefix.String(), maxlen, asn)
firstSeen := tCurrentUpdate
currentEntry, ok := c.vrps[key]
if ok {
firstSeen = currentEntry.FirstSeen
}
vrpSimple := VRPJsonSimple{
Prefix: prefix.String(),
ASN: asn,
Length: uint8(maxlen),
Prefix: prefix.String(),
ASN: asn,
Length: uint8(maxlen),
FirstSeen: firstSeen,
}
tmpVrpMap[key] = &vrpSimple
}
@ -320,9 +339,10 @@ func (c *Client) HandlePDU(cs *rtr.ClientSession, pdu rtr.PDU) {
switch pdu := pdu.(type) {
case *rtr.PDUIPv4Prefix:
vrp := VRPJsonSimple{
Prefix: pdu.Prefix.String(),
ASN: pdu.ASN,
Length: pdu.MaxLen,
Prefix: pdu.Prefix.String(),
ASN: pdu.ASN,
Length: pdu.MaxLen,
FirstSeen: time.Now().Unix(),
}
key := fmt.Sprintf("%s-%d-%d", pdu.Prefix.String(), pdu.MaxLen, pdu.ASN)
@ -337,9 +357,10 @@ func (c *Client) HandlePDU(cs *rtr.ClientSession, pdu rtr.PDU) {
c.compRtrLock.Unlock()
case *rtr.PDUIPv6Prefix:
vrp := VRPJsonSimple{
Prefix: pdu.Prefix.String(),
ASN: pdu.ASN,
Length: pdu.MaxLen,
Prefix: pdu.Prefix.String(),
ASN: pdu.ASN,
Length: pdu.MaxLen,
FirstSeen: time.Now().Unix(),
}
key := fmt.Sprintf("%s-%d-%d", pdu.Prefix.String(), pdu.MaxLen, pdu.ASN)
@ -486,6 +507,18 @@ func NewComparator(c1, c2 *Client) *Comparator {
}
}
func countFirstSeenOnOrBefore(vrps []*VRPJsonSimple, thresholdTimestamp int64) float64 {
count := 0
for _, vrp := range vrps {
if vrp.FirstSeen <= thresholdTimestamp {
count++
}
}
return float64(count)
}
func Diff(a, b map[string]*VRPJsonSimple) []*VRPJsonSimple {
onlyInA := make([]*VRPJsonSimple, 0)
for key, vrp := range a {
@ -509,9 +542,10 @@ type diffMetadata struct {
}
type VRPJsonSimple struct {
ASN uint32 `json:"asn"`
Length uint8 `json:"max-length"`
Prefix string `json:"prefix"`
ASN uint32 `json:"asn"`
Length uint8 `json:"max-length"`
Prefix string `json:"prefix"`
FirstSeen int64 `json:"first-seen"`
}
type diffExport struct {
@ -546,6 +580,7 @@ func (c *Comparator) ServeDiff(wr http.ResponseWriter, req *http.Request) {
func (c *Comparator) Compare() {
var donePrimary, doneSecondary bool
var stop bool
startedAt := time.Now().Unix()
for !stop {
select {
case <-c.q:
@ -595,6 +630,27 @@ func (c *Comparator) Compare() {
"type": "diff",
}).Set(float64(len(onlyIn2)))
for _, visibleFor := range VisibilityThresholds {
thresholdTimestamp := time.Now().Unix() - visibleFor
// Prevent differences with value 0 appearing if the process has not
// been running long enough for them to exist.
if thresholdTimestamp >= startedAt {
VRPDifferenceForDuration.With(
prometheus.Labels{
"lhs_url": md1.URL,
"rhs_url": md2.URL,
"visibility_seconds": strconv.FormatInt(visibleFor, 10),
}).Set(countFirstSeenOnOrBefore(onlyIn1, thresholdTimestamp))
VRPDifferenceForDuration.With(
prometheus.Labels{
"lhs_url": md2.URL,
"rhs_url": md1.URL,
"visibility_seconds": strconv.FormatInt(visibleFor, 10),
}).Set(countFirstSeenOnOrBefore(onlyIn2, thresholdTimestamp))
}
}
RTRSerial.With(
prometheus.Labels{
"server": "primary",