| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/prometheus/client_golang/prometheus" |
| 8 | ) |
| 9 | |
| 10 | func TestSanitizeMetricName(t *testing.T) { |
| 11 | tsts := map[string]string{ |
| 12 | "cluster_stats_messages_auth-req_received": "cluster_stats_messages_auth_req_received", |
| 13 | "cluster_stats_messages_auth_req_received": "cluster_stats_messages_auth_req_received", |
| 14 | } |
| 15 | |
| 16 | for m, want := range tsts { |
| 17 | if got := sanitizeMetricName(m); got != want { |
| 18 | t.Errorf("sanitizeMetricName( %s ) error, want: %s, got: %s", m, want, got) |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestRegisterConstHistogram(t *testing.T) { |
| 24 | metricName := "foo" |
| 25 | for _, inc := range []bool{false, true} { |
| 26 | exp := getTestExporterWithOptions(Options{Namespace: "test", AppendInstanceRoleLabel: inc}) |
| 27 | ch := make(chan prometheus.Metric) |
| 28 | go func() { |
| 29 | exp.createMetricDescription(metricName, []string{"test"}) |
| 30 | exp.registerConstHistogram(ch, metricName, 12, .24, map[float64]uint64{}, "test") |
| 31 | close(ch) |
| 32 | }() |
| 33 | |
| 34 | for m := range ch { |
| 35 | if strings.Contains(m.Desc().String(), metricName) { |
| 36 | if inc && !strings.Contains(m.Desc().String(), "instance_role") { |
| 37 | t.Errorf("want metrics to include instance_role label, have:\n%s", m.Desc().String()) |
| 38 | } |
| 39 | if !inc && strings.Contains(m.Desc().String(), "instance_role") { |
| 40 | t.Errorf("did NOT want metrics to include instance_role label, have:\n%s", m.Desc().String()) |
| 41 | } |
| 42 | continue |
| 43 | } |
| 44 | t.Errorf("Histogram was not registered") |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestRegisterConstMetric(t *testing.T) { |
| 50 | metricName := "bar" |
| 51 | for _, inc := range []bool{false, true} { |
| 52 | exp := getTestExporterWithOptions(Options{Namespace: "test", AppendInstanceRoleLabel: inc}) |
| 53 | ch := make(chan prometheus.Metric) |
| 54 | go func() { |
| 55 | exp.createMetricDescription(metricName, []string{"test"}) |
| 56 | exp.registerConstMetric(ch, metricName, 4.2, prometheus.GaugeValue, "test") |
| 57 | close(ch) |
| 58 | }() |
| 59 | |
| 60 | for m := range ch { |
| 61 | if strings.Contains(m.Desc().String(), metricName) { |
| 62 | if inc && !strings.Contains(m.Desc().String(), "instance_role") { |
| 63 | t.Errorf("want metrics to include instance_role label, have:\n%s", m.Desc().String()) |
| 64 | } |
| 65 | if !inc && strings.Contains(m.Desc().String(), "instance_role") { |
| 66 | t.Errorf("did NOT want metrics to include instance_role label, have:\n%s", m.Desc().String()) |
| 67 | } |
| 68 | continue |
| 69 | } |
| 70 | t.Errorf("ConstMetric was not registered") |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestFindOrCreateMetricsDescriptionFindExisting(t *testing.T) { |
| 76 | exp := getTestExporter() |
| 77 | exp.metricDescriptions = map[string]*prometheus.Desc{} |
| 78 | |
| 79 | metricName := "foo" |
| 80 | labels := []string{"1", "2"} |
| 81 | |
| 82 | ret := exp.createMetricDescription(metricName, labels) |
| 83 | ret2 := exp.createMetricDescription(metricName, labels) |
| 84 | |
| 85 | if ret == nil || ret2 == nil || ret != ret2 { |
| 86 | t.Errorf("Unexpected return values: (%v, %v)", ret, ret2) |
| 87 | } |
| 88 | |
| 89 | if len(exp.metricDescriptions) != 1 { |
| 90 | t.Errorf("Unexpected metricDescriptions entry count.") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestFindOrCreateMetricsDescriptionCreateNew(t *testing.T) { |
| 95 | exp := getTestExporter() |
| 96 | exp.metricDescriptions = map[string]*prometheus.Desc{} |
| 97 | |
| 98 | metricName := "foo" |
| 99 | labels := []string{"1", "2"} |
| 100 | |
| 101 | ret := exp.createMetricDescription(metricName, labels) |
| 102 | |
| 103 | if ret == nil { |
| 104 | t.Errorf("Unexpected return value: %s", ret) |
| 105 | } |
| 106 | } |