metrics_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v2
  14. import (
  15. "context"
  16. "strings"
  17. "testing"
  18. "time"
  19. )
  20. func TestSumMetricValues(t *testing.T) {
  21. metrics := MetricsMap{
  22. "grpc_pool_connections_total": {
  23. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-a"}, Value: 1},
  24. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-a"}, Value: 2},
  25. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-b"}, Value: 4},
  26. },
  27. }
  28. got := SumMetricValues(metrics, "grpc_pool_connections_total", map[string]string{"address": "provider-a"})
  29. if got != 3 {
  30. t.Fatalf("expected sum 3, got %v", got)
  31. }
  32. }
  33. func TestCountMetricSamples(t *testing.T) {
  34. metrics := MetricsMap{
  35. "grpc_pool_connections_total": {
  36. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-a"}, Value: 1},
  37. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-a"}, Value: 2},
  38. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-b"}, Value: 4},
  39. },
  40. }
  41. got := CountMetricSamples(metrics, "grpc_pool_connections_total", map[string]string{"address": "provider-a"})
  42. if got != 2 {
  43. t.Fatalf("expected count 2, got %d", got)
  44. }
  45. }
  46. func TestGetMetricValueMatchesSubsetOfLabels(t *testing.T) {
  47. metrics := MetricsMap{
  48. "grpc_pool_connections_total": {
  49. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-a", "state": "idle"}, Value: 2},
  50. {Name: "grpc_pool_connections_total", Labels: map[string]string{"address": "provider-b", "state": "active"}, Value: 4},
  51. },
  52. }
  53. got, found := GetMetricValue(metrics, "grpc_pool_connections_total", map[string]string{"address": "provider-a"})
  54. if !found {
  55. t.Fatalf("expected matching metric sample")
  56. }
  57. if got != 2 {
  58. t.Fatalf("expected value 2, got %v", got)
  59. }
  60. }
  61. func TestWaitForMetricHonorsContextCancellation(t *testing.T) {
  62. ctx, cancel := context.WithCancel(context.Background())
  63. cancel()
  64. err := WaitForMetric(ctx, func() (MetricsMap, error) {
  65. return MetricsMap{}, nil
  66. }, "grpc_pool_connections_total", map[string]string{"address": "provider-a"}, 1, 10*time.Second)
  67. if err == nil {
  68. t.Fatalf("expected cancellation error")
  69. }
  70. if !strings.Contains(err.Error(), "context canceled") {
  71. t.Fatalf("expected context cancellation error, got %v", err)
  72. }
  73. }
  74. func TestParsePrometheusMetricsParsesEscapedLabels(t *testing.T) {
  75. body := "grpc_pool_connections_total{address=\"provider-\\\"a\\\"\",state=\"idle\\nstate\"} 2\n"
  76. metrics, err := parsePrometheusMetrics(body)
  77. if err != nil {
  78. t.Fatalf("unexpected parse error: %v", err)
  79. }
  80. got, found := GetMetricValue(metrics, "grpc_pool_connections_total", map[string]string{
  81. "address": "provider-\"a\"",
  82. "state": "idle\nstate",
  83. })
  84. if !found {
  85. t.Fatalf("expected escaped metric labels to match parsed sample, got metrics %#v", metrics)
  86. }
  87. if got != 2 {
  88. t.Fatalf("expected value 2, got %v", got)
  89. }
  90. }