Skip to content

Commit

Permalink
Add useful labels to new metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellrj committed Aug 12, 2018
1 parent 5c25f8f commit b67393f
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ const (

func NewExporter() *Exporter {
var (
alias = "alias"
server = "server"
hop_id = "hop_id"
hop_ip = "hop_ip"
alias = "alias"
server = "server"
hop_id = "hop_id"
hop_ip = "hop_ip"
previousDest = "previous"
currentDest = "current"
)

return &Exporter{
Expand Down Expand Up @@ -118,15 +120,15 @@ func NewExporter() *Exporter {
Name: "route_changes",
Help: "route changes",
},
[]string{alias, server},
[]string{alias, server, hop_id},
),
destinationChanges: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Name: "destination_changes",
Help: "Number of times the destination IP has changed",
},
[]string{alias, server},
[]string{alias, server, previousDest, currentDest},
),
failed: prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand All @@ -152,6 +154,13 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.failed.Describe(ch)
}

func min(a int, b int) int {
if a > b {
return b
}
return a
}

func (e *Exporter) collect() error {
for {
results := make(chan *TargetFeedback)
Expand Down Expand Up @@ -187,12 +196,14 @@ func (e *Exporter) collect() error {
e.lost.WithLabelValues(tf.Alias, tf.Target, strconv.Itoa(host.Hop), host.IP.String()).Add(host.LostPercent * float64(host.Sent))
e.latency.WithLabelValues(tf.Alias, tf.Target, strconv.Itoa(host.Hop), host.IP.String()).Observe(host.Mean)
}
if !reflect.DeepEqual(route, e.lastRoute[tf.Alias]) {
e.routeChanges.WithLabelValues(tf.Alias, tf.Target).Inc()
e.lastRoute[tf.Alias] = route
for i := 0; i < min(len(route), len(e.lastRoute)); i++ {
if !reflect.DeepEqual(route[i], e.lastRoute[tf.Alias][i]) {
e.routeChanges.WithLabelValues(tf.Alias, tf.Target, fmt.Sprintf("%d", i)).Inc()
e.lastRoute[tf.Alias] = route
}
}
if !reflect.DeepEqual(destination, e.lastDest[tf.Alias]) {
e.destinationChanges.WithLabelValues(tf.Alias, tf.Target).Inc()
e.destinationChanges.WithLabelValues(tf.Alias, tf.Target, e.lastDest[tf.Alias].String(), destination.String()).Inc()
e.lastDest[tf.Alias] = destination
}
}
Expand Down

0 comments on commit b67393f

Please sign in to comment.