Skip to content

Commit

Permalink
access log
Browse files Browse the repository at this point in the history
Signed-off-by: huabingzhao <[email protected]>
  • Loading branch information
zhaohuabing committed Dec 20, 2022
1 parent 7ac9538 commit 8330b0f
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 16 deletions.
6 changes: 3 additions & 3 deletions demo/metaprotocol-brpc/brpc-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: brpc-echo-server
version: v1
Expand Down Expand Up @@ -57,7 +57,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: brpc-echo-server
version: v2
Expand Down Expand Up @@ -86,7 +86,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: brpc-echo-client
spec:
Expand Down
8 changes: 4 additions & 4 deletions demo/metaprotocol-dubbo/dubbo-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: dubbo-sample-provider
version: v1
Expand All @@ -55,7 +55,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: dubbo-sample-provider
version: v2
Expand All @@ -82,7 +82,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: dubbo-sample-second-provider
version: v2
Expand All @@ -109,7 +109,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
labels:
app: dubbo-sample-consumer
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
sidecar.istio.io/rewriteAppHTTPProbers: "false"
labels:
app: thrift-sample-server
Expand Down
6 changes: 3 additions & 3 deletions demo/metaprotocol-thrift/thrift-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
sidecar.istio.io/rewriteAppHTTPProbers: "false"
labels:
app: thrift-sample-server
Expand All @@ -55,7 +55,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
sidecar.istio.io/rewriteAppHTTPProbers: "false"
labels:
app: thrift-sample-server
Expand All @@ -82,7 +82,7 @@ spec:
metadata:
annotations:
sidecar.istio.io/bootstrapOverride: aeraki-bootstrap-config
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.1.4
sidecar.istio.io/proxyImage: aeraki/meta-protocol-proxy-debug:1.2.2
sidecar.istio.io/rewriteAppHTTPProbers: "false"
labels:
app: thrift-sample-client
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.5
//replace github.com/aeraki-mesh/meta-protocol-control-plane-api => github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20220325074604-63adf119a7bc

