Properties and CloudEvents
What a published message carries
sendMessage(topic, payload, properties, key) is the primitive every other publish method uses. Before the message
leaves, the client sets the properties the wire contract of klok-common assigns to the client
(messaging.properties.setByClient in klok-wire-contract-v1.json):
| Property |
Set by the client to |
klok-instance |
klokInstance from the configuration, always: a value in the caller's properties is replaced |
klok-sent-time |
the wall clock time of the send as epoch milliseconds (System.currentTimeMillis()), the contract's epoch-millis format, always |
klok-body-type |
application/vnd.apache.avro+binary for typed clients, application/octet-stream for the bytes client; a value the caller passes wins |
klok-correlation-id |
the global id (klok-corr-global), during the transition described below |
klok-reply-subscription |
on a request of KlokBrokerClientSync, its reply subscription (see Request/reply) |
klok-instance and klok-sent-time are always the client's since 0.4. Before, a caller's value won, so a service that
passed on the properties of a message it consumed published the new message under the upstream service's name and send
time. klok-body-type stays the caller's to set, because a sender may know its encoding better than the schema
(application/x-protobuf on a bytes client).
The correlation ids
The three correlation ids and their rules are defined once, on the klok-common page Log Fields And
Correlation. What the client does
with them:
- Global id.
klok-corr-global of the properties, else the correlationId argument of the Class based and
request/reply overloads, else klok-correlation-id or klokChainId of the properties. The client writes it as
klok-corr-global and, until every service in the fleet reads the new name, also as klok-correlation-id (and
klokChainId, the 0.2.x name, see below). A value that is not a usable id (1 to 128 characters of A-Z a-z 0-9 . _ : - / = + @) is not written.
- Platform id.
klok-corr-platform of the properties, written as it is. A reply of KlokBrokerClientSync takes it
from its request unless the caller set one.
- Never on a message:
klok-corr-service and klok-corr-global-source. The client removes them from the properties,
since the service id ends at the broker and the source is a log field.
- The message key is the caller's
key, else the global id, so the messages of one chain stay on one partition and a
Key_Shared subscription keeps them on one consumer. A reply to an addressed request is keyed by the reply subscription
instead, for the broker's entry filter.
- The client never makes an id up. A message published without a global id carries none and has no key; creating the
ids is the job of the route or invocation that handles the input (the Quarkus extension, or the Python and TypeScript
clients' own invocations).
Everything else is the caller's (setByCaller): klok-corr-global and klok-corr-platform (the Class based and
request/reply overloads take the global id as an argument and put it in the properties), klok-invocation-id,
klok-parent-invocation-id, klok-route-id, klok-solution-id, klok-tenant, klok-domain, klok-environment,
klok-task-id, klok-task-type, and the CloudEvents attributes (ce-type, ce-source, ce-id, ce-time,
ce-subject, ce-datacontenttype). The client cannot set them: an invocation id names one route invocation, not one
send, and the correlation ids are taken from the input or created by the route that handles it. A message sent without
them carries none. In the Quarkus extension the route's invocation context (KlokInvocationContext) sets the klok-*
ones on every message the route publishes. The client's routeId names its consumers and producers on the broker, not
its messages. The key argument becomes the Pulsar message key (the global id when it is not given), which keeps
messages about the same business object in order on partitioned topics and drives Key_Shared subscriptions.
Until 0.4 the typed client also writes the 0.2.x names (klokChainId, klokSolutionId for the instance,
klokReplyTopic, klokCorrelationId for the request/reply id, klokBodyType, klokSentTime) next to the canonical
ones, so that 0.2 readers of typed topics keep working; the bytes client writes only the canonical names. New code must
read the klok-* names; the Python client from 0.3 on neither writes nor reads the legacy names.
Reading a message
KlokBrokerMessage<T> wraps the Pulsar message:
getPayload(), getMessageId(), getOriginalMessage(), getMetadata() (all properties), getProperty(name).
- The correlation ids:
getCorrGlobal() (by the order of the klok-common page: klok-corr-global, then
klok-correlation-id and klokChainId of a service built before the three ids, then traceparent and the other trace
and correlation headers), getCorrGlobalWithSource() (the id and where it came from, for klok-corr-global-source) and
getCorrPlatform() (klok-corr-platform, else klok-correlation-id, then klokChainId). getCorrelationId() is
deprecated and returns the global id.
- Typed getters for the rest of the context:
getInvocationId(), getParentInvocationId(), getTaskId(),
getSolutionId(), getRouteId(), getInstance(), getSentTime(), getBodyType().
- Request/reply:
getRequestReplyId() (klok-reqres-id) and getReplyTopic() (klok-reply-topic).
getCloudEventAttributes() parses the ce-* properties into a KlokCloudEventAttributes record.
The getKlok* getters (getKlokCallchainId, getKlokCorrelationId, getKlokReplyTo, ...) are the deprecated 1.x
accessors; getKlokCorrelationId() in particular returns the request/reply id, not the chain id, which is why the new
names are explicit.
Topics
The Class based overloads (sendMessage(Class, payload, purpose, chainId), subscribe(Class, purpose, name, handler)) derive the simple topic name from the payload class and the purpose through KlokNamingConventions and are
kept for 1.x callers. 2.0 code resolves the full topic name first (KlokTopicResolver in the extension, or
KlokNamingConventions.fullTopicName directly) and uses the String topic overloads, so publish and subscribe never
disagree about tenant, environment or domain.