MeshAccessLog

With the MeshAccessLog policy you can easily set up access logs on every data plane proxy in a mesh.

This policy uses a new policy matching algorithm. Do not combine with TrafficLog.

This guide assumes you have already configured your observability tools to work with Kuma. If you haven’t, see the observability docs.

targetRef support matrix

targetRefAllowed kinds
targetRef.kindMesh, MeshSubset
to[].targetRef.kindMesh, MeshService, MeshExternalService
from[].targetRef.kindMesh
targetRefAllowed kinds
targetRef.kindMesh, MeshGateway, MeshGateway with tags
to[].targetRef.kindMesh, MeshService
from[].targetRef.kindMesh
targetRefAllowed kinds
targetRef.kindMesh, MeshSubset
to[].targetRef.kindMesh, MeshService, MeshExternalService

To learn more about the information in this table, see the matching docs.

Configuration

Format

Kuma gives you full control over the format of the access logs.

The shape of a single log record is defined by a template string that uses command operators to extract and format data about a TCP connection or an HTTP request.

For example:

  1. %START_TIME% %KUMA_SOURCE_SERVICE% => %KUMA_DESTINATION_SERVICE% %DURATION%

%START_TIME% and %KUMA_SOURCE_SERVICE% are examples of available command operators.

All command operators defined by Envoy are supported, along with additional command operators defined by Kuma:

Command OperatorDescription
%KUMA_MESH%Name of the mesh in which traffic is flowing.
%KUMA_SOURCE_SERVICE%Name of a service that is the source of traffic.
%KUMA_DESTINATION_SERVICE%Name of a service that is the destination of traffic.
%KUMA_SOURCE_ADDRESS_WITHOUT_PORT%Address of a Dataplane that is the source of traffic.
%KUMA_TRAFFIC_DIRECTION%Direction of the traffic, INBOUND, OUTBOUND, or UNSPECIFIED.

All additional access log command operators are valid to use with both TCP and HTTP traffic.

Internally, Kuma determines traffic protocol based on the value of kuma.io/protocol tag on the inbound interface of a destination Dataplane.

There are two types of format, plain and json.

Plain accepts a string with command operators and produces a string output.

JSON accepts a list of key-value pairs that produces a valid JSON object.

It is up to the user to decide which format type to use. Some system will automatically parse JSON logs and allow you to filter and query based on available keys.

If a command operator is specific to HTTP traffic, such as %REQ(X?Y):Z% or %RESP(X?Y):Z%, in the case of TCP traffic it will be replaced by a symbol “-” for plain and a null value for json. You can set the format.omitEmptyValues boolean option to change this to "" for plain and omit them entirely for json.

Plain

The default format string for TCP traffic is:

  1. [%START_TIME%] %RESPONSE_FLAGS% %KUMA_MESH% %KUMA_SOURCE_ADDRESS_WITHOUT_PORT%(%KUMA_SOURCE_SERVICE%)->%UPSTREAM_HOST%(%KUMA_DESTINATION_SERVICE%) took %DURATION%ms, sent %BYTES_SENT% bytes, received: %BYTES_RECEIVED% bytes

The default format string for HTTP traffic is:

  1. [%START_TIME%] %KUMA_MESH% "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%" %RESPONSE_CODE% %RESPONSE_FLAGS% %BYTES_RECEIVED% %BYTES_SENT% %DURATION% %RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)% "%REQ(X-FORWARDED-FOR)%" "%REQ(USER-AGENT)%" "%REQ(X-REQUEST-ID)%" "%REQ(:AUTHORITY)%" "%KUMA_SOURCE_SERVICE%" "%KUMA_DESTINATION_SERVICE%" "%KUMA_SOURCE_ADDRESS_WITHOUT_PORT%" "%UPSTREAM_HOST%"

Example configuration:

  1. format:
  2. type: Plain
  3. plain: '[%START_TIME%] %BYTES_RECEIVED%'

Example output:

  1. [2016-04-15T20:17:00.310Z] 154

JSON

Example configuration:

  1. format:
  2. type: Json
  3. json:
  4. - key: "start_time"
  5. value: "%START_TIME%"
  6. - key: "bytes_received"
  7. value: "%BYTES_RECEIVED%"

Example output:

  1. {
  2. "start_time": "2016-04-15T20:17:00.310Z",
  3. "bytes_received": "154"
  4. }

TCP configuration with default fields:

  1. format:
  2. type: Json
  3. json:
  4. - key: "start_time"
  5. value: "%START_TIME%"
  6. - key: "response_flags"
  7. value: "%RESPONSE_FLAGS%"
  8. - key: "kuma_mesh"
  9. value: "%KUMA_MESH%"
  10. - key: "kuma_source_address_without_port"
  11. value: "%KUMA_SOURCE_ADDRESS_WITHOUT_PORT%"
  12. - key: "kuma_source_service"
  13. value: "%KUMA_SOURCE_SERVICE%"
  14. - key: "upstream_host"
  15. value: "%UPSTREAM_HOST%"
  16. - key: "kuma_destination_service"
  17. value: "%KUMA_DESTINATION_SERVICE%"
  18. - key: "duration_ms"
  19. value: "%DURATION%"
  20. - key: "bytes_sent"
  21. value: "%BYTES_SENT%"
  22. - key: "bytes_received"
  23. value: "%BYTES_RECEIVED%"