require (
github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20220901010517-9d131fbad75f
github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20221213110004-4eab1878885f
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/envoyproxy/go-control-plane v0.10.2-0.20211130161932-f62def555c97
github.com/gogo/protobuf v1.3.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20220901010517-9d131fbad75f h1:AY0AlplojtIx0ze2CWFzBjshiaOslsKfhYj5vdLrneY=
github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20220901010517-9d131fbad75f/go.mod h1:kBlB/k9X2I3yO6mZyS1tcdUfJy3cz4u+z97kmpo8qD4=
github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20221213110004-4eab1878885f h1:LR/enSBQNFvTJmvccTT4q3C9W016fwgjN+le7URdeyg=
github.com/aeraki-mesh/meta-protocol-control-plane-api v0.0.0-20221213110004-4eab1878885f/go.mod h1:kBlB/k9X2I3yO6mZyS1tcdUfJy3cz4u+z97kmpo8qD4=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/ahmetb/gen-crd-api-reference-docs v0.3.0/go.mod h1:TdjdkYhlOifCQWPs1UdTma97kQQMozf5h26hTuG70u8=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
112 changes: 112 additions & 0 deletions plugin/metaprotocol/accesslog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright Aeraki Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metaprotocol

import (
"google.golang.org/protobuf/types/known/structpb"
"istio.io/istio/pkg/util/protomarshal"
"istio.io/pkg/log"

accesslog "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v3"
envoyconfig "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
fileaccesslog "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/file/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/pkg/networking/util"
)

const (
envoyTextLogFormat = "[%START_TIME%] %REQ(X-META-PROTOCOL-APPLICATION-PROTOCOL)% " +
"%RESPONSE_CODE% %RESPONSE_CODE_DETAILS% %CONNECTION_TERMINATION_DETAILS% " +
"\"%UPSTREAM_TRANSPORT_FAILURE_REASON%\" %BYTES_RECEIVED% %BYTES_SENT% " +
"%DURATION% \"%REQ(X-FORWARDED-FOR)%\" \"%REQ(X-REQUEST-ID)%\" " +
"%UPSTREAM_CLUSTER% %UPSTREAM_LOCAL_ADDRESS% %DOWNSTREAM_LOCAL_ADDRESS% " +
"%DOWNSTREAM_REMOTE_ADDRESS% %ROUTE_NAME%\n"
)

// nolint: lll
var (
envoyJSONLogFormatIstio = &structpb.Struct{
Fields: map[string]*structpb.Value{
"start_time": {Kind: &structpb.Value_StringValue{StringValue: "%START_TIME%"}},
"route_name": {Kind: &structpb.Value_StringValue{StringValue: "%ROUTE_NAME%"}},
"application_protocol": {Kind: &structpb.Value_StringValue{StringValue: "%REQ(X-META-PROTOCOL-APPLICATION-PROTOCOL)%"}},
"response_code": {Kind: &structpb.Value_StringValue{StringValue: "%RESPONSE_CODE%"}},
"response_code_details": {Kind: &structpb.Value_StringValue{StringValue: "%RESPONSE_CODE_DETAILS%"}},
"connection_termination_details": {Kind: &structpb.Value_StringValue{StringValue: "%CONNECTION_TERMINATION_DETAILS%"}},
"bytes_received": {Kind: &structpb.Value_StringValue{StringValue: "%BYTES_RECEIVED%"}},
"bytes_sent": {Kind: &structpb.Value_StringValue{StringValue: "%BYTES_SENT%"}},
"duration": {Kind: &structpb.Value_StringValue{StringValue: "%DURATION%"}},
"request_id": {Kind: &structpb.Value_StringValue{StringValue: "%REQ(X-REQUEST-ID)%"}},
"upstream_cluster": {Kind: &structpb.Value_StringValue{StringValue: "%UPSTREAM_CLUSTER%"}},
"upstream_local_address": {Kind: &structpb.Value_StringValue{StringValue: "%UPSTREAM_LOCAL_ADDRESS%"}},
"downstream_local_address": {Kind: &structpb.Value_StringValue{StringValue: "%DOWNSTREAM_LOCAL_ADDRESS%"}},
"downstream_remote_address": {Kind: &structpb.Value_StringValue{StringValue: "%DOWNSTREAM_REMOTE_ADDRESS%"}},
"upstream_transport_failure_reason": {Kind: &structpb.Value_StringValue{StringValue: "%UPSTREAM_TRANSPORT_FAILURE_REASON%"}},
},
}
)

func buildFileAccessLogHelper(path string, mesh *meshconfig.MeshConfig) *accesslog.AccessLog {
// We need to build access log. This is needed either on first access or when mesh config changes.
fl := &fileaccesslog.FileAccessLog{
Path: path,
}

switch mesh.AccessLogEncoding {
case meshconfig.MeshConfig_TEXT:
formatString := envoyTextLogFormat
if mesh.AccessLogFormat != "" {
formatString = mesh.AccessLogFormat
}
fl.AccessLogFormat = &fileaccesslog.FileAccessLog_LogFormat{
LogFormat: &envoyconfig.SubstitutionFormatString{
Format: &envoyconfig.SubstitutionFormatString_TextFormatSource{
TextFormatSource: &envoyconfig.DataSource{
Specifier: &envoyconfig.DataSource_InlineString{
InlineString: formatString,
},
},
},
},
}
case meshconfig.MeshConfig_JSON:
jsonLogStruct := envoyJSONLogFormatIstio
if len(mesh.AccessLogFormat) > 0 {
parsedJSONLogStruct := structpb.Struct{}
if err := protomarshal.UnmarshalAllowUnknown([]byte(mesh.AccessLogFormat), &parsedJSONLogStruct); err != nil {
log.Errorf("error parsing provided json log format, default log format will be used: %v", err)
} else {
jsonLogStruct = &parsedJSONLogStruct
}
}
fl.AccessLogFormat = &fileaccesslog.FileAccessLog_LogFormat{
LogFormat: &envoyconfig.SubstitutionFormatString{
Format: &envoyconfig.SubstitutionFormatString_JsonFormat{
JsonFormat: jsonLogStruct,
},
},
}
default:
log.Warnf("unsupported access log format %v", mesh.AccessLogEncoding)
}

al := &accesslog.AccessLog{
Name: wellknown.FileAccessLog,
ConfigType: &accesslog.AccessLog_TypedConfig{TypedConfig: util.MessageToAny(fl)},
}

return al
}
10 changes: 8 additions & 2 deletions plugin/metaprotocol/metaprotocolproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package metaprotocol
import (
"fmt"

metaprotocol "github.com/aeraki-mesh/meta-protocol-control-plane-api/meta_protocol_proxy/v1alpha"
accesslog "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v3"
envoyconfig "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoytype "github.com/envoyproxy/go-control-plane/envoy/type/v3"
istionetworking "istio.io/api/networking/v1alpha3"

metaprotocol "github.com/aeraki-mesh/meta-protocol-control-plane-api/meta_protocol_proxy/v1alpha"

"github.com/aeraki-mesh/aeraki/pkg/model"
metaprotocolmodel "github.com/aeraki-mesh/aeraki/pkg/model/metaprotocol"
)
Expand Down Expand Up @@ -69,6 +69,9 @@ func buildOutboundProxy(context *model.EnvoyFilterContext,
Name: codec,
},
MetaProtocolFilters: buildOutboundFilters(),
AccessLog: []*accesslog.AccessLog{
buildFileAccessLogHelper(context.MeshConfig.Mesh().AccessLogFile, context.MeshConfig.Mesh()),
},
}
configTracing(context, metaProtocolProy)
return metaProtocolProy, nil
Expand Down Expand Up @@ -103,6 +106,9 @@ func buildInboundProxy(context *model.EnvoyFilterContext,
Name: codec,
},
MetaProtocolFilters: filters,
AccessLog: []*accesslog.AccessLog{
buildFileAccessLogHelper(context.MeshConfig.Mesh().AccessLogFile, context.MeshConfig.Mesh()),
},
}
configTracing(context, metaProtocolProy)
return metaProtocolProy, nil
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/common/istio-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ spec:
meshConfig:
enableTracing: true
accessLogFile: /dev/stdout
accessLogFormat: "[%START_TIME%] %REQ(X-META-PROTOCOL-APPLICATION-PROTOCOL)%
%RESPONSE_CODE% %RESPONSE_CODE_DETAILS% %CONNECTION_TERMINATION_DETAILS% \"%UPSTREAM_TRANSPORT_FAILURE_REASON%\"
%BYTES_RECEIVED% %BYTES_SENT% %DURATION% \"%REQ(X-FORWARDED-FOR)%\" \"%REQ(X-REQUEST-ID)%\" %UPSTREAM_CLUSTER%
%UPSTREAM_LOCAL_ADDRESS% %DOWNSTREAM_LOCAL_ADDRESS% %DOWNSTREAM_REMOTE_ADDRESS% %ROUTE_NAME%\n"
defaultConfig:
holdApplicationUntilProxyStarts: true
proxyMetadata:
Expand Down

0 comments on commit 8330b0f

Please sign in to comment.