|
| 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("suppressed", func(t *testing.T) { |
| 31 | + defer promtest.NewPrometheusMetricTest(t, "25192", httpServerLoggingCounter, prometheus.Labels{ |
| 32 | + "upstream_issue": "25192", |
| 33 | + }).CheckDelta(2) |
| 34 | + |
| 35 | + var sb strings.Builder |
| 36 | + logger, err := httpServerLogger(initBaseLogger(&sb), true) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("httpServerLogger failed: %v", err) |
| 39 | + } |
| 40 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 41 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 42 | + if str := sb.String(); strings.TrimSpace(str) != "" { |
| 43 | + t.Errorf("Expected logs being suppressed, got %q", str) |
| 44 | + } |
| 45 | + |
| 46 | + sb.Reset() |
| 47 | + logger.Printf("Hello, world!") |
| 48 | + const want = `{"level":"warn","msg":"Hello, world!","from":"http-server"}` |
| 49 | + if got := sb.String(); strings.TrimSpace(got) != want { |
| 50 | + t.Errorf("Got %q, want %q", got, want) |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("not-suppressed", func(t *testing.T) { |
| 55 | + defer promtest.NewPrometheusMetricTest(t, "25192", httpServerLoggingCounter, prometheus.Labels{ |
| 56 | + "upstream_issue": "25192", |
| 57 | + }).CheckDelta(1) |
| 58 | + |
| 59 | + var sb strings.Builder |
| 60 | + logger, err := httpServerLogger(initBaseLogger(&sb), false) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("httpServerLogger failed: %v", err) |
| 63 | + } |
| 64 | + logger.Printf("Hello, golang.org/issue/25192!") |
| 65 | + if got, want := sb.String(), `{"level":"warn","msg":"Hello, golang.org/issue/25192!","from":"http-server"}`; strings.TrimSpace(got) != want { |
| 66 | + t.Errorf("Got %q, want %q", got, want) |
| 67 | + } |
| 68 | + |
| 69 | + sb.Reset() |
| 70 | + logger.Printf("Hello, world!") |
| 71 | + if got, want := sb.String(), `{"level":"warn","msg":"Hello, world!","from":"http-server"}`; strings.TrimSpace(got) != want { |
| 72 | + t.Errorf("Got %q, want %q", got, want) |
| 73 | + } |
| 74 | + }) |
| 75 | +} |
0 commit comments