Skip to content

Commit 9c2a2b4

Browse files
revert to reading body and using unmarshal because of issue gin-gonic/gin#1078; skip logging liveness and readiness
1 parent 1a3a7cf commit 9c2a2b4

File tree

4 files changed

+41
-55
lines changed

4 files changed

+41
-55
lines changed

bitbucket/bitbucketEventHandler.go

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bitbucket
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56
"net/http"
67

@@ -35,18 +36,17 @@ func (h *eventHandlerImpl) Handle(c *gin.Context) {
3536
eventType := c.GetHeader("X-Event-Key")
3637
h.prometheusInboundEventTotals.With(prometheus.Labels{"event": eventType, "source": "bitbucket"}).Inc()
3738

39+
body, err := ioutil.ReadAll(c.Request.Body)
40+
if err != nil {
41+
log.Error().Err(err).Msg("Reading body from Bitbucket webhook failed")
42+
c.String(http.StatusInternalServerError, "Reading body from Bitbucket webhook failed")
43+
return
44+
}
45+
3846
// unmarshal json body
3947
var b interface{}
40-
err := c.BindJSON(&b)
48+
err = json.Unmarshal(body, &b)
4149
if err != nil {
42-
43-
body, err := ioutil.ReadAll(c.Request.Body)
44-
if err != nil {
45-
log.Error().Err(err).Msg("Reading body from Bitbucket webhook failed")
46-
c.String(http.StatusInternalServerError, "Reading body from Bitbucket webhook failed")
47-
return
48-
}
49-
5050
log.Error().Err(err).Str("body", string(body)).Msg("Deserializing body from Bitbucket webhook failed")
5151
c.String(http.StatusInternalServerError, "Deserializing body from Github webhook failed")
5252
return
@@ -64,16 +64,8 @@ func (h *eventHandlerImpl) Handle(c *gin.Context) {
6464

6565
// unmarshal json body
6666
var pushEvent RepositoryPushEvent
67-
err := c.BindJSON(&pushEvent)
67+
err := json.Unmarshal(body, &pushEvent)
6868
if err != nil {
69-
70-
body, err := ioutil.ReadAll(c.Request.Body)
71-
if err != nil {
72-
log.Error().Err(err).Msg("Reading body from Bitbucket webhook failed")
73-
c.String(http.StatusInternalServerError, "Reading body from Bitbucket webhook failed")
74-
return
75-
}
76-
7769
log.Error().Err(err).Str("body", string(body)).Msg("Deserializing body to BitbucketRepositoryPushEvent failed")
7870
return
7971
}

estafette/estafetteEventHandler.go

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package estafette
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io/ioutil"
67
"net/http"
@@ -45,35 +46,26 @@ func (h *eventHandlerImpl) Handle(c *gin.Context) {
4546
eventType := c.GetHeader("X-Estafette-Event")
4647
h.prometheusInboundEventTotals.With(prometheus.Labels{"event": eventType, "source": "estafette"}).Inc()
4748

49+
body, err := ioutil.ReadAll(c.Request.Body)
50+
if err != nil {
51+
log.Error().Err(err).Msg("Reading body from Estafette 'build finished' event failed")
52+
c.String(http.StatusInternalServerError, "Reading body from Estafette 'build finished' event failed")
53+
return
54+
}
55+
4856
// unmarshal json body
4957
var b interface{}
50-
err := c.BindJSON(&b)
58+
err = json.Unmarshal(body, &b)
5159
if err != nil {
52-
53-
body, err := ioutil.ReadAll(c.Request.Body)
54-
if err != nil {
55-
log.Error().Err(err).Msg("Reading body from Estafette 'build finished' event failed")
56-
c.String(http.StatusInternalServerError, "Reading body from Estafette 'build finished' event failed")
57-
return
58-
}
59-
6060
log.Error().Err(err).Str("body", string(body)).Msg("Deserializing body from Estafette 'build finished' event failed")
6161
c.String(http.StatusInternalServerError, "Deserializing body from Estafette 'build finished' event failed")
6262
return
6363
}
6464

6565
// unmarshal json body
6666
var ciBuilderEvent CiBuilderEvent
67-
err = c.BindJSON(&ciBuilderEvent)
67+
err = json.Unmarshal(body, &ciBuilderEvent)
6868
if err != nil {
69-
70-
body, err := ioutil.ReadAll(c.Request.Body)
71-
if err != nil {
72-
log.Error().Err(err).Msg("Reading body from Estafette 'build finished' event failed")
73-
c.String(http.StatusInternalServerError, "Reading body from Estafette 'build finished' event failed")
74-
return
75-
}
76-
7769
log.Error().Err(err).Str("body", string(body)).Msg("Deserializing body to EstafetteCiBuilderEvent failed")
7870
return
7971
}

github/githubEventHandler.go

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56
"net/http"
67

@@ -34,18 +35,17 @@ func (h *eventHandlerImpl) Handle(c *gin.Context) {
3435
eventType := c.GetHeader("X-GitHub-Event")
3536
h.prometheusInboundEventTotals.With(prometheus.Labels{"event": eventType, "source": "github"}).Inc()
3637

38+
body, err := ioutil.ReadAll(c.Request.Body)
39+
if err != nil {
40+
log.Error().Err(err).Msg("Reading body from Github webhook failed")
41+
c.String(http.StatusInternalServerError, "Reading body from Github webhook failed")
42+
return
43+
}
44+
3745
// unmarshal json body
3846
var b interface{}
39-
err := c.BindJSON(&b)
47+
err = json.Unmarshal(body, &b)
4048
if err != nil {
41-
42-
body, err := ioutil.ReadAll(c.Request.Body)
43-
if err != nil {
44-
log.Error().Err(err).Msg("Reading body from Github webhook failed")
45-
c.String(http.StatusInternalServerError, "Reading body from Github webhook failed")
46-
return
47-
}
48-
4949
log.Error().Err(err).Str("body", string(body)).Msg("Deserializing body from Github webhook failed")
5050
c.String(http.StatusInternalServerError, "Deserializing body from Github webhook failed")
5151
return
@@ -63,16 +63,8 @@ func (h *eventHandlerImpl) Handle(c *gin.Context) {
6363

6464
// unmarshal json body
6565
var pushEvent PushEvent
66-
err := c.BindJSON(&pushEvent)
66+
err := json.Unmarshal(body, &pushEvent)
6767
if err != nil {
68-
69-
body, err := ioutil.ReadAll(c.Request.Body)
70-
if err != nil {
71-
log.Error().Err(err).Msg("Reading body from Github webhook failed")
72-
c.String(http.StatusInternalServerError, "Reading body from Github webhook failed")
73-
return
74-
}
75-
7668
log.Error().Err(err).Str("body", string(body)).Msg("Deserializing body to GithubPushEvent failed")
7769
return
7870
}

zerologMiddleware.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import (
1010
// ZeroLogMiddleware logs gin requests via zerolog
1111
func ZeroLogMiddleware() gin.HandlerFunc {
1212
return func(c *gin.Context) {
13+
14+
path := c.Request.URL.Path
15+
if path == "/liveness" || path == "/readiness" {
16+
// don't log these requests, only execute them
17+
c.Next()
18+
return
19+
}
20+
1321
// start timer
1422
start := time.Now()
1523

@@ -19,8 +27,8 @@ func ZeroLogMiddleware() gin.HandlerFunc {
1927
// stop timer
2028
end := time.Now()
2129

22-
path := c.Request.URL.Path
2330
raw := c.Request.URL.RawQuery
31+
headers := c.Header
2432
latency := end.Sub(start)
2533
clientIP := c.ClientIP()
2634
method := c.Request.Method
@@ -37,6 +45,7 @@ func ZeroLogMiddleware() gin.HandlerFunc {
3745
Dur("latencyMs", latency).
3846
Str("clientIP", clientIP).
3947
Str("path", path).
48+
Interface("headers", headers).
4049
Msgf("[GIN] %3d %13v %15s %-7s %s", statusCode, latency, clientIP, method, path)
4150

4251
} else {
@@ -46,6 +55,7 @@ func ZeroLogMiddleware() gin.HandlerFunc {
4655
Dur("latencyMs", latency).
4756
Str("clientIP", clientIP).
4857
Str("path", path).
58+
Interface("headers", headers).
4959
Msgf("[GIN] %3d %13v %15s %-7s %s", statusCode, latency, clientIP, method, path)
5060

5161
}

0 commit comments

Comments
 (0)