feature: extend logging with remote address from request context

This commit is contained in:
Johannes Senner 2020-08-11 11:45:29 +02:00
parent 6c2a61b171
commit d4750cd727
6 changed files with 25 additions and 13 deletions

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/pkg/errors v0.8.1 // indirect github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_golang v0.9.2 github.com/prometheus/client_golang v0.9.2
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce
go.uber.org/atomic v1.4.0 // indirect go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0 go.uber.org/zap v1.10.0

2
go.sum
View File

@ -30,6 +30,8 @@ github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc=
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU= go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=

View File

@ -2,6 +2,7 @@ package repo
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -93,15 +94,16 @@ func (repo *Repo) GetURIs(dimension string, ids []string) map[string]string {
} }
// GetNodes get nodes // GetNodes get nodes
func (repo *Repo) GetNodes(r *requests.Nodes) map[string]*content.Node { func (repo *Repo) GetNodes(ctx context.Context, r *requests.Nodes) map[string]*content.Node {
return repo.getNodes(r.Nodes, r.Env) return repo.getNodes(ctx, r.Nodes, r.Env)
} }
func (repo *Repo) getNodes(nodeRequests map[string]*requests.Node, env *requests.Env) map[string]*content.Node { func (repo *Repo) getNodes(ctx context.Context, nodeRequests map[string]*requests.Node, env *requests.Env) map[string]*content.Node {
var ( var (
nodes = map[string]*content.Node{} nodes = map[string]*content.Node{}
path = []*content.Item{} path = []*content.Item{}
remoteAddr = ctx.Value("remoteAddr").(string)
) )
for nodeName, nodeRequest := range nodeRequests { for nodeName, nodeRequest := range nodeRequests {
@ -140,6 +142,7 @@ func (repo *Repo) getNodes(nodeRequests map[string]*requests.Node, env *requests
nodes[nodeName] = repo.getNode(treeNode, nodeRequest.Expand, nodeRequest.MimeTypes, path, 0, groups, nodeRequest.DataFields, nodeRequest.ExposeHiddenNodes) nodes[nodeName] = repo.getNode(treeNode, nodeRequest.Expand, nodeRequest.MimeTypes, path, 0, groups, nodeRequest.DataFields, nodeRequest.ExposeHiddenNodes)
} else { } else {
Log.Error("an invalid tree node was requested", Log.Error("an invalid tree node was requested",
zap.String("remoteAddr", remoteAddr),
zap.String("nodeName", nodeName), zap.String("nodeName", nodeName),
zap.String("ID", nodeRequest.ID), zap.String("ID", nodeRequest.ID),
) )
@ -157,7 +160,7 @@ func (repo *Repo) getNodes(nodeRequests map[string]*requests.Node, env *requests
// In the second step it collects the requested nodes. // In the second step it collects the requested nodes.
// //
// those two steps are independent. // those two steps are independent.
func (repo *Repo) GetContent(r *requests.Content) (c *content.SiteContent, err error) { func (repo *Repo) GetContent(ctx context.Context, r *requests.Content) (c *content.SiteContent, err error) {
// add more input validation // add more input validation
err = repo.validateContentRequest(r) err = repo.validateContentRequest(r)
if err != nil { if err != nil {
@ -206,7 +209,7 @@ func (repo *Repo) GetContent(r *requests.Content) (c *content.SiteContent, err e
node.Dimension = resolvedDimension node.Dimension = resolvedDimension
} }
} }
c.Nodes = repo.getNodes(r.Nodes, r.Env) c.Nodes = repo.getNodes(ctx, r.Nodes, r.Env)
return c, nil return c, nil
} }

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
@ -12,7 +13,7 @@ import (
"github.com/foomo/contentserver/status" "github.com/foomo/contentserver/status"
) )
func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, source string) (replyBytes []byte, err error) { func handleRequest(ctx context.Context, r *repo.Repo, handler Handler, jsonBytes []byte, source string) (replyBytes []byte, err error) {
var ( var (
reply interface{} reply interface{}
@ -41,12 +42,12 @@ func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, source strin
case HandlerGetContent: case HandlerGetContent:
contentRequest := &requests.Content{} contentRequest := &requests.Content{}
processIfJSONIsOk(json.Unmarshal(jsonBytes, &contentRequest), func() { processIfJSONIsOk(json.Unmarshal(jsonBytes, &contentRequest), func() {
reply, apiErr = r.GetContent(contentRequest) reply, apiErr = r.GetContent(ctx, contentRequest)
}) })
case HandlerGetNodes: case HandlerGetNodes:
nodesRequest := &requests.Nodes{} nodesRequest := &requests.Nodes{}
processIfJSONIsOk(json.Unmarshal(jsonBytes, &nodesRequest), func() { processIfJSONIsOk(json.Unmarshal(jsonBytes, &nodesRequest), func() {
reply = r.GetNodes(nodesRequest) reply = r.GetNodes(ctx, nodesRequest)
}) })
case HandlerUpdate: case HandlerUpdate:
updateRequest := &requests.Update{} updateRequest := &requests.Update{}

View File

@ -2,6 +2,7 @@ package server
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"net" "net"
@ -56,8 +57,8 @@ func (s *socketServer) execute(handler Handler, jsonBytes []byte) (reply []byte)
addMetrics(handler, start, nil, nil, sourceSocketServer) addMetrics(handler, start, nil, nil, sourceSocketServer)
return b.Bytes() return b.Bytes()
} }
ctx := context.WithValue(context.TODO(), "remoteAddr", "socket") // TODO: set socket remote address
reply, handlingError := handleRequest(s.repo, handler, jsonBytes, sourceSocketServer) reply, handlingError := handleRequest(ctx, s.repo, handler, jsonBytes, sourceSocketServer)
if handlingError != nil { if handlingError != nil {
Log.Error("socketServer.execute failed", zap.Error(handlingError)) Log.Error("socketServer.execute failed", zap.Error(handlingError))
} }

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -11,6 +12,7 @@ import (
. "github.com/foomo/contentserver/logger" . "github.com/foomo/contentserver/logger"
"github.com/foomo/contentserver/repo" "github.com/foomo/contentserver/repo"
"github.com/tomasen/realip"
) )
const sourceWebserver = "webserver" const sourceWebserver = "webserver"
@ -53,7 +55,9 @@ func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
addMetrics(h, start, nil, nil, sourceWebserver) addMetrics(h, start, nil, nil, sourceWebserver)
return return
} }
reply, errReply := handleRequest(s.r, h, jsonBytes, "webserver") ip := realip.FromRequest(r)
ctx := context.WithValue(r.Context(), "remoteAddr", ip)
reply, errReply := handleRequest(ctx, s.r, h, jsonBytes, "webserver")
if errReply != nil { if errReply != nil {
http.Error(w, errReply.Error(), http.StatusInternalServerError) http.Error(w, errReply.Error(), http.StatusInternalServerError)
return return