HTTP configuration with default fields:

  1. format:
  2. type: Json
  3. json:
  4. - key: "start_time"
  5. value: "%START_TIME%"
  6. - key: "kuma_mesh"
  7. value: "%KUMA_MESH%"
  8. - key: 'method'
  9. value: '"%REQ(:METHOD)%'
  10. - key: "path"
  11. value: "%REQ(X-ENVOY-ORIGINAL-PATH?:PATH)%"
  12. - key: 'protocol'
  13. value: '%PROTOCOL%'
  14. - key: "response_code"
  15. value: "%RESPONSE_CODE%"
  16. - key: "response_flags"
  17. value: "%RESPONSE_FLAGS%"
  18. - key: "bytes_received"
  19. value: "%BYTES_RECEIVED%"
  20. - key: "bytes_sent"
  21. value: "%BYTES_SENT%"
  22. - key: "duration_ms"
  23. value: "%DURATION%"
  24. - key: "upstream_service_time"
  25. value: "%RESP(X-ENVOY-UPSTREAM-SERVICE-TIME)%"
  26. - key: 'x_forwarded_for'
  27. value: '"%REQ(X-FORWARDED-FOR)%"'
  28. - key: 'user_agent'
  29. value: '"%REQ(USER-AGENT)%"'
  30. - key: 'request_id'
  31. value: '"%REQ(X-REQUEST-ID)%"'
  32. - key: 'authority'
  33. value: '"%REQ(:AUTHORITY)%"'
  34. - key: "kuma_source_service"
  35. value: "%KUMA_SOURCE_SERVICE%"
  36. - key: "kuma_destination_service"
  37. value: "%KUMA_DESTINATION_SERVICE%"
  38. - key: "kuma_source_address_without_port"
  39. value: "%KUMA_SOURCE_ADDRESS_WITHOUT_PORT%"
  40. - key: "upstream_host"
  41. value: "%UPSTREAM_HOST%"

Backends

A backend determines where the logs end up.

TCP

A TCP backend streams logs to a server via TCP protocol. You can configure a TCP backend with an address:

  1. backends:
  2. - type: Tcp
  3. tcp:
  4. address: 127.0.0.1:5000

File

A file backend streams logs to a text file. You can configure a file backend with a path:

  1. backends:
  2. - type: File
  3. file:
  4. path: /dev/stdout

OpenTelemetry

An OpenTelemetry (OTel) backend sends data to an OpenTelemetry server. You can configure an OpenTelemetry backend with an endpoint, attributes (which contain additional information about the log) and body (can be a string message, including multi-line, or it can be a structured data). Attributes and endpoints can use placeholders described in the format section.

  1. backends:
  2. - type: OpenTelemetry
  3. openTelemetry:
  4. endpoint: otel-collector:4317
  5. body:
  6. kvlistValue:
  7. values:
  8. - key: "mesh"
  9. value:
  10. stringValue: "%KUMA_MESH%"
  11. attributes:
  12. - key: "start_time"
  13. value: "%START_TIME%"

Body

Body is of type any (defined here) and can be one of the following forms:

  1. body:
  2. stringValue: "%KUMA_MESH%"
  1. body:
  2. boolValue: true
  1. body:
  2. intValue: 123
  1. body:
  2. doubleValue: 1.2
  1. body:
  2. bytesValue: aGVsbG8=
  1. body:
  2. arrayValue:
  3. values:
  4. - stringValue: "%KUMA_MESH%"
  1. body:
  2. kvlistValue:
  3. values:
  4. - key: "mesh"
  5. value:
  6. stringValue: "%KUMA_MESH%"

Examples

Log outgoing traffic from specific frontend version to a backend service

  1. apiVersion: kuma.io/v1alpha1
  2. kind: MeshAccessLog
  3. metadata:
  4. name: frontend-to-backend
  5. namespace: kuma-demo
  6. labels:
  7. kuma.io/mesh: default
  8. spec:
  9. targetRef:
  10. kind: MeshSubset
  11. tags:
  12. app: frontend
  13. version: canary
  14. to:
  15. - targetRef:
  16. kind: MeshService
  17. name: backend_kuma-demo_svc_8080
  18. default:
  19. backends:
  20. - type: File
  21. file:
  22. path: "/dev/stdout"
  1. apiVersion: kuma.io/v1alpha1
  2. kind: MeshAccessLog
  3. metadata:
  4. name: frontend-to-backend
  5. namespace: kuma-demo
  6. labels:
  7. kuma.io/mesh: default
  8. spec:
  9. targetRef:
  10. kind: MeshSubset
  11. tags:
  12. app: frontend
  13. version: canary
  14. to:
  15. - targetRef:
  16. kind: MeshService
  17. name: backend
  18. namespace: kuma-demo
  19. sectionName: http
  20. default:
  21. backends:
  22. - type: File
  23. file:
  24. path: "/dev/stdout"

