github.com/google/cloudprober@v0.11.3/surfacers/postgres/postgres_test.go (about) 1 package postgres 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/google/cloudprober/metrics" 9 ) 10 11 func Test_emToPGMetrics_No_Distribution(t *testing.T) { 12 respCodesVal := metrics.NewMap("code", metrics.NewInt(0)) 13 respCodesVal.IncKeyBy("200", metrics.NewInt(19)) 14 ts := time.Now() 15 em := metrics.NewEventMetrics(ts). 16 AddMetric("sent", metrics.NewInt(32)). 17 AddMetric("rcvd", metrics.NewInt(22)). 18 AddMetric("latency", metrics.NewFloat(10.11111)). 19 AddMetric("resp_code", respCodesVal). 20 AddLabel("ptype", "http") 21 22 rows := emToPGMetrics(em) 23 24 if len(rows) != 4 { 25 t.Errorf("Expected %d rows, received: %d\n", 4, len(rows)) 26 } 27 28 if !isRowExpected(rows[0], ts, "sent", "32", map[string]string{"ptype": "http"}) { 29 t.Errorf("Incorrect Row found %+v", rows[0]) 30 } 31 32 if !isRowExpected(rows[1], ts, "rcvd", "22", map[string]string{"ptype": "http"}) { 33 t.Errorf("Incorrect Row found %+v", rows[1]) 34 } 35 36 if !isRowExpected(rows[2], ts, "latency", "10.111", map[string]string{"ptype": "http"}) { 37 t.Errorf("Incorrect Row found %+v", rows[2]) 38 } 39 40 if !isRowExpected(rows[3], ts, "resp_code", "19", map[string]string{"ptype": "http", "code": "200"}) { 41 t.Errorf("Incorrect Row found %+v", rows[3]) 42 } 43 } 44 45 func Test_emToPGMetrics_With_Distribution(t *testing.T) { 46 respCodesVal := metrics.NewMap("code", metrics.NewInt(0)) 47 respCodesVal.IncKeyBy("200", metrics.NewInt(19)) 48 latencyVal := metrics.NewDistribution([]float64{1, 4}) 49 latencyVal.AddSample(0.5) 50 latencyVal.AddSample(5) 51 ts := time.Now() 52 em := metrics.NewEventMetrics(ts). 53 AddMetric("latency", latencyVal). 54 AddLabel("ptype", "http") 55 56 rows := emToPGMetrics(em) 57 58 if len(rows) != 5 { 59 t.Errorf("Expected %d rows, received: %d\n", 5, len(rows)) 60 } 61 62 if !isRowExpected(rows[0], ts, "latency_sum", "5.5", map[string]string{"ptype": "http"}) { 63 t.Errorf("Incorrect Row found %+v", rows[0]) 64 } 65 66 if !isRowExpected(rows[1], ts, "latency_count", "2", map[string]string{"ptype": "http"}) { 67 t.Errorf("Incorrect Row found %+v", rows[1]) 68 } 69 70 if !isRowExpected(rows[2], ts, "latency_bucket", "1", map[string]string{"ptype": "http", "le": "1"}) { 71 t.Errorf("Incorrect Row found %+v", rows[2]) 72 } 73 74 if !isRowExpected(rows[3], ts, "latency_bucket", "1", map[string]string{"ptype": "http", "le": "4"}) { 75 t.Errorf("Incorrect Row found %+v", rows[3]) 76 } 77 78 if !isRowExpected(rows[4], ts, "latency_bucket", "2", map[string]string{"ptype": "http", "le": "+Inf"}) { 79 t.Errorf("Incorrect Row found %+v", rows[4]) 80 } 81 82 } 83 84 func isRowExpected(row pgMetric, t time.Time, metricName string, value string, labels map[string]string) bool { 85 if row.time != t { 86 return false 87 } 88 if row.metricName != metricName { 89 return false 90 } 91 if row.value != value { 92 return false 93 } 94 if !reflect.DeepEqual(row.labels, labels) { 95 return false 96 } 97 98 return true 99 }