Skip to content

Cratis Chronicle reactor filtering by event metadata

Cratis Chronicle reactor filters restrict delivery by event metadata before a handler runs. Use them when one event type serves several workflows but a reactor should handle only a subset, such as priority orders or events from one source type.

Filtering does not remove events from storage or change what other observers receive. It narrows this reactor’s subscription within its selected event sequence. Without filters, the reactor receives events of the types it handles.

Attribute Filters by Matches when
[FilterEventsByTag("...")] Appended or static event tag Any filter tag matches any tag on the appended event
[EventSourceType("...")] Event source type set at append time The event source type matches exactly
[EventStreamType("...")] Event stream type set at append time The event stream type matches exactly

The table uses .NET attribute names. Set the corresponding metadata when appending the event so the filter has a value to match:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
[EventType]
public record ReactorsFilteringOrderPlaced(decimal TotalAmount);
public class ReactorsFilteringMetadataExampleService(IEventLog eventLog)
{
public Task PlaceOrder(decimal totalAmount) =>
eventLog.Append(
EventSourceId.New(),
new ReactorsFilteringOrderPlaced(totalAmount),
tags: ["priority"],
eventSourceType: "order",
eventStreamType: "fulfillment");
}

View C# snippet source on GitHub

To run a handler only for priority orders, append the event with the priority tag and configure the reactor to match that tag. In .NET, use [FilterEventsByTag]:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reactors;
[EventType]
public record ReactorsFilteringByTagOrderPlaced(decimal TotalAmount);
public class ReactorsFilteringByTagOrderService(IEventLog eventLog)
{
public Task PlacePriorityOrder(decimal totalAmount) =>
eventLog.Append(
EventSourceId.New(),
new ReactorsFilteringByTagOrderPlaced(totalAmount),
tags: ["priority"]);
}
[FilterEventsByTag("priority")]
public class ReactorsFilteringPriorityOrderNotifier : IReactor
{
public Task Placed(ReactorsFilteringByTagOrderPlaced @event, EventContext context) =>
Task.CompletedTask;
}

View C# snippet source on GitHub

The matching priority tag allows delivery. An order without that tag does not reach this reactor, even though its event type matches the handler.

Multiple tag filters widen the match: an event needs any one of the configured tags, not all of them. In .NET, repeat [FilterEventsByTag] for each accepted tag:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reactors;
[EventType]
public record ReactorsFilteringMultiTagOrderPlaced(decimal TotalAmount);
[FilterEventsByTag("priority")]
[FilterEventsByTag("express")]
public class ReactorsFilteringFastTrackOrderNotifier : IReactor
{
public Task Placed(ReactorsFilteringMultiTagOrderPlaced @event, EventContext context) =>
Task.CompletedTask;
}

View C# snippet source on GitHub

Use an event source type filter when the handler belongs to one category of source. In .NET, [EventSourceType] matches the event’s eventSourceType exactly:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reactors;
[EventType]
public record ReactorsFilteringCustomerRegistered(string EmailAddress);
public class ReactorsFilteringCustomerService(IEventLog eventLog)
{
public Task Register(string emailAddress) =>
eventLog.Append(
EventSourceId.New(),
new ReactorsFilteringCustomerRegistered(emailAddress),
eventSourceType: "customer");
}
[EventSourceType("customer")]
public class ReactorsFilteringCustomerWelcomeReactor : IReactor
{
public Task Registered(ReactorsFilteringCustomerRegistered @event, EventContext context) =>
Task.CompletedTask;
}

View C# snippet source on GitHub

Use a stream type filter when the handler belongs to one kind of stream. In .NET, [EventStreamType] matches the event’s stream type exactly:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reactors;
[EventType]
public record ReactorsFilteringPaymentCaptured(decimal Amount);
public class ReactorsFilteringPaymentsService(IEventLog eventLog)
{
public Task Capture(decimal amount) =>
eventLog.Append(
EventSourceId.New(),
new ReactorsFilteringPaymentCaptured(amount),
eventStreamType: "payments");
}
[EventStreamType("payments")]
public class ReactorsFilteringPaymentReceivedNotifier : IReactor
{
public Task Captured(ReactorsFilteringPaymentCaptured @event, EventContext context) =>
Task.CompletedTask;
}

View C# snippet source on GitHub

Combine tag, source type, and stream type filters to narrow delivery further. Chronicle requires every configured category to match; the any-tag rule still applies within the tag category:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Reactors;
[EventType]
public record ReactorsFilteringShipmentDispatched(string TrackingNumber);
public class ReactorsFilteringShippingService(IEventLog eventLog)
{
public Task Dispatch(string trackingNumber) =>
eventLog.Append(
EventSourceId.New(),
new ReactorsFilteringShipmentDispatched(trackingNumber),
tags: ["express"],
eventSourceType: "shipment",
eventStreamType: "logistics");
}
[FilterEventsByTag("express")]
[EventSourceType("shipment")]
[EventStreamType("logistics")]
public class ReactorsFilteringExpressShipmentNotifier : IReactor
{
public Task Dispatched(ReactorsFilteringShipmentDispatched @event, EventContext context) =>
Task.CompletedTask;
}

View C# snippet source on GitHub

Check the append metadata against every configured category when an expected event does not arrive. A matching tag cannot compensate for a different event source type or stream type.

In .NET, [Tag] and [Tags] label the reactor itself for organization and discoverability. They do not select incoming events. Use [FilterEventsByTag] for delivery filtering:

using Cratis.Chronicle;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reactors;
[EventType]
public record ReactorsFilteringTagVsFilterShipmentDispatched(string TrackingNumber);
// These labels appear on the reactor definition — they do not affect dispatch
[Tag("notifications")]
[Tag("express")]
public class ReactorsFilteringLabeledShipmentNotifier : IReactor
{
public Task Dispatched(ReactorsFilteringTagVsFilterShipmentDispatched @event, EventContext context) =>
Task.CompletedTask;
}
// These filter which events are dispatched to the reactor
[FilterEventsByTag("express")]
[EventSourceType("shipment")]
public class ReactorsFilteringFilteredShipmentNotifier : IReactor
{
public Task Dispatched(ReactorsFilteringTagVsFilterShipmentDispatched @event, EventContext context) =>
Task.CompletedTask;
}

View C# snippet source on GitHub

For the matching rules and append metadata behind these examples, continue with the filter you need: