Skip to content

Commit

Permalink
internal/envoy: send custom JSON fields to Envoy
Browse files Browse the repository at this point in the history
Fixes projectcontour#3032, projectcontour#1507

Signed-off-by: Clay Kauzlaric <ckauzlaric@vmware.com>
  • Loading branch information
mike1808 committed Oct 23, 2020
1 parent a928f81 commit 9ee5aec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
17 changes: 9 additions & 8 deletions internal/envoy/v2/accesslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package v2

import (
"strings"

accesslogv2 "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v2"
accesslog "github.com/envoyproxy/go-control-plane/envoy/config/filter/accesslog/v2"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
Expand All @@ -38,18 +40,17 @@ func FileAccessLogEnvoy(path string) []*accesslog.AccessLog {

// FileAccessLogJSON returns a new file based access log filter
// that will log in JSON format
func FileAccessLogJSON(path string, keys []string) []*accesslog.AccessLog {

func FileAccessLogJSON(path string, fields config.AccessLogFields) []*accesslog.AccessLog {
jsonformat := &_struct.Struct{
Fields: make(map[string]*_struct.Value),
}

for _, k := range keys {
// This will silently ignore invalid headers.
// TODO(youngnick): this should tell users if a header is not valid
// https://github.com/projectcontour/contour/issues/1507
if template, ok := config.JSONFields[k]; ok {
jsonformat.Fields[k] = sv(template)
for _, val := range fields {
parts := strings.SplitN(val, "=", 2)
if len(parts) == 1 {
jsonformat.Fields[val] = sv(config.JSONFields[val])
} else {
jsonformat.Fields[parts[0]] = sv(parts[1])
}
}

Expand Down
9 changes: 7 additions & 2 deletions internal/envoy/v2/accesslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ func TestJSONFileAccessLog(t *testing.T) {
},
},
},
"invalid header should disappear": {
"custom fields should appear": {
path: "/dev/stdout",
headers: []string{
"@timestamp",
"invalid",
"method",
"custom1=%REQ(X-CUSTOM-HEADER)%",
"custom2=%DURATION%.0",
"custom3=ST=%START_TIME(%s.%6f)%",
},
want: []*envoy_accesslog.AccessLog{{
Name: wellknown.FileAccessLog,
Expand All @@ -92,6 +94,9 @@ func TestJSONFileAccessLog(t *testing.T) {
Fields: map[string]*_struct.Value{
"@timestamp": sv(config.JSONFields["@timestamp"]),
"method": sv(config.JSONFields["method"]),
"custom1": sv("%REQ(X-CUSTOM-HEADER)%"),
"custom2": sv("%DURATION%.0"),
"custom3": sv("ST=%START_TIME(%s.%6f)%"),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/xdscache/v2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type ListenerConfig struct {
// AccessLogFields sets the fields that should be shown in JSON logs.
// Valid entries are the keys from internal/envoy/accesslog.go:jsonheaders
// Defaults to a particular set of fields.
AccessLogFields []string
AccessLogFields config.AccessLogFields

// RequestTimeout configures the request_timeout for all Connection Managers.
RequestTimeout timeout.Setting
Expand Down Expand Up @@ -181,7 +181,7 @@ func (lvc *ListenerConfig) accesslogType() string {

// accesslogFields returns the access log fields that should be configured
// for Envoy, or a default set if not configured.
func (lvc *ListenerConfig) accesslogFields() []string {
func (lvc *ListenerConfig) accesslogFields() config.AccessLogFields {
if lvc.AccessLogFields != nil {
return lvc.AccessLogFields
}
Expand Down

0 comments on commit 9ee5aec

Please sign in to comment.