43 lines
1.8 KiB
Go
43 lines
1.8 KiB
Go
package metrics
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
healthDomain "proxy-pool/internal/domain/health"
|
|
)
|
|
|
|
func TestDrainCollectorRecordsOnlyFixedReasons(t *testing.T) {
|
|
registry := prometheus.NewRegistry()
|
|
collector, err := NewDrainCollector(registry)
|
|
if err != nil {
|
|
t.Fatalf("NewDrainCollector() error = %v", err)
|
|
}
|
|
collector.ObserveDrain(healthDomain.DrainReasonUnhealthy, 3, 2)
|
|
collector.ObserveDrain(healthDomain.DrainReasonUpstreamDisabled, 2, 1)
|
|
collector.ObserveDrain("invalid", 9, 9)
|
|
collector.ObserveDrain(healthDomain.DrainReasonUnhealthy, 1, 2)
|
|
|
|
assertMetricValue(t, registry, "proxy_pool_controller_drain_candidates_total", map[string]string{"reason": "unhealthy"}, 3)
|
|
assertMetricValue(t, registry, "proxy_pool_controller_drains_started_total", map[string]string{"reason": "unhealthy"}, 2)
|
|
assertMetricValue(t, registry, "proxy_pool_controller_drain_candidates_total", map[string]string{"reason": "upstream_disabled"}, 2)
|
|
assertMetricValue(t, registry, "proxy_pool_controller_drains_started_total", map[string]string{"reason": "upstream_disabled"}, 1)
|
|
}
|
|
|
|
func TestNewDrainCollectorReusesRegisteredCollectors(t *testing.T) {
|
|
registry := prometheus.NewRegistry()
|
|
first, err := NewDrainCollector(registry)
|
|
if err != nil {
|
|
t.Fatalf("first NewDrainCollector() error = %v", err)
|
|
}
|
|
second, err := NewDrainCollector(registry)
|
|
if err != nil {
|
|
t.Fatalf("second NewDrainCollector() error = %v", err)
|
|
}
|
|
first.ObserveDrain(healthDomain.DrainReasonUnhealthy, 1, 1)
|
|
second.ObserveDrain(healthDomain.DrainReasonUnhealthy, 2, 2)
|
|
assertMetricValue(t, registry, "proxy_pool_controller_drain_candidates_total", map[string]string{"reason": "unhealthy"}, 3)
|
|
assertMetricValue(t, registry, "proxy_pool_controller_drains_started_total", map[string]string{"reason": "unhealthy"}, 3)
|
|
}
|