diff --git a/p2p/protocol/circuitv2/relay/metrics.go b/p2p/protocol/circuitv2/relay/metrics.go index 63961b17e5..9b9ada7aca 100644 --- a/p2p/protocol/circuitv2/relay/metrics.go +++ b/p2p/protocol/circuitv2/relay/metrics.go @@ -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) } } diff --git a/p2p/protocol/circuitv2/relay/metrics_test.go b/p2p/protocol/circuitv2/relay/metrics_test.go new file mode 100644 index 0000000000..2c9de6c415 --- /dev/null +++ b/p2p/protocol/circuitv2/relay/metrics_test.go @@ -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) + } + } +}