arvo-event-handler 1.1.6 → 1.1.8

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
@@ -19,7 +19,7 @@
19
19
  ## [1.0.2] - 2024-09-10
20
20
 
21
21
  - Added ArvoEventRouter as a mechanism to group ArvoEventHandlers
22
+
22
23
  ## [1.1.0] - 2024-09-30
23
24
 
24
25
  - Added Abstract handler class and bound all handlers to it
25
-
package/README.md CHANGED
@@ -27,12 +27,11 @@ Whether you're building a small microservice or a large-scale distributed system
27
27
 
28
28
  Arvo is a collection of libraries which allows you to build the event driven system in the Arvo pattern. However, if you feel you don't have to use them or you can use them as you see fit.
29
29
 
30
- | Scope | NPM | Github | Documentation |
31
- | ------------ | ------------------------------------------------------------- | ------------------------------------------------------------- | ------------------------------------------------------------- |
32
- | Orchestration | https://www.npmjs.com/package/arvo-xstate?activeTab=readme | https://github.com/SaadAhmad123/arvo-xstate | https://saadahmad123.github.io/arvo-xstate/index.html |
33
- | Core | https://www.npmjs.com/package/arvo-core?activeTab=readme | https://github.com/SaadAhmad123/arvo-core | https://saadahmad123.github.io/arvo-core/index.html |
34
- | Event Handling | https://www.npmjs.com/package/arvo-event-handler?activeTab=readme | https://github.com/SaadAhmad123/arvo-event-handler | https://saadahmad123.github.io/arvo-event-handler/index.html |
35
-
30
+ | Scope | NPM | Github | Documentation |
31
+ | -------------- | ----------------------------------------------------------------- | -------------------------------------------------- | ------------------------------------------------------------ |
32
+ | Orchestration | https://www.npmjs.com/package/arvo-xstate?activeTab=readme | https://github.com/SaadAhmad123/arvo-xstate | https://saadahmad123.github.io/arvo-xstate/index.html |
33
+ | Core | https://www.npmjs.com/package/arvo-core?activeTab=readme | https://github.com/SaadAhmad123/arvo-core | https://saadahmad123.github.io/arvo-core/index.html |
34
+ | Event Handling | https://www.npmjs.com/package/arvo-event-handler?activeTab=readme | https://github.com/SaadAhmad123/arvo-event-handler | https://saadahmad123.github.io/arvo-event-handler/index.html |
36
35
 
37
36
  # Arvo - Event Handler
38
37
 
@@ -1,4 +1,4 @@
1
- import { ArvoContractRecord, ArvoEvent } from "arvo-core";
1
+ import { ArvoContractRecord, ArvoEvent } from 'arvo-core';
2
2
  /**
3
3
  * Abstract base class for Arvo event handlers.
4
4
  *
@@ -1,6 +1,6 @@
1
1
  import { ArvoContract, ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind, ResolveArvoContractRecord } from 'arvo-core';
2
2
  import { IArvoEventHandler } from './types';
3
- import { SpanKind } from '@opentelemetry/api';
3
+ import { SpanKind, Tracer } from '@opentelemetry/api';
4
4
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
5
5
  /**
6
6
  * Represents an event handler for Arvo contracts.
@@ -88,7 +88,10 @@ export default class ArvoEventHandler<TContract extends ArvoContract> extends Ab
88
88
  * - Propagates trace context to output events
89
89
  * - Handles error cases and sets appropriate span status
90
90
  */
91
- execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>): Promise<ArvoEvent[]>;
91
+ execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>, opentelemetry?: {
92
+ inheritFrom: 'event' | 'execution';
93
+ tracer?: Tracer;
94
+ }): Promise<ArvoEvent[]>;
92
95
  /**
93
96
  * Provides the schema for system error events.
94
97
  *
@@ -60,6 +60,7 @@ var api_1 = require("@opentelemetry/api");
60
60
  var utils_1 = require("../utils");
61
61
  var utils_2 = require("../OpenTelemetry/utils");
62
62
  var AbstractArvoEventHandler_1 = __importDefault(require("../AbstractArvoEventHandler"));
63
+ var OpenTelemetry_1 = require("../OpenTelemetry");
63
64
  /**
64
65
  * Represents an event handler for Arvo contracts.
65
66
  *
@@ -156,18 +157,35 @@ var ArvoEventHandler = /** @class */ (function (_super) {
156
157
  * - Propagates trace context to output events
157
158
  * - Handles error cases and sets appropriate span status
158
159
  */
159
- ArvoEventHandler.prototype.execute = function (event) {
160
- return __awaiter(this, void 0, void 0, function () {
161
- var span, eventFactory;
160
+ ArvoEventHandler.prototype.execute = function (event_1) {
161
+ return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
162
+ var spanName, spanKinds, spanOptions, span, eventFactory;
163
+ var _a;
162
164
  var _this = this;
163
- return __generator(this, function (_a) {
164
- switch (_a.label) {
165
+ var _b;
166
+ if (opentelemetry === void 0) { opentelemetry = {
167
+ inheritFrom: 'event',
168
+ tracer: OpenTelemetry_1.ArvoEventHandlerTracer,
169
+ }; }
170
+ return __generator(this, function (_c) {
171
+ switch (_c.label) {
165
172
  case 0:
166
- span = (0, utils_2.createSpanFromEvent)("ArvoEventHandler<".concat(this.contract.uri, ">.execute<").concat(event.type, ">"), event, {
173
+ spanName = "ArvoEventHandler<".concat(this.contract.uri, ">.execute<").concat(event.type, ">");
174
+ spanKinds = {
167
175
  kind: this.openTelemetrySpanKind,
168
176
  openInference: this.openInferenceSpanKind,
169
- arvoExecution: this.arvoExecutionSpanKind
170
- });
177
+ arvoExecution: this.arvoExecutionSpanKind,
178
+ };
179
+ spanOptions = {
180
+ kind: spanKinds.kind,
181
+ attributes: (_a = {},
182
+ _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = spanKinds.openInference,
183
+ _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = spanKinds.arvoExecution,
184
+ _a),
185
+ };
186
+ span = opentelemetry.inheritFrom === 'event'
187
+ ? (0, utils_2.createSpanFromEvent)(spanName, event, spanKinds, opentelemetry.tracer)
188
+ : ((_b = opentelemetry.tracer) !== null && _b !== void 0 ? _b : OpenTelemetry_1.ArvoEventHandlerTracer).startSpan(spanName, spanOptions);
171
189
  eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
172
190
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
173
191
  var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
@@ -241,7 +259,7 @@ var ArvoEventHandler = /** @class */ (function (_super) {
241
259
  }
242
260
  });
243
261
  }); })];
244
- case 1: return [2 /*return*/, _a.sent()];
262
+ case 1: return [2 /*return*/, _c.sent()];
245
263
  }
246
264
  });
247
265
  });
@@ -1,7 +1,7 @@
1
1
  import { ArvoContract, ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
2
2
  import ArvoEventHandler from '../ArvoEventHandler';
3
3
  import { IArvoEventRouter } from './types';
4
- import { SpanKind } from '@opentelemetry/api';
4
+ import { SpanKind, Tracer } from '@opentelemetry/api';
5
5
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
6
6
  /**
7
7
  * ArvoEventRouter class handles routing of ArvoEvents to appropriate event handlers.
@@ -89,7 +89,10 @@ export declare class ArvoEventRouter extends AbstractArvoEventHandler {
89
89
  * - Execution units are tracked for both successful executions and errors.
90
90
  * - The router's default execution units are used for error events.
91
91
  */
92
- execute(event: ArvoEvent): Promise<ArvoEvent[]>;
92
+ execute(event: ArvoEvent, opentelemetry?: {
93
+ inheritFrom: 'event' | 'execution';
94
+ tracer?: Tracer;
95
+ }): Promise<ArvoEvent[]>;
93
96
  /**
94
97
  * Provides the schema for system error events.
95
98
  *
@@ -61,6 +61,7 @@ var api_1 = require("@opentelemetry/api");
61
61
  var utils_2 = require("./utils");
62
62
  var utils_3 = require("../OpenTelemetry/utils");
63
63
  var AbstractArvoEventHandler_1 = __importDefault(require("../AbstractArvoEventHandler"));
64
+ var OpenTelemetry_1 = require("../OpenTelemetry");
64
65
  /**
65
66
  * ArvoEventRouter class handles routing of ArvoEvents to appropriate event handlers.
66
67
  */
@@ -163,19 +164,35 @@ var ArvoEventRouter = /** @class */ (function (_super) {
163
164
  * - Execution units are tracked for both successful executions and errors.
164
165
  * - The router's default execution units are used for error events.
165
166
  */
166
- ArvoEventRouter.prototype.execute = function (event) {
167
- return __awaiter(this, void 0, void 0, function () {
168
- var span;
169
- var _this = this;
167
+ ArvoEventRouter.prototype.execute = function (event_1) {
168
+ return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
169
+ var spanName, spanKinds, spanOptions, span;
170
170
  var _a;
171
- return __generator(this, function (_b) {
172
- switch (_b.label) {
171
+ var _this = this;
172
+ var _b, _c;
173
+ if (opentelemetry === void 0) { opentelemetry = {
174
+ inheritFrom: 'event',
175
+ tracer: OpenTelemetry_1.ArvoEventHandlerTracer,
176
+ }; }
177
+ return __generator(this, function (_d) {
178
+ switch (_d.label) {
173
179
  case 0:
174
- span = (0, utils_3.createSpanFromEvent)("ArvoEventRouter.source<".concat((_a = this._source) !== null && _a !== void 0 ? _a : 'arvo.event.router', ">.execute<").concat(event.type, ">"), event, {
180
+ spanName = "ArvoEventRouter.source<".concat((_b = this._source) !== null && _b !== void 0 ? _b : 'arvo.event.router', ">.execute<").concat(event.type, ">");
181
+ spanKinds = {
175
182
  kind: this.openTelemetrySpanKind,
176
183
  openInference: this.openInferenceSpanKind,
177
184
  arvoExecution: this.arvoExecutionSpanKind,
178
- });
185
+ };
186
+ spanOptions = {
187
+ kind: spanKinds.kind,
188
+ attributes: (_a = {},
189
+ _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = spanKinds.openInference,
190
+ _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = spanKinds.arvoExecution,
191
+ _a),
192
+ };
193
+ span = opentelemetry.inheritFrom === 'event'
194
+ ? (0, utils_3.createSpanFromEvent)(spanName, event, spanKinds, opentelemetry.tracer)
195
+ : ((_c = opentelemetry.tracer) !== null && _c !== void 0 ? _c : OpenTelemetry_1.ArvoEventHandlerTracer).startSpan(spanName, spanOptions);
179
196
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
180
197
  var otelSpanHeaders, newEvent, results, error_1;
181
198
  var _this = this;
@@ -188,13 +205,14 @@ var ArvoEventRouter = /** @class */ (function (_super) {
188
205
  case 1:
189
206
  _a.trys.push([1, 3, 4, 5]);
190
207
  span.setStatus({ code: api_1.SpanStatusCode.OK });
191
- if (!(0, utils_1.isNullOrUndefined)(this._source) && newEvent.to !== this._source) {
208
+ if (!(0, utils_1.isNullOrUndefined)(this._source) &&
209
+ newEvent.to !== this._source) {
192
210
  throw new Error((0, arvo_core_1.cleanString)("\n Invalid event. The 'event.to' is ".concat(newEvent.to, " while this handler \n listens to only 'event.to' equal to ").concat(this._source, ". If this is a mistake,\n please update the 'source' field of the handler\n ")));
193
211
  }
194
212
  if (!this.handlersMap[newEvent.type]) {
195
213
  throw new Error((0, arvo_core_1.cleanString)("\n Invalid event (type=".concat(newEvent.type, "). No valid handler \n <handler[*].contract.accepts.type> found in the router.\n ")));
196
214
  }
197
- return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent)];
215
+ return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent, { inheritFrom: 'execution', tracer: opentelemetry.tracer })];
198
216
  case 2:
199
217
  results = _a.sent();
200
218
  return [2 /*return*/, results.map(function (event) {
@@ -232,7 +250,7 @@ var ArvoEventRouter = /** @class */ (function (_super) {
232
250
  }
233
251
  });
234
252
  }); })];
235
- case 1: return [2 /*return*/, _b.sent()];
253
+ case 1: return [2 /*return*/, _d.sent()];
236
254
  }
237
255
  });
238
256
  });
@@ -1,4 +1,4 @@
1
- import { SpanKind } from '@opentelemetry/api';
1
+ import { SpanKind, Tracer } from '@opentelemetry/api';
2
2
  import { ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
3
3
  import { IMultiArvoEventHandler } from './types';
4
4
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
@@ -90,7 +90,10 @@ export default class MultiArvoEventHandler extends AbstractArvoEventHandler {
90
90
  * - If they don't match, an error is thrown with a descriptive message.
91
91
  * - This ensures that the handler only processes events intended for it.
92
92
  */
93
- execute(event: ArvoEvent): Promise<ArvoEvent[]>;
93
+ execute(event: ArvoEvent, opentelemetry?: {
94
+ inheritFrom: 'event' | 'execution';
95
+ tracer?: Tracer;
96
+ }): Promise<ArvoEvent[]>;
94
97
  /**
95
98
  * Provides the schema for system error events.
96
99
  *
@@ -59,6 +59,7 @@ var arvo_core_1 = require("arvo-core");
59
59
  var utils_1 = require("../utils");
60
60
  var utils_2 = require("../OpenTelemetry/utils");
61
61
  var AbstractArvoEventHandler_1 = __importDefault(require("../AbstractArvoEventHandler"));
62
+ var OpenTelemetry_1 = require("../OpenTelemetry");
62
63
  /**
63
64
  * Represents a Multi ArvoEvent handler that can process multiple event types.
64
65
  *
@@ -149,18 +150,35 @@ var MultiArvoEventHandler = /** @class */ (function (_super) {
149
150
  * - If they don't match, an error is thrown with a descriptive message.
150
151
  * - This ensures that the handler only processes events intended for it.
151
152
  */
152
- MultiArvoEventHandler.prototype.execute = function (event) {
153
- return __awaiter(this, void 0, void 0, function () {
154
- var span;
153
+ MultiArvoEventHandler.prototype.execute = function (event_1) {
154
+ return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
155
+ var spanName, spanKinds, spanOptions, span;
156
+ var _a;
155
157
  var _this = this;
156
- return __generator(this, function (_a) {
157
- switch (_a.label) {
158
+ var _b;
159
+ if (opentelemetry === void 0) { opentelemetry = {
160
+ inheritFrom: 'event',
161
+ tracer: OpenTelemetry_1.ArvoEventHandlerTracer,
162
+ }; }
163
+ return __generator(this, function (_c) {
164
+ switch (_c.label) {
158
165
  case 0:
159
- span = (0, utils_2.createSpanFromEvent)("MutliArvoEventHandler.source<".concat(this.source, ">.execute<").concat(event.type, ">"), event, {
166
+ spanName = "MutliArvoEventHandler.source<".concat(this.source, ">.execute<").concat(event.type, ">");
167
+ spanKinds = {
160
168
  kind: this.openTelemetrySpanKind,
161
169
  openInference: this.openInferenceSpanKind,
162
170
  arvoExecution: this.arvoExecutionSpanKind,
163
- });
171
+ };
172
+ spanOptions = {
173
+ kind: spanKinds.kind,
174
+ attributes: (_a = {},
175
+ _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = spanKinds.openInference,
176
+ _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = spanKinds.arvoExecution,
177
+ _a),
178
+ };
179
+ span = opentelemetry.inheritFrom === 'event'
180
+ ? (0, utils_2.createSpanFromEvent)(spanName, event, spanKinds, opentelemetry.tracer)
181
+ : ((_b = opentelemetry.tracer) !== null && _b !== void 0 ? _b : OpenTelemetry_1.ArvoEventHandlerTracer).startSpan(spanName, spanOptions);
164
182
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
165
183
  var otelSpanHeaders, _handlerOutput, outputs, error_1;
166
184
  return __generator(this, function (_a) {
@@ -216,7 +234,7 @@ var MultiArvoEventHandler = /** @class */ (function (_super) {
216
234
  }
217
235
  });
218
236
  }); })];
219
- case 1: return [2 /*return*/, _a.sent()];
237
+ case 1: return [2 /*return*/, _c.sent()];
220
238
  }
221
239
  });
222
240
  });
package/dist/index.d.ts CHANGED
@@ -11,4 +11,4 @@ import { ArvoEventRouter } from './ArvoEventRouter';
11
11
  import { createArvoEventRouter } from './ArvoEventRouter/helpers';
12
12
  import AbstractArvoEventHandler from './AbstractArvoEventHandler';
13
13
  import { createSpanFromEvent } from './OpenTelemetry/utils';
14
- export { ArvoEventHandler, createArvoEventHandler, IArvoEventHandler, ArvoEventHandlerFunctionOutput, ArvoEventHandlerFunctionInput, ArvoEventHandlerFunction, PartialExcept, MultiArvoEventHandler, MultiArvoEventHandlerFunctionInput, MultiArvoEventHandlerFunctionOutput, MultiArvoEventHandlerFunction, IMultiArvoEventHandler, createMultiArvoEventHandler, isNullOrUndefined, getValueOrDefault, coalesce, coalesceOrDefault, IArvoEventRouter, ArvoEventRouter, createArvoEventRouter, AbstractArvoEventHandler, createSpanFromEvent as createOtelSpanFromEvent };
14
+ export { ArvoEventHandler, createArvoEventHandler, IArvoEventHandler, ArvoEventHandlerFunctionOutput, ArvoEventHandlerFunctionInput, ArvoEventHandlerFunction, PartialExcept, MultiArvoEventHandler, MultiArvoEventHandlerFunctionInput, MultiArvoEventHandlerFunctionOutput, MultiArvoEventHandlerFunction, IMultiArvoEventHandler, createMultiArvoEventHandler, isNullOrUndefined, getValueOrDefault, coalesce, coalesceOrDefault, IArvoEventRouter, ArvoEventRouter, createArvoEventRouter, AbstractArvoEventHandler, createSpanFromEvent as createOtelSpanFromEvent, };
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { ArvoEvent, CreateArvoEvent, OpenTelemetryHeaders } from "arvo-core";
2
- import { ArvoEventHandlerFunctionOutput } from "./ArvoEventHandler/types";
3
- import { MultiArvoEventHandlerFunctionOutput } from "./MultiArvoEventHandler/types";
1
+ import { ArvoEvent, CreateArvoEvent, OpenTelemetryHeaders } from 'arvo-core';
2
+ import { ArvoEventHandlerFunctionOutput } from './ArvoEventHandler/types';
3
+ import { MultiArvoEventHandlerFunctionOutput } from './MultiArvoEventHandler/types';
4
4
  /**
5
5
  * Checks if the item is null or undefined.
6
6
  *
package/dist/utils.js CHANGED
@@ -140,7 +140,7 @@ var createHandlerErrorOutputEvent = function (error, otelSpanHeaders, type, sour
140
140
  errorMessage: error.message,
141
141
  errorStack: (_d = error.stack) !== null && _d !== void 0 ? _d : null,
142
142
  },
143
- accesscontrol: (_e = originalEvent.accesscontrol) !== null && _e !== void 0 ? _e : undefined
143
+ accesscontrol: (_e = originalEvent.accesscontrol) !== null && _e !== void 0 ? _e : undefined,
144
144
  });
145
145
  Object.entries(result.otelAttributes).forEach(function (_a) {
146
146
  var _b;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "This package contains class and function for event handlers in an Arvo Event Driven system",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "@opentelemetry/api": "^1.9.0",
52
- "arvo-core": "^1.1.15",
52
+ "arvo-core": "^1.1.16",
53
53
  "uuid": "^10.0.0",
54
54
  "zod": "^3.23.8"
55
55
  }