|
7 | 7 |
|
8 | 8 | "github.com/aws/smithy-go/ptr" |
9 | 9 | "github.com/prometheus/client_golang/prometheus" |
| 10 | + prometheus_client "github.com/prometheus/client_model/go" |
10 | 11 | "github.com/stretchr/testify/require" |
11 | 12 |
|
12 | 13 | "github.com/coder/coder/v2/coderd/entitlements" |
@@ -48,16 +49,131 @@ func TestCollectLicenseMetrics(t *testing.T) { |
48 | 49 | err = json.Unmarshal(goldenFile, &golden) |
49 | 50 | require.NoError(t, err) |
50 | 51 |
|
51 | | - collected := map[string]int{} |
| 52 | + for name, expected := range golden { |
| 53 | + actual, ok := findMetric(metrics, name) |
| 54 | + require.True(t, ok, "metric %s not found", name) |
| 55 | + require.Equal(t, expected, actual, "metric %s", name) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestCollectLicenseMetrics_WarningsAndErrors(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + t.Run("NoWarningsOrErrors", func(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + |
| 65 | + registry := prometheus.NewRegistry() |
| 66 | + var sut license.MetricsCollector |
| 67 | + sut.Entitlements = entitlements.New() |
| 68 | + |
| 69 | + registry.Register(&sut) |
| 70 | + |
| 71 | + metrics, err := registry.Gather() |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + warnings, ok := findMetric(metrics, "coderd_license_warnings") |
| 75 | + require.True(t, ok) |
| 76 | + require.Zero(t, warnings) |
| 77 | + |
| 78 | + errors, ok := findMetric(metrics, "coderd_license_errors") |
| 79 | + require.True(t, ok) |
| 80 | + require.Zero(t, errors) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("WithWarnings", func(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + registry := prometheus.NewRegistry() |
| 87 | + var sut license.MetricsCollector |
| 88 | + sut.Entitlements = entitlements.New() |
| 89 | + sut.Entitlements.Modify(func(entitlements *codersdk.Entitlements) { |
| 90 | + entitlements.Warnings = []string{ |
| 91 | + "License expires in 30 days", |
| 92 | + "User limit is at 90% capacity", |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + registry.Register(&sut) |
| 97 | + |
| 98 | + metrics, err := registry.Gather() |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + warnings, ok := findMetric(metrics, "coderd_license_warnings") |
| 102 | + require.True(t, ok) |
| 103 | + require.Equal(t, 2, warnings) |
| 104 | + |
| 105 | + errors, ok := findMetric(metrics, "coderd_license_errors") |
| 106 | + require.True(t, ok) |
| 107 | + require.Zero(t, errors) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("WithErrors", func(t *testing.T) { |
| 111 | + t.Parallel() |
| 112 | + |
| 113 | + registry := prometheus.NewRegistry() |
| 114 | + var sut license.MetricsCollector |
| 115 | + sut.Entitlements = entitlements.New() |
| 116 | + sut.Entitlements.Modify(func(entitlements *codersdk.Entitlements) { |
| 117 | + entitlements.Errors = []string{ |
| 118 | + "License has expired", |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + registry.Register(&sut) |
| 123 | + |
| 124 | + metrics, err := registry.Gather() |
| 125 | + require.NoError(t, err) |
| 126 | + |
| 127 | + warnings, ok := findMetric(metrics, "coderd_license_warnings") |
| 128 | + require.True(t, ok) |
| 129 | + require.Zero(t, warnings) |
| 130 | + |
| 131 | + errors, ok := findMetric(metrics, "coderd_license_errors") |
| 132 | + require.True(t, ok) |
| 133 | + require.Equal(t, 1, errors) |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("WithBothWarningsAndErrors", func(t *testing.T) { |
| 137 | + t.Parallel() |
| 138 | + |
| 139 | + registry := prometheus.NewRegistry() |
| 140 | + var sut license.MetricsCollector |
| 141 | + sut.Entitlements = entitlements.New() |
| 142 | + sut.Entitlements.Modify(func(entitlements *codersdk.Entitlements) { |
| 143 | + entitlements.Warnings = []string{ |
| 144 | + "License expires in 7 days", |
| 145 | + "User limit is at 95% capacity", |
| 146 | + "Feature X is deprecated", |
| 147 | + } |
| 148 | + entitlements.Errors = []string{ |
| 149 | + "Invalid license signature", |
| 150 | + "License UUID mismatch", |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + registry.Register(&sut) |
| 155 | + |
| 156 | + metrics, err := registry.Gather() |
| 157 | + require.NoError(t, err) |
| 158 | + |
| 159 | + warnings, ok := findMetric(metrics, "coderd_license_warnings") |
| 160 | + require.True(t, ok) |
| 161 | + require.Equal(t, 3, warnings) |
| 162 | + |
| 163 | + errors, ok := findMetric(metrics, "coderd_license_errors") |
| 164 | + require.True(t, ok) |
| 165 | + require.Equal(t, 2, errors) |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +// findMetric searches for a metric by name and returns its value. |
| 170 | +func findMetric(metrics []*prometheus_client.MetricFamily, name string) (int, bool) { |
52 | 171 | for _, metric := range metrics { |
53 | | - switch metric.GetName() { |
54 | | - case "coderd_license_active_users", "coderd_license_limit_users", "coderd_license_user_limit_enabled": |
| 172 | + if metric.GetName() == name { |
55 | 173 | for _, m := range metric.Metric { |
56 | | - collected[metric.GetName()] = int(m.Gauge.GetValue()) |
| 174 | + return int(m.Gauge.GetValue()), true |
57 | 175 | } |
58 | | - default: |
59 | | - require.FailNowf(t, "unexpected metric collected", "metric: %s", metric.GetName()) |
60 | 176 | } |
61 | 177 | } |
62 | | - require.EqualValues(t, golden, collected) |
| 178 | + return 0, false |
63 | 179 | } |
0 commit comments