Skip to content

Commit

Permalink
Merge pull request #3596 from ruks/choreo
Browse files Browse the repository at this point in the history
Support Insights access logs and configs
  • Loading branch information
ruks authored Oct 9, 2024
2 parents 2208e9e + 366be3e commit 4a67b46
Show file tree
Hide file tree
Showing 18 changed files with 1,780 additions and 62 deletions.
10 changes: 10 additions & 0 deletions adapter/internal/discovery/xds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ func UpdateAPI(vHost string, apiProject mgw.ProjectAPI, deployedEnvironments []*
mgwSwagger.APIProvider = apiProject.APIYaml.Data.Provider
mgwSwagger.EnvironmentID = deployedEnvironments[0].ID
mgwSwagger.EnvironmentName = deployedEnvironments[0].Name

choreoComponentInfo := mgw.ChoreoComponentInfo{
OrganizationID: apiYaml.ChoreoComponentInfo.OrganizationID,
ProjectID: apiYaml.ChoreoComponentInfo.ProjectID,
ComponentID: apiYaml.ChoreoComponentInfo.ComponentID,
VersionID: apiYaml.ChoreoComponentInfo.VersionID,
}

mgwSwagger.ChoreoComponentInfo = &choreoComponentInfo

organizationID := apiProject.OrganizationID
apiHashValue := generateHashValue(apiYaml.Name, apiYaml.Version)

Expand Down
9 changes: 9 additions & 0 deletions adapter/internal/oasparser/config_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ func GetEnforcerAPI(mgwSwagger model.MgwSwagger, lifeCycleState string, vhost st
}
}

choreoComponentInfo := &api.ChoreoComponentInfo{}
if mgwSwagger.ChoreoComponentInfo != nil {
choreoComponentInfo.ComponentID = mgwSwagger.ChoreoComponentInfo.ComponentID
choreoComponentInfo.VersionID = mgwSwagger.ChoreoComponentInfo.VersionID
choreoComponentInfo.OrganizationID = mgwSwagger.ChoreoComponentInfo.OrganizationID
choreoComponentInfo.ProjectID = mgwSwagger.ChoreoComponentInfo.ProjectID
}

return &api.Api{
Id: mgwSwagger.GetID(),
Title: mgwSwagger.GetTitle(),
Expand All @@ -221,6 +229,7 @@ func GetEnforcerAPI(mgwSwagger model.MgwSwagger, lifeCycleState string, vhost st
DeploymentType: mgwSwagger.DeploymentType,
EnvironmentId: mgwSwagger.EnvironmentID,
EnvironmentName: mgwSwagger.EnvironmentName,
ChoreoComponentInfo: choreoComponentInfo,
}
}

Expand Down
90 changes: 72 additions & 18 deletions adapter/internal/oasparser/envoyconf/access_loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,90 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"
)

