Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select metric by label in e2e tests #3073

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tests/e2e/x/transfer/virtuous.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = e2e.DescribeXChainSerial("[Virtuous Transfer Tx AVAX]", func() {
require.NoError(err)

for _, metrics := range allNodeMetrics {
xBlksProcessing, ok := tests.GetFirstMetricValue(metrics, xBlksProcessingMetric)
xBlksProcessing, ok := tests.GetMetricValue(metrics, xBlksProcessingMetric, nil)
if !ok || xBlksProcessing > 0 {
return false
}
Expand Down Expand Up @@ -248,13 +248,13 @@ RECEIVER NEW BALANCE (AFTER) : %21d AVAX

// +0 since X-chain tx must have been processed and accepted
// by now
currentXBlksProcessing, _ := tests.GetFirstMetricValue(mm, xBlksProcessingMetric)
previousXBlksProcessing, _ := tests.GetFirstMetricValue(prev, xBlksProcessingMetric)
currentXBlksProcessing, _ := tests.GetMetricValue(mm, xBlksProcessingMetric, nil)
previousXBlksProcessing, _ := tests.GetMetricValue(prev, xBlksProcessingMetric, nil)
require.Equal(currentXBlksProcessing, previousXBlksProcessing)

// +1 since X-chain tx must have been accepted by now
currentXBlksAccepted, _ := tests.GetFirstMetricValue(mm, xBlksAcceptedMetric)
previousXBlksAccepted, _ := tests.GetFirstMetricValue(prev, xBlksAcceptedMetric)
currentXBlksAccepted, _ := tests.GetMetricValue(mm, xBlksAcceptedMetric, nil)
previousXBlksAccepted, _ := tests.GetMetricValue(prev, xBlksAcceptedMetric, nil)
require.Equal(currentXBlksAccepted, previousXBlksAccepted+1)

metricsBeforeTx[u] = mm
Expand Down
48 changes: 38 additions & 10 deletions tests/http.go → tests/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/avalanchego/api/metrics"

dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -37,19 +39,45 @@ func GetNodesMetrics(ctx context.Context, nodeURIs []string) (NodesMetrics, erro
return metrics, nil
}

func GetFirstMetricValue(metrics NodeMetrics, name string) (float64, bool) {
// GetMetricValue returns the value of the specified metric which has the
// required labels.
//
// If multiple metrics match the provided labels, the first metric found is
// returned.
//
// Only Counter and Gauge metrics are supported.
func GetMetricValue(metrics NodeMetrics, name string, labels prometheus.Labels) (float64, bool) {
metricFamily, ok := metrics[name]
if !ok || len(metricFamily.Metric) < 1 {
if !ok {
return 0, false
}

metric := metricFamily.Metric[0]
switch {
case metric.Gauge != nil:
return metric.Gauge.GetValue(), true
case metric.Counter != nil:
return metric.Counter.GetValue(), true
default:
return 0, false
for _, metric := range metricFamily.Metric {
if !labelsMatch(metric, labels) {
continue
}

switch {
case metric.Gauge != nil:
return metric.Gauge.GetValue(), true
case metric.Counter != nil:
return metric.Counter.GetValue(), true
}
}
return 0, false
}

func labelsMatch(metric *dto.Metric, labels prometheus.Labels) bool {
var found int
for _, label := range metric.Label {
expectedValue, ok := labels[label.GetName()]
if !ok {
continue
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved
}
if label.GetValue() != expectedValue {
return false
}
found++
}
return found == len(labels)
}
Loading