Skip to content

Commit 0b240fe

Browse files
authored
Merge pull request #20 from snabble/do-not-log-health-request
Silence health requests.
2 parents 04412ae + 7b35db4 commit 0b240fe

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

helper.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,22 @@ func Access(r *http.Request, start time.Time, statusCode int) {
8484
}
8585

8686
if statusCode >= 200 && statusCode <= 399 {
87-
e.Info(msg)
87+
if isHealthRequest(r) {
88+
e.Debug(msg)
89+
} else {
90+
e.Info(msg)
91+
}
8892
} else if statusCode >= 400 && statusCode <= 499 {
8993
e.Warn(msg)
9094
} else {
9195
e.Error(msg)
9296
}
9397
}
9498

99+
func isHealthRequest(r *http.Request) bool {
100+
return r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/health")
101+
}
102+
95103
// AccessError logs an error while accessing
96104
func AccessError(r *http.Request, start time.Time, err error) {
97105
e := access(r, start, 0, err)

log_middleware_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func Test_LogMiddleware_GeneratesTracesAndSubSpans(t *testing.T) {
3939
w.WriteHeader(200)
4040
}))
4141

42-
r, _ := http.NewRequest("GET", "https://www.example.org/foo", nil)
42+
r, _ := http.NewRequest(http.MethodGet, "https://www.example.org/foo", nil)
4343

4444
lm.ServeHTTP(httptest.NewRecorder(), r)
4545

@@ -77,7 +77,7 @@ func Test_LogMiddleware_Panic(t *testing.T) {
7777
i[100]++
7878
}))
7979

80-
r, _ := http.NewRequest("GET", "http://www.example.org/foo", nil)
80+
r, _ := http.NewRequest(http.MethodGet, "http://www.example.org/foo", nil)
8181

8282
lm.ServeHTTP(httptest.NewRecorder(), r)
8383

@@ -96,12 +96,12 @@ func Test_LogMiddleware_Log_implicit200(t *testing.T) {
9696
b := bytes.NewBuffer(nil)
9797
Log.Out = b
9898

99-
// and a handler which gets an 200er code implicitly
99+
// and a handler which gets a 200er code implicitly
100100
lm := NewLogMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
101101
_, _ = w.Write([]byte("hello"))
102102
}))
103103

104-
r, _ := http.NewRequest("GET", "http://www.example.org/foo", nil)
104+
r, _ := http.NewRequest(http.MethodGet, "http://www.example.org/foo", nil)
105105

106106
lm.ServeHTTP(httptest.NewRecorder(), r)
107107

@@ -112,6 +112,36 @@ func Test_LogMiddleware_Log_implicit200(t *testing.T) {
112112
a.Equal("info", data.Level)
113113
}
114114

115+
func Test_LogMiddleware_Log_HealthRequestAreNotLogged(t *testing.T) {
116+
tests := []struct {
117+
name string
118+
method string
119+
url string
120+
containsLogMsg bool
121+
}{
122+
{"health on base path", http.MethodGet, "http://www.example.org/health", false},
123+
{"health on sub path", http.MethodGet, "http://www.example.org/sub/health", false},
124+
{"post to health", http.MethodPost, "http://www.example.org/sub/health", true},
125+
{"health in query param", http.MethodGet, "http://www.example.org/sub?health", true},
126+
{"health somewhere in path", http.MethodGet, "http://www.example.org/health/more", true},
127+
}
128+
for _, test := range tests {
129+
t.Run(test.name, func(t *testing.T) {
130+
logBuffer := bytes.NewBuffer(nil)
131+
Log.Out = logBuffer
132+
133+
lm := NewLogMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
134+
_, _ = w.Write([]byte("OK++"))
135+
}))
136+
137+
r, _ := http.NewRequest(test.method, test.url, nil)
138+
139+
lm.ServeHTTP(httptest.NewRecorder(), r)
140+
assert.Equal(t, test.containsLogMsg, logBuffer.Len() > 0)
141+
})
142+
}
143+
}
144+
115145
func Test_LogMiddleware_Log_404(t *testing.T) {
116146
a := assert.New(t)
117147

@@ -124,7 +154,7 @@ func Test_LogMiddleware_Log_404(t *testing.T) {
124154
w.WriteHeader(404)
125155
}))
126156

127-
r, _ := http.NewRequest("GET", "http://www.example.org/foo", nil)
157+
r, _ := http.NewRequest(http.MethodGet, "http://www.example.org/foo", nil)
128158

129159
lm.ServeHTTP(httptest.NewRecorder(), r)
130160

0 commit comments

Comments
 (0)