|
14 | 14 | package event |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "fmt" |
| 18 | + "reflect" |
17 | 19 | "testing" |
18 | 20 | "time" |
19 | 21 |
|
20 | 22 | "github.com/prometheus/client_golang/prometheus" |
| 23 | + |
21 | 24 | "github.com/prometheus/statsd_exporter/pkg/clock" |
| 25 | + "github.com/prometheus/statsd_exporter/pkg/mapper" |
22 | 26 | ) |
23 | 27 |
|
24 | 28 | var eventsFlushed = prometheus.NewCounter( |
@@ -85,3 +89,206 @@ func TestEventIntervalFlush(t *testing.T) { |
85 | 89 | t.Fatal("Expected 10 events in the event channel, but got", len(events)) |
86 | 90 | } |
87 | 91 | } |
| 92 | + |
| 93 | +func TestMultiValueEvent(t *testing.T) { |
| 94 | + tests := []struct { |
| 95 | + name string |
| 96 | + event MultiValueEvent |
| 97 | + wantValues []float64 |
| 98 | + wantName string |
| 99 | + wantType mapper.MetricType |
| 100 | + wantLabels map[string]string |
| 101 | + }{ |
| 102 | + { |
| 103 | + name: "MultiObserverEvent with single value", |
| 104 | + event: &MultiObserverEvent{ |
| 105 | + OMetricName: "test_metric", |
| 106 | + OValues: []float64{1.0}, |
| 107 | + OLabels: map[string]string{"label": "value"}, |
| 108 | + SampleRate: 0, |
| 109 | + }, |
| 110 | + wantValues: []float64{1.0}, |
| 111 | + wantName: "test_metric", |
| 112 | + wantType: mapper.MetricTypeObserver, |
| 113 | + wantLabels: map[string]string{"label": "value"}, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "MultiObserverEvent with multiple values", |
| 117 | + event: &MultiObserverEvent{ |
| 118 | + OMetricName: "test_metric", |
| 119 | + OValues: []float64{1.0, 2.0, 3.0}, |
| 120 | + OLabels: map[string]string{"label": "value"}, |
| 121 | + SampleRate: 0.5, |
| 122 | + }, |
| 123 | + wantValues: []float64{1.0, 2.0, 3.0}, |
| 124 | + wantName: "test_metric", |
| 125 | + wantType: mapper.MetricTypeObserver, |
| 126 | + wantLabels: map[string]string{"label": "value"}, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "CounterEvent implements MultiValueEvent", |
| 130 | + event: &CounterEvent{ |
| 131 | + CMetricName: "test_counter", |
| 132 | + CValue: 42.0, |
| 133 | + CLabels: map[string]string{"label": "value"}, |
| 134 | + }, |
| 135 | + wantValues: []float64{42.0}, |
| 136 | + wantName: "test_counter", |
| 137 | + wantType: mapper.MetricTypeCounter, |
| 138 | + wantLabels: map[string]string{"label": "value"}, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "GaugeEvent implements MultiValueEvent", |
| 142 | + event: &GaugeEvent{ |
| 143 | + GMetricName: "test_gauge", |
| 144 | + GValue: 123.0, |
| 145 | + GLabels: map[string]string{"label": "value"}, |
| 146 | + }, |
| 147 | + wantValues: []float64{123.0}, |
| 148 | + wantName: "test_gauge", |
| 149 | + wantType: mapper.MetricTypeGauge, |
| 150 | + wantLabels: map[string]string{"label": "value"}, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "ObserverEvent implements MultiValueEvent", |
| 154 | + event: &ObserverEvent{ |
| 155 | + OMetricName: "test_observer", |
| 156 | + OValue: 99.0, |
| 157 | + OLabels: map[string]string{"label": "value"}, |
| 158 | + }, |
| 159 | + wantValues: []float64{99.0}, |
| 160 | + wantName: "test_observer", |
| 161 | + wantType: mapper.MetricTypeObserver, |
| 162 | + wantLabels: map[string]string{"label": "value"}, |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + for _, tt := range tests { |
| 167 | + t.Run(tt.name, func(t *testing.T) { |
| 168 | + if got := tt.event.Values(); !reflect.DeepEqual(got, tt.wantValues) { |
| 169 | + t.Errorf("MultiValueEvent.Values() = %v, want %v", got, tt.wantValues) |
| 170 | + } |
| 171 | + if got := tt.event.MetricName(); got != tt.wantName { |
| 172 | + t.Errorf("MultiValueEvent.MetricName() = %v, want %v", got, tt.wantName) |
| 173 | + } |
| 174 | + if got := tt.event.MetricType(); got != tt.wantType { |
| 175 | + t.Errorf("MultiValueEvent.MetricType() = %v, want %v", got, tt.wantType) |
| 176 | + } |
| 177 | + if got := tt.event.Labels(); !reflect.DeepEqual(got, tt.wantLabels) { |
| 178 | + t.Errorf("MultiValueEvent.Labels() = %v, want %v", got, tt.wantLabels) |
| 179 | + } |
| 180 | + }) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func TestMultiObserverEvent_Expand(t *testing.T) { |
| 185 | + t.Parallel() |
| 186 | + tests := []struct { |
| 187 | + name string |
| 188 | + event *MultiObserverEvent |
| 189 | + wantEvents []Event |
| 190 | + }{ |
| 191 | + { |
| 192 | + name: "single value no sampling", |
| 193 | + event: &MultiObserverEvent{ |
| 194 | + OMetricName: "test_metric", |
| 195 | + OValues: []float64{1.0}, |
| 196 | + OLabels: map[string]string{"label": "value"}, |
| 197 | + SampleRate: 0, |
| 198 | + }, |
| 199 | + wantEvents: []Event{ |
| 200 | + &ObserverEvent{ |
| 201 | + OMetricName: "test_metric", |
| 202 | + OValue: 1.0, |
| 203 | + OLabels: map[string]string{"label": "value"}, |
| 204 | + }, |
| 205 | + }, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "multiple values no sampling", |
| 209 | + event: &MultiObserverEvent{ |
| 210 | + OMetricName: "test_metric", |
| 211 | + OValues: []float64{1.0, 2.0, 3.0}, |
| 212 | + OLabels: map[string]string{"label": "value"}, |
| 213 | + SampleRate: 0, |
| 214 | + }, |
| 215 | + wantEvents: []Event{ |
| 216 | + &ObserverEvent{ |
| 217 | + OMetricName: "test_metric", |
| 218 | + OValue: 1.0, |
| 219 | + OLabels: map[string]string{"label": "value"}, |
| 220 | + }, |
| 221 | + &ObserverEvent{ |
| 222 | + OMetricName: "test_metric", |
| 223 | + OValue: 2.0, |
| 224 | + OLabels: map[string]string{"label": "value"}, |
| 225 | + }, |
| 226 | + &ObserverEvent{ |
| 227 | + OMetricName: "test_metric", |
| 228 | + OValue: 3.0, |
| 229 | + OLabels: map[string]string{"label": "value"}, |
| 230 | + }, |
| 231 | + }, |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "multiple values with sampling", |
| 235 | + event: &MultiObserverEvent{ |
| 236 | + OMetricName: "test_metric", |
| 237 | + OValues: []float64{1.0, 2.0}, |
| 238 | + OLabels: map[string]string{"label": "value"}, |
| 239 | + SampleRate: 0.5, |
| 240 | + }, |
| 241 | + wantEvents: []Event{ |
| 242 | + &ObserverEvent{ |
| 243 | + OMetricName: "test_metric", |
| 244 | + OValue: 1.0, |
| 245 | + OLabels: map[string]string{"label": "value"}, |
| 246 | + }, |
| 247 | + &ObserverEvent{ |
| 248 | + OMetricName: "test_metric", |
| 249 | + OValue: 2.0, |
| 250 | + OLabels: map[string]string{"label": "value"}, |
| 251 | + }, |
| 252 | + &ObserverEvent{ |
| 253 | + OMetricName: "test_metric", |
| 254 | + OValue: 1.0, |
| 255 | + OLabels: map[string]string{"label": "value"}, |
| 256 | + }, |
| 257 | + &ObserverEvent{ |
| 258 | + OMetricName: "test_metric", |
| 259 | + OValue: 2.0, |
| 260 | + OLabels: map[string]string{"label": "value"}, |
| 261 | + }, |
| 262 | + }, |
| 263 | + }, |
| 264 | + } |
| 265 | + |
| 266 | + for _, tt := range tests { |
| 267 | + t.Run(tt.name, func(t *testing.T) { |
| 268 | + t.Parallel() |
| 269 | + got := tt.event.Expand() |
| 270 | + if len(tt.wantEvents) != len(got) { |
| 271 | + t.Fatalf("Expected %d events, but got %d", len(tt.wantEvents), len(got)) |
| 272 | + } |
| 273 | + |
| 274 | + eventCount := func(events []Event) map[string]int { |
| 275 | + counts := make(map[string]int) |
| 276 | + for _, event := range events { |
| 277 | + oe := event.(*ObserverEvent) |
| 278 | + key := fmt.Sprintf("%s%f%v", oe.OMetricName, oe.OValue, oe.OLabels) |
| 279 | + counts[key]++ |
| 280 | + } |
| 281 | + return counts |
| 282 | + } |
| 283 | + |
| 284 | + wantMap := eventCount(tt.wantEvents) |
| 285 | + gotMap := eventCount(got) |
| 286 | + |
| 287 | + for key, count := range wantMap { |
| 288 | + if gotMap[key] != count { |
| 289 | + t.Fatalf("Event mismatch for key %v: expected %d, got %d", key, count, gotMap[key]) |
| 290 | + } |
| 291 | + } |
| 292 | + }) |
| 293 | + } |
| 294 | +} |
0 commit comments