PrintBody plug-in

Updated at:

When debugging API issues or auditing traffic in a service mesh, you often need to inspect the actual HTTP request and response payloads flowing through your sidecar proxies. The PrintBody plug-in captures HTTP request and response bodies at the sidecar proxy and prints them in access logs with a single configuration parameter.

Important

PrintBody requires a custom access log variable to surface captured bodies. Add a variable with the value %DYNAMIC_METADATA(envoy.lua)% to your access log format. You choose the variable name -- this guide uses http_body.

How it works

PrintBody uses an Envoy Lua filter to read HTTP bodies and write them to dynamic metadata. The %DYNAMIC_METADATA(envoy.lua)% access log variable renders that metadata into each log entry, requiring only a single port_number parameter to configure.

Prerequisites

Before you begin, make sure that you have:

Configuration reference

ParameterTypeRequiredDefaultDescription
port_numberstringYesNoneContainer port of the pod corresponding to the service that is used to parse and respond to the request and response bodies. For example, set to "80" for a service listening on port 80.

Enable PrintBody and verify body logging

This procedure walks through three tasks: enabling the plug-in, adding a body field to your access log format, and verifying the output.

Step 1: Enable the plug-in

Configure PrintBody with the target service's container port. For example, httpbin listens on port 80:

port_number: "80"

Set the plug-in scope to Global so that all sidecar proxies in the mesh capture bodies for traffic directed at port 80.

Step 2: Add a body field to the access log format

Follow the steps in Customize data plane access logs to add a new field to the access log format:

http_body: %DYNAMIC_METADATA(envoy.lua)%

When a request is logged, this field contains both the request body and the response body.

Step 3: Send a test request

Send a POST request with a JSON body to the httpbin service through the ingress gateway:

curl -H "Content-Type: application/json" \
     -X POST \
     -d '{"msg":"OK!"}' \
     <gateway-ip-address>/post

Replace <gateway-ip-address> with the external IP of your ingress gateway.

Expected response:

{
  "args": {},
  "data": "{\"msg\":\"OK!\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "<gateway-ip-address>",
    "User-Agent": "curl/7.79.1",
    "X-Envoy-Attempt-Count": "1",
    "X-Envoy-Internal": "true",
    "X-Forwarded-Client-Cert": "By=spiffe://..."
  },
  "json": {
    "msg": "OK!"
  },
  "origin": "...",
  "url": "http://<gateway-ip-address>/post"
}

Step 4: Check the access log

View the sidecar access log in the httpbin pod:

kubectl logs <httpbin-pod-name> -c istio-proxy | grep msg

Replace <httpbin-pod-name> with the actual pod name (run kubectl get pods to find it).

Access log output structure

The http_body field in the access log contains two subfields:

SubfieldContentExample value
requestBodyThe body sent in the HTTP request{"msg":"OK!"}
responseBodyThe body returned by the upstream serviceThe full JSON response from httpbin

Sample access log entry (formatted for readability):

{
  "method": "POST",
  "path": "/post",
  "protocol": "HTTP/1.1",
  "response_code": 200,
  "downstream_local_address": "172.17.80.6:80",
  "upstream_cluster": "inbound|80||",
  "http_body": {
    "requestBody": "{\"msg\":\"OK!\"}",
    "responseBody": "{\n  \"args\": {}, \n  \"data\": \"{\\\"msg\\\":\\\"OK!\\\"}\", \n  ...  \n}"
  }
}
Note

The actual log entry contains additional Envoy fields such as start_time, bytes_sent, x_forwarded_for, request_id, and upstream_service_time. The sample above highlights the fields most relevant to body logging.