Enrich access logs
Use a transformation template to inject additional metadata to your access logs.
In this guide, you use an Inja function to extract an environment variable value from the gateway proxy pod and add this value to the dynamic metadata filter in Envoy. Then, you inject the value that you stored in the dynamic metadata filter when writing access logs.
This guide uses a subset of the supported transformation template attributes. To review all the attributes that you can set, see Templating language.
Before you begin
Follow the Get started guide to install Gloo Gateway.
Follow the Sample app guide to create a gateway proxy with an HTTP listener and deploy the httpbin sample app.
Get the external address of the gateway and save it in an environment variable.
Enrich access logs
Follow the steps to enable access logs on your gateway proxy.
Create a GlooTrafficPolicy with the following transformation rules:
- The
envInja function is used to extract thePOD_NAMEenvironment variable from the gateway proxy pod. - The value of the environment variable is added to the
pod_namekey in the dynamic metadata values.
kubectl apply -f- <<EOF apiVersion: gloo.solo.io/v1alpha1 kind: GlooTrafficPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin glooTransformation: stages: early: requests: - transformation: template: dynamicMetadataValues: - key: 'pod_name' value: '{{ env("POD_NAME") }}' EOF- The
Update the HTTPListenerPolicy resource to include the
pod_namefrom the dynamic metadata values.kubectl apply -f- <<EOF apiVersion: gateway.kgateway.dev/v1alpha1 kind: HTTPListenerPolicy metadata: name: access-logs namespace: gloo-system spec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: http accessLog: - fileSink: path: /dev/stdout jsonFormat: start_time: "%START_TIME%" method: "%REQ(X-ENVOY-ORIGINAL-METHOD?:METHOD)%" path: "%REQ(X-ENVOY-ORIGINAL-PATH?:PATH)%" protocol: "%PROTOCOL%" response_code: "%RESPONSE_CODE%" response_flags: "%RESPONSE_FLAGS%" bytes_received: "%BYTES_RECEIVED%" bytes_sent: "%BYTES_SENT%" total_duration: "%DURATION%" resp_backend_service_time: "%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)%" req_x_forwarded_for: "%REQ(X-FORWARDED-FOR)%" user_agent: "%REQ(USER-AGENT)%" request_id: "%REQ(X-REQUEST-ID)%" authority: "%REQ(:AUTHORITY)%" backendHost: "%UPSTREAM_HOST%" backendCluster: "%UPSTREAM_CLUSTER%" pod_name: '%DYNAMIC_METADATA(io.solo.transformation:pod_name)%' EOFSend another request to the httpbin app.
Review the access logs and verify that the
pod_nameis now injected into your access logs.kubectl -n gloo-system logs deployments/http | tail -1 | jq --sort-keysExample output:
{ "authority": "www.example.com:8080", "bytes_received": 0, "bytes_sent": 0, "method": "GET", "path": "/status/200", "pod_name": "http-844ff8bc4d-dh4pn", "protocol": "HTTP/1.1", "req_x_forwarded_for": null, "request_id": "132892e6-085e-400f-98da-c055ca04b714", "resp_upstream_service_time": "2", "response_code": 200, "response_flags": "-", "start_time": "2025-05-14T16:43:07.506Z", "total_duration": 2, "upstreamCluster": "kube-svc_httpbin-httpbin-8000_httpbin_httpbin_httpbin_8000", "upstreamHost": "10.0.12.250:8080", "user_agent": "curl/8.7.1" }
Cleanup
You can remove the resources that you created in this guide.
kubectl delete glootrafficpolicy transformation -n httpbin
kubectl delete httplistenerpolicy access-logs -n gloo-system