Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging improvement #288

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pkg/middleware/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/optimizely/agent/pkg/optimizely"

Expand Down Expand Up @@ -49,11 +48,9 @@ func GetOptlyClient(r *http.Request) (*optimizely.OptlyClient, error) {

// GetLogger gets the logger with some info coming from http request
func GetLogger(r *http.Request) *zerolog.Logger {
sdkKey := r.Header.Get(OptlySDKHeader)
reqID := r.Header.Get(OptlyRequestHeader)

sdkKeySplit := strings.Split(sdkKey, ":")
logger := log.With().Str("sdkKey", sdkKeySplit[0]).Str("requestId", reqID).Logger()
logger := log.With().Str("requestId", reqID).Logger()
return &logger
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/middleware/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,11 @@ func TestGetLogger(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)

req.Header.Set(OptlyRequestHeader, "12345")
req.Header.Set(OptlySDKHeader, "some_key")
logger := GetLogger(req)
newLogger := logger.Output(out)
newLogger.Info().Msg("some_message")

assert.Contains(t, out.String(), `"requestId":"12345"`)
assert.Contains(t, out.String(), `"sdkKey":"some_key"`)

}

func TestGetFeature(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/optimizely/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewCache(ctx context.Context, conf config.ClientConfig, metricsRegistry *Me
func (c *OptlyCache) Init(sdkKeys []string) {
for _, sdkKey := range sdkKeys {
if _, err := c.GetClient(sdkKey); err != nil {
log.Warn().Str("sdkKey", sdkKey).Msg("Failed to initialize Optimizely Client.")
log.Warn().Msg("Failed to initialize Optimizely Client.")
}
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func defaultLoader(
var configManager SyncedConfigManager

if !validator(clientKey) {
log.Warn().Msgf("failed to validate sdk key: %q", sdkKey)
log.Warn().Msgf("failed to validate sdk key")
return &OptlyClient{}, ErrValidationFailure
}

Expand All @@ -158,7 +158,7 @@ func defaultLoader(
datafileAccessToken = clientKeySplit[1]
}

log.Info().Str("sdkKey", sdkKey).Msg("Loading Optimizely instance")
log.Info().Msg("Loading Optimizely instance")

if datafileAccessToken != "" {
configManager = pcFactory(
Expand Down
1 change: 1 addition & 0 deletions pkg/routers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func WithAPIRouter(opt *APIOptions, r chi.Router) {
r.Use(chimw.Throttle(opt.maxConns))
}

r.Use(chimw.Logger)
r.Use(middleware.SetTime)
r.Use(render.SetContentType(render.ContentTypeJSON), middleware.SetRequestID)

Expand Down