// getAccessLogConfigs provides file access log configurations for envoy
func getFileAccessLogConfigs() *config_access_logv3.AccessLog {
var logFormat *file_accesslogv3.FileAccessLog_LogFormat
logpath := defaultAccessLogPath //default access log path

logConf := config.ReadLogConfigs()

if !logConf.AccessLogs.Enable {
logger.LoggerOasparser.Info("Accesslog Configurations are disabled.")
return nil
}

formatters := []*corev3.TypedExtensionConfig{
// getAccessLogConfigs provides default formatters
func getDefaultFormatters() []*corev3.TypedExtensionConfig {
return []*corev3.TypedExtensionConfig{
{
Name: "envoy.formatter.req_without_query",
TypedConfig: &anypb.Any{
TypeUrl: "type.googleapis.com/envoy.extensions.formatter.req_without_query.v3.ReqWithoutQuery",
},
},
}
// Set the default log format
logFormat = &file_accesslogv3.FileAccessLog_LogFormat{
}

// getDefaultTextLogFormat provides default text log format
func getDefaultTextLogFormat(loggingFormat string) *file_accesslogv3.FileAccessLog_LogFormat {
formatters := getDefaultFormatters()

return &file_accesslogv3.FileAccessLog_LogFormat{
LogFormat: &corev3.SubstitutionFormatString{
Format: &corev3.SubstitutionFormatString_TextFormatSource{
TextFormatSource: &corev3.DataSource{
Specifier: &corev3.DataSource_InlineString{
InlineString: logConf.AccessLogs.ReservedLogFormat +
strings.TrimLeft(logConf.AccessLogs.SecondaryLogFormat, "'") + "\n",
InlineString: loggingFormat,
},
},
},
Formatters: formatters,
},
}
}

// getAccessLogConfigs provides file access log configurations for envoy
func getInsightsAccessLogConfigs() *config_access_logv3.AccessLog {
var logFormat *file_accesslogv3.FileAccessLog_LogFormat
logpath := defaultAccessLogPath //default access log path

logConf := config.ReadLogConfigs()

if !logConf.InsightsLogs.Enable {
return nil
}

// Set the default log format
loggingFormat := logConf.InsightsLogs.LoggingFormat + "\n"
logFormat = getDefaultTextLogFormat(loggingFormat)

logpath = logConf.InsightsLogs.LogFile
accessLogConf := &file_accesslogv3.FileAccessLog{
Path: logpath,
AccessLogFormat: logFormat,
}

accessLogTypedConf, err := anypb.New(accessLogConf)
if err != nil {
logger.LoggerOasparser.Error("Error marshaling access log configs. ", err)
return nil
}

accessLog := config_access_logv3.AccessLog{
Name: fileAccessLogName,
Filter: nil,
ConfigType: &config_access_logv3.AccessLog_TypedConfig{
TypedConfig: accessLogTypedConf,
},
}

return &accessLog
}

// getAccessLogConfigs provides file access log configurations for envoy
func getFileAccessLogConfigs() *config_access_logv3.AccessLog {
var logFormat *file_accesslogv3.FileAccessLog_LogFormat
logpath := defaultAccessLogPath //default access log path

logConf := config.ReadLogConfigs()

if !logConf.AccessLogs.Enable {
logger.LoggerOasparser.Info("Accesslog Configurations are disabled.")
return nil
}

// Set the default log format
loggingFormat := logConf.AccessLogs.ReservedLogFormat +
strings.TrimLeft(logConf.AccessLogs.SecondaryLogFormat, "'") + "\n"
logFormat = getDefaultTextLogFormat(loggingFormat)

// Configure the log format based on the log type
switch logConf.AccessLogs.LogType {
Expand All @@ -95,7 +145,7 @@ func getFileAccessLogConfigs() *config_access_logv3.AccessLog {
Fields: logFields,
},
},
Formatters: formatters,
Formatters: getDefaultFormatters(),
},
}
logger.LoggerOasparser.Debug("Access log type is set to json.")
Expand Down Expand Up @@ -197,12 +247,16 @@ func getAccessLogs() []*config_access_logv3.AccessLog {
var accessLoggers []*config_access_logv3.AccessLog
fileAccessLog := getFileAccessLogConfigs()
grpcAccessLog := getGRPCAccessLogConfigs(conf)
insightsAccessLog := getInsightsAccessLogConfigs()
if fileAccessLog != nil {
accessLoggers = append(accessLoggers, fileAccessLog)
}
if grpcAccessLog != nil {
accessLoggers = append(accessLoggers, getGRPCAccessLogConfigs(conf))
}
if insightsAccessLog != nil {
accessLoggers = append(accessLoggers, insightsAccessLog)
}
return accessLoggers
}

Expand Down
15 changes: 12 additions & 3 deletions adapter/internal/oasparser/model/mgw_swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ type MgwSwagger struct {
// APIProvider is required for analytics purposes as /apis call is avoided temporarily.
APIProvider string
// DeploymentType could be either "PRODUCTION" or "SANDBOX"
DeploymentType string
EnvironmentID string
EnvironmentName string
DeploymentType string
EnvironmentID string
EnvironmentName string
ChoreoComponentInfo *ChoreoComponentInfo
}

// ChoreoComponentInfo represents the information of the Choreo component
type ChoreoComponentInfo struct {
OrganizationID string
ProjectID string
ComponentID string
VersionID string
}

// EndpointCluster represent an upstream cluster
Expand Down
8 changes: 8 additions & 0 deletions adapter/internal/oasparser/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ type apiData struct {
BackendJWTConfiguration backendJWTConfiguration `json:"backendJWTConfiguration,omitempty"`
EndpointConfig endpointConfigStruct `json:"endpointConfig,omitempty"`
Operations []OperationYaml `json:"Operations,omitempty"`
ChoreoComponentInfo choreoComponentInfo `json:"choreoComponentInfo,omitempty"`
}

type choreoComponentInfo struct {
OrganizationID string `json:"organizationId,omitempty"`
ProjectID string `json:"projectId,omitempty"`
ComponentID string `json:"componentId,omitempty"`
VersionID string `json:"versionId,omitempty"`
}

type backendJWTConfiguration struct {
Expand Down
18 changes: 16 additions & 2 deletions adapter/pkg/config/log_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ type accessLog struct {
JSONFormat map[string]string
}

type insightsLog struct {
Enable bool
LogFile string
LoggingFormat string
OmitEmptyValues bool
}

// AccessLogExcludes represents the configurations related to excludes from access logs.
type AccessLogExcludes struct {
SystemHost AccessLogExcludesSystemHost
Expand All @@ -60,8 +67,9 @@ type LogConfig struct {
Compress bool
}

Pkg []pkg
AccessLogs *accessLog
Pkg []pkg
AccessLogs *accessLog
InsightsLogs *insightsLog
}

func getDefaultLogConfig() *LogConfig {
Expand Down Expand Up @@ -115,6 +123,12 @@ func getDefaultLogConfig() *LogConfig {
"extAuthDtls": "%DYNAMIC_METADATA(envoy.filters.http.ext_authz:extAuthDetails)%",
},
},
InsightsLogs: &insightsLog{
Enable: false,
LogFile: "/dev/stdout",
LoggingFormat: "-",
OmitEmptyValues: true,
},
}
adapterLogConfig.Rotation.MaxSize = 10
adapterLogConfig.Rotation.MaxAge = 2
Expand Down
48 changes: 34 additions & 14 deletions adapter/pkg/discovery/api/wso2/discovery/api/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4a67b46

Please sign in to comment.