|
| 1 | +package httpbp |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + "go.uber.org/zap" |
| 10 | + "go.uber.org/zap/zapcore" |
| 11 | + |
| 12 | + "github.com/reddit/baseplate.go/prometheusbp/promtest" |
| 13 | +) |
| 14 | + |
| 15 | +func initBaseLogger(w io.Writer) *zap.Logger { |
| 16 | + // Mostly copied from zap.NewExample, to make the log deterministic. |
| 17 | + encoderCfg := zapcore.EncoderConfig{ |
| 18 | + MessageKey: "msg", |
| 19 | + LevelKey: "level", |
| 20 | + NameKey: "logger", |
| 21 | + EncodeLevel: zapcore.LowercaseLevelEncoder, |
| 22 | + EncodeTime: zapcore.ISO8601TimeEncoder, |
| 23 | + EncodeDuration: zapcore.StringDurationEncoder, |
| 24 | + } |
| 25 | + core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), zapcore.AddSync(w), zap.DebugLevel) |
| 26 | + return zap.New(core) |
| 27 | +} |
| 28 | + |
| 29 | +func TestHTTPServerLogger(t *testing.T) { |
| 30 | + t.Run("no-panic", func(t *testing.T) { |
| 31 | + defer promtest.NewPrometheusMetricTest(t, "25192", httpServerLoggingCounter, prometheus.Labels{ |
| 32 | + "upstream_issue": "25192", |
| 33 | + }).CheckDelta(1) |
| 34 | + |
| 35 | + logger, err := httpServerLogger(nil, initBaseLogger(io.Discard)) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("httpServerLogger failed: %v", err) |
| 38 | + } |
| 39 | + // Make sure that empty map won't cause panics with matching log lines. |
| 40 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("suppressed", func(t *testing.T) { |
| 44 | + defer promtest.NewPrometheusMetricTest(t, "25192", httpServerLoggingCounter, prometheus.Labels{ |
| 45 | + "upstream_issue": "25192", |
| 46 | + }).CheckDelta(2) |
| 47 | + |
| 48 | + var sb strings.Builder |
| 49 | + logger, err := httpServerLogger(map[int64]float64{ |
| 50 | + 25192: 0, |
| 51 | + }, initBaseLogger(&sb)) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("httpServerLogger failed: %v", err) |
| 54 | + } |
| 55 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 56 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 57 | + if str := sb.String(); strings.TrimSpace(str) != "" { |
| 58 | + t.Errorf("Expected logs being suppressed, got %q", str) |
| 59 | + } |
| 60 | + |
| 61 | + sb.Reset() |
| 62 | + logger.Printf("Hello, world!") |
| 63 | + const want = `{"level":"warn","msg":"Hello, world!","from":"http-server"}` |
| 64 | + if got := sb.String(); strings.TrimSpace(got) != want { |
| 65 | + t.Errorf("Got %q, want %q", got, want) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("not-suppressed", func(t *testing.T) { |
| 70 | + defer promtest.NewPrometheusMetricTest(t, "25192", httpServerLoggingCounter, prometheus.Labels{ |
| 71 | + "upstream_issue": "25192", |
| 72 | + }).CheckDelta(1) |
| 73 | + |
| 74 | + var sb strings.Builder |
| 75 | + logger, err := httpServerLogger(map[int64]float64{ |
| 76 | + 25192: 1, |
| 77 | + }, initBaseLogger(&sb)) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("httpServerLogger failed: %v", err) |
| 80 | + } |
| 81 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 82 | + if got, want := sb.String(), `{"level":"warn","msg":"Hello, golang.org/issue/25192!","from":"http-server"}`; strings.TrimSpace(got) != want { |
| 83 | + t.Errorf("Got %q, want %q", got, want) |
| 84 | + } |
| 85 | + |
| 86 | + sb.Reset() |
| 87 | + logger.Printf("Hello, world!") |
| 88 | + if got, want := sb.String(), `{"level":"warn","msg":"Hello, world!","from":"http-server"}`; strings.TrimSpace(got) != want { |
| 89 | + t.Errorf("Got %q, want %q", got, want) |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
0 commit comments