|
| 1 | +apiVersion: networking.istio.io/v1alpha3 |
| 2 | +kind: EnvoyFilter |
| 3 | +metadata: |
| 4 | + name: accesslog-print-body |
| 5 | + # highlight-next-line |
| 6 | + namespace: test # 只为 test 命名空间开启 accesslog,若改为 istio-system 表示作用于所有命名空间 |
| 7 | +spec: |
| 8 | + # highlight-start |
| 9 | + workloadSelector: # 通常打印 body 用于调试,可指定改配置只作用于指定 workload,避免影响其它不需要调试的 workload 的性能 |
| 10 | + labels: |
| 11 | + app: nginx |
| 12 | + # highlight-end |
| 13 | + configPatches: |
| 14 | + - applyTo: NETWORK_FILTER |
| 15 | + match: |
| 16 | + listener: |
| 17 | + filterChain: |
| 18 | + filter: |
| 19 | + name: "envoy.filters.network.http_connection_manager" |
| 20 | + patch: |
| 21 | + operation: MERGE |
| 22 | + value: |
| 23 | + typed_config: |
| 24 | + "@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager" |
| 25 | + access_log: |
| 26 | + - name: envoy.access_loggers.file |
| 27 | + typed_config: |
| 28 | + "@type": "type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog" |
| 29 | + path: /dev/stdout |
| 30 | + log_format: |
| 31 | + json_format: |
| 32 | + start_time: "%START_TIME%" |
| 33 | + route_name: "%ROUTE_NAME%" |
| 34 | + method: "%REQ(:METHOD)%" |
| 35 | + path: "%REQ(X-ENVOY-ORIGINAL-PATH?:PATH)%" |
| 36 | + protocol: "%PROTOCOL%" |
| 37 | + response_code: "%RESPONSE_CODE%" |
| 38 | + response_flags: "%RESPONSE_FLAGS%" |
| 39 | + response_code_details: "%RESPONSE_CODE_DETAILS%" |
| 40 | + connection_termination_details: "%CONNECTION_TERMINATION_DETAILS%" |
| 41 | + bytes_received: "%BYTES_RECEIVED%" |
| 42 | + bytes_sent: "%BYTES_SENT%" |
| 43 | + duration: "%DURATION%" |
| 44 | + upstream_service_time: "%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)%" |
| 45 | + x_forwarded_for: "%REQ(X-FORWARDED-FOR)%" |
| 46 | + user_agent: "%REQ(USER-AGENT)%" |
| 47 | + request_id: "%REQ(X-REQUEST-ID)%" |
| 48 | + authority: "%REQ(:AUTHORITY)%" |
| 49 | + upstream_host: "%UPSTREAM_HOST%" |
| 50 | + upstream_cluster: "%UPSTREAM_CLUSTER%" |
| 51 | + upstream_local_address: "%UPSTREAM_LOCAL_ADDRESS%" |
| 52 | + downstream_local_address: "%DOWNSTREAM_LOCAL_ADDRESS%" |
| 53 | + downstream_remote_address: "%DOWNSTREAM_REMOTE_ADDRESS%" |
| 54 | + requested_server_name: "%REQUESTED_SERVER_NAME%" |
| 55 | + upstream_transport_failure_reason: "%UPSTREAM_TRANSPORT_FAILURE_REASON%" |
| 56 | + # highlight-start |
| 57 | + request_body: "%DYNAMIC_METADATA(envoy.lua:request_body)%" |
| 58 | + response_body: "%DYNAMIC_METADATA(envoy.lua:response_body)%" |
| 59 | + # highlight-end |
| 60 | + - applyTo: HTTP_FILTER |
| 61 | + match: |
| 62 | + context: ANY |
| 63 | + listener: |
| 64 | + filterChain: |
| 65 | + filter: |
| 66 | + name: "envoy.filters.network.http_connection_manager" |
| 67 | + subFilter: |
| 68 | + name: "envoy.filters.http.router" |
| 69 | + patch: |
| 70 | + operation: INSERT_BEFORE |
| 71 | + value: |
| 72 | + name: envoy.lua |
| 73 | + typed_config: |
| 74 | + "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua" |
| 75 | + # highlight-start |
| 76 | + inlineCode: | |
| 77 | + function envoy_on_request(request_handle) |
| 78 | + local request_body_buffer = request_handle:body() |
| 79 | + if(request_body_buffer == nil) |
| 80 | + then |
| 81 | + request_handle:streamInfo():dynamicMetadata():set("envoy.lua", "request_body", "-") |
| 82 | + else |
| 83 | + local request_body_data = request_body_buffer:getBytes(0, request_body_buffer:length()) |
| 84 | + request_handle:streamInfo():dynamicMetadata():set("envoy.lua", "request_body", request_body_data) |
| 85 | + end |
| 86 | + end |
| 87 | + function envoy_on_response(response_handle) |
| 88 | + local response_body_buffer = response_handle:body() |
| 89 | + if(response_body_buffer == nil) |
| 90 | + then |
| 91 | + response_handle:streamInfo():dynamicMetadata():set("envoy.lua", "response_body", "-") |
| 92 | + else |
| 93 | + local response_body_data = response_body_buffer:getBytes(0, response_body_buffer:length()) |
| 94 | + response_handle:streamInfo():dynamicMetadata():set("envoy.lua", "response_body", response_body_data) |
| 95 | + end |
| 96 | + end |
| 97 | + # highlight-end |
0 commit comments