-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from xmidt-org/zap-logger
Replaced webpa-common/logging with zap logger
- Loading branch information
Showing
6 changed files
with
881 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/textproto" | ||
"strings" | ||
|
||
"github.com/xmidt-org/candlelight" | ||
"github.com/xmidt-org/sallust" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// 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 != "" { | ||
filtered.Del("Authorization") | ||
parts := strings.Split(authHeader, " ") | ||
if len(parts) == 2 { | ||
filtered.Set("Authorization-Type", parts[0]) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func setLogger(logger *zap.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.Context() | ||
ctx = addFieldsToLog(ctx, logger, kvs) | ||
delegate.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} | ||
} | ||
|
||
func addFieldsToLog(ctx context.Context, logger *zap.Logger, kvs []interface{}) context.Context { | ||
|
||
for i := 0; i <= len(kvs)-2; i += 2 { | ||
logger = logger.With(zap.Any(fmt.Sprint(kvs[i]), kvs[i+1])) | ||
} | ||
|
||
return sallust.With(ctx, logger) | ||
|
||
} | ||
|
||
func header(headerName, keyName string) LoggerFunc { | ||
headerName = textproto.CanonicalMIMEHeaderKey(headerName) | ||
|
||
return func(kv []interface{}, request *http.Request) []interface{} { | ||
values := request.Header[headerName] | ||
switch len(values) { | ||
case 0: | ||
return append(kv, keyName, "") | ||
case 1: | ||
return append(kv, keyName, values[0]) | ||
default: | ||
return append(kv, keyName, values) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xmidt-org/sallust" | ||
"go.uber.org/zap/zaptest" | ||
) | ||
|
||
func TestSanitizeHeaders(t *testing.T) { | ||
testCases := []struct { | ||
Description string | ||
Input http.Header | ||
Expected http.Header | ||
}{ | ||
{ | ||
Description: "Filtered", | ||
Input: http.Header{"Authorization": []string{"Basic xyz"}, "HeaderA": []string{"x"}}, | ||
Expected: http.Header{"HeaderA": []string{"x"}, "Authorization-Type": []string{"Basic"}}, | ||
}, | ||
{ | ||
Description: "Handled human error", | ||
Input: http.Header{"Authorization": []string{"BasicXYZ"}, "HeaderB": []string{"y"}}, | ||
Expected: http.Header{"HeaderB": []string{"y"}}, | ||
}, | ||
{ | ||
Description: "Not a perfect system", | ||
Input: http.Header{"Authorization": []string{"MySecret IWantToLeakIt"}}, | ||
Expected: http.Header{"Authorization-Type": []string{"MySecret"}}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Description, func(t *testing.T) { | ||
assert := assert.New(t) | ||
actual := sanitizeHeaders(tc.Input) | ||
assert.Equal(tc.Expected, actual) | ||
}) | ||
|
||
} | ||
} | ||
|
||
func TestAddFieldToLog_NotNil(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
kvs []interface{} | ||
}{ | ||
{ | ||
name: "header", | ||
kvs: []interface{}{"headerName", "petasos"}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
testCtx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
testLogger := zaptest.NewLogger(t) | ||
|
||
assert := assert.New(t) | ||
ctx := addFieldsToLog(testCtx, testLogger, tc.kvs) | ||
|
||
assert.NotNil(sallust.Get(ctx)) | ||
|
||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.