Cratis Chronicle reducer event filtering
Cratis Chronicle reducer filters select which events can update a read model by matching stored event metadata. Use them when the same event type serves several views, but one view should include only a tagged subset or a particular source or stream type.
Place filter attributes on a .NET reducer class, or on the model for a self-reducing read model. Events must match both the reducer’s observed event types and its metadata filters before they reach its transition methods. A nonmatching event is skipped for that reducer, not removed from the event store.
Filter attributes
Section titled “Filter attributes”| 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 |
An event source type categorizes the source, such as customer; a stream type categorizes the stream, such as fulfillment. These are types, not individual source or stream IDs. Set the metadata when appending the event:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;
[EventType]public record ReducersFilteringOrderPlaced(decimal TotalAmount);
public class ReducersFilteringMetadataExampleService(IEventLog eventLog){ public Task PlaceOrder(decimal totalAmount) => eventLog.Append( EventSourceId.New(), new ReducersFilteringOrderPlaced(totalAmount), tags: ["priority"], eventSourceType: "order", eventStreamType: "fulfillment");}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.eventSequences.AppendOptionsimport io.cratis.chronicle.eventSequences.IEventLog
@EventTypedata class ReducersFilteringOrderPlaced(val totalAmount: Double)
class ReducersFilteringMetadataExampleService(private val eventLog: IEventLog) { suspend fun placeOrder(eventSourceId: String, totalAmount: Double) = eventLog.append( eventSourceId, ReducersFilteringOrderPlaced(totalAmount), AppendOptions( tags = listOf("priority"), eventSourceType = "order", eventStreamType = "fulfillment" ) )}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.eventSequences.IEventLog;import io.cratis.chronicle.java.AppendOptionsBuilder;import io.cratis.chronicle.java.EventLogJavaBridge;
@EventTyperecord ReducersFilteringOrderPlaced(double totalAmount) {}
class ReducersFilteringMetadataExampleService { private final IEventLog eventLog;
ReducersFilteringMetadataExampleService(IEventLog eventLog) { this.eventLog = eventLog; }
void placeOrder(String eventSourceId, double totalAmount) { EventLogJavaBridge.append( eventLog, eventSourceId, new ReducersFilteringOrderPlaced(totalAmount), new AppendOptionsBuilder() .tag("priority") .eventSourceType("order") .eventStreamType("fulfillment") .build()); }}defmodule ReducersFilteringOrderPlaced do use Cratis.Chronicle.Events.EventType, id: "reducers-filtering-order-placed"
defstruct [:total_amount]end
defmodule ReducersFilteringMetadataExampleService do alias ReducersFilteringOrderPlaced
def place_order(order_id, total_amount) do Cratis.Chronicle.append( order_id, %ReducersFilteringOrderPlaced{total_amount: total_amount}, tags: ["priority"], event_source_type: "order", event_stream_type: "fulfillment" ) endendimport { eventType, IEventLog } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ReducersFilteringOrderPlaced { @field(Number) readonly totalAmount: number;
constructor(totalAmount: number) { this.totalAmount = totalAmount; }}
class ReducersFilteringMetadataExampleService { constructor(private readonly eventLog: IEventLog) {}
async placeOrder(orderId: string, totalAmount: number): Promise<void> { await this.eventLog.append(orderId, new ReducersFilteringOrderPlaced(totalAmount), { tags: ['priority'], sourceType: 'order', streamType: 'fulfillment' }); }}Filter by tag
Section titled “Filter by tag”Append an event with a tag and place [FilterEventsByTag] on the reducer to accumulate state only from tagged events.
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Reducers;
[EventType]public record ReducersFilteringByTagOrderPlaced(decimal TotalAmount);
public record ReducersFilteringPriorityOrderTotals(int Count, decimal Total);
public class ReducersFilteringByTagOrderService(IEventLog eventLog){ public Task PlacePriorityOrder(decimal totalAmount) => eventLog.Append( EventSourceId.New(), new ReducersFilteringByTagOrderPlaced(totalAmount), tags: ["priority"]);}
[FilterEventsByTag("priority")]public class ReducersFilteringPriorityOrderTotalsReducer : IReducerFor<ReducersFilteringPriorityOrderTotals>{ public ReducersFilteringPriorityOrderTotals Placed(ReducersFilteringByTagOrderPlaced @event, ReducersFilteringPriorityOrderTotals? current, EventContext context) => new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.eventSequences.AppendOptionsimport io.cratis.chronicle.eventSequences.IEventLogimport io.cratis.chronicle.observation.FilterEventsByTagimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersFilteringByTagOrderPlaced(val totalAmount: Double)
@ReadModeldata class ReducersFilteringPriorityOrderTotals(val count: Int = 0, val total: Double = 0.0)
class ReducersFilteringByTagOrderService(private val eventLog: IEventLog) { suspend fun placePriorityOrder(eventSourceId: String, totalAmount: Double) = eventLog.append( eventSourceId, ReducersFilteringByTagOrderPlaced(totalAmount), AppendOptions(tags = listOf("priority")) )}
@Reducer@FilterEventsByTag("priority")class ReducersFilteringPriorityOrderTotalsReducer { fun placed(event: ReducersFilteringByTagOrderPlaced, current: ReducersFilteringPriorityOrderTotals?, context: EventContext) = ReducersFilteringPriorityOrderTotals((current?.count ?: 0) + 1, (current?.total ?: 0.0) + event.totalAmount)}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.eventSequences.IEventLog;import io.cratis.chronicle.java.AppendOptionsBuilder;import io.cratis.chronicle.java.EventLogJavaBridge;import io.cratis.chronicle.observation.FilterEventsByTag;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersFilteringByTagOrderPlaced(double totalAmount) {}
@ReadModelrecord ReducersFilteringPriorityOrderTotals(int count, double total) { ReducersFilteringPriorityOrderTotals() { this(0, 0.0); }}
class ReducersFilteringByTagOrderService { private final IEventLog eventLog;
ReducersFilteringByTagOrderService(IEventLog eventLog) { this.eventLog = eventLog; }
void placePriorityOrder(String eventSourceId, double totalAmount) { EventLogJavaBridge.append( eventLog, eventSourceId, new ReducersFilteringByTagOrderPlaced(totalAmount), new AppendOptionsBuilder().tag("priority").build()); }}
@Reducer@FilterEventsByTag("priority")class ReducersFilteringPriorityOrderTotalsReducer { ReducersFilteringPriorityOrderTotals placed(ReducersFilteringByTagOrderPlaced event, ReducersFilteringPriorityOrderTotals current, EventContext context) { int count = current == null ? 0 : current.count(); double total = current == null ? 0.0 : current.total(); return new ReducersFilteringPriorityOrderTotals(count + 1, total + event.totalAmount()); }}Elixir does not support this workflow yet.import { eventType, filterEventsByTag, IEventStore, reducer } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ReducersFilteringByTagOrderPlaced { @field(Number) readonly totalAmount: number;
constructor(totalAmount: number) { this.totalAmount = totalAmount; }}
class ReducersFilteringPriorityOrderTotals { count = 0; total = 0;}
@reducer('', undefined, ReducersFilteringPriorityOrderTotals)@filterEventsByTag('priority')class ReducersFilteringPriorityOrderTotalsReducer { reducersFilteringByTagOrderPlaced( event: ReducersFilteringByTagOrderPlaced, current: ReducersFilteringPriorityOrderTotals | undefined ): ReducersFilteringPriorityOrderTotals { return { count: (current?.count ?? 0) + 1, total: (current?.total ?? 0) + event.totalAmount }; }}
class ReducersFilteringByTagOrderService { constructor(private readonly store: IEventStore) {}
async placePriorityOrder(eventSourceId: string, totalAmount: number): Promise<void> { await this.store.eventLog.append( eventSourceId, new ReducersFilteringByTagOrderPlaced(totalAmount), { tags: ['priority'] }); }}In this example, only orders whose appended events carry the priority tag update the totals. An order event without that tag leaves this read model unchanged.
Multiple filter tags
Section titled “Multiple filter tags”Multiple [FilterEventsByTag] attributes widen the match. The reducer receives the event if the appended event has any of the configured tags:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Reducers;
[EventType]public record ReducersFilteringMultiTagOrderPlaced(decimal TotalAmount);
public record ReducersFilteringFastTrackOrderTotals(int Count);
[FilterEventsByTag("priority")][FilterEventsByTag("express")]public class ReducersFilteringFastTrackOrderTotalsReducer : IReducerFor<ReducersFilteringFastTrackOrderTotals>{ public ReducersFilteringFastTrackOrderTotals Placed(ReducersFilteringMultiTagOrderPlaced @event, ReducersFilteringFastTrackOrderTotals? current, EventContext context) => new((current?.Count ?? 0) + 1);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.FilterEventsByTagimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersFilteringMultiTagOrderPlaced(val totalAmount: Double)
@ReadModeldata class ReducersFilteringFastTrackOrderTotals(val count: Int = 0)
@Reducer@FilterEventsByTag("priority")@FilterEventsByTag("express")class ReducersFilteringFastTrackOrderTotalsReducer { fun placed(event: ReducersFilteringMultiTagOrderPlaced, current: ReducersFilteringFastTrackOrderTotals?, context: EventContext) = ReducersFilteringFastTrackOrderTotals((current?.count ?: 0) + 1)}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.FilterEventsByTag;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersFilteringMultiTagOrderPlaced(double totalAmount) {}
@ReadModelrecord ReducersFilteringFastTrackOrderTotals(int count) { ReducersFilteringFastTrackOrderTotals() { this(0); }}
@Reducer@FilterEventsByTag("priority")@FilterEventsByTag("express")class ReducersFilteringFastTrackOrderTotalsReducer { ReducersFilteringFastTrackOrderTotals placed(ReducersFilteringMultiTagOrderPlaced event, ReducersFilteringFastTrackOrderTotals current, EventContext context) { int count = current == null ? 0 : current.count(); return new ReducersFilteringFastTrackOrderTotals(count + 1); }}Elixir does not support this workflow yet.import { eventType, filterEventsByTag, reducer } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ReducersFilteringMultiTagOrderPlaced { @field(Number) readonly totalAmount: number;
constructor(totalAmount: number) { this.totalAmount = totalAmount; }}
class ReducersFilteringFastTrackOrderTotals { count = 0;}
@reducer('', undefined, ReducersFilteringFastTrackOrderTotals)@filterEventsByTag('priority')@filterEventsByTag('express')class ReducersFilteringFastTrackOrderTotalsReducer { reducersFilteringMultiTagOrderPlaced( _event: ReducersFilteringMultiTagOrderPlaced, current: ReducersFilteringFastTrackOrderTotals | undefined ): ReducersFilteringFastTrackOrderTotals { return { count: (current?.count ?? 0) + 1 }; }}Filter by event source type
Section titled “Filter by event source type”Place [EventSourceType] on the reducer to accumulate state only from events appended with a matching eventSourceType:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Reducers;
[EventType]public record ReducersFilteringInvoiceIssued(decimal Amount);
public record ReducersFilteringCustomerInvoiceTotal(decimal Amount);
public class ReducersFilteringInvoicingService(IEventLog eventLog){ public Task IssueCustomerInvoice(decimal amount) => eventLog.Append( EventSourceId.New(), new ReducersFilteringInvoiceIssued(amount), eventSourceType: "customer");}
[EventSourceType("customer")]public class ReducersFilteringCustomerInvoiceTotalReducer : IReducerFor<ReducersFilteringCustomerInvoiceTotal>{ public ReducersFilteringCustomerInvoiceTotal Issued(ReducersFilteringInvoiceIssued @event, ReducersFilteringCustomerInvoiceTotal? current, EventContext context) => new((current?.Amount ?? 0m) + @event.Amount);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.eventSequences.AppendOptionsimport io.cratis.chronicle.eventSequences.IEventLogimport io.cratis.chronicle.observation.EventSourceTypeimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersFilteringInvoiceIssued(val amount: Double)
@ReadModeldata class ReducersFilteringCustomerInvoiceTotal(val amount: Double = 0.0)
class ReducersFilteringInvoicingService(private val eventLog: IEventLog) { suspend fun issueCustomerInvoice(eventSourceId: String, amount: Double) = eventLog.append( eventSourceId, ReducersFilteringInvoiceIssued(amount), AppendOptions(eventSourceType = "customer") )}
@Reducer@EventSourceType("customer")class ReducersFilteringCustomerInvoiceTotalReducer { fun issued(event: ReducersFilteringInvoiceIssued, current: ReducersFilteringCustomerInvoiceTotal?, context: EventContext) = ReducersFilteringCustomerInvoiceTotal((current?.amount ?: 0.0) + event.amount)}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.eventSequences.IEventLog;import io.cratis.chronicle.java.AppendOptionsBuilder;import io.cratis.chronicle.java.EventLogJavaBridge;import io.cratis.chronicle.observation.EventSourceType;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersFilteringInvoiceIssued(double amount) {}
@ReadModelrecord ReducersFilteringCustomerInvoiceTotal(double amount) { ReducersFilteringCustomerInvoiceTotal() { this(0.0); }}
class ReducersFilteringInvoicingService { private final IEventLog eventLog;
ReducersFilteringInvoicingService(IEventLog eventLog) { this.eventLog = eventLog; }
void issueCustomerInvoice(String eventSourceId, double amount) { EventLogJavaBridge.append( eventLog, eventSourceId, new ReducersFilteringInvoiceIssued(amount), new AppendOptionsBuilder().eventSourceType("customer").build()); }}
@Reducer@EventSourceType("customer")class ReducersFilteringCustomerInvoiceTotalReducer { ReducersFilteringCustomerInvoiceTotal issued(ReducersFilteringInvoiceIssued event, ReducersFilteringCustomerInvoiceTotal current, EventContext context) { double amount = current == null ? 0.0 : current.amount(); return new ReducersFilteringCustomerInvoiceTotal(amount + event.amount()); }}Elixir does not support this workflow yet.import { field } from '@cratis/fundamentals';import { eventType, eventSourceType, Guid, IEventLog, reducer } from '@cratis/chronicle';
@eventType()class ReducersFilteringInvoiceIssued { @field(Number) amount = 0;}
class ReducersFilteringCustomerInvoiceTotal { amount = 0; }
class ReducersFilteringInvoicingService { constructor(private readonly eventLog: IEventLog) {}
async issueCustomerInvoice(amount: number): Promise<void> { const result = await this.eventLog.append(Guid.create().toString(), Object.assign(new ReducersFilteringInvoiceIssued(), { amount }), { sourceType: 'customer' }); if (!result.isSuccess) throw new Error('Invoice issue failed'); }}
@reducer('', undefined, ReducersFilteringCustomerInvoiceTotal)@eventSourceType('customer')class ReducersFilteringCustomerInvoiceTotalReducer { reducersFilteringInvoiceIssued(event: ReducersFilteringInvoiceIssued, current: ReducersFilteringCustomerInvoiceTotal | undefined): ReducersFilteringCustomerInvoiceTotal { return { amount: (current?.amount ?? 0) + event.amount }; }}If the same event is appended with eventSourceType: "supplier", this reducer does not receive it.
Filter by event stream type
Section titled “Filter by event stream type”Place [EventStreamType] on the reducer to accumulate state only from events appended to a matching stream type:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Reducers;
[EventType]public record ReducersFilteringShipmentSent(decimal ShippingCost);
public record ReducersFilteringShippingTotals(int Count, decimal TotalCost);
public class ReducersFilteringShippingService(IEventLog eventLog){ public Task Send(decimal shippingCost) => eventLog.Append( EventSourceId.New(), new ReducersFilteringShipmentSent(shippingCost), eventStreamType: "shipping");}
[EventStreamType("shipping")]public class ReducersFilteringShippingTotalsReducer : IReducerFor<ReducersFilteringShippingTotals>{ public ReducersFilteringShippingTotals Sent(ReducersFilteringShipmentSent @event, ReducersFilteringShippingTotals? current, EventContext context) => new((current?.Count ?? 0) + 1, (current?.TotalCost ?? 0m) + @event.ShippingCost);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.eventSequences.AppendOptionsimport io.cratis.chronicle.eventSequences.IEventLogimport io.cratis.chronicle.observation.EventStreamTypeimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersFilteringShipmentSent(val shippingCost: Double)
@ReadModeldata class ReducersFilteringShippingTotals(val count: Int = 0, val totalCost: Double = 0.0)
class ReducersFilteringShippingService(private val eventLog: IEventLog) { suspend fun send(eventSourceId: String, shippingCost: Double) = eventLog.append( eventSourceId, ReducersFilteringShipmentSent(shippingCost), AppendOptions(eventStreamType = "shipping") )}
@Reducer@EventStreamType("shipping")class ReducersFilteringShippingTotalsReducer { fun sent(event: ReducersFilteringShipmentSent, current: ReducersFilteringShippingTotals?, context: EventContext) = ReducersFilteringShippingTotals((current?.count ?: 0) + 1, (current?.totalCost ?: 0.0) + event.shippingCost)}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.eventSequences.IEventLog;import io.cratis.chronicle.java.AppendOptionsBuilder;import io.cratis.chronicle.java.EventLogJavaBridge;import io.cratis.chronicle.observation.EventStreamType;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersFilteringShipmentSent(double shippingCost) {}
@ReadModelrecord ReducersFilteringShippingTotals(int count, double totalCost) { ReducersFilteringShippingTotals() { this(0, 0.0); }}
class ReducersFilteringShippingService { private final IEventLog eventLog;
ReducersFilteringShippingService(IEventLog eventLog) { this.eventLog = eventLog; }
void send(String eventSourceId, double shippingCost) { EventLogJavaBridge.append( eventLog, eventSourceId, new ReducersFilteringShipmentSent(shippingCost), new AppendOptionsBuilder().eventStreamType("shipping").build()); }}
@Reducer@EventStreamType("shipping")class ReducersFilteringShippingTotalsReducer { ReducersFilteringShippingTotals sent(ReducersFilteringShipmentSent event, ReducersFilteringShippingTotals current, EventContext context) { int count = current == null ? 0 : current.count(); double totalCost = current == null ? 0.0 : current.totalCost(); return new ReducersFilteringShippingTotals(count + 1, totalCost + event.shippingCost()); }}Elixir does not support this workflow yet.import { field } from '@cratis/fundamentals';import { eventType, eventStreamType, Guid, IEventLog, reducer } from '@cratis/chronicle';
@eventType()class ReducersFilteringShipmentSent { @field(Number) shippingCost = 0;}
class ReducersFilteringShippingTotals { count = 0; totalCost = 0; }
class ReducersFilteringShippingService { constructor(private readonly eventLog: IEventLog) {}
async send(shippingCost: number): Promise<void> { const result = await this.eventLog.append(Guid.create().toString(), Object.assign(new ReducersFilteringShipmentSent(), { shippingCost }), { streamType: 'shipping' }); if (!result.isSuccess) throw new Error('Shipment send failed'); }}
@reducer('', undefined, ReducersFilteringShippingTotals)@eventStreamType('shipping')class ReducersFilteringShippingTotalsReducer { reducersFilteringShipmentSent(event: ReducersFilteringShipmentSent, current: ReducersFilteringShippingTotals | undefined): ReducersFilteringShippingTotals { return { count: (current?.count ?? 0) + 1, totalCost: (current?.totalCost ?? 0) + event.shippingCost }; }}Combine multiple filters
Section titled “Combine multiple filters”You can combine [FilterEventsByTag], [EventSourceType], and [EventStreamType] on the same reducer. Chronicle requires all filter categories to match:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Reducers;
[EventType]public record ReducersFilteringCombineOrderPlaced(decimal TotalAmount);
public record ReducersFilteringPremiumFulfillmentTotals(int Count, decimal Total);
public class ReducersFilteringCombineOrderService(IEventLog eventLog){ public Task PlacePremiumOrder(decimal totalAmount) => eventLog.Append( EventSourceId.New(), new ReducersFilteringCombineOrderPlaced(totalAmount), tags: ["premium"], eventSourceType: "order", eventStreamType: "fulfillment");}
[FilterEventsByTag("premium")][EventSourceType("order")][EventStreamType("fulfillment")]public class ReducersFilteringPremiumFulfillmentTotalsReducer : IReducerFor<ReducersFilteringPremiumFulfillmentTotals>{ public ReducersFilteringPremiumFulfillmentTotals Placed(ReducersFilteringCombineOrderPlaced @event, ReducersFilteringPremiumFulfillmentTotals? current, EventContext context) => new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.eventSequences.AppendOptionsimport io.cratis.chronicle.eventSequences.IEventLogimport io.cratis.chronicle.observation.EventSourceTypeimport io.cratis.chronicle.observation.EventStreamTypeimport io.cratis.chronicle.observation.FilterEventsByTagimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersFilteringCombineOrderPlaced(val totalAmount: Double)
@ReadModeldata class ReducersFilteringPremiumFulfillmentTotals(val count: Int = 0, val total: Double = 0.0)
class ReducersFilteringCombineOrderService(private val eventLog: IEventLog) { suspend fun placePremiumOrder(eventSourceId: String, totalAmount: Double) = eventLog.append( eventSourceId, ReducersFilteringCombineOrderPlaced(totalAmount), AppendOptions(tags = listOf("premium"), eventSourceType = "order", eventStreamType = "fulfillment") )}
@Reducer@FilterEventsByTag("premium")@EventSourceType("order")@EventStreamType("fulfillment")class ReducersFilteringPremiumFulfillmentTotalsReducer { fun placed(event: ReducersFilteringCombineOrderPlaced, current: ReducersFilteringPremiumFulfillmentTotals?, context: EventContext) = ReducersFilteringPremiumFulfillmentTotals((current?.count ?: 0) + 1, (current?.total ?: 0.0) + event.totalAmount)}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.eventSequences.IEventLog;import io.cratis.chronicle.java.AppendOptionsBuilder;import io.cratis.chronicle.java.EventLogJavaBridge;import io.cratis.chronicle.observation.EventSourceType;import io.cratis.chronicle.observation.EventStreamType;import io.cratis.chronicle.observation.FilterEventsByTag;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersFilteringCombineOrderPlaced(double totalAmount) {}
@ReadModelrecord ReducersFilteringPremiumFulfillmentTotals(int count, double total) { ReducersFilteringPremiumFulfillmentTotals() { this(0, 0.0); }}
class ReducersFilteringCombineOrderService { private final IEventLog eventLog;
ReducersFilteringCombineOrderService(IEventLog eventLog) { this.eventLog = eventLog; }
void placePremiumOrder(String eventSourceId, double totalAmount) { EventLogJavaBridge.append( eventLog, eventSourceId, new ReducersFilteringCombineOrderPlaced(totalAmount), new AppendOptionsBuilder() .tag("premium") .eventSourceType("order") .eventStreamType("fulfillment") .build()); }}
@Reducer@FilterEventsByTag("premium")@EventSourceType("order")@EventStreamType("fulfillment")class ReducersFilteringPremiumFulfillmentTotalsReducer { ReducersFilteringPremiumFulfillmentTotals placed(ReducersFilteringCombineOrderPlaced event, ReducersFilteringPremiumFulfillmentTotals current, EventContext context) { int count = current == null ? 0 : current.count(); double total = current == null ? 0.0 : current.total(); return new ReducersFilteringPremiumFulfillmentTotals(count + 1, total + event.totalAmount()); }}Elixir does not support this workflow yet.import { field } from '@cratis/fundamentals';import { eventType, eventSourceType, eventStreamType, filterEventsByTag, Guid, IEventLog, reducer } from '@cratis/chronicle';
@eventType()class ReducersFilteringCombineOrderPlaced { @field(Number) totalAmount = 0;}
class ReducersFilteringPremiumFulfillmentTotals { count = 0; total = 0; }
class ReducersFilteringCombineOrderService { constructor(private readonly eventLog: IEventLog) {}
async placePremiumOrder(totalAmount: number): Promise<void> { const result = await this.eventLog.append(Guid.create().toString(), Object.assign(new ReducersFilteringCombineOrderPlaced(), { totalAmount }), { tags: ['premium'], sourceType: 'order', streamType: 'fulfillment' }); if (!result.isSuccess) throw new Error('Order placement failed'); }}
@reducer('', undefined, ReducersFilteringPremiumFulfillmentTotals)@filterEventsByTag('premium')@eventSourceType('order')@eventStreamType('fulfillment')class ReducersFilteringPremiumFulfillmentTotalsReducer { reducersFilteringCombineOrderPlaced(event: ReducersFilteringCombineOrderPlaced, current: ReducersFilteringPremiumFulfillmentTotals | undefined): ReducersFilteringPremiumFulfillmentTotals { return { count: (current?.count ?? 0) + 1, total: (current?.total ?? 0) + event.totalAmount }; }}The reducer only receives events that match all three: the premium tag, the order event source type, and the fulfillment stream type. Multiple tags are alternatives within the tag category; they do not relax the source or stream requirements.
Without a source filter, Unspecified matches any source type. Without a stream filter, All matches any stream type. An explicit Default source filter matches only the default source type, not every source. Subscription filtering by an individual stream ID is not supported.
Passive reads and forwarded events
Section titled “Passive reads and forwarded events”The .NET client applies the same matching rules to passive reads and reducer scenarios for separate and self-reducing models, using stored or seeded metadata. When a collection read has an event-count limit, it selects that many events of the observed types before applying metadata filters. It does not keep reading until that many matching events have been accepted.
Inbox forwarding preserves tags and route metadata. Older inbox events affected by the tag-loss issue (#4823) remain untagged and cannot match a tag filter. See external-store route filters before relying on tags in an existing inbox history.
Tagging the reducer vs filtering events
Section titled “Tagging the reducer vs filtering events”[Tag] and [Tags] label the reducer itself, not the events it accepts. Use [FilterEventsByTag] when delivery should depend on event tags:
using Cratis.Chronicle;using Cratis.Chronicle.Events;using Cratis.Chronicle.Reducers;
[EventType]public record ReducersFilteringTagVsFilterOrderPlaced(decimal TotalAmount);
public record ReducersFilteringTagVsFilterTotals(int Count, decimal Total);
// These labels appear on the reducer definition — they do not affect dispatch[Tag("reporting")][Tag("premium")]public class ReducersFilteringLabeledFulfillmentTotalsReducer : IReducerFor<ReducersFilteringTagVsFilterTotals>{ public ReducersFilteringTagVsFilterTotals Placed(ReducersFilteringTagVsFilterOrderPlaced @event, ReducersFilteringTagVsFilterTotals? current, EventContext context) => new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);}
// These filter which events are dispatched to the reducer[FilterEventsByTag("premium")][EventSourceType("order")]public class ReducersFilteringFilteredFulfillmentTotalsReducer : IReducerFor<ReducersFilteringTagVsFilterTotals>{ public ReducersFilteringTagVsFilterTotals Placed(ReducersFilteringTagVsFilterOrderPlaced @event, ReducersFilteringTagVsFilterTotals? current, EventContext context) => new((current?.Count ?? 0) + 1, (current?.Total ?? 0m) + @event.TotalAmount);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.EventSourceTypeimport io.cratis.chronicle.observation.FilterEventsByTagimport io.cratis.chronicle.observation.Reducerimport io.cratis.chronicle.observation.Tagimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ReducersFilteringTagVsFilterOrderPlaced(val totalAmount: Double)
@ReadModeldata class ReducersFilteringTagVsFilterTotals(val count: Int = 0, val total: Double = 0.0)
// These labels appear on the reducer definition - they do not affect dispatch@Reducer@Tag("reporting")@Tag("premium")class ReducersFilteringLabeledFulfillmentTotalsReducer { fun placed(event: ReducersFilteringTagVsFilterOrderPlaced, current: ReducersFilteringTagVsFilterTotals?, context: EventContext) = ReducersFilteringTagVsFilterTotals((current?.count ?: 0) + 1, (current?.total ?: 0.0) + event.totalAmount)}
// These filter which events are dispatched to the reducer@Reducer@FilterEventsByTag("premium")@EventSourceType("order")class ReducersFilteringFilteredFulfillmentTotalsReducer { fun placed(event: ReducersFilteringTagVsFilterOrderPlaced, current: ReducersFilteringTagVsFilterTotals?, context: EventContext) = ReducersFilteringTagVsFilterTotals((current?.count ?: 0) + 1, (current?.total ?: 0.0) + event.totalAmount)}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.EventSourceType;import io.cratis.chronicle.observation.FilterEventsByTag;import io.cratis.chronicle.observation.Reducer;import io.cratis.chronicle.observation.Tag;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ReducersFilteringTagVsFilterOrderPlaced(double totalAmount) {}
@ReadModelrecord ReducersFilteringTagVsFilterTotals(int count, double total) { ReducersFilteringTagVsFilterTotals() { this(0, 0.0); }}
// These labels appear on the reducer definition - they do not affect dispatch@Reducer@Tag("reporting")@Tag("premium")class ReducersFilteringLabeledFulfillmentTotalsReducer { ReducersFilteringTagVsFilterTotals placed(ReducersFilteringTagVsFilterOrderPlaced event, ReducersFilteringTagVsFilterTotals current, EventContext context) { int count = current == null ? 0 : current.count(); double total = current == null ? 0.0 : current.total(); return new ReducersFilteringTagVsFilterTotals(count + 1, total + event.totalAmount()); }}
// These filter which events are dispatched to the reducer@Reducer@FilterEventsByTag("premium")@EventSourceType("order")class ReducersFilteringFilteredFulfillmentTotalsReducer { ReducersFilteringTagVsFilterTotals placed(ReducersFilteringTagVsFilterOrderPlaced event, ReducersFilteringTagVsFilterTotals current, EventContext context) { int count = current == null ? 0 : current.count(); double total = current == null ? 0.0 : current.total(); return new ReducersFilteringTagVsFilterTotals(count + 1, total + event.totalAmount()); }}Elixir does not support this workflow yet.TypeScript does not support this workflow yet.If you only need to describe or group reducers, continue with tagging reducers. To choose which sequence supplies their events, use event sequence selection.