arvo-event-handler 2.0.4 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -27,3 +27,7 @@
27
27
  ## [2.0.0] - 2024-11-26
28
28
 
29
29
  - Added support for versioned contracts in event handler for better versioning support
30
+ ## [2.1.1] - 2024-12-10
31
+
32
+ - Updated the telemetry implementation to be more streamlined, fixed some minor bugs and added better telemetry logging
33
+
@@ -1,5 +1,5 @@
1
1
  import { ArvoContractRecord, ArvoEvent } from 'arvo-core';
2
- import { OpenTelemetryConfig } from '../OpenTelemetry/types';
2
+ import { ArvoEventHandlerOpenTelemetryOptions } from '../types';
3
3
  /**
4
4
  * Abstract base class for Arvo event handlers.
5
5
  *
@@ -11,27 +11,14 @@ import { OpenTelemetryConfig } from '../OpenTelemetry/types';
11
11
  * ```
12
12
  */
13
13
  export default abstract class AbstractArvoEventHandler {
14
- /**
15
- * The source identifier for the event handler.
16
- *
17
- * @description
18
- * Uniquely identifies the '<ArvoEvent>.type' of events processed by this handler.
19
- *
20
- * @remarks
21
- * - Should be unique across all event handlers in the system
22
- * - Typically follows a dotted notation pattern (e.g., 'domain.entity.action')
23
- * - Used for routing, logging, and observability purposes
24
- */
25
- abstract readonly source: string;
26
14
  /**
27
15
  * Executes the event handling logic for a given Arvo event.
28
16
  *
29
17
  * @abstract
30
- * @param {ArvoEvent} event - The Arvo event to be processed. This event should conform
18
+ * @param event - The Arvo event to be processed. This event should conform
31
19
  * to the expected schema for the specific handler implementation.
32
- * @param {OpenTelemetryConfig} opentelemetry - Configuration for OpenTelemetry
33
- * integration, including tracing options
34
- * and context inheritance settings.
20
+ * @param opentelemetry - Configuration for OpenTelemetry integration
21
+ *
35
22
  * @returns {Promise<ArvoEvent[]>} A promise that resolves to an array of resulting Arvo events.
36
23
  * These events represent the outcome of processing the input event.
37
24
  *
@@ -64,7 +51,7 @@ export default abstract class AbstractArvoEventHandler {
64
51
  * - Properly handling span lifecycle (creation and completion)
65
52
  * - Propagating context appropriately
66
53
  */
67
- abstract execute(event: ArvoEvent, opentelemetry?: OpenTelemetryConfig): Promise<ArvoEvent[]>;
54
+ abstract execute(event: ArvoEvent, opentelemetry: ArvoEventHandlerOpenTelemetryOptions): Promise<ArvoEvent[]>;
68
55
  /**
69
56
  * Provides the schema for system error events.
70
57
  *
@@ -2,32 +2,21 @@ import { ArvoContract } from 'arvo-core';
2
2
  import { IArvoEventHandler } from './types';
3
3
  import ArvoEventHandler from '.';
4
4
  /**
5
- * Creates an ArvoEventHandler instance for a given ArvoContract.
5
+ * Creates an ArvoEventHandler for processing events defined by a specific contract.
6
+ * Each handler manages event validation, processing, and telemetry for its contract.
6
7
  *
7
- * @template TContract - The type of ArvoContract this handler is associated with.
8
- * @param param - The configuration parameters for the event handler.
9
- * @returns A new instance of ArvoEventHandler<TContract>.
10
- *
11
- * @remarks
12
- * This function is a factory for creating ArvoEventHandler instances.
13
- * It encapsulates the creation process and provides a convenient way to instantiate
14
- * handlers for specific Arvo contracts.
8
+ * @param param Configuration including contract, execution metrics and version handlers
9
+ * @returns Configured ArvoEventHandler instance for the specified contract
15
10
  *
16
11
  * @example
17
- * ```typescript
18
- * const myContract = new ArvoContract(...);
19
- * const myHandler = createArvoEventHandler({
20
- * contract: myContract,
21
- * executionunits: 100,
12
+ * const handler = createArvoEventHandler({
13
+ * contract: userContract,
14
+ * executionunits: 10,
22
15
  * handler: {
23
- * '0.0.1': async ({ event }) => {
24
- * // Handler implementation
25
- * }
16
+ * '1.0.0': async ({ event }) => {
17
+ * // Process event according to contract v1.0.0
18
+ * }
26
19
  * }
27
20
  * });
28
- * ```
29
- *
30
- * @see {@link IArvoEventHandler} for the full configuration options
31
- * @see {@link ArvoEventHandler} for the handler class implementation
32
21
  */
33
22
  export declare const createArvoEventHandler: <TContract extends ArvoContract>(param: IArvoEventHandler<TContract>) => ArvoEventHandler<TContract>;
@@ -6,33 +6,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createArvoEventHandler = void 0;
7
7
  var _1 = __importDefault(require("."));
8
8
  /**
9
- * Creates an ArvoEventHandler instance for a given ArvoContract.
9
+ * Creates an ArvoEventHandler for processing events defined by a specific contract.
10
+ * Each handler manages event validation, processing, and telemetry for its contract.
10
11
  *
11
- * @template TContract - The type of ArvoContract this handler is associated with.
12
- * @param param - The configuration parameters for the event handler.
13
- * @returns A new instance of ArvoEventHandler<TContract>.
14
- *
15
- * @remarks
16
- * This function is a factory for creating ArvoEventHandler instances.
17
- * It encapsulates the creation process and provides a convenient way to instantiate
18
- * handlers for specific Arvo contracts.
12
+ * @param param Configuration including contract, execution metrics and version handlers
13
+ * @returns Configured ArvoEventHandler instance for the specified contract
19
14
  *
20
15
  * @example
21
- * ```typescript
22
- * const myContract = new ArvoContract(...);
23
- * const myHandler = createArvoEventHandler({
24
- * contract: myContract,
25
- * executionunits: 100,
16
+ * const handler = createArvoEventHandler({
17
+ * contract: userContract,
18
+ * executionunits: 10,
26
19
  * handler: {
27
- * '0.0.1': async ({ event }) => {
28
- * // Handler implementation
29
- * }
20
+ * '1.0.0': async ({ event }) => {
21
+ * // Process event according to contract v1.0.0
22
+ * }
30
23
  * }
31
24
  * });
32
- * ```
33
- *
34
- * @see {@link IArvoEventHandler} for the full configuration options
35
- * @see {@link ArvoEventHandler} for the handler class implementation
36
25
  */
37
26
  var createArvoEventHandler = function (param) { return new _1.default(param); };
38
27
  exports.createArvoEventHandler = createArvoEventHandler;
@@ -1,110 +1,75 @@
1
- import { ArvoContract, ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
2
- import { IArvoEventHandler } from './types';
3
- import { SpanKind } from '@opentelemetry/api';
1
+ import { ArvoContract, ArvoEvent } from 'arvo-core';
2
+ import { IArvoEventHandler, ArvoEventHandlerFunction } from './types';
3
+ import { SpanOptions } from '@opentelemetry/api';
4
4
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
5
- import { OpenTelemetryConfig } from '../OpenTelemetry/types';
5
+ import { ArvoEventHandlerOpenTelemetryOptions } from '../types';
6
6
  /**
7
- * Represents an event handler for Arvo contracts.
7
+ * ArvoEventHandler manages the execution and processing of events in accordance with
8
+ * Arvo contracts. This class serves as the cornerstone for event handling operations,
9
+ * integrating contract validation, telemetry management, and event processing.
8
10
  *
9
- * @template TContract - The type of ArvoContract this handler is associated with.
10
- *
11
- * @remarks
12
- * This class is the core component for handling Arvo events. It encapsulates the logic
13
- * for executing event handlers, managing telemetry, and ensuring proper contract validation.
14
- * It's designed to be flexible and reusable across different Arvo contract implementations.
11
+ * The handler implements a robust execution flow that ensures proper validation,
12
+ * versioning, and error handling while maintaining detailed telemetry through
13
+ * OpenTelemetry integration. It supports versioned contracts and handles routing
14
+ * of both successful and error events.
15
15
  */
16
16
  export default class ArvoEventHandler<TContract extends ArvoContract> extends AbstractArvoEventHandler {
17
- /** The contract of the handler to which it is bound */
17
+ /** Contract instance that defines the event schema and validation rules */
18
18
  readonly contract: TContract;
19
- /** The default execution cost associated with this handler */
19
+ /** Computational cost metric associated with event handling operations */
20
20
  readonly executionunits: number;
21
+ /** OpenTelemetry configuration for event handling spans */
22
+ readonly spanOptions: SpanOptions;
23
+ /** Version-specific event handler implementation map */
24
+ readonly handler: ArvoEventHandlerFunction<TContract>;
25
+ /** The source identifier for events produced by this handler */
26
+ get source(): TContract['type'];
21
27
  /**
22
- * The source identifier for events produced by this handler
23
- *
24
- * @remarks
25
- * For all the events which are emitted by the handler, this is
26
- * the source field value of them all.
27
- */
28
- readonly source: string;
29
- readonly openInferenceSpanKind: OpenInferenceSpanKind;
30
- readonly arvoExecutionSpanKind: ArvoExecutionSpanKind;
31
- readonly openTelemetrySpanKind: SpanKind;
32
- private readonly _handler;
33
- /**
34
- * Creates an instance of ArvoEventHandler.
28
+ * Initializes a new ArvoEventHandler instance with the specified contract and configuration.
29
+ * Validates handler implementations against contract versions during initialization.
35
30
  *
36
- * @param param - The configuration parameters for the event handler.
31
+ * The constructor ensures that handler implementations exist for all supported contract
32
+ * versions and configures OpenTelemetry span attributes for monitoring event handling.
37
33
  *
38
- * @throws {Error} Throws an error if the provided source is invalid.
39
- *
40
- * @remarks
41
- * The constructor validates the source parameter against the CloudEventContextSchema.
42
- * If no source is provided, it defaults to the contract's accepted event type.
34
+ * @param param - Handler configuration including contract, execution units, and handler implementations
35
+ * @throws When handler implementations are missing for any contract version
43
36
  */
44
37
  constructor(param: IArvoEventHandler<TContract>);
45
38
  /**
46
- * Executes the event handler for a given event.
47
- *
48
- * @param event - The event to handle.
49
- * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
50
- * and context inheritance settings. Default is inherit from event and internal tracer
51
- * @returns A promise that resolves to an array of resulting ArvoEvents.
52
- *
53
- * This method performs the following steps:
54
- * 1. Creates an OpenTelemetry span for the execution.
55
- * 2. Validates the input event against the contract.
56
- * 3. Executes the handler function.
57
- * 4. Creates and returns the result event(s).
58
- * 5. Handles any errors and creates an error event if necessary.
59
- *
60
- * All telemetry data is properly set and propagated throughout the execution.
61
- *
62
- * @example
63
- * ```typescript
64
- * const contract = createArvoContract({ ... })
65
- * const handler = createArvoEventHandler({
66
- * contract: contract,
67
- * ...
68
- * });
69
- * const inputEvent: ArvoEvent<...> = createArvoEvent({ ... });
70
- * const resultEvents = await handler.execute(inputEvent);
71
- * ```
72
- *
73
- * @throws All error throw during the execution are returned as a system error event
74
- *
75
- * **Routing**
76
- *
77
- * The routing of the resulting events is determined as follows:
78
- * - The `to` field of the output event is set in this priority:
79
- * 1. The `to` field provided by the handler result
80
- * 2. The `redirectto` field from the input event
81
- * 3. The `source` field from the input event (as a form of reply)
82
- * - For system error events, the `to` field is always set to the `source` of the input event.
83
- *
84
- * **Telemetry**
85
- *
86
- * - Creates a new span for each execution as per the traceparent and tracestate field
87
- * of the event. If those are not present, then a brand new span is created and distributed
88
- * tracing is disabled
89
- * - Sets span attributes for input and output events
90
- * - Propagates trace context to output events
91
- * - Handles error cases and sets appropriate span status
39
+ * Processes an event according to the contract specifications. The execution flow encompasses
40
+ * span management, validation, handler execution, and error processing phases.
41
+ *
42
+ * Event Routing Logic:
43
+ * - Success events: Routes based on priority (handler result -> redirectto -> source)
44
+ * - Error events: Always routes back to the source
45
+ *
46
+ * Telemetry Integration:
47
+ * - Creates and manages OpenTelemetry spans for execution tracking
48
+ * - Propagates trace context through the event chain
49
+ * - Records execution metrics and error details
50
+ *
51
+ * Version Resolution:
52
+ * - Extracts version from event dataschema
53
+ * - Falls back to latest version if unspecified
54
+ * - Validates event data against versioned contract schema
55
+ *
56
+ * Error Handling:
57
+ * - Converts all errors to system error events
58
+ * - Maintains telemetry context for error scenarios
59
+ * - Ensures proper error event routing
60
+ *
61
+ * @param event - The event to process
62
+ * @param opentelemetry - Configuration for OpenTelemetry context inheritance
63
+ * @returns Promise resolving to array of result events or error event
92
64
  */
93
- execute(event: ArvoEvent, opentelemetry?: OpenTelemetryConfig): Promise<ArvoEvent[]>;
65
+ execute(event: ArvoEvent, opentelemetry?: ArvoEventHandlerOpenTelemetryOptions): Promise<ArvoEvent[]>;
94
66
  /**
95
- * Provides the schema for system error events.
96
- *
97
- * @returns An object containing the error event type and schema.
98
- *
99
- * @remarks
100
- * This getter defines the structure for system error events that may be emitted
101
- * when an unexpected error occurs during event handling. The error event type
102
- * is prefixed with 'sys.' followed by the contract's accepted event type and '.error'.
103
- * The schema used for these error events is the standard ArvoErrorSchema.
67
+ * Provides access to the system error event schema configuration.
68
+ * The schema defines the structure of error events emitted during execution failures.
104
69
  *
105
- * @example
106
- * // If the contract's accepted event type is 'user.created'
107
- * // The system error event type would be 'sys.user.created.error'
70
+ * Error events follow the naming convention: sys.<contract-type>.error
71
+ * For example, a contract handling 'user.created' events will emit error events
72
+ * with the type 'sys.user.created.error'.
108
73
  */
109
74
  get systemErrorSchema(): import("arvo-core").ArvoContractRecord<`sys.${string}.error`, import("zod").ZodObject<{
110
75
  errorName: import("zod").ZodString;