Skip to content

Cratis Chronicle event correlation, identity, and causation

Cratis Chronicle records correlation, identity and causation alongside each event so you can trace a change back to the operation and actor that produced it. When investigating an unexpected event, these answer different questions:

  • A correlation ID groups work belonging to one logical operation, such as an HTTP request or message handler invocation.
  • An identity records who caused the change: a user, system or service.
  • Causation records the chain of operations that led to the event.

Establish this context at the start of a request or background job, then let the client attach it to appends within that context. Carry it explicitly when work crosses a boundary that does not preserve the client’s context.

using Cratis.Execution;
public class CorrelationIdentityCausationCorrelation(ICorrelationIdAccessor accessor, ICorrelationIdModifier modifier)
{
public CorrelationId GetCurrent() => accessor.Current;
public void SetForRequest() => modifier.Modify(CorrelationId.New());
}

View C# snippet source on GitHub

Use the same correlation ID for work that belongs to one operation. The client scopes that value according to its runtime: Kotlin uses per-thread context (ThreadLocal), Elixir uses the process dictionary, and TypeScript uses AsyncLocalStorage for the active asynchronous call context. Starting a new thread, process or independent context requires you to carry the value across explicitly.

using Cratis.Chronicle.Identities;
public class CorrelationIdentityCausationIdentity(IIdentityProvider identityProvider)
{
public void SetForRequest(string subject, string name, string userName) =>
identityProvider.SetCurrentIdentity(new Identity(subject, name, userName));
public Identity GetCurrent() => identityProvider.GetCurrent();
}

View C# snippet source on GitHub

An identity has a stable subject and a display name. Use the subject to identify the actor, and the display name to present it to a reader. This identity subject identifies who caused the event; the event’s compliance subject identifies who the event is about.

The clients provide predefined identities, or sentinels, for System, NotSet and Unknown, with language-specific naming. An on-behalf-of chain records delegation when one identity acts for another. Helpers such as withoutDuplicates() and without_duplicates/1 remove repeated subjects from that chain while retaining their first occurrence.

When someone’s display name changes, rename the identity instead of changing their past events. Events refer to the identity by its stable key, so metadata reads can show the updated name for existing history.

using Cratis.Chronicle;
public static class RenamingAnIdentity
{
public static async Task Rename(IEventStore eventStore)
{
await eventStore.Identities.Rename("subject-42", "Jane Austen");
}
}

View C# snippet source on GitHub

Keep the existing subject when renaming so earlier events continue to resolve to the same identity.

Metadata reads resolve caused-by identities directly from storage instead of a kernel node’s identity cache. A rename is visible on the next metadata read, including for events appended before the rename. Each chain link has a Resolution status:

Status Meaning Name
Resolved The identity exists and has a display name. The current name.
NameUnavailable The identity exists but its display name is empty. null.
Missing The stored identity identifier no longer resolves. null.
NotSet No caused-by identity was set. null.
System The subject is Chronicle’s well-known system identity. null.

Chronicle never substitutes the subject for an unavailable name. OnBehalfOf retains the chain order and resolves each link separately.

InitiatorType classifies the first identity in the chain. A resolved identity is an Agent when acting on behalf of another identity, or a User without that chain. Chronicle’s system identity is System; the well-known unknown identity and missing or unset identities are Unknown. An unavailable display name does not change whether an identity acts as an agent.

Application-defined service identities are not automatically classified as systems. Recognize them by their subjects in your application, rather than relying on display names or the User classification.

Erasing personally identifiable information (PII) deletes encryption keys for protected event and read-model data, not identity display names. The rename API requires a non-empty display name and leaves the subject and username unchanged, so it is not an identity-erasure operation. Your application must authorize access before calling the metadata API.

For a signed-in Chronicle user (an internal password token, Identity cookie, or protected bearer ticket), sequence commands always record the authenticated user as CausedBy. A supplied causedBy, including its onBehalfOf chain, is ignored. This applies to append, batch append, append-once, revision, and redaction commands.

Application callers using client credentials may still supply an end-user identity and an onBehalfOf chain. When omitted, the application principal is recorded. Unmarked external-authority tokens and anonymous requests retain the same supplied-identity behavior; external authorities are responsible for their own identity and delegation policy. The chronicle_principal_kind=user claim is reserved for Chronicle user principals, not a delegation permission.

Existing Identity application cookies acquire the user marker during claims transformation. Refreshing an existing Chronicle user refresh token also adds the marker, and newly issued password tokens retain it when refreshed. An already-issued access token without the marker keeps its supplied-identity behavior until replaced. For uniform authorship, update every request-serving server node; older nodes do not enforce this rule. No request, protobuf, or grain contract changes are required.

using Cratis.Chronicle.Auditing;
public class CorrelationIdentityCausationCausation(ICausationManager causationManager)
{
public void RecordPlaceOrder(string orderId) =>
causationManager.Add(
"MyApp.Commands.PlaceOrder",
new Dictionary<string, string> { ["orderId"] = orderId });
}

View C# snippet source on GitHub

Add causation entries as an operation progresses so the event records how it was reached. In TypeScript, causationManager.add(type, properties) adds an entry to the current chain. For root-cause setup, scoped entries and reading the chain, continue with your client’s reference below.

Use your client’s reference to wire context into request handling and check what happens when work crosses thread, process or asynchronous-context boundaries:

  • .NET client — Cratis.Execution.ICorrelationIdAccessor/ICorrelationIdModifier, Cratis.Chronicle.Identities.IIdentityProvider, Cratis.Chronicle.Auditing.ICausationManager
  • Kotlin client — io.cratis.chronicle.correlation, io.cratis.chronicle.identity, io.cratis.chronicle.auditing
  • Elixir client — Chronicle.CorrelationId, Chronicle.Identity, Chronicle.Auditing.CausationManager
  • TypeScript client — correlationIdManager, identityProvider, causationManager