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,110 +1,68 @@
|
|
|
1
|
-
import { ArvoContract, ArvoEvent
|
|
1
|
+
import { ArvoContract, ArvoEvent } from 'arvo-core';
|
|
2
2
|
import ArvoEventHandler from '../ArvoEventHandler';
|
|
3
3
|
import { IArvoEventRouter } from './types';
|
|
4
|
-
import {
|
|
4
|
+
import { SpanOptions } from '@opentelemetry/api';
|
|
5
5
|
import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
|
|
6
|
-
import {
|
|
6
|
+
import { ArvoEventHandlerOpenTelemetryOptions } from '../types';
|
|
7
7
|
/**
|
|
8
|
-
* ArvoEventRouter
|
|
8
|
+
* ArvoEventRouter manages event routing and execution within the Arvo event system. It directs
|
|
9
|
+
* incoming events to appropriate handlers based on event type while maintaining telemetry
|
|
10
|
+
* and error handling.
|
|
11
|
+
*
|
|
12
|
+
* The router enforces contract validation, manages execution costs, and provides comprehensive
|
|
13
|
+
* telemetry via OpenTelemetry integration. It handles event lifecycle management from initial
|
|
14
|
+
* receipt through processing and response generation.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const router = createArvoEventRouter({
|
|
19
|
+
* source: "payment.service",
|
|
20
|
+
* executionunits: 1,
|
|
21
|
+
* handlers: [paymentProcessedHandler, paymentFailedHandler]
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Route an incoming event
|
|
25
|
+
* const results = await router.execute(incomingEvent);
|
|
26
|
+
* ```
|
|
9
27
|
*/
|
|
10
28
|
export declare class ArvoEventRouter extends AbstractArvoEventHandler {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
* @remarks
|
|
17
|
-
* The router attempts to match the `event.to` field with this value.
|
|
18
|
-
* If the source is 'arvo.event.router', the `event.to` is not matched and any event is allowed.
|
|
19
|
-
* 'arvo.event.router' is the default source which is set automatically in case a source
|
|
20
|
-
* is not explicitly provided
|
|
21
|
-
*/
|
|
22
|
-
get source(): string;
|
|
23
|
-
/**
|
|
24
|
-
* A list of all available event handlers to be used by the router.
|
|
25
|
-
*/
|
|
26
|
-
readonly handlers: ArvoEventHandler<ArvoContract>[];
|
|
29
|
+
/** Source identifier for the router used in event routing */
|
|
30
|
+
readonly source: string;
|
|
31
|
+
/** Registry mapping event types to their handlers */
|
|
32
|
+
readonly handlersMap: Record<string, ArvoEventHandler<ArvoContract>>;
|
|
27
33
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
34
|
+
* Computational cost metric for router operations.
|
|
35
|
+
* Used for resource tracking and billing calculations.
|
|
30
36
|
*/
|
|
31
37
|
readonly executionunits: number;
|
|
32
38
|
/**
|
|
33
|
-
*
|
|
39
|
+
* The OpenTelemetry span options
|
|
34
40
|
*/
|
|
35
|
-
readonly
|
|
36
|
-
readonly openInferenceSpanKind: OpenInferenceSpanKind;
|
|
37
|
-
readonly arvoExecutionSpanKind: ArvoExecutionSpanKind;
|
|
38
|
-
readonly openTelemetrySpanKind: SpanKind;
|
|
41
|
+
readonly spanOptions: SpanOptions;
|
|
39
42
|
/**
|
|
40
|
-
* Creates an instance
|
|
41
|
-
*
|
|
42
|
-
* @
|
|
43
|
-
*
|
|
43
|
+
* Creates an ArvoEventRouter instance with specified configuration.
|
|
44
|
+
*
|
|
45
|
+
* @param param - Router configuration containing source, handlers, and execution parameters
|
|
46
|
+
*
|
|
47
|
+
* @throws {Error} When source contains invalid characters (non-alphanumeric)
|
|
48
|
+
* @throws {Error} When multiple handlers are registered for the same event type
|
|
44
49
|
*/
|
|
45
50
|
constructor(param: IArvoEventRouter);
|
|
46
51
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* @returns A Promise that resolves to an array of ArvoEvents.
|
|
53
|
-
*
|
|
54
|
-
* @remarks
|
|
55
|
-
* This function performs the following steps:
|
|
56
|
-
* 1. Initializes OpenTelemetry span for tracing and monitoring.
|
|
57
|
-
* 2. Validates the event's destination ('to' field) against the router's source.
|
|
58
|
-
* 3. Finds the appropriate handler for the event based on its type.
|
|
59
|
-
* 4. Executes the handler and processes the results.
|
|
60
|
-
* 5. Handles any errors that occur during the process.
|
|
52
|
+
* Routes and executes an event through its appropriate handler. Creates a telemetry span,
|
|
53
|
+
* validates the event destination, finds a matching handler, and processes the event.
|
|
54
|
+
* Handles routing errors, missing handlers, and execution failures by returning error
|
|
55
|
+
* events with telemetry context. Tracks performance through execution units and span
|
|
56
|
+
* propagation.
|
|
61
57
|
*
|
|
62
|
-
* @
|
|
63
|
-
* @
|
|
64
|
-
*
|
|
65
|
-
* **Telemetry**
|
|
66
|
-
*
|
|
67
|
-
* - Creates an OpenTelemetry span for the entire execution process.
|
|
68
|
-
* - Sets span attributes for OpenInference and ArvoExecution.
|
|
69
|
-
* - Propagates trace context from the input event if available.
|
|
70
|
-
* - Records any errors in the span and sets the span status accordingly.
|
|
71
|
-
* - Adds event attributes to the span for both input and output events.
|
|
72
|
-
*
|
|
73
|
-
* **Routing**
|
|
74
|
-
*
|
|
75
|
-
* The routing process involves:
|
|
76
|
-
* 1. Matching the event's 'to' field with the router's source (if specified).
|
|
77
|
-
* 2. Finding a handler that accepts the event's type.
|
|
78
|
-
* 3. Executing the matched handler with the event.
|
|
79
|
-
* 4. Processing the handler's results and creating new events.
|
|
80
|
-
*
|
|
81
|
-
* **Error Handling**
|
|
82
|
-
*
|
|
83
|
-
* If an error occurs during execution:
|
|
84
|
-
* 1. The error is recorded in the OpenTelemetry span.
|
|
85
|
-
* 2. A new error event is created and returned.
|
|
86
|
-
* 3. The error event is sent back to the original event's source.
|
|
87
|
-
*
|
|
88
|
-
* **Performance**
|
|
89
|
-
*
|
|
90
|
-
* - Execution units are tracked for both successful executions and errors.
|
|
91
|
-
* - The router's default execution units are used for error events.
|
|
58
|
+
* @param event The event to be routed and processed
|
|
59
|
+
* @param opentelemetry Configuration for telemetry context inheritance
|
|
60
|
+
* @returns Promise resolving to resulting events or error events
|
|
92
61
|
*/
|
|
93
|
-
execute(event: ArvoEvent, opentelemetry?:
|
|
62
|
+
execute(event: ArvoEvent, opentelemetry?: ArvoEventHandlerOpenTelemetryOptions): Promise<ArvoEvent[]>;
|
|
94
63
|
/**
|
|
95
|
-
*
|
|
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 handler's source and '.error'.
|
|
103
|
-
* The schema used for these error events is the standard ArvoErrorSchema.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* // If the handler's source is 'user.service'
|
|
107
|
-
* // The system error event type would be 'sys.user.service.error'
|
|
64
|
+
* System error schema configuration.
|
|
65
|
+
* Error events follow format: sys.<handler-source>.error
|
|
108
66
|
*/
|
|
109
67
|
get systemErrorSchema(): {
|
|
110
68
|
type: string;
|
|
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
17
28
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
29
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
30
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -60,209 +71,181 @@ var utils_1 = require("../utils");
|
|
|
60
71
|
var api_1 = require("@opentelemetry/api");
|
|
61
72
|
var utils_2 = require("./utils");
|
|
62
73
|
var AbstractArvoEventHandler_1 = __importDefault(require("../AbstractArvoEventHandler"));
|
|
63
|
-
var OpenTelemetry_1 = require("../OpenTelemetry");
|
|
64
|
-
var utils_3 = require("../OpenTelemetry/utils");
|
|
65
74
|
/**
|
|
66
|
-
* ArvoEventRouter
|
|
75
|
+
* ArvoEventRouter manages event routing and execution within the Arvo event system. It directs
|
|
76
|
+
* incoming events to appropriate handlers based on event type while maintaining telemetry
|
|
77
|
+
* and error handling.
|
|
78
|
+
*
|
|
79
|
+
* The router enforces contract validation, manages execution costs, and provides comprehensive
|
|
80
|
+
* telemetry via OpenTelemetry integration. It handles event lifecycle management from initial
|
|
81
|
+
* receipt through processing and response generation.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const router = createArvoEventRouter({
|
|
86
|
+
* source: "payment.service",
|
|
87
|
+
* executionunits: 1,
|
|
88
|
+
* handlers: [paymentProcessedHandler, paymentFailedHandler]
|
|
89
|
+
* });
|
|
90
|
+
*
|
|
91
|
+
* // Route an incoming event
|
|
92
|
+
* const results = await router.execute(incomingEvent);
|
|
93
|
+
* ```
|
|
67
94
|
*/
|
|
68
95
|
var ArvoEventRouter = /** @class */ (function (_super) {
|
|
69
96
|
__extends(ArvoEventRouter, _super);
|
|
70
97
|
/**
|
|
71
|
-
* Creates an instance
|
|
72
|
-
*
|
|
73
|
-
* @
|
|
74
|
-
*
|
|
98
|
+
* Creates an ArvoEventRouter instance with specified configuration.
|
|
99
|
+
*
|
|
100
|
+
* @param param - Router configuration containing source, handlers, and execution parameters
|
|
101
|
+
*
|
|
102
|
+
* @throws {Error} When source contains invalid characters (non-alphanumeric)
|
|
103
|
+
* @throws {Error} When multiple handlers are registered for the same event type
|
|
75
104
|
*/
|
|
76
105
|
function ArvoEventRouter(param) {
|
|
106
|
+
var _a;
|
|
107
|
+
var _b, _c;
|
|
77
108
|
var _this = _super.call(this) || this;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* A map of all the available event handlers
|
|
81
|
-
*/
|
|
109
|
+
/** Registry mapping event types to their handlers */
|
|
82
110
|
_this.handlersMap = {};
|
|
83
|
-
_this.openInferenceSpanKind = arvo_core_1.OpenInferenceSpanKind.CHAIN;
|
|
84
|
-
_this.arvoExecutionSpanKind = arvo_core_1.ArvoExecutionSpanKind.EVENT_HANDLER;
|
|
85
|
-
_this.openTelemetrySpanKind = api_1.SpanKind.INTERNAL;
|
|
86
|
-
_this.handlers = param.handlers;
|
|
87
111
|
if (param.source && !(0, utils_1.isLowerAlphanumeric)(param.source)) {
|
|
88
|
-
throw new Error("Invalid
|
|
112
|
+
throw new Error("Invalid source identifier '".concat(param.source, "': Must contain only alphanumeric characters (example: payment.service)"));
|
|
89
113
|
}
|
|
90
|
-
_this.
|
|
114
|
+
_this.source = param.source;
|
|
91
115
|
_this.executionunits = param.executionunits;
|
|
92
|
-
for (var _i = 0,
|
|
93
|
-
var handler =
|
|
116
|
+
for (var _i = 0, _d = param.handlers; _i < _d.length; _i++) {
|
|
117
|
+
var handler = _d[_i];
|
|
94
118
|
if (_this.handlersMap[handler.contract.type]) {
|
|
95
119
|
var existingHandler = _this.handlersMap[handler.contract.type];
|
|
96
|
-
throw new Error(
|
|
120
|
+
throw new Error("Duplicate handler registration detected for event type '".concat(handler.contract.type, "'. ") +
|
|
121
|
+
"Conflicts between contracts: ".concat(existingHandler.contract.uri, " and ").concat(handler.contract.uri));
|
|
97
122
|
}
|
|
98
123
|
_this.handlersMap[handler.contract.type] = handler;
|
|
99
124
|
}
|
|
100
|
-
|
|
101
|
-
Object.freeze(_this.handlersMap);
|
|
125
|
+
_this.spanOptions = __assign(__assign({ kind: api_1.SpanKind.CONSUMER }, param.spanOptions), { attributes: __assign(__assign((_a = {}, _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.EVENT_HANDLER, _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _a), ((_c = (_b = param.spanOptions) === null || _b === void 0 ? void 0 : _b.attributes) !== null && _c !== void 0 ? _c : {})), { 'arvo.handler.source': _this.source }) });
|
|
102
126
|
return _this;
|
|
103
127
|
}
|
|
104
|
-
Object.defineProperty(ArvoEventRouter.prototype, "source", {
|
|
105
|
-
/**
|
|
106
|
-
* The source name of the router.
|
|
107
|
-
*
|
|
108
|
-
* @remarks
|
|
109
|
-
* The router attempts to match the `event.to` field with this value.
|
|
110
|
-
* If the source is 'arvo.event.router', the `event.to` is not matched and any event is allowed.
|
|
111
|
-
* 'arvo.event.router' is the default source which is set automatically in case a source
|
|
112
|
-
* is not explicitly provided
|
|
113
|
-
*/
|
|
114
|
-
get: function () {
|
|
115
|
-
var _a;
|
|
116
|
-
return (_a = this._source) !== null && _a !== void 0 ? _a : this._handlerDefaultSource;
|
|
117
|
-
},
|
|
118
|
-
enumerable: false,
|
|
119
|
-
configurable: true
|
|
120
|
-
});
|
|
121
128
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* @returns A Promise that resolves to an array of ArvoEvents.
|
|
128
|
-
*
|
|
129
|
-
* @remarks
|
|
130
|
-
* This function performs the following steps:
|
|
131
|
-
* 1. Initializes OpenTelemetry span for tracing and monitoring.
|
|
132
|
-
* 2. Validates the event's destination ('to' field) against the router's source.
|
|
133
|
-
* 3. Finds the appropriate handler for the event based on its type.
|
|
134
|
-
* 4. Executes the handler and processes the results.
|
|
135
|
-
* 5. Handles any errors that occur during the process.
|
|
136
|
-
*
|
|
137
|
-
* @throws Error event if the event's 'to' field doesn't match the router's source.
|
|
138
|
-
* @throws Error event if no valid handler is found for the event type.
|
|
139
|
-
*
|
|
140
|
-
* **Telemetry**
|
|
141
|
-
*
|
|
142
|
-
* - Creates an OpenTelemetry span for the entire execution process.
|
|
143
|
-
* - Sets span attributes for OpenInference and ArvoExecution.
|
|
144
|
-
* - Propagates trace context from the input event if available.
|
|
145
|
-
* - Records any errors in the span and sets the span status accordingly.
|
|
146
|
-
* - Adds event attributes to the span for both input and output events.
|
|
147
|
-
*
|
|
148
|
-
* **Routing**
|
|
129
|
+
* Routes and executes an event through its appropriate handler. Creates a telemetry span,
|
|
130
|
+
* validates the event destination, finds a matching handler, and processes the event.
|
|
131
|
+
* Handles routing errors, missing handlers, and execution failures by returning error
|
|
132
|
+
* events with telemetry context. Tracks performance through execution units and span
|
|
133
|
+
* propagation.
|
|
149
134
|
*
|
|
150
|
-
* The
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* 3. Executing the matched handler with the event.
|
|
154
|
-
* 4. Processing the handler's results and creating new events.
|
|
155
|
-
*
|
|
156
|
-
* **Error Handling**
|
|
157
|
-
*
|
|
158
|
-
* If an error occurs during execution:
|
|
159
|
-
* 1. The error is recorded in the OpenTelemetry span.
|
|
160
|
-
* 2. A new error event is created and returned.
|
|
161
|
-
* 3. The error event is sent back to the original event's source.
|
|
162
|
-
*
|
|
163
|
-
* **Performance**
|
|
164
|
-
*
|
|
165
|
-
* - Execution units are tracked for both successful executions and errors.
|
|
166
|
-
* - The router's default execution units are used for error events.
|
|
135
|
+
* @param event The event to be routed and processed
|
|
136
|
+
* @param opentelemetry Configuration for telemetry context inheritance
|
|
137
|
+
* @returns Promise resolving to resulting events or error events
|
|
167
138
|
*/
|
|
168
|
-
ArvoEventRouter.prototype.execute = function (
|
|
169
|
-
return __awaiter(this,
|
|
170
|
-
var span;
|
|
139
|
+
ArvoEventRouter.prototype.execute = function (event_1) {
|
|
140
|
+
return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
|
|
171
141
|
var _this = this;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
142
|
+
if (opentelemetry === void 0) { opentelemetry = {
|
|
143
|
+
inheritFrom: 'EVENT',
|
|
144
|
+
}; }
|
|
145
|
+
return __generator(this, function (_a) {
|
|
146
|
+
switch (_a.label) {
|
|
147
|
+
case 0: return [4 /*yield*/, arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan({
|
|
148
|
+
name: 'ArvoEventRouter',
|
|
149
|
+
spanOptions: this.spanOptions,
|
|
150
|
+
disableSpanManagement: true,
|
|
151
|
+
context: opentelemetry.inheritFrom === 'EVENT'
|
|
152
|
+
? {
|
|
153
|
+
inheritFrom: 'TRACE_HEADERS',
|
|
154
|
+
traceHeaders: {
|
|
155
|
+
traceparent: event.traceparent,
|
|
156
|
+
tracestate: event.tracestate,
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
: {
|
|
160
|
+
inheritFrom: 'CONTEXT',
|
|
161
|
+
context: api_1.context.active(),
|
|
162
|
+
},
|
|
163
|
+
fn: function (span) { return __awaiter(_this, void 0, void 0, function () {
|
|
164
|
+
var otelSpanHeaders, newEvent, results, resultingEvents, error_1;
|
|
188
165
|
var _this = this;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
switch (_b.label) {
|
|
166
|
+
return __generator(this, function (_a) {
|
|
167
|
+
switch (_a.label) {
|
|
192
168
|
case 0:
|
|
193
169
|
otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
|
|
194
170
|
newEvent = (0, utils_2.deleteOtelHeaders)(event);
|
|
195
|
-
|
|
171
|
+
_a.label = 1;
|
|
196
172
|
case 1:
|
|
197
|
-
|
|
173
|
+
_a.trys.push([1, 3, 4, 5]);
|
|
198
174
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
175
|
+
Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
176
|
+
var key = _a[0], value = _a[1];
|
|
177
|
+
return span.setAttribute("to_process.0.".concat(key), value);
|
|
178
|
+
});
|
|
179
|
+
(0, arvo_core_1.logToSpan)({
|
|
180
|
+
level: 'INFO',
|
|
181
|
+
message: "Initiating event resolution - Type: ".concat(newEvent.type, ", Source: ").concat(newEvent.source, ", Destination: ").concat(newEvent.to),
|
|
182
|
+
});
|
|
183
|
+
if (newEvent.to !== this.source) {
|
|
184
|
+
throw new Error("Event destination mismatch: Received destination '".concat(newEvent.to, "', ") +
|
|
185
|
+
"but router accepts only '".concat(this.source, "'"));
|
|
202
186
|
}
|
|
203
187
|
if (!this.handlersMap[newEvent.type]) {
|
|
204
|
-
throw new Error(
|
|
188
|
+
throw new Error("No registered handler found for event type '".concat(newEvent.type, "'"));
|
|
205
189
|
}
|
|
190
|
+
(0, arvo_core_1.logToSpan)({
|
|
191
|
+
level: 'INFO',
|
|
192
|
+
message: "Handler found for event type '".concat(newEvent.type, "' - Beginning event processing"),
|
|
193
|
+
});
|
|
206
194
|
return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent, {
|
|
207
|
-
inheritFrom: '
|
|
208
|
-
tracer: (_a = opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.tracer) !== null && _a !== void 0 ? _a : (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
|
|
195
|
+
inheritFrom: 'CONTEXT',
|
|
209
196
|
})];
|
|
210
197
|
case 2:
|
|
211
|
-
results =
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
198
|
+
results = _a.sent();
|
|
199
|
+
resultingEvents = results.map(function (event) {
|
|
200
|
+
var _a;
|
|
201
|
+
return new arvo_core_1.ArvoEvent({
|
|
202
|
+
id: event.id,
|
|
203
|
+
time: event.time,
|
|
204
|
+
source: _this.source,
|
|
205
|
+
specversion: '1.0',
|
|
206
|
+
type: event.type,
|
|
207
|
+
subject: event.subject,
|
|
208
|
+
datacontenttype: event.datacontenttype,
|
|
209
|
+
dataschema: event.dataschema,
|
|
210
|
+
to: event.to,
|
|
211
|
+
accesscontrol: event.accesscontrol,
|
|
212
|
+
redirectto: event.redirectto,
|
|
213
|
+
executionunits: ((_a = event.executionunits) !== null && _a !== void 0 ? _a : 0) + _this.executionunits,
|
|
214
|
+
traceparent: otelSpanHeaders.traceparent,
|
|
215
|
+
tracestate: otelSpanHeaders.tracestate,
|
|
216
|
+
}, event.data, event.cloudevent.extensions);
|
|
217
|
+
});
|
|
218
|
+
(0, arvo_core_1.logToSpan)({
|
|
219
|
+
level: 'INFO',
|
|
220
|
+
message: "Event processing completed successfully - Generated ".concat(resultingEvents.length, " new event(s)"),
|
|
221
|
+
});
|
|
222
|
+
resultingEvents.forEach(function (event, index) {
|
|
223
|
+
return Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
224
|
+
var key = _a[0], value = _a[1];
|
|
225
|
+
return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
return [2 /*return*/, resultingEvents];
|
|
231
229
|
case 3:
|
|
232
|
-
error_1 =
|
|
233
|
-
return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders,
|
|
234
|
-
var _a;
|
|
235
|
-
return (0, arvo_core_1.createArvoEvent)(param, extension, {
|
|
236
|
-
tracer: (_a = opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.tracer) !== null && _a !== void 0 ? _a : (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
|
|
237
|
-
});
|
|
238
|
-
})];
|
|
230
|
+
error_1 = _a.sent();
|
|
231
|
+
return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, this.systemErrorSchema.type, this.source, event, this.executionunits, function (param, extensions) { return (0, arvo_core_1.createArvoEvent)(param, extensions); })];
|
|
239
232
|
case 4:
|
|
240
233
|
span.end();
|
|
241
234
|
return [7 /*endfinally*/];
|
|
242
235
|
case 5: return [2 /*return*/];
|
|
243
236
|
}
|
|
244
237
|
});
|
|
245
|
-
}); }
|
|
246
|
-
|
|
238
|
+
}); },
|
|
239
|
+
})];
|
|
240
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
247
241
|
}
|
|
248
242
|
});
|
|
249
243
|
});
|
|
250
244
|
};
|
|
251
245
|
Object.defineProperty(ArvoEventRouter.prototype, "systemErrorSchema", {
|
|
252
246
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
* @returns An object containing the error event type and schema.
|
|
256
|
-
*
|
|
257
|
-
* @remarks
|
|
258
|
-
* This getter defines the structure for system error events that may be emitted
|
|
259
|
-
* when an unexpected error occurs during event handling. The error event type
|
|
260
|
-
* is prefixed with 'sys.' followed by the handler's source and '.error'.
|
|
261
|
-
* The schema used for these error events is the standard ArvoErrorSchema.
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* // If the handler's source is 'user.service'
|
|
265
|
-
* // The system error event type would be 'sys.user.service.error'
|
|
247
|
+
* System error schema configuration.
|
|
248
|
+
* Error events follow format: sys.<handler-source>.error
|
|
266
249
|
*/
|
|
267
250
|
get: function () {
|
|
268
251
|
return {
|
|
@@ -1,28 +1,18 @@
|
|
|
1
|
-
import { ArvoContract
|
|
1
|
+
import { ArvoContract } from 'arvo-core';
|
|
2
2
|
import ArvoEventHandler from '../ArvoEventHandler';
|
|
3
|
-
import {
|
|
3
|
+
import { SpanOptions } from '@opentelemetry/api';
|
|
4
4
|
/**
|
|
5
5
|
* Interface for defining an Arvo Event Router.
|
|
6
|
-
*
|
|
7
|
-
* @interface IArvoEventRouter
|
|
8
6
|
*/
|
|
9
7
|
export interface IArvoEventRouter {
|
|
10
8
|
/**
|
|
11
9
|
* Defines the source name of the router.
|
|
12
10
|
*
|
|
13
|
-
* @property {string} [source]
|
|
14
|
-
*
|
|
15
11
|
* @remarks
|
|
16
12
|
* If this field is defined:
|
|
17
13
|
* - The router will only listen to events with a `to` field matching this `source`.
|
|
18
14
|
* - If an event's `to` field doesn't match, a system error event will be emitted.
|
|
19
15
|
* - For all emitted events, the `source` field will be overridden by this value.
|
|
20
|
-
*
|
|
21
|
-
* If this field is not defined:
|
|
22
|
-
* - The router will listen to all events.
|
|
23
|
-
* - If no appropriate handler is found for an event, a system error event will be emitted.
|
|
24
|
-
* - The `source` field of emitted events will be set according to the configuration
|
|
25
|
-
* in the relevant `ArvoEventHandler`.
|
|
26
16
|
*/
|
|
27
17
|
source: string;
|
|
28
18
|
/**
|
|
@@ -40,15 +30,7 @@ export interface IArvoEventRouter {
|
|
|
40
30
|
*/
|
|
41
31
|
handlers: ArvoEventHandler<ArvoContract<any, any, any>>[];
|
|
42
32
|
/**
|
|
43
|
-
* The OpenTelemetry span
|
|
44
|
-
* executor.
|
|
45
|
-
* @param [openInference] - The OpenInference span kind. Default is "CHAIN"
|
|
46
|
-
* @param [arvoExecution] - The ArvoExecution span kind. Default is "EVENT_HANDLER"
|
|
47
|
-
* @param [openTelemetry] - The OpenTelemetry span kind. Default is "INTERNAL"
|
|
33
|
+
* The OpenTelemetry span options
|
|
48
34
|
*/
|
|
49
|
-
|
|
50
|
-
openInference?: OpenInferenceSpanKind;
|
|
51
|
-
arvoExecution?: ArvoExecutionSpanKind;
|
|
52
|
-
openTelemetry?: SpanKind;
|
|
53
|
-
};
|
|
35
|
+
spanOptions?: SpanOptions;
|
|
54
36
|
}
|