github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/metric_deprecated_test.go (about) 1 package processor 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/Jeffail/benthos/v3/lib/log" 8 "github.com/Jeffail/benthos/v3/lib/message" 9 "github.com/Jeffail/benthos/v3/lib/metrics" 10 ) 11 12 //------------------------------------------------------------------------------ 13 14 func TestMetricDeprecatedBad(t *testing.T) { 15 conf := NewConfig() 16 conf.Type = "metric" 17 conf.Metric.Type = "bad type" 18 conf.Metric.Path = "some.path" 19 if _, err := New(conf, nil, log.Noop(), metrics.Noop()); err == nil { 20 t.Error("Expected error from bad type") 21 } 22 23 conf = NewConfig() 24 conf.Type = "metric" 25 if _, err := New(conf, nil, log.Noop(), metrics.Noop()); err == nil { 26 t.Error("Expected error from empty path") 27 } 28 } 29 30 func TestMetricDeprecatedCounter(t *testing.T) { 31 mockStats := &mockMetric{ 32 values: map[string]int64{}, 33 } 34 35 conf := NewConfig() 36 conf.Type = "metric" 37 conf.Metric.Type = "counter" 38 conf.Metric.Path = "foo.bar" 39 conf.Metric.Value = "${!json(\"foo.bar\")}" 40 41 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 42 if err != nil { 43 t.Fatal(err) 44 } 45 46 inputs := [][][]byte{ 47 {}, 48 {}, 49 { 50 []byte(`{}`), 51 []byte(`{}`), 52 }, 53 { 54 []byte(`not even json`), 55 }, 56 {}, 57 { 58 []byte(`{"foo":{"bar":"hello world"}}`), 59 }, 60 { 61 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 62 }, 63 } 64 65 expMetrics := map[string]int64{ 66 "foo.bar": 7, 67 } 68 69 for _, i := range inputs { 70 msg, res := proc.ProcessMessage(message.New(i)) 71 if exp, act := 1, len(msg); exp != act { 72 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 73 } 74 if res != nil { 75 t.Error(res.Error()) 76 } 77 } 78 79 if !reflect.DeepEqual(expMetrics, mockStats.values) { 80 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 81 } 82 } 83 84 func TestMetricDeprecatedCounterParts(t *testing.T) { 85 mockStats := &mockMetric{ 86 values: map[string]int64{}, 87 } 88 89 conf := NewConfig() 90 conf.Type = "metric" 91 conf.Metric.Type = "counter_parts" 92 conf.Metric.Path = "foo.bar" 93 conf.Metric.Value = "${!json(\"foo.bar\")}" 94 95 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 inputs := [][][]byte{ 101 {}, 102 {}, 103 { 104 []byte(`{}`), 105 []byte(`{}`), 106 }, 107 { 108 []byte(`not even json`), 109 }, 110 {}, 111 { 112 []byte(`{"foo":{"bar":"hello world"}}`), 113 }, 114 { 115 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 116 }, 117 } 118 119 expMetrics := map[string]int64{ 120 "foo.bar": 5, 121 } 122 123 for _, i := range inputs { 124 msg, res := proc.ProcessMessage(message.New(i)) 125 if exp, act := 1, len(msg); exp != act { 126 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 127 } 128 if res != nil { 129 t.Error(res.Error()) 130 } 131 } 132 133 if !reflect.DeepEqual(expMetrics, mockStats.values) { 134 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 135 } 136 } 137 138 func TestMetricDeprecatedCounterBy(t *testing.T) { 139 mockStats := &mockMetric{ 140 values: map[string]int64{}, 141 } 142 143 conf := NewConfig() 144 conf.Type = "metric" 145 conf.Metric.Type = "counter_by" 146 conf.Metric.Path = "foo.bar" 147 conf.Metric.Value = "${!json(\"foo.bar\")}" 148 149 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 inputs := [][][]byte{ 155 {}, 156 {}, 157 { 158 []byte(`{"foo":{"bar":2}}`), 159 []byte(`{}`), 160 }, 161 { 162 []byte(`not even json`), 163 }, 164 { 165 []byte(`{"foo":{"bar":-2}}`), 166 }, 167 { 168 []byte(`{"foo":{"bar":3}}`), 169 }, 170 { 171 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 172 }, 173 } 174 175 expMetrics := map[string]int64{ 176 "foo.bar": 5, 177 } 178 179 for _, i := range inputs { 180 msg, res := proc.ProcessMessage(message.New(i)) 181 if exp, act := 1, len(msg); exp != act { 182 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 183 } 184 if res != nil { 185 t.Error(res.Error()) 186 } 187 } 188 189 if !reflect.DeepEqual(expMetrics, mockStats.values) { 190 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 191 } 192 } 193 194 func TestMetricDeprecatedGauge(t *testing.T) { 195 mockStats := &mockMetric{ 196 values: map[string]int64{}, 197 } 198 199 conf := NewConfig() 200 conf.Type = "metric" 201 conf.Metric.Type = "gauge" 202 conf.Metric.Path = "foo.bar" 203 conf.Metric.Value = "${!json(\"foo.bar\")}" 204 205 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 206 if err != nil { 207 t.Fatal(err) 208 } 209 210 inputs := [][][]byte{ 211 {}, 212 {}, 213 { 214 []byte(`{"foo":{"bar":5}}`), 215 []byte(`{}`), 216 }, 217 { 218 []byte(`not even json`), 219 }, 220 { 221 []byte(`{"foo":{"bar":-5}}`), 222 }, 223 { 224 []byte(`{"foo":{"bar":"hello world"}}`), 225 }, 226 { 227 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 228 }, 229 } 230 231 expMetrics := map[string]int64{ 232 "foo.bar": 5, 233 } 234 235 for _, i := range inputs { 236 msg, res := proc.ProcessMessage(message.New(i)) 237 if exp, act := 1, len(msg); exp != act { 238 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 239 } 240 if res != nil { 241 t.Error(res.Error()) 242 } 243 } 244 245 if !reflect.DeepEqual(expMetrics, mockStats.values) { 246 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 247 } 248 } 249 250 func TestMetricDeprecatedTiming(t *testing.T) { 251 mockStats := &mockMetric{ 252 values: map[string]int64{}, 253 } 254 255 conf := NewConfig() 256 conf.Type = "metric" 257 conf.Metric.Type = "timing" 258 conf.Metric.Path = "foo.bar" 259 conf.Metric.Value = "${!json(\"foo.bar\")}" 260 261 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 262 if err != nil { 263 t.Fatal(err) 264 } 265 266 inputs := [][][]byte{ 267 {}, 268 {}, 269 { 270 []byte(`{"foo":{"bar":5}}`), 271 []byte(`{}`), 272 }, 273 { 274 []byte(`not even json`), 275 }, 276 { 277 []byte(`{"foo":{"bar":-5}}`), 278 }, 279 { 280 []byte(`{"foo":{"bar":"hello world"}}`), 281 }, 282 { 283 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 284 }, 285 } 286 287 expMetrics := map[string]int64{ 288 "foo.bar": 5, 289 } 290 291 for _, i := range inputs { 292 msg, res := proc.ProcessMessage(message.New(i)) 293 if exp, act := 1, len(msg); exp != act { 294 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 295 } 296 if res != nil { 297 t.Error(res.Error()) 298 } 299 } 300 301 if !reflect.DeepEqual(expMetrics, mockStats.values) { 302 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 303 } 304 } 305 306 func TestMetricDeprecatedCounterLabels(t *testing.T) { 307 mockStats := &mockMetric{ 308 values: map[string]int64{}, 309 } 310 311 conf := NewConfig() 312 conf.Type = "metric" 313 conf.Metric.Type = "counter" 314 conf.Metric.Path = "foo.bar" 315 conf.Metric.Value = "${!json(\"foo.bar\")}" 316 conf.Metric.Labels = map[string]string{ 317 "batch_size": "${!batch_size()}", 318 } 319 320 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 321 if err != nil { 322 t.Fatal(err) 323 } 324 325 inputs := [][][]byte{ 326 {}, 327 {}, 328 { 329 []byte(`{}`), 330 []byte(`{}`), 331 }, 332 { 333 []byte(`not even json`), 334 }, 335 {}, 336 { 337 []byte(`{"foo":{"bar":"hello world"}}`), 338 }, 339 { 340 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 341 }, 342 } 343 344 expMetrics := map[string]int64{ 345 "foo.bar": 7, 346 } 347 348 for _, i := range inputs { 349 msg, res := proc.ProcessMessage(message.New(i)) 350 if exp, act := 1, len(msg); exp != act { 351 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 352 } 353 if res != nil { 354 t.Error(res.Error()) 355 } 356 } 357 358 if !reflect.DeepEqual(expMetrics, mockStats.values) { 359 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 360 } 361 } 362 363 func TestMetricDeprecatedCounterPartsLabels(t *testing.T) { 364 mockStats := &mockMetric{ 365 values: map[string]int64{}, 366 } 367 368 conf := NewConfig() 369 conf.Type = "metric" 370 conf.Metric.Type = "counter_parts" 371 conf.Metric.Path = "foo.bar" 372 conf.Metric.Value = "${!json(\"foo.bar\")}" 373 conf.Metric.Labels = map[string]string{ 374 "batch_size": "${!batch_size()}", 375 } 376 377 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 378 if err != nil { 379 t.Fatal(err) 380 } 381 382 inputs := [][][]byte{ 383 {}, 384 {}, 385 { 386 []byte(`{}`), 387 []byte(`{}`), 388 }, 389 { 390 []byte(`not even json`), 391 }, 392 {}, 393 { 394 []byte(`{"foo":{"bar":"hello world"}}`), 395 }, 396 { 397 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 398 }, 399 } 400 401 expMetrics := map[string]int64{ 402 "foo.bar": 5, 403 } 404 405 for _, i := range inputs { 406 msg, res := proc.ProcessMessage(message.New(i)) 407 if exp, act := 1, len(msg); exp != act { 408 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 409 } 410 if res != nil { 411 t.Error(res.Error()) 412 } 413 } 414 415 if !reflect.DeepEqual(expMetrics, mockStats.values) { 416 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 417 } 418 } 419 420 func TestMetricDeprecatedCounterByLabels(t *testing.T) { 421 mockStats := &mockMetric{ 422 values: map[string]int64{}, 423 } 424 425 conf := NewConfig() 426 conf.Type = "metric" 427 conf.Metric.Type = "counter_by" 428 conf.Metric.Path = "foo.bar" 429 conf.Metric.Value = "${!json(\"foo.bar\")}" 430 conf.Metric.Labels = map[string]string{ 431 "batch_size": "${!batch_size()}", 432 } 433 434 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 435 if err != nil { 436 t.Fatal(err) 437 } 438 439 inputs := [][][]byte{ 440 {}, 441 {}, 442 { 443 []byte(`{"foo":{"bar":2}}`), 444 []byte(`{}`), 445 }, 446 { 447 []byte(`not even json`), 448 }, 449 { 450 []byte(`{"foo":{"bar":-2}}`), 451 }, 452 { 453 []byte(`{"foo":{"bar":3}}`), 454 }, 455 { 456 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 457 }, 458 } 459 460 expMetrics := map[string]int64{ 461 "foo.bar": 5, 462 } 463 464 for _, i := range inputs { 465 msg, res := proc.ProcessMessage(message.New(i)) 466 if exp, act := 1, len(msg); exp != act { 467 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 468 } 469 if res != nil { 470 t.Error(res.Error()) 471 } 472 } 473 474 if !reflect.DeepEqual(expMetrics, mockStats.values) { 475 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 476 } 477 } 478 479 func TestMetricDeprecatedGaugeLabels(t *testing.T) { 480 mockStats := &mockMetric{ 481 values: map[string]int64{}, 482 } 483 484 conf := NewConfig() 485 conf.Type = "metric" 486 conf.Metric.Type = "gauge" 487 conf.Metric.Path = "foo.bar" 488 conf.Metric.Value = "${!json(\"foo.bar\")}" 489 conf.Metric.Labels = map[string]string{ 490 "batch_size": "${!batch_size()}", 491 } 492 493 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 494 if err != nil { 495 t.Fatal(err) 496 } 497 498 inputs := [][][]byte{ 499 {}, 500 {}, 501 { 502 []byte(`{"foo":{"bar":5}}`), 503 []byte(`{}`), 504 }, 505 { 506 []byte(`not even json`), 507 }, 508 { 509 []byte(`{"foo":{"bar":-5}}`), 510 }, 511 { 512 []byte(`{"foo":{"bar":"hello world"}}`), 513 }, 514 { 515 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 516 }, 517 } 518 519 expMetrics := map[string]int64{ 520 "foo.bar": 5, 521 } 522 523 for _, i := range inputs { 524 msg, res := proc.ProcessMessage(message.New(i)) 525 if exp, act := 1, len(msg); exp != act { 526 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 527 } 528 if res != nil { 529 t.Error(res.Error()) 530 } 531 } 532 533 if !reflect.DeepEqual(expMetrics, mockStats.values) { 534 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 535 } 536 } 537 538 func TestMetricDeprecatedTimingLabels(t *testing.T) { 539 mockStats := &mockMetric{ 540 values: map[string]int64{}, 541 } 542 543 conf := NewConfig() 544 conf.Type = "metric" 545 conf.Metric.Type = "timing" 546 conf.Metric.Path = "foo.bar" 547 conf.Metric.Value = "${!json(\"foo.bar\")}" 548 conf.Metric.Labels = map[string]string{ 549 "batch_size": "${!batch_size()}", 550 } 551 552 proc, err := New(conf, nil, log.Noop(), metrics.WrapFlat(mockStats)) 553 if err != nil { 554 t.Fatal(err) 555 } 556 557 inputs := [][][]byte{ 558 {}, 559 {}, 560 { 561 []byte(`{"foo":{"bar":5}}`), 562 []byte(`{}`), 563 }, 564 { 565 []byte(`not even json`), 566 }, 567 { 568 []byte(`{"foo":{"bar":-5}}`), 569 }, 570 { 571 []byte(`{"foo":{"bar":"hello world"}}`), 572 }, 573 { 574 []byte(`{"foo":{"bar":{"baz":"hello world"}}}`), 575 }, 576 } 577 578 expMetrics := map[string]int64{ 579 "foo.bar": 5, 580 } 581 582 for _, i := range inputs { 583 msg, res := proc.ProcessMessage(message.New(i)) 584 if exp, act := 1, len(msg); exp != act { 585 t.Errorf("Wrong count of resulting messages: %v != %v", act, exp) 586 } 587 if res != nil { 588 t.Error(res.Error()) 589 } 590 } 591 592 if !reflect.DeepEqual(expMetrics, mockStats.values) { 593 t.Errorf("Wrong result: %v != %v", mockStats.values, expMetrics) 594 } 595 } 596 597 //------------------------------------------------------------------------------