Log fields and correlation

Every Klok service writes three kinds of log records, and all three end up in the tenant's OpenSearch:

Log type What it is Written by Index
transaction one document per global chain, its status and counters klok-egress-opensearch (KLK001E6 from KlokLogEventV6, KLK001E2 from KlokLogEventV4) klok-logs-<tenant>-trans-<yyyy>-w<ww>
event one document per log event: START, PROGRESS, FINISHED, FAILED of an invocation klok-egress-opensearch (KLK001E5 from KlokLogEventV6, KLK001E1 from KlokLogEventV4) klok-logs-<tenant>-event-<yyyy>-w<ww>
console one document per console line a pod printed Fluent Bit (klok-environment-setup, role k8s_logging) klok-logs-<namespace>-console-<GGGG>-w<VV>

This page is the one definition of the fields those records carry. It has two parts. The first part covers the three correlation ids and how a service gets them. The second part covers the field names, which are the same in all three types, so that a transaction, its events and its console lines show up side by side in one OpenSearch Dashboards table and one query finds all of them.

The three correlation ids

A chain of work is followed at three widths. Each id is narrower than the one before it.

Id Property / header MDC key Field What it groups
global klok-corr-global klok.corr-global klok_corr_global everything that belongs to one business call, inside Klok and outside it
platform klok-corr-platform klok.corr-platform klok_corr_platform everything one call does inside the Klok platform, across the broker
service klok-corr-service klok.corr-service klok_corr_service everything one service does with one input, up to the broker or from it

The invocation ids, klok-invocation-id and klok-parent-invocation-id, stay as the finest level: one route invocation and the invocation it came from.

None of the three ids is ever empty inside a Klok service. Each one is taken from the input when the rules below allow it. When it is missing, the service creates it. Every log event, every console line written inside an invocation and every broker message carries the ids that its rules say it carries.

Global: the id that may come from outside

The global id is the only id a caller outside Klok may give us, and the only one we pass on to systems outside Klok. A caller that sends the same id on its next call, or a system that sends it back to us, keeps the whole conversation under one global id.

The service takes the first usable value from the input, in this order:

