Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Pulak Bhowmick committed Dec 12, 2023
1 parent 1baadbf commit e770ccd
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 41 deletions.
4 changes: 1 addition & 3 deletions pkg/handlers/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
"github.com/optimizely/agent/pkg/optimizely"
Expand All @@ -48,7 +47,6 @@ func Activate(w http.ResponseWriter, r *http.Request) {
}

logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

uc, err := getUserContext(r)
if err != nil {
Expand Down Expand Up @@ -110,7 +108,7 @@ func Activate(w http.ResponseWriter, r *http.Request) {
}

decisions = filterDecisions(r, decisions)
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msgf("Made activate decisions for user %s", uc.ID)
logger.Info().Msgf("Made activate decisions for user %s", uc.ID)
render.JSON(w, r, decisions)
}

Expand Down
7 changes: 2 additions & 5 deletions pkg/handlers/decide.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
"github.com/optimizely/go-sdk/pkg/client"
Expand Down Expand Up @@ -63,8 +62,6 @@ func Decide(w http.ResponseWriter, r *http.Request) {
return
}

span := trace.SpanFromContext(r.Context())

db, err := getUserContextWithOptions(r)
if err != nil {
RenderError(err, http.StatusBadRequest, w, r)
Expand Down Expand Up @@ -110,7 +107,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
key := keys[0]
logger.Debug().Str("featureKey", key).Msg("fetching feature decision")
d := optimizelyUserContext.Decide(key, decideOptions)
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msgf("Feature %q is enabled for user %s? %t", d.FlagKey, d.UserContext.UserID, d.Enabled)
logger.Info().Msgf("Feature %q is enabled for user %s? %t", d.FlagKey, d.UserContext.UserID, d.Enabled)
decideOut := DecideOut{d, d.Variables.ToMap()}
render.JSON(w, r, decideOut)
return
Expand All @@ -123,7 +120,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
for _, d := range decides {
decideOut := DecideOut{d, d.Variables.ToMap()}
decideOuts = append(decideOuts, decideOut)
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msgf("Feature %q is enabled for user %s? %t", d.FlagKey, d.UserContext.UserID, d.Enabled)
logger.Info().Msgf("Feature %q is enabled for user %s? %t", d.FlagKey, d.UserContext.UserID, d.Enabled)
}
render.JSON(w, r, decideOuts)
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/handlers/get_datafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
)
Expand All @@ -36,7 +35,6 @@ func GetDatafile(w http.ResponseWriter, r *http.Request) {
}

logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

datafile := optlyClient.GetOptimizelyConfig().GetDatafile()
var raw map[string]interface{}
Expand All @@ -45,6 +43,6 @@ func GetDatafile(w http.ResponseWriter, r *http.Request) {
return
}

logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg("Successfully returned datafile")
logger.Info().Msg("Successfully returned datafile")
render.JSON(w, r, raw)
}
4 changes: 1 addition & 3 deletions pkg/handlers/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
)
Expand All @@ -49,7 +48,6 @@ func Lookup(w http.ResponseWriter, r *http.Request) {
}

logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

if optlyClient.UserProfileService == nil {
RenderError(ErrNoUPS, http.StatusInternalServerError, w, r)
Expand Down Expand Up @@ -79,6 +77,6 @@ func Lookup(w http.ResponseWriter, r *http.Request) {
experimentBucketMap[k.ExperimentID] = map[string]interface{}{k.Field: v}
}
lookupResponse.ExperimentBucketMap = experimentBucketMap
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msgf("Looked up user profile for user %s", body.UserID)
logger.Info().Msgf("Looked up user profile for user %s", body.UserID)
render.JSON(w, r, lookupResponse)
}
4 changes: 1 addition & 3 deletions pkg/handlers/optimizely_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
)
Expand All @@ -35,9 +34,8 @@ func OptimizelyConfig(w http.ResponseWriter, r *http.Request) {
}

logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

conf := optlyClient.GetOptimizelyConfig()
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg("Successfully returned OptimizelyConfig")
logger.Info().Msg("Successfully returned OptimizelyConfig")
render.JSON(w, r, conf)
}
5 changes: 1 addition & 4 deletions pkg/handlers/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
)
Expand All @@ -43,8 +42,6 @@ func Override(w http.ResponseWriter, r *http.Request) {
return
}

span := trace.SpanFromContext(r.Context())

