Skip to content

Commit

Permalink
add metrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Mar 3, 2023
1 parent e1fcfcb commit d0e0743
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
56 changes: 46 additions & 10 deletions p2p/protocol/circuitv2/relay/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,41 +183,77 @@ func (mt *metricsTracer) RelayStatus(enabled bool) {
}

func (mt *metricsTracer) ConnectionRequestReceived() {
connectionTotal.WithLabelValues(typeReceived).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, typeReceived)

connectionTotal.WithLabelValues(*tags...).Add(1)
}

func (mt *metricsTracer) ConnectionOpened() {
connectionTotal.WithLabelValues(typeOpened).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, typeOpened)

connectionTotal.WithLabelValues(*tags...).Add(1)
}

func (mt *metricsTracer) ConnectionClosed(d time.Duration) {
connectionTotal.WithLabelValues(typeClosed).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, typeClosed)

connectionTotal.WithLabelValues(*tags...).Add(1)
connectionDurationSeconds.Observe(d.Seconds())
}

func (mt *metricsTracer) ConnectionRequestHandled(status string, rejectionReason string) {
connectionRequestStatusTotal.WithLabelValues(status).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, status)

connectionRequestStatusTotal.WithLabelValues(*tags...).Add(1)
if status == requestStatusRejected {
connectionRejectionTotal.WithLabelValues(rejectionReason).Add(1)
*tags = (*tags)[:0]
*tags = append(*tags, rejectionReason)
connectionRejectionTotal.WithLabelValues(*tags...).Add(1)
}
}

func (mt *metricsTracer) ReservationRequestReceived() {
reservationTotal.WithLabelValues(typeReceived).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, typeReceived)

reservationTotal.WithLabelValues(*tags...).Add(1)
}

func (mt *metricsTracer) ReservationOpened() {
reservationTotal.WithLabelValues(typeOpened).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, typeOpened)

reservationTotal.WithLabelValues(*tags...).Add(1)
}

func (mt *metricsTracer) ReservationClosed(cnt int) {
reservationTotal.WithLabelValues(typeClosed).Add(float64(cnt))
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, typeClosed)

reservationTotal.WithLabelValues(*tags...).Add(float64(cnt))
}

func (mt *metricsTracer) ReservationRequestHandled(status string, rejectionReason string) {
reservationRequestStatusTotal.WithLabelValues(status).Add(1)
tags := metricshelper.GetStringSlice()
defer metricshelper.PutStringSlice(tags)
*tags = append(*tags, status)

reservationRequestStatusTotal.WithLabelValues(*tags...).Add(1)
if status == requestStatusRejected {
reservationRejectedTotal.WithLabelValues(rejectionReason).Add(1)
*tags = (*tags)[:0]
*tags = append(*tags, rejectionReason)
reservationRejectedTotal.WithLabelValues(*tags...).Add(1)
}
}

Expand Down
37 changes: 37 additions & 0 deletions p2p/protocol/circuitv2/relay/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build nocover

package relay

import (
"math/rand"
"testing"
"time"
)

func TestNoCoverNoAlloc(t *testing.T) {
statuses := []string{requestStatusOK, requestStatusRejected, requestStatusError}
rejectionReason := []string{"", rejectionReasonAttemptOverRelay, rejectionReasonBadRequest, rejectionReasonDisallowed}
mt := NewMetricsTracer()
tests := map[string]func(){
"RelayStatus": func() { mt.RelayStatus(rand.Intn(2) == 1) },
"ConnectionRequestReceived": func() { mt.ConnectionRequestReceived() },
"ConnectionOpened": func() { mt.ConnectionOpened() },
"ConnectionClosed": func() { mt.ConnectionClosed(time.Duration(rand.Intn(10)) * time.Second) },
"ConnectionRequestHandled": func() {
mt.ConnectionRequestHandled(statuses[rand.Intn(len(statuses))], rejectionReason[rand.Intn(len(rejectionReason))])
},
"ReservationRequestReceived": func() { mt.ReservationRequestReceived() },
"ReservationOpened": func() { mt.ReservationOpened() },
"ReservationClosed": func() { mt.ReservationClosed(rand.Intn(10)) },
"ReservationRequestHandled": func() {
mt.ReservationRequestHandled(statuses[rand.Intn(len(statuses))], rejectionReason[rand.Intn(len(rejectionReason))])
},
"BytesTransferred": func() { mt.BytesTransferred(rand.Intn(1000)) },
}
for method, f := range tests {
allocs := testing.AllocsPerRun(1000, f)
if allocs > 0 {
t.Fatalf("Alloc Test: %s, got: %0.2f, expected: 0 allocs", method, allocs)
}
}
}

0 comments on commit d0e0743

Please sign in to comment.