envoyfilter.yaml
apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata: name: reviews-lua namespace: default #应用的名称空间 spec: workloadSelector: labels: app: crm-bms #app label configPatches: - applyTo: HTTP_FILTER match: context: SIDECAR_INBOUND listener: portNumber: 80 filterChain: filter: name: "envoy.filters.network.http_connection_manager" subFilter: name: "envoy.filters.http.router" patch: operation: INSERT_BEFORE value: name: envoy.lua typed_config: "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua" inlineCode: | function envoy_on_request(request_handle) local request_body_buffer = request_handle:body() if(request_body_buffer == nil) then request_handle:streamInfo():dynamicMetadata():set("envoy.lua", "requestBody", "-") else local request_body_data = request_body_buffer:getBytes(0, request_body_buffer:length()) request_handle:streamInfo():dynamicMetadata():set("envoy.lua", "requestBody", request_body_data) end end function envoy_on_response(response_handle) local response_body_buffer = response_handle:body() if(response_body_buffer == nil) then response_handle:streamInfo():dynamicMetadata():set("envoy.lua", "responseBody", "-") else local response_body_data = response_body_buffer:getBytes(0, response_body_buffer:length()) response_handle:streamInfo():dynamicMetadata():set("envoy.lua", "responseBody", response_body_data) end end
将以上规则应用到集群,然后修改istio配置文件,定制日志格式: accessLogFormat: "%RESPONSE_CODE% %DYNAMIC_METADATA(envoy.lua)%\n"
用以下命令测试:curl -d '"test":"ok"' crm-bms
查看效果: