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 #3032, #1507

Signed-off-by: Clay Kauzlaric <ckauzlaric@vmware.com>
Co-authored-by: Clay Kauzlaric <ckauzlaric@vmware.com>
Co-authored-by: Alexander Standke <astandke@vmware.com>
  • Loading branch information
3 people committed Oct 26, 2020
1 parent f7094b2 commit 19a8fbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
12 changes: 5 additions & 7 deletions internal/envoy/v2/accesslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,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 k, v := range fields {
if v == "" {
v = config.JSONFields[k]
}
jsonformat.Fields[k] = sv(v)
}

return []*accesslog.AccessLog{{
Expand Down
19 changes: 12 additions & 7 deletions internal/envoy/v2/accesslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func TestFileAccessLog(t *testing.T) {
func TestJSONFileAccessLog(t *testing.T) {
tests := map[string]struct {
path string
headers []string
headers map[string]string
want []*envoy_accesslog.AccessLog
}{
"only timestamp": {
path: "/dev/stdout",
headers: []string{"@timestamp"},
headers: map[string]string{"@timestamp": "%START_TIME%"},
want: []*envoy_accesslog.AccessLog{{
Name: wellknown.FileAccessLog,
ConfigType: &envoy_accesslog.AccessLog_TypedConfig{
Expand All @@ -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",
headers: map[string]string{
"@timestamp": config.JSONFields["@timestamp"],
"method": config.JSONFields["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
6 changes: 3 additions & 3 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,11 +181,11 @@ 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
}
return config.DefaultFields
return config.ParsedDefaultFields
}

func (lvc *ListenerConfig) newInsecureAccessLog() []*envoy_api_v2_accesslog.AccessLog {
Expand Down

0 comments on commit 19a8fbb

Please sign in to comment.