Order Source Value Source field value
1 klok-corr-global as is klok
2 klok-correlation-id, then klokChainId (Klok's earlier names, read during the transition) as is klok
3 W3C traceparent (OpenTelemetry) the 32 hex digit trace id traceparent
4 Camel breadcrumbId as it arrived (never the one Camel creates for the exchange) as is breadcrumbId
5 x-correlation-id, correlation-id, correlationId, x-request-id, request-id, x-b3-traceid, b3 (its trace id), x-amzn-trace-id (its Root=), x-cloud-trace-context (its trace id), in that order as described the header name in lower case
6 nothing usable a new KlokFastID generated

Header names are matched without regard to case. A value is usable when it has 1–128 characters from `A-Z a-z 0-9 . _ :

  • / = + @. A traceparent` trace id of all zeros is not usable. When a value is not usable, the next source in the order is tried. The order is the same for HTTP requests, broker messages, files and timers. A timer or a file pickup has no incoming headers, so it starts with a new id.

klok_corr_global_source records which source gave the value. It is a log field and an MDC key, and it is never a message property.

Where it goes:

  • It goes on every broker message, as klok-corr-global, and it is also the message key.
  • It goes on every HTTP request a Klok channel adapter sends to another system, as klok-corr-global. <RouteId>.correlation.outgoing-headers can add more header names for a system that expects its own (x-correlation-id, for example). By default it adds none.
  • It goes on every HTTP response, as klok-corr-global.

Platform: the part of the chain inside Klok

The platform id groups what one call does inside Klok. It follows the work across the broker, from the onramp through every processing leg to every offramp. It never leaves the platform. An outgoing call to another system never carries it, so a system that calls us back starts a new platform id. The global id still joins the two.

  • It is created when work enters Klok from outside: an HTTP request, a webhook, a file pickup, a timer, a consumed message without klok-corr-platform.
  • It is never taken from an HTTP request, even when the request has the header. An HTTP caller is outside the platform, so its call is a new platform chain.
  • It is taken from a consumed broker message (klok-corr-platform) and copied onto every message published while handling it.

Service: one service's share

The service id groups what one service does with one input: from an incoming request or a consumed message up to the message it publishes, or up to the answer it gives. It never goes on a broker message and never on an HTTP request or response.

  • It is created for every incoming HTTP request, every consumed broker message, every timer firing and every file.
  • Sub routes, the direct calls a route makes inside the same service, and request/reply calls made while handling the input keep the service id of the input.

How the ids narrow a search

  • Transaction, by klok_corr_global: the whole business call, possibly several platform chains when the call went out to another system and came back.
  • Platform, by klok_corr_platform: one side of a long chain, from one onramp through the broker to its offramps.
  • Service, by klok_corr_service: what one service did with one message or one request.
  • Invocation, by klok_invocation_id, or route, by klok_route_id: one route.

Transition from klok-correlation-id

Services built before this change carry one id, klok-correlation-id, which plays the role of both global and platform. Until every service in the fleet has been rebuilt:

  • A service that consumes a message with no klok-corr-global takes klok-correlation-id (or klokChainId) as the global id. When the message also has no klok-corr-platform, it uses the same value as the platform id, so the chain stays together across old and new services.
  • A service publishes klok-correlation-id with the value of the global id next to klok-corr-global, so old consumers still continue the chain.
  • klok-egress-opensearch keeps its KlokLogEventV4 receiver. It stores V4 events under the field names below: globalChainId becomes both klok_corr_global and klok_corr_platform, and an event without a service id gets none.

Writing klok-correlation-id stops once the fleet no longer publishes KlokLogEventV4 (klok-common#12).

Field names

The rule

A Klok context value has one name, in all of its forms:

  • the message property and HTTP header: klok-corr-global
  • the MDC key, made with mdcKey: klok.corr-global
  • the OpenSearch field and the JSON console key: klok_corr_global, the property name with every - and . written as _

The same rule covers values a route adds itself. klok.file.name becomes klok_file_name. Fields that are not Klok context use the usual names that OpenSearch Dashboards and other tools expect: @timestamp, message, log_level, logger, log_type, kubernetes.*.

Every klok_* string field is mapped as keyword, so a query uses the field itself and never <field>.keyword. message is text. Numbers are long.

Fields every type has

Field Meaning transaction event console
@timestamp when it happened; for a transaction, its last event ✓ ✓ ✓
log_type transaction, event or console ✓ ✓ ✓
message the readable text: what the line says, the event's text, the chain's summary ✓ ✓ ✓
log_level TRACE, DEBUG, INFO, WARN, ERROR; for a transaction, its worst level ✓ ✓ ✓
klok_tenant the tenant of the service ✓ ✓ ✓
klok_domain the domain of the service ✓ ✓ ✓
klok_environment the environment (prod, test, ...) ✓ ✓ ✓
klok_instance the application instance (the service's name) ✓ (list) ✓ ✓
klok_corr_global the global id ✓ (the document id) ✓ ✓ inside an invocation
klok_corr_global_source where the global id came from (see above) ✓ ✓ ✓ inside an invocation
klok_corr_platform the platform id ✓ (list) ✓ ✓ inside an invocation
klok_corr_service the service id — ✓ ✓ inside an invocation
klok_solution_id KLK002 ✓ (list) ✓ ✓ inside an invocation
klok_route_id KLK002I1_OdlScheduledQuery ✓ (list) ✓ ✓ inside an invocation
klok_task_id the async task, when there is one ✓ ✓ ✓
klok_trace_id the W3C trace id the input came with ✓ (list) ✓ ✓

On a console line, a field is present when the line was written inside an invocation. klok_tenant, klok_domain, klok_environment and klok_instance are on every line a Klok service writes, including the lines it writes at startup. They come from the service's configuration, not from an invocation.

Fields of events and console lines

Field Meaning
klok_invocation_id the route invocation
klok_parent_invocation_id the invocation that produced the consumed message
klok_leg_id KLK002I1: the solution id followed by the leg and its number
klok_direction IN, OUT, PROCESSING, API, SUB
klok_route_instance the instance of a route that runs several times with different configuration
klok_task_type the type of the task
klok_status START, PROGRESS, FAILED, FINISHED
klok_status_numeric 10000, 20000, 50000, 99000
klok_duration_ms the time from START
klok_step the step of a PROGRESS entry
klok_retry_attempt, klok_retry_max the retry, 1-based
klok_error_code the error code of a failure
klok_disposition, klok_outcome, klok_quarantine_ref what happened to the failed input
klok_span_id the caller's span id from traceparent
klok_file_* the file fields a file route adds (klok_file_flow_id, klok_file_transfer_id, klok_file_name, klok_file_sha256, klok_file_source, klok_file_destination, klok_file_state, klok_file_size)
klok_<name> any other klok.<name> value a route adds
kubernetes.pod_name, kubernetes.namespace_name the pod
logger the Java logger; on an event, the route id
exception_class, exception_message, exception_stacktrace the exception

On a console line, the status fields come from the MDC of the line. They describe the invocation's last log entry at the moment the line was written.

Fields of one type only

  • transaction: klok_first_seen, klok_last_solution_id, klok_count_in_start, klok_count_in_done, klok_count_in_failure, klok_count_out_start, klok_count_out_done, klok_count_out_failure, klok_count_errors, klok_duration_ms (first to last event), klok_status (IN_PROGRESS, FAILED, PARTIAL_FINISHED, FINISHED) and klok_status_numeric (1000, 9000, 9500, 9900).
  • event: klok_message_id, klok_message_type, klok_endpoint, klok_cloud_event_id, host, host_ip, process_id, thread_id, payload (only when enabled), header_<name> (only when enabled).
  • console: kubernetes.container_name, kubernetes.labels.*, stream, log_continuation (a stack trace line that continues the line before it, and carries its ids), and log_line (the raw line, only on a six-bracket text line, whose message is the text after -). A line that is neither JSON nor six-bracket text is stored with the whole line as message. A JSON line from a container that is not a Klok service keeps its own keys; it has a message only when its object has one.

Names that are gone

Earlier name Now
correlation_id, chain_id_global klok_corr_global
invocation_id, chain_id_platform klok_invocation_id (a V4 platformChainId was the invocation id, never the platform id)
parent_invocation_id, chain_id_service klok_parent_invocation_id
log_message, extra_klok_message message, and klok_status for the status name
console log (the whole line) log_line on a six-bracket text line, message on any other line
severity log_level
WARNING, FATAL (levels) WARN, ERROR
klok_env klok_environment
route_id, route_ids klok_route_id
solution_id klok_solution_id
leg_id, contract_id, component_id klok_leg_id
status, status_numeric, direction, task_id, trace_id, span_id, step, error_code, duration_ms, retry_attempt, route_instance, file_* the same name with klok_ in front
logger_name logger
first_seen, last_solution_id, count_* the same name with klok_ in front
extra_<name>, extra_combined klok_<name> for a klok.<name> entry, dropped otherwise
pod_name, es_index_prefix (console) kubernetes.pod_name; the index prefix is not stored

Indexes written before the change keep their old names until the retention of 30 days removes them.

The console line

In a cluster, a Klok service writes its console as JSON, one object per line. The object holds the line's text and level, the MDC as an object mdc with its klok.<name> keys, and the configured context values klok_tenant, klok_domain, klok_environment and klok_instance. Fluent Bit writes every Klok key under its field name by the rule above (mdc."klok.corr-global" becomes klok_corr_global), renames the Quarkus keys (level to log_level, loggerName to logger, the exception to exception_*) and drops the rest of the MDC. A person reading kubectl logs reads JSON. klok logs console reads the same lines through OpenSearch and prints them as text.

In dev mode and in tests (%dev, %test), a service writes readable text instead:

<time> <level> [<klok-route-id>] [<klok-corr-global>] [<klok-status>] [<klok-invocation-id>] [<klok-task-id>] [<klok-trace-id>] (<logger>) - <text>

These are the six brackets that services built before this change write in the cluster too. Fluent Bit still reads them: the second bracket is the earlier correlation id, and it is stored as both klok_corr_global and klok_corr_platform.