Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 7c59ff4

Browse files
authored
Add HTTP request debug logging (#300)
Signed-off-by: Bill Farner <[email protected]>
1 parent 67c52c9 commit 7c59ff4

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ endif
8484
$(call build_binary,infrakit,github.com/docker/infrakit/cmd/cli)
8585
$(call build_binary,infrakit-manager,github.com/docker/infrakit/cmd/manager)
8686
$(call build_binary,infrakit-group-default,github.com/docker/infrakit/cmd/group)
87-
$(call build_binary,infrakit-flavor-combo,github.com/docker/infrakit/example/flavor/combo)
88-
$(call build_binary,infrakit-flavor-swarm,github.com/docker/infrakit/example/flavor/swarm)
89-
$(call build_binary,infrakit-flavor-vanilla,github.com/docker/infrakit/example/flavor/vanilla)
90-
$(call build_binary,infrakit-flavor-zookeeper,github.com/docker/infrakit/example/flavor/zookeeper)
91-
$(call build_binary,infrakit-instance-file,github.com/docker/infrakit/example/instance/file)
92-
$(call build_binary,infrakit-instance-terraform,github.com/docker/infrakit/example/instance/terraform)
93-
$(call build_binary,infrakit-instance-vagrant,github.com/docker/infrakit/example/instance/vagrant)
87+
$(call build_binary,infrakit-flavor-combo,github.com/docker/infrakit/pkg/example/flavor/combo)
88+
$(call build_binary,infrakit-flavor-swarm,github.com/docker/infrakit/pkg/example/flavor/swarm)
89+
$(call build_binary,infrakit-flavor-vanilla,github.com/docker/infrakit/pkg/example/flavor/vanilla)
90+
$(call build_binary,infrakit-flavor-zookeeper,github.com/docker/infrakit/pkg/example/flavor/zookeeper)
91+
$(call build_binary,infrakit-instance-file,github.com/docker/infrakit/pkg/example/instance/file)
92+
$(call build_binary,infrakit-instance-terraform,github.com/docker/infrakit/pkg/example/instance/terraform)
93+
$(call build_binary,infrakit-instance-vagrant,github.com/docker/infrakit/pkg/example/instance/vagrant)
9494

9595
install:
9696
@echo "+ $@"

pkg/rpc/client/client.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package client
22

33
import (
44
"bytes"
5+
log "github.com/Sirupsen/logrus"
56
"github.com/gorilla/rpc/v2/json"
67
"net"
78
"net/http"
9+
"net/http/httputil"
810
)
911

1012
// Client is an HTTP client for sending JSON-RPC requests.
@@ -28,12 +30,32 @@ func (c Client) Call(method string, arg interface{}, result interface{}) error {
2830
return err
2931
}
3032

33+
req, err := http.NewRequest("POST", "http:///", bytes.NewReader(message))
34+
if err != nil {
35+
return err
36+
}
37+
req.Header.Set("Content-Type", "application/json")
38+
39+
requestData, err := httputil.DumpRequest(req, true)
40+
if err == nil {
41+
log.Debugf("Sending request %s", string(requestData))
42+
} else {
43+
log.Error(err)
44+
}
45+
3146
resp, err := c.http.Post("http://d/rpc", "application/json", bytes.NewReader(message))
3247
if err != nil {
3348
return err
3449
}
3550

3651
defer resp.Body.Close()
3752

53+
responseData, err := httputil.DumpResponse(resp, true)
54+
if err == nil {
55+
log.Debugf("Received response %s", string(responseData))
56+
} else {
57+
log.Error(err)
58+
}
59+
3860
return json.DecodeClientResponse(resp.Body, result)
3961
}

pkg/rpc/server/server.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"net/http"
99
"time"
1010

11-
"github.com/gorilla/handlers"
1211
"github.com/gorilla/rpc/v2"
1312
"github.com/gorilla/rpc/v2/json"
13+
"net/http/httptest"
14+
"net/http/httputil"
1415
)
1516

1617
// Stoppable support proactive stopping, and blocking until stopped.
@@ -31,6 +32,33 @@ func (s *stoppableServer) AwaitStopped() {
3132
<-s.server.StopChan()
3233
}
3334

35+
type loggingHandler struct {
36+
handler http.Handler
37+
}
38+
39+
func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
40+
requestData, err := httputil.DumpRequest(req, true)
41+
if err == nil {
42+
log.Debugf("Received request %s", string(requestData))
43+
} else {
44+
log.Error(err)
45+
}
46+
47+
recorder := httptest.NewRecorder()
48+
49+
h.handler.ServeHTTP(recorder, req)
50+
51+
responseData, err := httputil.DumpResponse(recorder.Result(), true)
52+
if err == nil {
53+
log.Debugf("Sending response %s", string(responseData))
54+
} else {
55+
log.Error(err)
56+
}
57+
58+
w.WriteHeader(recorder.Code)
59+
recorder.Body.WriteTo(w)
60+
}
61+
3462
// StartPluginAtPath starts an HTTP server listening on a unix socket at the specified path.
3563
// Returns a Stoppable that can be used to stop or block on the server.
3664
func StartPluginAtPath(socketPath string, receiver interface{}) (Stoppable, error) {
@@ -44,7 +72,7 @@ func StartPluginAtPath(socketPath string, receiver interface{}) (Stoppable, erro
4472
httpLog := log.New()
4573
httpLog.Level = log.GetLevel()
4674

47-
handler := handlers.LoggingHandler(httpLog.WriterLevel(log.DebugLevel), server)
75+
handler := loggingHandler{handler: server}
4876
gracefulServer := graceful.Server{
4977
Timeout: 10 * time.Second,
5078
Server: &http.Server{Addr: fmt.Sprintf("unix://%s", socketPath), Handler: handler},

0 commit comments

Comments
 (0)