Skip to content

Commit

Permalink
as there is only one log value or log error there is no need for slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Koch committed Jan 18, 2023
1 parent 4b5093c commit 6eeed68
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
4 changes: 2 additions & 2 deletions config/request/context_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (
Error
Handler
LogCustomAccess
LogCustomUpstreamValues
LogCustomUpstreamErrors
LogCustomUpstreamValue
LogCustomUpstreamError
LogDebugLevel
LogEntry
OpenAPI
Expand Down
23 changes: 7 additions & 16 deletions eval/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,8 @@ func EvalCustomLogFields(httpCtx *hcl.EvalContext, body *hclsyntax.Body) (cty.Va
return Value(httpCtx, attr.Expr)
}

func MergeCustomLogs(values []cty.Value, errors []error, logger *logrus.Entry) logrus.Fields {
for _, err := range errors {
logger.Debug(err)
}

val, err := lib.Merge(values)
if err != nil {
logger.Debug(err)
}

return seetie.ValueToLogFields(val)
}

func ApplyCustomLogs(httpCtx *hcl.EvalContext, bodies []hcl.Body, logger *logrus.Entry) logrus.Fields {
var values []cty.Value
var errors []error

for _, body := range bodies {
if body == nil {
Expand All @@ -373,14 +359,19 @@ func ApplyCustomLogs(httpCtx *hcl.EvalContext, bodies []hcl.Body, logger *logrus

val, err := Value(httpCtx, logs.Expr)
if err != nil {
errors = append(errors, err)
logger.Debug(err)
continue
}

values = append(values, val)
}

return MergeCustomLogs(values, errors, logger)
val, err := lib.Merge(values)
if err != nil {
logger.Debug(err)
}

return seetie.ValueToLogFields(val)
}

func ApplyResponseContext(ctx *hcl.EvalContext, body *hclsyntax.Body, beresp *http.Response) error {
Expand Down
8 changes: 4 additions & 4 deletions handler/transport/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ func (b *Backend) RoundTrip(req *http.Request) (*http.Response, error) {

clfValue, err := eval.EvalCustomLogFields(evalCtx.HCLContext(), ctxBody)
if err != nil {
logErrors, _ := req.Context().Value(request.LogCustomUpstreamErrors).(*[]error)
*logErrors = append(*logErrors, err)
logError, _ := req.Context().Value(request.LogCustomUpstreamError).(*error)
*logError = err
} else if clfValue != cty.NilVal {
logValues, _ := req.Context().Value(request.LogCustomUpstreamValues).(*[]cty.Value)
*logValues = append(*logValues, clfValue)
logValue, _ := req.Context().Value(request.LogCustomUpstreamValue).(*cty.Value)
*logValue = clfValue
}

err = eval.ApplyResponseContext(evalCtx.HCLContext(), ctxBody, beresp)
Expand Down
13 changes: 8 additions & 5 deletions logging/hooks/custom_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/avenga/couper/config/env"
"github.com/avenga/couper/config/request"
"github.com/avenga/couper/eval"
"github.com/avenga/couper/internal/seetie"
"github.com/avenga/couper/logging"
)

Expand Down Expand Up @@ -77,10 +78,12 @@ func fireAccess(entry *logrus.Entry) {
}

func fireUpstream(entry *logrus.Entry) {
logValues, _ := entry.Context.Value(request.LogCustomUpstreamValues).(*[]cty.Value)
logErrors, _ := entry.Context.Value(request.LogCustomUpstreamErrors).(*[]error)

if fields := eval.MergeCustomLogs(*logValues, *logErrors, entry); len(fields) > 0 {
entry.Data[customLogField] = fields
logError, _ := entry.Context.Value(request.LogCustomUpstreamError).(*error)
if *logError != nil {
entry.Debug(*logError)
return
}
logValue, _ := entry.Context.Value(request.LogCustomUpstreamValue).(*cty.Value)
fields := seetie.ValueToLogFields(*logValue)
entry.Data[customLogField] = fields
}
8 changes: 4 additions & 4 deletions logging/upstream_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func (u *UpstreamLog) RoundTrip(req *http.Request) (*http.Response, error) {

fields["request"] = requestFields

var logValues []cty.Value
var logErrors []error
var logValue cty.Value
var logError error
berespBytes := int64(0)
tokenRetries := uint8(0)
outctx := context.WithValue(req.Context(), request.LogCustomUpstreamValues, &logValues)
outctx = context.WithValue(outctx, request.LogCustomUpstreamErrors, &logErrors)
outctx := context.WithValue(req.Context(), request.LogCustomUpstreamValue, &logValue)
outctx = context.WithValue(outctx, request.LogCustomUpstreamError, &logError)
outctx = context.WithValue(outctx, request.BackendBytes, &berespBytes)
outctx = context.WithValue(outctx, request.TokenRequestRetries, &tokenRetries)
oCtx, openAPIContext := validation.NewWithContext(outctx)
Expand Down

0 comments on commit 6eeed68

Please sign in to comment.