Skip to content

Commit

Permalink
Use another context for access log
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Schneider authored and Marcel Ludwig committed Nov 26, 2021
1 parent d2a013f commit 9a6db27
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
1 change: 1 addition & 0 deletions config/request/context_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
BackendLogFields
BackendName
BufferOptions
CustomLogsCtx
Endpoint
EndpointKind
Error
Expand Down
7 changes: 1 addition & 6 deletions eval/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,10 @@ func IsUpgradeResponse(req *http.Request, res *http.Response) bool {
}

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

var httpCtx *hcl.EvalContext
if c, ok := ctx.Value(request.ContextType).(*Context); ok {
httpCtx = c.HCLContext()
}

for _, body := range bodies {
if body == nil {
continue // Test cases
Expand Down
5 changes: 2 additions & 3 deletions handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ func (e *Endpoint) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
evalContext := eval.ContextFromRequest(req)
evalContext = evalContext.WithBeresps(beresps.List()...)

// TODO: Fix context
// ctx := context.WithValue(req.Context(), request.ContextType, evalContext)
// *req = *req.WithContext(ctx)
ctx := context.WithValue(req.Context(), request.CustomLogsCtx, evalContext)
*req = *req.WithContext(ctx)

// assume prio or err on conf load if set with response
if e.opts.Redirect != nil {
Expand Down
12 changes: 6 additions & 6 deletions logging/hooks/custom_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ func (c *CustomLogs) Fire(entry *logrus.Entry) error {
if t, exists := entry.Data["type"]; exists {
switch t {
case "couper_access":
fire(entry, request.AccessLogFields)
fire(entry, request.AccessLogFields, request.CustomLogsCtx)
case "couper_backend":
fire(entry, request.BackendLogFields)
fire(entry, request.BackendLogFields, request.ContextType)
}
}
}

return nil
}

func fire(entry *logrus.Entry, key request.ContextKey) {
func fire(entry *logrus.Entry, fieldsKey, ctxKey request.ContextKey) {
var evalCtx *eval.Context

if ctx, ok := entry.Context.Value(request.ContextType).(*eval.Context); ok {
if ctx, ok := entry.Context.Value(ctxKey).(*eval.Context); ok {
evalCtx = ctx
} else {
evalCtx = eval.NewContext(nil, nil)
}

if bodies := entry.Context.Value(key); bodies != nil {
if fields := eval.ApplyCustomLogs(evalCtx, bodies.([]hcl.Body), entry); len(fields) > 0 {
if bodies := entry.Context.Value(fieldsKey); bodies != nil {
if fields := eval.ApplyCustomLogs(evalCtx.HCLContext(), bodies.([]hcl.Body), entry); len(fields) > 0 {
entry.Data["custom"] = fields
}
}
Expand Down
1 change: 1 addition & 0 deletions server/http_custom_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestCustomLogs_Upstream(t *testing.T) {
"float": 1.23,
"int": float64(123),
"object": logrus.Fields{"a": "A", "b": "B", "c": float64(123)},
"req": "GET",
"string": "couper test-backend",
},
},
Expand Down
1 change: 1 addition & 0 deletions server/testdata/integration/logs/01_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ server "logs" {
int = 123
float = 1.23
string = backend_responses.default.headers.server
req = request.method

array = [
1,
Expand Down

0 comments on commit 9a6db27

Please sign in to comment.