arvo-event-handler 2.0.4 → 2.1.0
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/dist/AbstractArvoEventHandler/index.d.ts +5 -18
- package/dist/ArvoEventHandler/helpers.d.ts +10 -21
- package/dist/ArvoEventHandler/helpers.js +10 -21
- package/dist/ArvoEventHandler/index.d.ts +56 -91
- package/dist/ArvoEventHandler/index.js +118 -135
- package/dist/ArvoEventHandler/types.d.ts +7 -30
- package/dist/ArvoEventRouter/helpers.d.ts +8 -25
- package/dist/ArvoEventRouter/helpers.js +8 -25
- package/dist/ArvoEventRouter/index.d.ts +47 -89
- package/dist/ArvoEventRouter/index.js +138 -155
- package/dist/ArvoEventRouter/types.d.ts +4 -22
- package/dist/MultiArvoEventHandler/index.d.ts +31 -96
- package/dist/MultiArvoEventHandler/index.js +75 -120
- package/dist/MultiArvoEventHandler/types.d.ts +6 -14
- package/dist/index.d.ts +2 -4
- package/dist/index.js +3 -5
- package/dist/types.d.ts +3 -0
- package/package.json +2 -2
- package/dist/OpenTelemetry/index.d.ts +0 -4
- package/dist/OpenTelemetry/index.js +0 -11
- package/dist/OpenTelemetry/types.d.ts +0 -48
- package/dist/OpenTelemetry/types.js +0 -2
- package/dist/OpenTelemetry/utils.d.ts +0 -87
- package/dist/OpenTelemetry/utils.js +0 -133
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ArvoContractRecord, ArvoEvent } from 'arvo-core';
|
|
2
|
-
import {
|
|
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
|
|
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
|
|
33
|
-
*
|
|
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
|
|
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
|
|
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
|
-
* @
|
|
8
|
-
* @
|
|
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
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* contract: myContract,
|
|
21
|
-
* executionunits: 100,
|
|
12
|
+
* const handler = createArvoEventHandler({
|
|
13
|
+
* contract: userContract,
|
|
14
|
+
* executionunits: 10,
|
|
22
15
|
* handler: {
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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
|
|
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
|
-
* @
|
|
12
|
-
* @
|
|
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
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* contract: myContract,
|
|
25
|
-
* executionunits: 100,
|
|
16
|
+
* const handler = createArvoEventHandler({
|
|
17
|
+
* contract: userContract,
|
|
18
|
+
* executionunits: 10,
|
|
26
19
|
* handler: {
|
|
27
|
-
*
|
|
28
|
-
*
|
|
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
|
|
2
|
-
import { IArvoEventHandler } from './types';
|
|
3
|
-
import {
|
|
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 {
|
|
5
|
+
import { ArvoEventHandlerOpenTelemetryOptions } from '../types';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
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
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
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
|
-
/**
|
|
17
|
+
/** Contract instance that defines the event schema and validation rules */
|
|
18
18
|
readonly contract: TContract;
|
|
19
|
-
/**
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
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?:
|
|
65
|
+
execute(event: ArvoEvent, opentelemetry?: ArvoEventHandlerOpenTelemetryOptions): Promise<ArvoEvent[]>;
|
|
94
66
|
/**
|
|
95
|
-
* Provides
|
|
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
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
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;
|