/
access_loggers.go
137 lines (125 loc) · 4.77 KB
/
access_loggers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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 envoyconf
import (
config_access_logv3 "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v3"
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
grpc_accesslogv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/grpc/v3"
stream_accesslogv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/stream/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/wso2/apk/adapter/config"
logger "github.com/wso2/apk/adapter/internal/loggers"
logging "github.com/wso2/apk/adapter/internal/logging"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// getAccessLogConfigs provides file access log configurations for envoy
func getFileAccessLogConfigs() *config_access_logv3.AccessLog {
var logFormat *stream_accesslogv3.StdoutAccessLog_LogFormat
logConf := config.ReadLogConfigs()
if !logConf.AccessLogs.Enable {
logger.LoggerOasparser.Info("Accesslog Configurations are disabled.")
return nil
}
logFormat = &stream_accesslogv3.StdoutAccessLog_LogFormat{
LogFormat: &corev3.SubstitutionFormatString{
Format: &corev3.SubstitutionFormatString_TextFormatSource{
TextFormatSource: &corev3.DataSource{
Specifier: &corev3.DataSource_InlineString{
InlineString: logConf.AccessLogs.Format,
},
},
},
Formatters: []*corev3.TypedExtensionConfig{
{
Name: "envoy.formatter.req_without_query",
TypedConfig: &anypb.Any{
TypeUrl: "type.googleapis.com/envoy.extensions.formatter.req_without_query.v3.ReqWithoutQuery",
},
},
},
},
}
accessLogConf := &stream_accesslogv3.StdoutAccessLog{
AccessLogFormat: logFormat,
}
accessLogTypedConf, err := anypb.New(accessLogConf)
if err != nil {
logger.LoggerOasparser.ErrorC(logging.PrintError(logging.Error2200, logging.CRITICAL, "Error marsheling access log configs. %v", err.Error()))
return nil
}
accessLog := config_access_logv3.AccessLog{
Name: wellknown.FileAccessLog,
Filter: nil,
ConfigType: &config_access_logv3.AccessLog_TypedConfig{
TypedConfig: accessLogTypedConf,
},
}
return &accessLog
}
// getAccessLogConfigs provides grpc access log configurations for envoy
func getGRPCAccessLogConfigs(conf *config.Config) *config_access_logv3.AccessLog {
grpcAccessLogsEnabled := conf.Analytics.Adapter.Enabled || conf.Enforcer.Metrics.Enabled
if !grpcAccessLogsEnabled {
logger.LoggerOasparser.Debug("gRPC access logs are not enabled as analytics is disabled.")
return nil
}
accessLogConf := &grpc_accesslogv3.HttpGrpcAccessLogConfig{
CommonConfig: &grpc_accesslogv3.CommonGrpcAccessLogConfig{
TransportApiVersion: corev3.ApiVersion_V3,
LogName: grpcAccessLogLogName,
BufferFlushInterval: durationpb.New(conf.Analytics.Adapter.BufferFlushInterval),
BufferSizeBytes: wrapperspb.UInt32(conf.Analytics.Adapter.BufferSizeBytes),
GrpcService: &corev3.GrpcService{
TargetSpecifier: &corev3.GrpcService_EnvoyGrpc_{
EnvoyGrpc: &corev3.GrpcService_EnvoyGrpc{
ClusterName: accessLoggerClusterName,
},
},
Timeout: durationpb.New(conf.Analytics.Adapter.GRPCRequestTimeout),
},
},
}
accessLogTypedConf, err := anypb.New(accessLogConf)
if err != nil {
logger.LoggerOasparser.ErrorC(logging.PrintError(logging.Error2201, logging.CRITICAL, "Error marshalling gRPC access log configs. %v", err.Error()))
return nil
}
accessLog := config_access_logv3.AccessLog{
Name: wellknown.HTTPGRPCAccessLog,
Filter: nil,
ConfigType: &config_access_logv3.AccessLog_TypedConfig{
TypedConfig: accessLogTypedConf,
},
}
return &accessLog
}
// getAccessLogs provides access logs for envoy
func getAccessLogs() []*config_access_logv3.AccessLog {
conf := config.ReadConfigs()
var accessLoggers []*config_access_logv3.AccessLog
fileAccessLog := getFileAccessLogConfigs()
grpcAccessLog := getGRPCAccessLogConfigs(conf)
if fileAccessLog != nil {
accessLoggers = append(accessLoggers, fileAccessLog)
}
if grpcAccessLog != nil {
accessLoggers = append(accessLoggers, getGRPCAccessLogConfigs(conf))
}
return accessLoggers
}