github.com/Jeffail/benthos/v3@v3.65.0/lib/metrics/local_test.go (about) 1 package metrics 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "testing" 8 9 "golang.org/x/sync/errgroup" 10 ) 11 12 func TestCounter(t *testing.T) { 13 path := "testing.label" 14 local := NewLocal() 15 label := "tested" 16 counter := local.GetCounterVec(path, []string{label}) 17 value := "true" 18 19 counter.With(value).Incr(1) 20 21 counters := local.GetCountersWithLabels() 22 c, ok := counters[path] 23 if !ok { 24 t.Fatal("did not find counter for path") 25 } 26 27 if *c.Value != 1 { 28 t.Fatalf("value for counter: got %d, wanted 1", *c.Value) 29 } 30 31 // test second call results in the same value 32 counters = local.GetCountersWithLabels() 33 c, ok = counters[path] 34 if !ok { 35 t.Fatal("did not find counter for path") 36 } 37 38 if *c.Value != 1 { 39 t.Fatalf("value for counter: got %d, wanted 1", *c.Value) 40 } 41 } 42 43 func TestFlushCounter(t *testing.T) { 44 path := "testing.label" 45 local := NewLocal() 46 label := "tested" 47 counter := local.GetCounterVec(path, []string{label}) 48 value := "true" 49 50 counter.With(value).Incr(1) 51 52 counters := local.FlushCounters() 53 c, ok := counters[path] 54 if !ok { 55 t.Fatal("did not find counter for path") 56 } 57 58 if c != 1 { 59 t.Fatalf("value for counter: got %d, wanted 1", c) 60 } 61 62 // test second call results in a reset counter 63 counters = local.FlushCounters() 64 c, ok = counters[path] 65 if !ok { 66 t.Fatal("did not find counter for path") 67 } 68 69 if c != 0 { 70 t.Fatalf("value for flushed counter: got %d, wanted 0", c) 71 } 72 73 } 74 75 func TestCounterWithLabelsAndValues(t *testing.T) { 76 path := "testing.label" 77 local := NewLocal() 78 label := "tested" 79 counter := local.GetCounterVec(path, []string{label}) 80 value := "true" 81 82 counter.With(value).Incr(1) 83 84 counters := local.GetCountersWithLabels() 85 c, ok := counters[path] 86 if !ok { 87 t.Fatal("did not find counter for path") 88 } 89 90 if !c.HasLabelWithValue(label, value) { 91 t.Fatalf("counter does not have label with value %s - %#v", value, c) 92 } 93 94 if c.HasLabelWithValue(label, "unknown") { 95 t.Fatal("counter has label with value unknown") 96 } 97 } 98 99 func TestCounterWithLabelsAndValuesConcurrent(t *testing.T) { 100 path := "testing.label" 101 local := NewLocal() 102 label := "tested" 103 counter := local.GetCounterVec(path, []string{label}) 104 value := "true" 105 106 counter.With(value).Incr(1) 107 108 wg, _ := errgroup.WithContext(context.Background()) 109 wg.Go(func() error { 110 for i := 0; i < 1000; i++ { 111 if err := counter.With(value).Incr(1); err != nil { 112 return err 113 } 114 } 115 return nil 116 }) 117 wg.Go(func() error { 118 for i := 0; i < 1000; i++ { 119 counters := local.GetCountersWithLabels() 120 c, ok := counters[path] 121 if !ok { 122 return errors.New("did not find counter for path") 123 } 124 125 if !c.HasLabelWithValue(label, value) { 126 return fmt.Errorf("counter does not have label with value %s - %#v", value, c) 127 } 128 129 if c.HasLabelWithValue(label, "unknown") { 130 return errors.New("counter has label with value unknown") 131 } 132 if err := counter.With(value).Incr(1); err != nil { 133 return err 134 } 135 } 136 return nil 137 }) 138 139 if err := wg.Wait(); err != nil { 140 t.Error(err) 141 } 142 } 143 144 func TestTimer(t *testing.T) { 145 path := "testing.label" 146 local := NewLocal() 147 label := "tested" 148 counter := local.GetTimerVec(path, []string{label}) 149 value := "true" 150 151 counter.With(value).Timing(1) 152 153 counters := local.GetTimingsWithLabels() 154 c, ok := counters[path] 155 if !ok { 156 t.Fatal("did not find counter for path") 157 } 158 159 if *c.Value != 1 { 160 t.Fatalf("value for counter: got %d, wanted 1", *c.Value) 161 } 162 163 // test second call results in the same value 164 counters = local.GetTimingsWithLabels() 165 c, ok = counters[path] 166 if !ok { 167 t.Fatal("did not find counter for path") 168 } 169 170 if *c.Value != 1 { 171 t.Fatalf("value for counter: got %d, wanted 1", *c.Value) 172 } 173 } 174 175 func TestFlushTimer(t *testing.T) { 176 path := "testing.label" 177 local := NewLocal() 178 label := "tested" 179 counter := local.GetTimerVec(path, []string{label}) 180 value := "true" 181 182 counter.With(value).Timing(1) 183 184 counters := local.FlushTimings() 185 c, ok := counters[path] 186 if !ok { 187 t.Fatal("did not find counter for path") 188 } 189 190 if c != 1 { 191 t.Fatalf("value for counter: got %d, wanted 1", c) 192 } 193 194 // test second call results in a reset counter 195 counters = local.FlushTimings() 196 c, ok = counters[path] 197 if !ok { 198 t.Fatal("did not find counter for path") 199 } 200 201 if c != 0 { 202 t.Fatalf("value for counter: got %d, wanted 0", c) 203 } 204 } 205 206 func TestTimerWithLabelsAndValues(t *testing.T) { 207 path := "testing.label" 208 local := NewLocal() 209 label := "tested" 210 counter := local.GetTimerVec(path, []string{label}) 211 value := "true" 212 213 counter.With(value).Timing(1) 214 215 counters := local.GetTimingsWithLabels() 216 c, ok := counters[path] 217 if !ok { 218 t.Fatal("did not find counter for path") 219 } 220 221 if !c.HasLabelWithValue(label, value) { 222 t.Fatalf("counter does not have label with value %s - %#v", value, c) 223 } 224 225 if c.HasLabelWithValue(label, "unknown") { 226 t.Fatal("counter has label with value unknown") 227 } 228 } 229 230 func TestGauge(t *testing.T) { 231 path := "testing.label" 232 local := NewLocal() 233 label := "tested" 234 counter := local.GetGaugeVec(path, []string{label}) 235 value := "true" 236 237 counter.With(value).Incr(1) 238 239 counters := local.GetCountersWithLabels() 240 c, ok := counters[path] 241 if !ok { 242 t.Fatal("did not find counter for path") 243 } 244 245 if *c.Value != 1 { 246 t.Fatalf("value for counter: got %d, wanted 1", *c.Value) 247 } 248 } 249 250 func TestGaugeWithLabelsAndValues(t *testing.T) { 251 path := "testing.label" 252 local := NewLocal() 253 label := "tested" 254 counter := local.GetGaugeVec(path, []string{label}) 255 value := "true" 256 257 counter.With(value).Incr(1) 258 259 counters := local.GetCountersWithLabels() 260 c, ok := counters[path] 261 if !ok { 262 t.Fatal("did not find counter for path") 263 } 264 265 if !c.HasLabelWithValue(label, value) { 266 t.Fatalf("counter does not have label with value %s - %#v", value, c) 267 } 268 269 if c.HasLabelWithValue(label, "unknown") { 270 t.Fatal("counter has label with value unknown") 271 } 272 }