Initializes a new ArvoEventHandler instance with the specified contract and configuration. Validates handler implementations against contract versions during initialization.
The constructor ensures that handler implementations exist for all supported contract versions and configures OpenTelemetry span attributes for monitoring event handling.
Handler configuration including contract, execution units, and handler implementations
Readonly
contractContract instance that defines the event schema and validation rules
Readonly
executionunitsComputational cost metric associated with event handling operations
Readonly
handlerVersion-specific event handler implementation map
Readonly
spanOpenTelemetry configuration for event handling spans
Provides access to the system error event schema configuration. The schema defines the structure of error events emitted during execution failures.
Error events follow the naming convention: sys.
Processes an incoming event according to the handler's contract specifications. This method handles the complete lifecycle of event processing including validation, execution, and error handling, while maintaining detailed telemetry through OpenTelemetry.
The execution follows a careful sequence to ensure reliability: First, it validates that the event matches the handler's contract type. Then it extracts and validates the event's schema version, defaulting to the latest version if none is specified. After validation passes, it executes the version-specific handler function and processes its output into new events.
The method handles routing through three distinct paths:
Throughout execution, comprehensive telemetry is maintained through OpenTelemetry spans, tracking the complete event journey including validation steps, processing time, and any errors that occur. This enables detailed monitoring and debugging of the event flow.
The incoming event to process
Configuration for OpenTelemetry context inheritance
Promise resolving to an array of output events or error events
ArvoEventHandler is the core component for processing events in the Arvo system. It enforces contracts between services by ensuring that all events follow their specified formats and rules.
The handler is built on two fundamental patterns: Meyer's Design by Contract and Fowler's Tolerant Reader. It binds to an ArvoContract that defines what events it can receive and send across all versions. This versioning is strict - the handler must implement every version defined in its contract, or it will fail both at compile time and runtime.
Following the Tolerant Reader pattern, the handler accepts any incoming event but only processes those that exactly match one of its contract versions. When an event matches, it's handled by the specific implementation for that version. This approach maintains compatibility while ensuring precise contract adherence.
The handler uses Zod for validation, automatically checking both incoming and outgoing events. This means it not only verifies data formats but also applies default values where needed and ensures all conditions are met before and after processing.
Error handling in the handler divides issues into two categories:
Violations
are serious contract breaches that indicate fundamental problems with how services are communicating. These errors bubble up to the calling code, allowing developers to handle these critical issues explicitly.System Error Events
cover normal runtime errors that occur during event processing. These are typically workflow-related issues that need to be reported back to the event's source but don't indicate a broken contract.Example