I am using MeshService

  1. type: MeshAccessLog
  2. name: frontend-to-backend
  3. mesh: default
  4. spec:
  5. targetRef:
  6. kind: MeshSubset
  7. tags:
  8. app: frontend
  9. version: canary
  10. to:
  11. - targetRef:
  12. kind: MeshService
  13. name: backend
  14. default:
  15. backends:
  16. - type: File
  17. file:
  18. path: "/dev/stdout"
  1. type: MeshAccessLog
  2. name: frontend-to-backend
  3. mesh: default
  4. spec:
  5. targetRef:
  6. kind: MeshSubset
  7. tags:
  8. app: frontend
  9. version: canary
  10. to:
  11. - targetRef:
  12. kind: MeshService
  13. name: backend
  14. sectionName: http
  15. default:
  16. backends:
  17. - type: File
  18. file:
  19. path: "/dev/stdout"

Logging to multiple backends

This configuration logs to three backends: TCP, file and OpenTelemetry.

  1. apiVersion: kuma.io/v1alpha1
  2. kind: MeshAccessLog
  3. metadata:
  4. name: multiple-backends
  5. namespace: kuma-demo
  6. labels:
  7. kuma.io/mesh: default
  8. spec:
  9. from:
  10. - targetRef:
  11. kind: Mesh
  12. default:
  13. backends:
  14. - type: Tcp
  15. tcp:
  16. address: 127.0.0.1:5000
  17. format:
  18. type: Json
  19. json:
  20. - key: start_time
  21. value: "%START_TIME%"
  22. - type: File
  23. file:
  24. path: "/dev/stdout"
  25. format:
  26. type: Plain
  27. plain: "[%START_TIME%]"
  28. - type: OpenTelemetry
  29. openTelemetry:
  30. endpoint: otel-collector:4317
  31. body:
  32. kvlistValue:
  33. values:
  34. - key: mesh
  35. value:
  36. stringValue: "%KUMA_MESH%"
  37. attributes:
  38. - key: start_time
  39. value: "%START_TIME%"
  1. type: MeshAccessLog
  2. name: multiple-backends
  3. mesh: default
  4. spec:
  5. from:
  6. - targetRef:
  7. kind: Mesh
  8. default:
  9. backends:
  10. - type: Tcp
  11. tcp:
  12. address: 127.0.0.1:5000
  13. format:
  14. type: Json
  15. json:
  16. - key: start_time
  17. value: "%START_TIME%"
  18. - type: File
  19. file:
  20. path: "/dev/stdout"
  21. format:
  22. type: Plain
  23. plain: "[%START_TIME%]"
  24. - type: OpenTelemetry
  25. openTelemetry:
  26. endpoint: otel-collector:4317
  27. body:
  28. kvlistValue:
  29. values:
  30. - key: mesh
  31. value:
  32. stringValue: "%KUMA_MESH%"
  33. attributes:
  34. - key: start_time
  35. value: "%START_TIME%"

Log all incoming and outgoing traffic

For this use case we recommend creating two separate policies. One for incoming traffic:

  1. apiVersion: kuma.io/v1alpha1
  2. kind: MeshAccessLog
  3. metadata:
  4. name: all-incoming-traffic
  5. namespace: kuma-demo
  6. labels:
  7. kuma.io/mesh: default
  8. spec:
  9. from:
  10. - targetRef:
  11. kind: Mesh
  12. default:
  13. backends:
  14. - type: File
  15. file:
  16. path: "/dev/stdout"
  1. type: MeshAccessLog
  2. name: all-incoming-traffic
  3. mesh: default
  4. spec:
  5. from:
  6. - targetRef:
  7. kind: Mesh
  8. default:
  9. backends:
  10. - type: File
  11. file:
  12. path: "/dev/stdout"

And one for outgoing traffic:

  1. apiVersion: kuma.io/v1alpha1
  2. kind: MeshAccessLog
  3. metadata:
  4. name: all-outgoing-traffic
  5. namespace: kuma-demo
  6. labels:
  7. kuma.io/mesh: default
  8. spec:
  9. to:
  10. - targetRef:
  11. kind: Mesh
  12. default:
  13. backends:
  14. - type: File
  15. file:
  16. path: "/dev/stdout"
  1. type: MeshAccessLog
  2. name: all-outgoing-traffic
  3. mesh: default
  4. spec:
  5. to:
  6. - targetRef:
  7. kind: Mesh
  8. default:
  9. backends:
  10. - type: File
  11. file:
  12. path: "/dev/stdout"

Logging traffic going outside the Mesh

To target ExternalServices, use MeshService as the targetRef kind with name set to
the kuma.io/service value.

To target other non-mesh traffic, for example passthrough traffic, use Mesh as the targetRef kind. In this case, %KUMA_DESTINATION_SERVICE% is set to external.

Select a built-in gateway

You can select a built-in gateway using the kuma.io/service value. A current limitation is that traffic routed from a gateway to a service is logged by that gateway as having destination "*".

All policy options