github.com/prysmaticlabs/prysm@v1.4.4/shared/prometheus/logrus_collector_test.go (about) 1 package prometheus_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "strconv" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/prysmaticlabs/prysm/shared/prometheus" 13 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 14 "github.com/prysmaticlabs/prysm/shared/testutil/require" 15 log "github.com/sirupsen/logrus" 16 ) 17 18 const addr = "127.0.0.1:8989" 19 20 type logger interface { 21 Info(args ...interface{}) 22 Warn(args ...interface{}) 23 Error(args ...interface{}) 24 } 25 26 func TestLogrusCollector(t *testing.T) { 27 service := prometheus.NewService(addr, nil) 28 hook := prometheus.NewLogrusCollector() 29 log.AddHook(hook) 30 go service.Start() 31 defer func() { 32 err := service.Stop() 33 require.NoError(t, err) 34 }() 35 36 tests := []struct { 37 name string 38 want int 39 count int 40 prefix string 41 level log.Level 42 }{ 43 {"info message with empty prefix", 3, 3, "", log.InfoLevel}, 44 {"warn message with empty prefix", 2, 2, "", log.WarnLevel}, 45 {"error message with empty prefix", 1, 1, "", log.ErrorLevel}, 46 {"error message with prefix", 1, 1, "foo", log.ErrorLevel}, 47 {"info message with prefix", 3, 3, "foo", log.InfoLevel}, 48 {"warn message with prefix", 2, 2, "foo", log.WarnLevel}, 49 } 50 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 prefix := "global" 54 for i := 0; i < tt.count; i++ { 55 if tt.prefix != "" { 56 prefix = tt.prefix 57 subLog := log.WithField("prefix", tt.prefix) 58 logExampleMessage(subLog, tt.level) 59 continue 60 } 61 logExampleMessage(log.StandardLogger(), tt.level) 62 } 63 time.Sleep(time.Millisecond) 64 metrics := metrics(t) 65 count := valueFor(t, metrics, prefix, tt.level) 66 if count != tt.want { 67 t.Errorf("Expecting %d and receive %d", tt.want, count) 68 } 69 }) 70 } 71 } 72 73 func metrics(t *testing.T) []string { 74 resp, err := http.Get(fmt.Sprintf("http://%s/metrics", addr)) 75 require.NoError(t, err) 76 body, err := ioutil.ReadAll(resp.Body) 77 require.NoError(t, err) 78 return strings.Split(string(body), "\n") 79 } 80 81 func valueFor(t *testing.T, metrics []string, prefix string, level log.Level) int { 82 // Expect line with this pattern: 83 // # HELP log_entries_total Total number of log messages. 84 // # TYPE log_entries_total counter 85 // log_entries_total{level="error",prefix="empty"} 1 86 pattern := fmt.Sprintf("log_entries_total{level=\"%s\",prefix=\"%s\"}", level, prefix) 87 for _, line := range metrics { 88 if strings.HasPrefix(line, pattern) { 89 parts := strings.Split(line, " ") 90 count, err := strconv.ParseFloat(parts[1], 64) 91 assert.NoError(t, err) 92 return int(count) 93 } 94 } 95 t.Errorf("Pattern \"%s\" not found", pattern) 96 return 0 97 } 98 99 func logExampleMessage(logger logger, level log.Level) { 100 switch level { 101 case log.InfoLevel: 102 logger.Info("Info message") 103 case log.WarnLevel: 104 logger.Warn("Warning message!") 105 case log.ErrorLevel: 106 logger.Error("Error message!!") 107 } 108 }