Skip to content

Commit

Permalink
WIP: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yskopets committed Feb 21, 2020
1 parent b9cd952 commit 8e754f1
Show file tree
Hide file tree
Showing 28 changed files with 450 additions and 412 deletions.
78 changes: 0 additions & 78 deletions pkg/envoy/accesslog/composite_formatter.go

This file was deleted.

12 changes: 8 additions & 4 deletions pkg/envoy/accesslog/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ Envoy access log command syntax.
Use ParseFormat() function to parse a format string.
Use LogConfigurer interface to configure `envoy.http_grpc_access_log` and `envoy.tcp_grpc_access_log`.
Use HttpLogEntryFormatter interface to format an HTTP log entry.
Use LogEntryFormatter interface to format HTTP and TCP log entries.
Use TcpLogEntryFormatter interface to format a TCP log entry.
The initial implementation misses the following features:
1. `%START_TIME%` commands ignore a format string that could have been defined by a user
Use HttpLogConfigurer interface to configure `envoy.http_grpc_access_log` filter.
Use TcpLogConfigurer interface to configure `envoy.tcp_grpc_access_log` filter.
The initial implementation is missing the following features:
1. `%START_TIME%` commands ignore the user-defined format string
2. `%DYNAMIC_METADATA(NAMESPACE:KEY*):Z%` commands return a stub value
3. `%FILTER_STATE(KEY):Z%` commands return a stub value
*/
Expand Down
48 changes: 0 additions & 48 deletions pkg/envoy/accesslog/dynamic_metadata_formatter.go

This file was deleted.

59 changes: 59 additions & 0 deletions pkg/envoy/accesslog/dynamic_metadata_operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package accesslog

import (
"strconv"
"strings"

accesslog_config "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v2"
accesslog_data "github.com/envoyproxy/go-control-plane/envoy/data/accesslog/v2"
)

// DynamicMetadataOperator represents a `%DYNAMIC_METADATA(NAMESPACE:KEY*):Z%` command operator.
type DynamicMetadataOperator struct {
FilterNamespace string
Path []string
MaxLength int
}

func (f *DynamicMetadataOperator) FormatHttpLogEntry(entry *accesslog_data.HTTPAccessLogEntry) (string, error) {
return f.format(entry.GetCommonProperties())
}

func (f *DynamicMetadataOperator) FormatTcpLogEntry(entry *accesslog_data.TCPAccessLogEntry) (string, error) {
return f.format(entry.GetCommonProperties())
}

func (f *DynamicMetadataOperator) format(entry *accesslog_data.AccessLogCommon) (string, error) {
// TODO(yskopets): implement
return "UNSUPPORTED_COMMAND(%DYNAMIC_METADATA(NAMESPACE:KEY*):Z%)", nil
}

func (f *DynamicMetadataOperator) ConfigureHttpLog(config *accesslog_config.HttpGrpcAccessLogConfig) error {
// has no effect on HttpGrpcAccessLogConfig
return nil
}

func (f *DynamicMetadataOperator) ConfigureTcpLog(config *accesslog_config.TcpGrpcAccessLogConfig) error {
// has no effect on TcpGrpcAccessLogConfig
return nil
}

