Skip to content

Commit 0c4bd35

Browse files
authored
Remoe check for accesslog formatter (#5985)
* Remoe check for accesslog formatter Signed-off-by: zirain <[email protected]> * gen Signed-off-by: zirain <[email protected]> * lint Signed-off-by: zirain <[email protected]> --------- Signed-off-by: zirain <[email protected]>
1 parent 4a25025 commit 0c4bd35

File tree

2 files changed

+11
-123
lines changed

2 files changed

+11
-123
lines changed

internal/xds/translator/accesslog.go

Lines changed: 11 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
cel "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/filters/cel/v3"
1717
grpcaccesslog "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/grpc/v3"
1818
otelaccesslog "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/open_telemetry/v3"
19-
celformatter "github.com/envoyproxy/go-control-plane/envoy/extensions/formatter/cel/v3"
20-
metadataformatter "github.com/envoyproxy/go-control-plane/envoy/extensions/formatter/metadata/v3"
2119
reqwithoutqueryformatter "github.com/envoyproxy/go-control-plane/envoy/extensions/formatter/req_without_query/v3"
2220
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
2321
otlpcommonv1 "go.opentelemetry.io/proto/otlp/common/v1"
@@ -35,8 +33,6 @@ const (
3533
otelAccessLog = "envoy.access_loggers.open_telemetry"
3634

3735
reqWithoutQueryCommandOperator = "%REQ_WITHOUT_QUERY"
38-
metadataCommandOperator = "%METADATA"
39-
celCommandOperator = "%CEL"
4036

4137
tcpGRPCAccessLog = "envoy.access_loggers.tcp_grpc"
4238
celFilter = "envoy.access_loggers.extension_filters.cel"
@@ -76,45 +72,17 @@ var listenerAccessLogFilter = &accesslog.AccessLogFilter{
7672
},
7773
}
7874

79-
var (
80-
// reqWithoutQueryFormatter configures additional formatters needed for some of the format strings like "REQ_WITHOUT_QUERY"
81-
reqWithoutQueryFormatter *cfgcore.TypedExtensionConfig
82-
83-
// metadataFormatter configures additional formatters needed for some of the format strings like "METADATA"
84-
// for more information, see https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/formatter/metadata/v3/metadata.proto
85-
metadataFormatter *cfgcore.TypedExtensionConfig
86-
87-
// celFormatter configures additional formatters needed for some of the format strings like "CEL"
88-
// for more information, see https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/formatter/cel/v3/cel.proto
89-
celFormatter *cfgcore.TypedExtensionConfig
90-
)
75+
// reqWithoutQueryFormatter configures additional formatters needed for some of the format strings like "REQ_WITHOUT_QUERY"
76+
var reqWithoutQueryFormatter *cfgcore.TypedExtensionConfig
9177

9278
func init() {
93-
any, err := proto.ToAnyWithValidation(&reqwithoutqueryformatter.ReqWithoutQuery{})
79+
anyCfg, err := proto.ToAnyWithValidation(&reqwithoutqueryformatter.ReqWithoutQuery{})
9480
if err != nil {
9581
panic(err)
9682
}
9783
reqWithoutQueryFormatter = &cfgcore.TypedExtensionConfig{
9884
Name: "envoy.formatter.req_without_query",
99-
TypedConfig: any,
100-
}
101-
102-
any, err = proto.ToAnyWithValidation(&metadataformatter.Metadata{})
103-
if err != nil {
104-
panic(err)
105-
}
106-
metadataFormatter = &cfgcore.TypedExtensionConfig{
107-
Name: "envoy.formatter.metadata",
108-
TypedConfig: any,
109-
}
110-
111-
any, err = proto.ToAnyWithValidation(&celformatter.Cel{})
112-
if err != nil {
113-
panic(err)
114-
}
115-
celFormatter = &cfgcore.TypedExtensionConfig{
116-
Name: "envoy.formatter.cel",
117-
TypedConfig: any,
85+
TypedConfig: anyCfg,
11886
}
11987
}
12088

@@ -388,7 +356,7 @@ func celAccessLogFilter(expr string) (*accesslog.AccessLogFilter, error) {
388356
fl := &cel.ExpressionFilter{
389357
Expression: expr,
390358
}
391-
any, err := proto.ToAnyWithValidation(fl)
359+
anyCfg, err := proto.ToAnyWithValidation(fl)
392360
if err != nil {
393361
return nil, err
394362
}
@@ -397,7 +365,7 @@ func celAccessLogFilter(expr string) (*accesslog.AccessLogFilter, error) {
397365
FilterSpecifier: &accesslog.AccessLogFilter_ExtensionFilter{
398366
ExtensionFilter: &accesslog.ExtensionFilter{
399367
Name: celFilter,
400-
ConfigType: &accesslog.ExtensionFilter_TypedConfig{TypedConfig: any},
368+
ConfigType: &accesslog.ExtensionFilter_TypedConfig{TypedConfig: anyCfg},
401369
},
402370
},
403371
}, nil
@@ -441,36 +409,20 @@ func accessLogTextFormatters(text string) []*cfgcore.TypedExtensionConfig {
441409
formatters = append(formatters, reqWithoutQueryFormatter)
442410
}
443411

444-
if strings.Contains(text, metadataCommandOperator) {
445-
formatters = append(formatters, metadataFormatter)
446-
}
447-
448-
if strings.Contains(text, celCommandOperator) {
449-
formatters = append(formatters, celFormatter)
450-
}
451-
452412
return formatters
453413
}
454414

455415
func accessLogJSONFormatters(json map[string]string) []*cfgcore.TypedExtensionConfig {
456-
reqWithoutQuery, metadata, cel := false, false, false
416+
reqWithoutQuery := false
457417

458418
for _, value := range json {
459-
if reqWithoutQuery && metadata && cel {
419+
if reqWithoutQuery {
460420
break
461421
}
462422

463423
if strings.Contains(value, reqWithoutQueryCommandOperator) {
464424
reqWithoutQuery = true
465425
}
466-
467-
if strings.Contains(value, metadataCommandOperator) {
468-
metadata = true
469-
}
470-
471-
if strings.Contains(value, celCommandOperator) {
472-
cel = true
473-
}
474426
}
475427

476428
formatters := make([]*cfgcore.TypedExtensionConfig, 0, 3)
@@ -479,64 +431,28 @@ func accessLogJSONFormatters(json map[string]string) []*cfgcore.TypedExtensionCo
479431
formatters = append(formatters, reqWithoutQueryFormatter)
480432
}
481433

482-
if metadata {
483-
formatters = append(formatters, metadataFormatter)
484-
}
485-
486-
if cel {
487-
formatters = append(formatters, celFormatter)
488-
}
489-
490434
return formatters
491435
}
492436

493437
func accessLogOpenTelemetryFormatters(body string, attributes map[string]string) []*cfgcore.TypedExtensionConfig {
494-
reqWithoutQuery, metadata, cel := false, false, false
438+
var reqWithoutQuery bool
495439

496440
if strings.Contains(body, reqWithoutQueryCommandOperator) {
497441
reqWithoutQuery = true
498442
}
499443

500-
if strings.Contains(body, metadataCommandOperator) {
501-
metadata = true
502-
}
503-
504-
if strings.Contains(body, celCommandOperator) {
505-
cel = true
506-
}
507-
508444
for _, value := range attributes {
509-
if reqWithoutQuery && metadata && cel {
510-
break
511-
}
512-
513-
if !reqWithoutQuery && strings.Contains(value, reqWithoutQueryCommandOperator) {
445+
if strings.Contains(value, reqWithoutQueryCommandOperator) {
514446
reqWithoutQuery = true
515-
}
516-
517-
if !metadata && strings.Contains(value, metadataCommandOperator) {
518-
metadata = true
519-
}
520-
521-
if !cel && strings.Contains(value, celCommandOperator) {
522-
cel = true
447+
break
523448
}
524449
}
525450

526451
formatters := make([]*cfgcore.TypedExtensionConfig, 0, 3)
527-
528452
if reqWithoutQuery {
529453
formatters = append(formatters, reqWithoutQueryFormatter)
530454
}
531455

532-
if metadata {
533-
formatters = append(formatters, metadataFormatter)
534-
}
535-
536-
if cel {
537-
formatters = append(formatters, celFormatter)
538-
}
539-
540456
return formatters
541457
}
542458

internal/xds/translator/testdata/out/xds-ir/accesslog-formatters.listeners.yaml

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
typedConfig:
88
'@type': type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
99
logFormat:
10-
formatters:
11-
- name: envoy.formatter.cel
12-
typedConfig:
13-
'@type': type.googleapis.com/envoy.extensions.formatter.cel.v3.Cel
1410
textFormatSource:
1511
inlineString: |
1612
[%START_TIME%] "%CEL(request.method)% %CEL(request.path)% %CEL(request.protocol)%" %CEL(response.code)% %CEL(response.flags)% %CEL(request.total_size)% %CEL(response.total_size)% %CEL(request.duration)% %CEL(response.headers['X-ENVOY-UPSTREAM-SERVICE-TIME'])% "%CEL(request.headers['X-FORWARDED-FOR'])%" "%CEL(request.useragent)%" "%CEL(request.id)%" "%CEL(request.host)%" "%CEL(upstream.address)%"
@@ -23,10 +19,6 @@
2319
typedConfig:
2420
'@type': type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
2521
logFormat:
26-
formatters:
27-
- name: envoy.formatter.metadata
28-
typedConfig:
29-
'@type': type.googleapis.com/envoy.extensions.formatter.metadata.v3.Metadata
3022
jsonFormat:
3123
method: '%REQ(:METHOD)%'
3224
path: '%REQ(X-ENVOY-ORIGINAL-PATH?:PATH)%'
@@ -92,12 +84,6 @@
9284
- name: envoy.formatter.req_without_query
9385
typedConfig:
9486
'@type': type.googleapis.com/envoy.extensions.formatter.req_without_query.v3.ReqWithoutQuery
95-
- name: envoy.formatter.metadata
96-
typedConfig:
97-
'@type': type.googleapis.com/envoy.extensions.formatter.metadata.v3.Metadata
98-
- name: envoy.formatter.cel
99-
typedConfig:
100-
'@type': type.googleapis.com/envoy.extensions.formatter.cel.v3.Cel
10187
resourceAttributes:
10288
values:
10389
- key: cluster_name
@@ -117,10 +103,6 @@
117103
typedConfig:
118104
'@type': type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
119105
logFormat:
120-
formatters:
121-
- name: envoy.formatter.cel
122-
typedConfig:
123-
'@type': type.googleapis.com/envoy.extensions.formatter.cel.v3.Cel
124106
textFormatSource:
125107
inlineString: |
126108
[%START_TIME%] "%CEL(request.method)% %CEL(request.path)% %CEL(request.protocol)%" %CEL(response.code)% %CEL(response.flags)% %CEL(request.total_size)% %CEL(response.total_size)% %CEL(request.duration)% %CEL(response.headers['X-ENVOY-UPSTREAM-SERVICE-TIME'])% "%CEL(request.headers['X-FORWARDED-FOR'])%" "%CEL(request.useragent)%" "%CEL(request.id)%" "%CEL(request.host)%" "%CEL(upstream.address)%"
@@ -129,10 +111,6 @@
129111
typedConfig:
130112
'@type': type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
131113
logFormat:
132-
formatters:
133-
- name: envoy.formatter.metadata
134-
typedConfig:
135-
'@type': type.googleapis.com/envoy.extensions.formatter.metadata.v3.Metadata
136114
jsonFormat:
137115
method: '%REQ(:METHOD)%'
138116
path: '%REQ(X-ENVOY-ORIGINAL-PATH?:PATH)%'
@@ -190,12 +168,6 @@
190168
- name: envoy.formatter.req_without_query
191169
typedConfig:
192170
'@type': type.googleapis.com/envoy.extensions.formatter.req_without_query.v3.ReqWithoutQuery
193-
- name: envoy.formatter.metadata
194-
typedConfig:
195-
'@type': type.googleapis.com/envoy.extensions.formatter.metadata.v3.Metadata
196-
- name: envoy.formatter.cel
197-
typedConfig:
198-
'@type': type.googleapis.com/envoy.extensions.formatter.cel.v3.Cel
199171
resourceAttributes:
200172
values:
201173
- key: cluster_name

0 commit comments

Comments
 (0)