var body OverrideBody
if parseErr := ParseRequestBody(r, &body); parseErr != nil {
RenderError(parseErr, http.StatusBadRequest, w, r)
Expand Down Expand Up @@ -75,7 +72,7 @@ func Override(w http.ResponseWriter, r *http.Request) {
if override, err := optlyClient.SetForcedVariation(r.Context(), experimentKey, body.UserID, body.VariationKey); err != nil {
RenderError(err, http.StatusInternalServerError, w, r)
} else {
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Str("experimentKey", experimentKey).Str("variationKey", body.VariationKey).Msg("Successfully set override")
logger.Info().Str("experimentKey", experimentKey).Str("variationKey", body.VariationKey).Msg("Successfully set override")
render.JSON(w, r, override)
}
}
4 changes: 1 addition & 3 deletions pkg/handlers/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
"github.com/optimizely/go-sdk/pkg/decision"
Expand All @@ -40,7 +39,6 @@ func Save(w http.ResponseWriter, r *http.Request) {
}

logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

if optlyClient.UserProfileService == nil {
RenderError(ErrNoUPS, http.StatusInternalServerError, w, r)
Expand All @@ -62,7 +60,7 @@ func Save(w http.ResponseWriter, r *http.Request) {

convertedProfile := convertToUserProfile(body)
optlyClient.UserProfileService.Save(convertedProfile)
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msgf("Saved user profile for user %s", body.UserID)
logger.Info().Msgf("Saved user profile for user %s", body.UserID)
render.Status(r, http.StatusOK)
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/handlers/send_odp_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
"github.com/optimizely/agent/pkg/optimizely"
Expand All @@ -38,7 +37,6 @@ func SendOdpEvent(w http.ResponseWriter, r *http.Request) {
}

logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

body, err := getRequestOdpEvent(r)
if err != nil {
Expand All @@ -56,7 +54,7 @@ func SendOdpEvent(w http.ResponseWriter, r *http.Request) {
Success: true,
}

logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg("Successfully sent event to ODP platform")
logger.Info().Msg("Successfully sent event to ODP platform")
render.JSON(w, r, returnResult)
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/handlers/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
"github.com/optimizely/go-sdk/pkg/entities"
Expand All @@ -42,7 +41,6 @@ func TrackEvent(w http.ResponseWriter, r *http.Request) {
return
}
logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

var body trackBody
err = ParseRequestBody(r, &body)
Expand All @@ -69,6 +67,6 @@ func TrackEvent(w http.ResponseWriter, r *http.Request) {
return
}

logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Str("eventKey", eventKey).Msg("tracked event")
logger.Info().Str("eventKey", eventKey).Msg("tracked event")
render.JSON(w, r, track)
}
11 changes: 4 additions & 7 deletions pkg/handlers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"net/http"

"github.com/go-chi/render"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/middleware"
)
Expand All @@ -36,8 +35,7 @@ type ErrorResponse struct {

// RenderError sets the request status and renders the error message.
func RenderError(err error, status int, w http.ResponseWriter, r *http.Request) {
span := trace.SpanFromContext(r.Context())
middleware.GetLogger(r).Err(err).Int("status", status).Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg("render error")
middleware.GetLogger(r).Err(err).Int("status", status).Msg("render error")
render.Status(r, status)
render.JSON(w, r, ErrorResponse{Error: err.Error()})
}
Expand All @@ -47,24 +45,23 @@ func RenderError(err error, status int, w http.ResponseWriter, r *http.Request)
// so that it is not leaked back to the requestor.
func ParseRequestBody(r *http.Request, v interface{}) error {
logger := middleware.GetLogger(r)
span := trace.SpanFromContext(r.Context())

body, err := io.ReadAll(r.Body)
if err != nil {
msg := "error reading request body"
logger.Err(err).Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg(msg)
logger.Err(err).Msg(msg)
return fmt.Errorf("%s", msg)
}

if len(body) == 0 {
logger.Info().Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg("body was empty skip JSON unmarshal")
logger.Info().Msg("body was empty skip JSON unmarshal")
return nil
}

err = json.Unmarshal(body, &v)
if err != nil {
msg := "error parsing request body"
logger.Err(err).Str("traceID", span.SpanContext().TraceID().String()).Str("spanID", span.SpanContext().SpanID().String()).Msg(msg)
logger.Err(err).Msg(msg)
return fmt.Errorf("%s", msg)
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/middleware/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"strconv"
"strings"

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

"github.com/optimizely/go-sdk/pkg/config"

"github.com/go-chi/render"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel/trace"

"github.com/optimizely/agent/pkg/optimizely"
"github.com/optimizely/go-sdk/pkg/config"
)

// ErrorResponse Model
Expand All @@ -52,6 +52,12 @@ func GetLogger(r *http.Request) *zerolog.Logger {
reqID := r.Header.Get(OptlyRequestHeader)
logger := log.With().Str("requestId", reqID).Logger()

span := trace.SpanFromContext(r.Context())
if span.SpanContext().TraceID().IsValid() {
logger = logger.With().Str("traceId", span.SpanContext().TraceID().String()).Logger()
logger = logger.With().Str("spanId", span.SpanContext().SpanID().String()).Logger()
}

if optimizely.ShouldIncludeSDKKey {
sdkKey := r.Header.Get(OptlySDKHeader)
sdkKeySplit := strings.Split(sdkKey, ":")
Expand Down

0 comments on commit e770ccd

Please sign in to comment.