// String returns the canonical representation of this access log fragment.
func (f *DynamicMetadataOperator) String() string {
var builder []string
builder = append(builder, "%DYNAMIC_METADATA(")
builder = append(builder, f.FilterNamespace)
if len(f.Path) > 0 {
for _, segment := range f.Path {
builder = append(builder, ":")
builder = append(builder, segment)
}
}
builder = append(builder, ")")
if f.MaxLength != 0 {
builder = append(builder, ":")
builder = append(builder, strconv.FormatInt(int64(f.MaxLength), 10))
}
builder = append(builder, "%")
return strings.Join(builder, "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
. "github.com/Kong/kuma/pkg/envoy/accesslog"
)

var _ = Describe("DynamicMetadataFormatter", func() {
var _ = Describe("DynamicMetadataOperator", func() {

Describe("String()", func() {
type testCase struct {
Expand All @@ -21,10 +21,10 @@ var _ = Describe("DynamicMetadataFormatter", func() {
DescribeTable("should return correct canonical representation",
func(given testCase) {
// setup
formatter := &DynamicMetadataFormatter{FilterNamespace: given.filterNamespace, Path: given.path, MaxLength: given.maxLength}
fragment := &DynamicMetadataOperator{FilterNamespace: given.filterNamespace, Path: given.path, MaxLength: given.maxLength}

// when
actual := formatter.String()
actual := fragment.String()
// then
Expect(actual).To(Equal(given.expected))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golang/protobuf/ptypes/duration"
"github.com/golang/protobuf/ptypes/wrappers"

accesslog_config "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v2"
accesslog_data "github.com/envoyproxy/go-control-plane/envoy/data/accesslog/v2"

envoy_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
Expand All @@ -21,16 +22,26 @@ const (
excludePort = false
)

// DynamicMetadataFormatter represents a simple field command operator,
// FieldOperator represents a simple field command operator,
// such as `%BYTES_RECEIVED%` or `%PROTOCOL%`.
type FieldFormatter string
type FieldOperator string

// String returns the canonical representation of this command operator.
func (f FieldFormatter) String() string {
// String returns the canonical representation of this access log fragment.
func (f FieldOperator) String() string {
return CommandOperatorDescriptor(f).String()
}

func (f FieldFormatter) FormatHttpLogEntry(entry *accesslog_data.HTTPAccessLogEntry) (string, error) {
func (f FieldOperator) ConfigureHttpLog(config *accesslog_config.HttpGrpcAccessLogConfig) error {
// has no effect on HttpGrpcAccessLogConfig
return nil
}

func (f FieldOperator) ConfigureTcpLog(config *accesslog_config.TcpGrpcAccessLogConfig) error {
// has no effect on TcpGrpcAccessLogConfig
return nil
}

func (f FieldOperator) FormatHttpLogEntry(entry *accesslog_data.HTTPAccessLogEntry) (string, error) {
switch f {
case CMD_BYTES_RECEIVED:
return f.formatUint(entry.GetRequest().GetRequestBodyBytes())
Expand All @@ -53,7 +64,7 @@ func (f FieldFormatter) FormatHttpLogEntry(entry *accesslog_data.HTTPAccessLogEn
}
}

func (f FieldFormatter) FormatTcpLogEntry(entry *accesslog_data.TCPAccessLogEntry) (string, error) {
func (f FieldOperator) FormatTcpLogEntry(entry *accesslog_data.TCPAccessLogEntry) (string, error) {
switch f {
case CMD_BYTES_RECEIVED:
return f.formatUint(entry.GetConnectionProperties().GetReceivedBytes())
Expand All @@ -76,7 +87,7 @@ func (f FieldFormatter) FormatTcpLogEntry(entry *accesslog_data.TCPAccessLogEntr
}
}

func (f FieldFormatter) formatAccessLogCommon(entry *accesslog_data.AccessLogCommon) (string, error) {
func (f FieldOperator) formatAccessLogCommon(entry *accesslog_data.AccessLogCommon) (string, error) {
switch f {
case CMD_UPSTREAM_TRANSPORT_FAILURE_REASON:
return entry.GetUpstreamTransportFailureReason(), nil
Expand Down Expand Up @@ -134,15 +145,15 @@ func (f FieldFormatter) formatAccessLogCommon(entry *accesslog_data.AccessLogCom
}
}

func (f FieldFormatter) formatUint(value uint64) (string, error) {
func (f FieldOperator) formatUint(value uint64) (string, error) {
return strconv.FormatUint(value, 10), nil
}

func (f FieldFormatter) formatInt(value int64) (string, error) {
func (f FieldOperator) formatInt(value int64) (string, error) {
return strconv.FormatInt(value, 10), nil
}

func (f FieldFormatter) formatDuration(dur *duration.Duration) (string, error) {
func (f FieldOperator) formatDuration(dur *duration.Duration) (string, error) {
if dur == nil {
return "", nil
}
Expand All @@ -153,7 +164,7 @@ func (f FieldFormatter) formatDuration(dur *duration.Duration) (string, error) {
return f.formatInt(int64(durNanos / time.Millisecond))
}

func (f FieldFormatter) formatDurationDelta(outer *duration.Duration, inner *duration.Duration) (string, error) {
func (f FieldOperator) formatDurationDelta(outer *duration.Duration, inner *duration.Duration) (string, error) {
if outer == nil || inner == nil {
return "", nil
}
Expand All @@ -168,7 +179,7 @@ func (f FieldFormatter) formatDurationDelta(outer *duration.Duration, inner *dur
return f.formatInt(int64((outerNanos - innerNanos) / time.Millisecond))
}

func (f FieldFormatter) formatHttpVersion(value accesslog_data.HTTPAccessLogEntry_HTTPVersion) (string, error) {
func (f FieldOperator) formatHttpVersion(value accesslog_data.HTTPAccessLogEntry_HTTPVersion) (string, error) {
switch value {
case accesslog_data.HTTPAccessLogEntry_PROTOCOL_UNSPECIFIED:
return "", nil
Expand All @@ -185,7 +196,7 @@ func (f FieldFormatter) formatHttpVersion(value accesslog_data.HTTPAccessLogEntr
}
}

func (f FieldFormatter) formatResponseFlags(flags *accesslog_data.ResponseFlags) (string, error) {
func (f FieldOperator) formatResponseFlags(flags *accesslog_data.ResponseFlags) (string, error) {
values := make([]string, 0)
if flags.GetFailedLocalHealthcheck() {
values = append(values, ResponseFlagFailedLocalHealthCheck)
Expand Down Expand Up @@ -247,7 +258,7 @@ func (f FieldFormatter) formatResponseFlags(flags *accesslog_data.ResponseFlags)
return strings.Join(values, ","), nil
}

func (f FieldFormatter) formatAddress(address *envoy_core.Address, includePort bool) (string, error) {
func (f FieldOperator) formatAddress(address *envoy_core.Address, includePort bool) (string, error) {
switch typ := address.GetAddress().(type) {
case *envoy_core.Address_SocketAddress:
if includePort {
Expand All @@ -261,7 +272,7 @@ func (f FieldFormatter) formatAddress(address *envoy_core.Address, includePort b
}
}

func (f FieldFormatter) formatUriSans(sans []*accesslog_data.TLSProperties_CertificateProperties_SubjectAltName) (string, error) {
func (f FieldOperator) formatUriSans(sans []*accesslog_data.TLSProperties_CertificateProperties_SubjectAltName) (string, error) {
values := make([]string, 0, len(sans))
for _, san := range sans {
switch typ := san.GetSan().(type) {
Expand All @@ -272,14 +283,14 @@ func (f FieldFormatter) formatUriSans(sans []*accesslog_data.TLSProperties_Certi
return strings.Join(values, ","), nil
}

func (f FieldFormatter) formatTlsCipherSuite(value *wrappers.UInt32Value) (string, error) {
func (f FieldOperator) formatTlsCipherSuite(value *wrappers.UInt32Value) (string, error) {
if value == nil || value.GetValue() == 0xFFFF {
return "", nil
}
return TlsCipherSuite(value.GetValue()).String(), nil
}

func (f FieldFormatter) formatTlsVersion(value accesslog_data.TLSProperties_TLSVersion) (string, error) {
func (f FieldOperator) formatTlsVersion(value accesslog_data.TLSProperties_TLSVersion) (string, error) {
switch value {
case accesslog_data.TLSProperties_VERSION_UNSPECIFIED:
return "", nil
Expand Down
Loading

0 comments on commit 8e754f1

Please sign in to comment.