Skip to content

Commit

Permalink
add Transport.LogKeys()
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbus committed Sep 6, 2018
1 parent 2fe94a4 commit 1b9650f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
18 changes: 18 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ func (t *HTTPTransport) RegisterEndpoints(m endpoint.Middleware) error {
return nil
}

var httpLogkeys = map[string]interface{}{
"http-method": kithttp.ContextKeyRequestMethod,
"http-uri": kithttp.ContextKeyRequestURI,
"http-path": kithttp.ContextKeyRequestPath,
"http-proto": kithttp.ContextKeyRequestProto,
"http-requesthost": kithttp.ContextKeyRequestHost,
"http-remote-addr": kithttp.ContextKeyRequestRemoteAddr,
"http-x-forwarded-for": kithttp.ContextKeyRequestXForwardedFor,
"http-x-forwarded-proto": kithttp.ContextKeyRequestXForwardedProto,
"http-user-agent": kithttp.ContextKeyRequestUserAgent,
"http-x-request-id": kithttp.ContextKeyRequestXRequestID,
}

// LogKeys returns the list of name key to context key mappings
func (t *HTTPTransport) LogKeys() map[string]interface{} {
return httpLogkeys
}

// Start starts the HTTP server.
func (t *HTTPTransport) Start(ctx context.Context) error {
t.svr = &http.Server{
Expand Down
33 changes: 11 additions & 22 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,45 @@ import (

"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
kithttp "github.com/go-kit/kit/transport/http"
)

// nopLogger is the default logger and does nothing.
type nopLogger struct{}

func (l *nopLogger) Log(keyvals ...interface{}) error { return nil }

var logkeys = map[string]interface{}{
"http-method": kithttp.ContextKeyRequestMethod,
"http-uri": kithttp.ContextKeyRequestURI,
"http-path": kithttp.ContextKeyRequestPath,
"http-proto": kithttp.ContextKeyRequestProto,
"http-requesthost": kithttp.ContextKeyRequestHost,
"http-remote-addr": kithttp.ContextKeyRequestRemoteAddr,
"http-x-forwarded-for": kithttp.ContextKeyRequestXForwardedFor,
"http-x-forwarded-proto": kithttp.ContextKeyRequestXForwardedProto,
"http-user-agent": kithttp.ContextKeyRequestUserAgent,
"http-x-request-id": kithttp.ContextKeyRequestXRequestID,
}

// Logger sets the logger.
func (s *Server) Logger(l log.Logger) *Server {
s.logger = l
return s
}

// LogContext defines the list of keys to add to all log lines.
// Available keys are : http-method, http-uri, http-path, http-proto, http-requesthost, http-remote-addr,
// http-x-forwarded-for, http-x-forwarded-proto, http-user-agent and http-x-request-id.
// Keys may vary depending on transport.
// Available keys for the http transport are : http-method, http-uri, http-path, http-proto, http-requesthost,
// http-remote-addr, http-x-forwarded-for, http-x-forwarded-proto, http-user-agent and http-x-request-id.
func (s *Server) LogContext(keys ...string) *Server {
s.logkeys = keys
return s
}

func (s *Server) addLoggerToContext(ctx context.Context) context.Context {
func (s *Server) addLoggerToContext(ctx context.Context, keys map[string]interface{}) context.Context {
l := s.logger
for _, k := range s.logkeys {
if val, ok := ctx.Value(logkeys[k]).(string); ok && val != "" {
l = log.With(l, k, val)
if keys != nil {
for _, k := range s.logkeys {
if val, ok := ctx.Value(keys[k]).(string); ok && val != "" {
l = log.With(l, k, val)
}
}
}
return context.WithValue(ctx, logKey, l)
}

func (s *Server) addLoggerToContextMiddleware(m endpoint.Middleware) endpoint.Middleware {
func (s *Server) addLoggerToContextMiddleware(m endpoint.Middleware, t Transport) endpoint.Middleware {
return func(e endpoint.Endpoint) endpoint.Endpoint {
e = m(e)
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
return e(s.addLoggerToContext(ctx), request)
return e(s.addLoggerToContext(ctx, t.LogKeys()), request)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func NewServer(t ...Transport) *Server {

// Run starts the server.
func (s *Server) Run(ctx context.Context) error {
ctx = s.addLoggerToContext(ctx)
m := s.addLoggerToContextMiddleware(s.middleware)
ctx = s.addLoggerToContext(ctx, nil)
for _, t := range s.transports {
m := s.addLoggerToContextMiddleware(s.middleware, t)
if err := t.RegisterEndpoints(m); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
type Transport interface {
// RegisterEndpoints registers all endpoints. Endpoints needs to be wrapped with the specified middleware.
RegisterEndpoints(m endpoint.Middleware) error
// LogKeys returns the list of name key to context key mappings for logging
LogKeys() map[string]interface{}
// Start starts the transport.
Start(ctx context.Context) error
// Shutdown shutdowns the transport.
Expand Down

0 comments on commit 1b9650f

Please sign in to comment.