Skip to content

Commit

Permalink
Merge pull request #166 from xmidt-org/fixOverridingLogger
Browse files Browse the repository at this point in the history
Fix override of logger in request context
  • Loading branch information
renaz6 authored Mar 21, 2022
2 parents a367732 + b6fd6cb commit 0e55518
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
- Updated spec file and rpkg version macro to be able to choose when the 'v' is included in the version. [#163](https://github.com/xmidt-org/scytale/pull/163)
- Reconfigured the Bascule Logger settings so that the logger isn't overwritten [#166](https://github.com/xmidt-org/scytale/pull/166)

## [v0.6.1]
- Fixed url parsing bug where we were leaving a '/'. [#161](https://github.com/xmidt-org/scytale/pull/161)
Expand Down
17 changes: 16 additions & 1 deletion basculeLogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/xmidt-org/webpa-common/v2/logging"
)

// LoggerFunc is a strategy for adding key/value pairs (possibly) based on an HTTP request.
// Functions of this type must append key/value pairs to the supplied slice and then return
// the new slice.
type LoggerFunc func([]interface{}, *http.Request) []interface{}

func sanitizeHeaders(headers http.Header) (filtered http.Header) {
filtered = headers.Clone()
if authHeader := filtered.Get("Authorization"); authHeader != "" {
Expand All @@ -22,11 +27,21 @@ func sanitizeHeaders(headers http.Header) (filtered http.Header) {
return
}

func setLogger(logger log.Logger) func(delegate http.Handler) http.Handler {
func setLogger(logger log.Logger, lf ...LoggerFunc) func(delegate http.Handler) http.Handler {

if logger == nil {
panic("The base Logger cannot be nil")
}

return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
kvs := []interface{}{"requestHeaders", sanitizeHeaders(r.Header), "requestURL", r.URL.EscapedPath(), "method", r.Method}
for _, f := range lf {
if f != nil {
kvs = f(kvs, r)
}
}
kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs)
ctx := r.WithContext(logging.WithLogger(r.Context(), log.With(logger, kvs...)))
delegate.ServeHTTP(w, ctx)
Expand Down
39 changes: 16 additions & 23 deletions primaryHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"github.com/xmidt-org/webpa-common/v2/basculechecks"
"github.com/xmidt-org/webpa-common/v2/basculemetrics"
"github.com/xmidt-org/webpa-common/v2/logging"
"github.com/xmidt-org/webpa-common/v2/logging/logginghttp"
"github.com/xmidt-org/webpa-common/v2/service"
"github.com/xmidt-org/webpa-common/v2/service/monitor"
"github.com/xmidt-org/webpa-common/v2/xhttp"
Expand Down Expand Up @@ -158,11 +157,25 @@ func authChain(v *viper.Viper, logger log.Logger, registry xmetrics.Registry) (a
basculehttp.WithEErrorResponseFunc(listener.OnErrorResponse),
)

constructors := []alice.Constructor{setLogger(logger), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener)}
constructors := []alice.Constructor{setLogger(logger, extractIntendedDestination), authConstructor, authEnforcer, basculehttp.NewListenerDecorator(listener)}

return alice.New(constructors...), nil
}

// custom logger func that extracts the intended destination of requests
func extractIntendedDestination(kv []interface{}, request *http.Request) []interface{} {
if deviceName := request.Header.Get("X-Webpa-Device-Name"); len(deviceName) > 0 {
return append(kv, "X-Webpa-Device-Name", deviceName)
}

if variables := mux.Vars(request); len(variables) > 0 {
if deviceID := variables["deviceID"]; len(deviceID) > 0 {
return append(kv, "deviceID", deviceID)
}
}
return kv
}

// createEndpoints examines the configuration and produces an appropriate fanout.Endpoints, either using the configured
// endpoints or service discovery.
func createEndpoints(logger log.Logger, cfg fanout.Configuration, registry xmetrics.Registry, e service.Environment) (fanout.Endpoints, error) {
Expand Down Expand Up @@ -298,27 +311,7 @@ func NewPrimaryHandler(logger log.Logger, v *viper.Viper, registry xmetrics.Regi
xhttp.WriteError(response, http.StatusBadRequest, "Invalid endpoint")
})

fanoutChain := fanout.NewChain(
cfg,
logginghttp.SetLogger(
logger,
logginghttp.RequestInfo,

// custom logger func that extracts the intended destination of requests
func(kv []interface{}, request *http.Request) []interface{} {
if deviceName := request.Header.Get("X-Webpa-Device-Name"); len(deviceName) > 0 {
return append(kv, "X-Webpa-Device-Name", deviceName)
}

if variables := mux.Vars(request); len(variables) > 0 {
if deviceID := variables["deviceID"]; len(deviceID) > 0 {
return append(kv, "deviceID", deviceID)
}
}
return kv
}, candlelight.InjectTraceInfoInLogger(),
),
)
fanoutChain := fanout.NewChain(cfg)

HTTPFanoutHandler := fanoutChain.Then(
fanout.New(
Expand Down

0 comments on commit 0e55518

Please sign in to comment.