Message properties and CloudEvents

Every Klok message carries a fixed set of context properties. They have the same name as Pulsar message property, HTTP header and CloudEvents extension, and derived names as MDC key and as log field, so a correlation id can be followed from an HTTP request through the broker into the log index without any translation table.

The klok-* properties

Constants in dev.klok.framework.common.messaging.KlokMessageProperties:

Constant Property Meaning
CORR_GLOBAL klok-corr-global the global id: one business call, inside Klok and outside it; also the message key
CORR_PLATFORM klok-corr-platform the platform id: what one call does inside Klok, across the broker; never sent outside
CORR_SERVICE, CORR_GLOBAL_SOURCE klok-corr-service, klok-corr-global-source the service id and where the global id came from: MDC keys and log fields only, never on a message or an HTTP call
CORRELATION_ID (deprecated) klok-correlation-id the transition name of the global id: written next to klok-corr-global with the same value, read only when it is absent
INVOCATION_ID klok-invocation-id id of one route invocation
PARENT_INVOCATION_ID klok-parent-invocation-id invocation that produced this message (sub route, publish)
TASK_ID klok-task-id asynchronous task the message belongs to
TASK_TYPE klok-task-type type of that task: the process it is a run of, and the purpose suffix of its request topic
SOLUTION_ID, ROUTE_ID klok-solution-id, klok-route-id KLK002, KLK002I1_OdlScheduledQuery
TENANT, DOMAIN, ENVIRONMENT, INSTANCE klok-tenant, ... where the message was produced
SENT_TIME, BODY_TYPE klok-sent-time, klok-body-type wall clock time of publication as epoch milliseconds, media type (BODY_TYPE_AVRO)
REQUEST_REPLY_ID, REPLY_TOPIC, REPLY_SUBSCRIPTION klok-reqres-id, klok-reply-topic, klok-reply-subscription request/reply over the broker; the last names the requester's reply subscription, which keys the reply (see the wire contract)
ERROR_CODE, ERROR_MESSAGE, FAILED_ROUTE_ID, RETRY_COUNT, ORIGINAL_TOPIC, ORIGINAL_MESSAGE_ID klok-error-code, ... set on messages republished to a _DLQ topic
CLAIM_CHECK_REF klok-claimcheck-ref klokcc:// reference when the payload was stored in the claim check
ASYNC, TASK_CALLBACK_URL, PARENT_TASK_ID, TASK_NAME, TASK_EXPECTED_CHILDREN klok-async, klok-task-callback-url, ... task request keys: set by the caller that starts a task, consumed by the initiating route, never forwarded (see the extension's async tasks chapter)

How the three ids are taken from an input, created and passed on, and the order of the headers a global id may come from, is on Log fields and correlation. KlokCorrelation implements it:

  • fromHeaders(map) gives the global id and its source (Global(value, source)) from the headers or properties that arrived, or nothing; globalOrNew(map) falls back to a new id with source generated;
  • platformFromMessage(map) gives the platform id of a consumed broker message (klok-corr-platform, then the transition names klok-correlation-id and klokChainId); never call it with an HTTP request;
  • usable(value) says whether a value may be an id (1–128 characters of A-Z a-z 0-9 . _ : - / = + @), newId() creates one, and GLOBAL_HEADERS is the order.

CONTEXT_KEYS is what a route copies from a consumed message to the messages it publishes: the global and the platform id, the invocation ids, the task and the route's place.

Helpers:

  • mdcKey("klok-corr-global") gives klok.corr-global. Camel propagates MDC keys matching klok*, which is why the MDC form uses a dot after the prefix and the property form a dash.
  • logField("klok-corr-global") and logField("klok.corr-global") give klok_corr_global, the OpenSearch field and JSON console key: every - and . written as _. A route's own klok.file.name becomes klok_file_name.
  • cloudEventAttribute("klok-corr-global") gives klokcorrglobal, the CloudEvents extension attribute form (lower case letters and digits only, as the specification requires).

The LEGACY_* constants (klokChainId, klokSolutionId, klokReplyTopic, klokCorrelationId, klokBodyType, klokSentTime) are the 1.x names; LEGACY_ALIASES maps each canonical name to its alias. The Java broker client writes them next to the canonical names until 0.4 and lifts a legacy value to the canonical name when only the legacy one is present. The TypeScript and Python clients never write or read them. klokChainId is the oldest name of the global id; it stays the alias of klok-correlation-id, the newer transition name, because both are retired together (klok-common#12), and KlokCorrelation reads them in the order klok-corr-global, klok-correlation-id, klokChainId. Which side sets which property, and in which format, is stated in the wire contract.

CloudEvents in binary content mode

dev.klok.framework.common.cloudevents.KlokCloudEventAttributes is a record of the CloudEvents 1.0 context attributes. Klok uses the binary content mode: the attributes travel as Pulsar properties prefixed with ce- (ce-specversion, ce-type, ce-source, ce-id, ce-time, ce-datacontenttype, ce-subject, optional ce-dataschema) and the payload stays the typed datamodel. Nothing is wrapped in a JSON envelope, so a consumer that knows nothing about CloudEvents still reads the same Avro or JSON body.

Conventions:

  • type is a reverse DNS name that says what happened, for example dev.klok.odl.object.persisted or dev.klok.acme.finance.CustomerV1.published (the default built by defaultType(tenant, domain, payloadType)). The datamodel describes the shape, the type describes the meaning.
  • source is urn:klok:<tenant>:<solutionId>:<routeId> (source(tenant, solutionId, routeId)).
  • subject is the business id of the affected object.
  • dataschema is the identity of the payload's datamodel version, urn:klok:schema:<docId>:<Record>:<artifactVersion> (dataSchemaUrn(docId, record, version)); see Wire contract for how it resolves.
  • id and time are generated when absent (KlokFastID, Instant.now()); specversion is always 1.0.

builder() builds an instance, klokContext(map) copies the klok-* properties into extension attributes in one call, toProperties() produces the ce-* map to put on a message and fromProperties(map) reads it back. The Quarkus extension's message publisher (KlokMessagePublisher) and the broker client's KlokBrokerMessage.getCloudEventAttributes() are the two users. A route sets only what it knows: with the extension's KlokEmitCloudEventOnSuccess it gives the event type and subject, and the extension puts them on the message it publishes or, for a route that publishes nothing, emits a KlokCloudEventV1 on the KlokCloudEventV1_events topic.

Event datamodels

dev.klok.framework.common.event.KlokCloudEvent is the interface for a datamodel that is an event: it carries the CloudEvents attributes (specversion, id, type, source, subject, time, datacontenttype, dataschema) as fields of its payload as well, so the event stays self-describing when it is persisted. Nothing implements it today: the datamodel pipeline generates no implements clause, and the Quarkus extension has no capability that publishes such an event (klok-quarkus-extension#47). The KlokCloudEventV1 the extension emits is its own class, not the generated datamodel of the same name (klok-quarkus-extension#23).