Skip to content

Commit

Permalink
Refactor error_handler context update to own method
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Nov 30, 2021
1 parent 75361fb commit 65ddb0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func (e *Endpoint) BufferOptions() eval.BufferOption {
return e.opts.BufferOpts
}

// BodyContext exposes the current endpoint hcl.Body.
func (e *Endpoint) BodyContext() hcl.Body {
return e.opts.Context
}
Expand Down
22 changes: 9 additions & 13 deletions handler/error.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package handler

import (
"context"
"net/http"

"github.com/hashicorp/hcl/v2"

"github.com/avenga/couper/config/request"
"github.com/avenga/couper/errors"
"github.com/avenga/couper/logging"
)

var _ http.Handler = &Error{}
Expand Down Expand Up @@ -37,28 +35,26 @@ func (e *Error) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

for _, kind := range err.Kinds() {
if kind == "access_control" {
kind = "*"
}

ep, defined := e.kindsHandler[kind]
if !defined {
continue
}

if eph, ek := ep.(interface{ BodyContext() hcl.Body }); ek {
if b := req.Context().Value(request.LogCustomAccess); b != nil {
bodies := b.([]hcl.Body)
bodies = append(bodies, eph.BodyContext())
*req = *req.WithContext(context.WithValue(req.Context(), request.LogCustomAccess, bodies))
}
if eph, ek := ep.(*Endpoint); ek {
// Custom log context is set on configuration load, however which error events got thrown
// during runtime is unknown at this point, so we must update the hcl context here and
// with the wildcard cases below.
logging.UpdateCustomAccessLogContext(req, eph.BodyContext())
}

ep.ServeHTTP(rw, req)
return
}

if ep, defined := e.kindsHandler[errors.Wildcard]; defined {
if eph, ek := ep.(*Endpoint); ek {
logging.UpdateCustomAccessLogContext(req, eph.BodyContext())
}
ep.ServeHTTP(rw, req)
return
}
Expand Down
10 changes: 10 additions & 0 deletions logging/access_log.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package logging

import (
"context"
"net/http"
"net/url"
"time"

"github.com/hashicorp/hcl/v2"
"github.com/sirupsen/logrus"

"github.com/avenga/couper/config/request"
Expand Down Expand Up @@ -166,3 +168,11 @@ func (log *AccessLog) Do(writer http.ResponseWriter, req *http.Request) {
entry.Info()
}
}

func UpdateCustomAccessLogContext(req *http.Request, body hcl.Body) {
if b := req.Context().Value(request.LogCustomAccess); b != nil {
bodies, _ := b.([]hcl.Body)
bodies = append(bodies, body)
*req = *req.WithContext(context.WithValue(req.Context(), request.LogCustomAccess, bodies))
}
}

0 comments on commit 65ddb0c

Please sign in to comment.