|
| 1 | +package fixtures |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gavv/httpexpect/v2" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// MetricBaseline stores baseline metric values for comparison |
| 14 | +type MetricBaseline struct { |
| 15 | + t *testing.T |
| 16 | + httpClient func() *httpexpect.Expect |
| 17 | + baselines map[string]float64 |
| 18 | + expectedIncreases map[string]float64 |
| 19 | +} |
| 20 | + |
| 21 | +// NewMetricBaseline creates a new metric baseline tracker |
| 22 | +func NewMetricBaseline(t *testing.T, httpClient func() *httpexpect.Expect) *MetricBaseline { |
| 23 | + return &MetricBaseline{ |
| 24 | + t: t, |
| 25 | + httpClient: httpClient, |
| 26 | + baselines: make(map[string]float64), |
| 27 | + expectedIncreases: make(map[string]float64), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// getCurrentMetricValues fetches current values for the specified metrics |
| 32 | +func (mb *MetricBaseline) getCurrentMetricValues(metricsMap map[string]float64) map[string]float64 { |
| 33 | + mb.t.Helper() |
| 34 | + |
| 35 | + body := mb.httpClient().GET(""). |
| 36 | + Expect(). |
| 37 | + Status(200). |
| 38 | + Body(). |
| 39 | + Raw() |
| 40 | + |
| 41 | + values := make(map[string]float64) |
| 42 | + for metric := range metricsMap { |
| 43 | + values[metric] = parseMetricValue(body, metric) |
| 44 | + } |
| 45 | + |
| 46 | + return values |
| 47 | +} |
| 48 | + |
| 49 | +// CaptureBaseline captures baselines for all metrics in the expected increases map |
| 50 | +// and stores the expected increases for later use with ExpectIncrease() |
| 51 | +func (mb *MetricBaseline) CaptureBaseline(expectedIncreases map[string]float64) *MetricBaseline { |
| 52 | + mb.t.Helper() |
| 53 | + |
| 54 | + // Capture current metric values and store them as baselines |
| 55 | + currentValues := mb.getCurrentMetricValues(expectedIncreases) |
| 56 | + |
| 57 | + for metric := range expectedIncreases { |
| 58 | + value := currentValues[metric] |
| 59 | + mb.baselines[metric] = value |
| 60 | + mb.t.Logf("Baseline for %s: %f", metric, value) |
| 61 | + } |
| 62 | + |
| 63 | + // Store the expected increases for later use |
| 64 | + mb.expectedIncreases = expectedIncreases |
| 65 | + |
| 66 | + return mb |
| 67 | +} |
| 68 | + |
| 69 | +// ExpectIncrease checks that the metrics have increased by the amounts |
| 70 | +// specified in the map passed to CaptureBaseline() |
| 71 | +func (mb *MetricBaseline) ExpectIncrease() { |
| 72 | + mb.t.Helper() |
| 73 | + |
| 74 | + if len(mb.expectedIncreases) == 0 { |
| 75 | + mb.t.Fatal("No expected increases stored. Call CaptureBaseline() first.") |
| 76 | + } |
| 77 | + |
| 78 | + currentValues := mb.getCurrentMetricValues(mb.expectedIncreases) |
| 79 | + |
| 80 | + for metric, expectedIncrease := range mb.expectedIncreases { |
| 81 | + baseline := mb.baselines[metric] // defaults to 0 if not found |
| 82 | + currentValue := currentValues[metric] |
| 83 | + actualIncrease := currentValue - baseline |
| 84 | + |
| 85 | + mb.t.Logf("Metric %s: baseline=%f, current=%f, expected_increase=%f, actual_increase=%f", |
| 86 | + metric, baseline, currentValue, expectedIncrease, actualIncrease) |
| 87 | + |
| 88 | + assert.InDelta(mb.t, expectedIncrease, actualIncrease, 0.001, |
| 89 | + "Expected %s to increase by %f, but it increased by %f (from %f to %f)", metric, expectedIncrease, actualIncrease, baseline, currentValue) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// parseMetricValue extracts the numeric value from a prometheus metric line |
| 94 | +// Returns 0 if the metric is not found |
| 95 | +func parseMetricValue(body, metricPattern string) float64 { |
| 96 | + // Escape special regex characters in the metric pattern, but keep the spaces |
| 97 | + // We'll look for lines that match the pattern and extract the value |
| 98 | + lines := strings.Split(body, "\n") |
| 99 | + |
| 100 | + for _, line := range lines { |
| 101 | + line = strings.TrimSpace(line) |
| 102 | + if line == "" || strings.HasPrefix(line, "#") { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + // Check if this line matches our metric pattern |
| 107 | + if strings.Contains(line, metricPattern) { |
| 108 | + // Extract the value using regex |
| 109 | + // Prometheus format: metric_name{labels} value |
| 110 | + re := regexp.MustCompile(`\s+([0-9.]+)$`) |
| 111 | + matches := re.FindStringSubmatch(line) |
| 112 | + if len(matches) > 1 { |
| 113 | + value, err := strconv.ParseFloat(matches[1], 64) |
| 114 | + if err == nil { |
| 115 | + return value |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return 0 // Default to 0 if metric not found |
| 122 | +} |
0 commit comments