arvo-event-handler 1.1.8 → 1.1.10

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.
@@ -1,4 +1,5 @@
1
1
  import { ArvoContractRecord, ArvoEvent } from 'arvo-core';
2
+ import { ExecutionOpenTelemetryConfiguration } from './types';
2
3
  /**
3
4
  * Abstract base class for Arvo event handlers.
4
5
  *
@@ -14,22 +15,44 @@ export default abstract class AbstractArvoEventHandler {
14
15
  * Executes the event handling logic for a given Arvo event.
15
16
  *
16
17
  * @abstract
17
- * @param {ArvoEvent} event - The Arvo event to be processed.
18
+ * @param {ArvoEvent} event - The Arvo event to be processed. This event should conform
19
+ * to the expected schema for the specific handler implementation.
20
+ * @param {ExecutionOpenTelemetryConfiguration} opentelemetry - Configuration for OpenTelemetry
21
+ * integration, including tracing options
22
+ * and context inheritance settings.
18
23
  * @returns {Promise<ArvoEvent[]>} A promise that resolves to an array of resulting Arvo events.
24
+ * These events represent the outcome of processing the input event.
19
25
  *
20
26
  * @description
21
- * This method should contain the core logic for processing an Arvo event.
22
- * Implementations should handle the event according to their specific requirements
23
- * and return any resulting events.
27
+ * This method defines the core event processing logic that each concrete handler must implement.
28
+ * It should handle the complete lifecycle of an event, including:
29
+ * - Validation of the input event
30
+ * - Processing of the event according to business rules
31
+ * - Generation of any resulting events
32
+ * - Error handling and reporting
33
+ * - OpenTelemetry integration for observability
24
34
  *
25
- * @throws {Error} Implementations may throw errors for invalid inputs or processing failures.
35
+ * @throws {Error}
36
+ * - When the input event fails validation
37
+ * - When processing encounters an unrecoverable error
38
+ * - When the handler is unable to properly execute the event
26
39
  *
27
40
  * @remarks
28
- * - The method is asynchronous to allow for potentially time-consuming operations.
29
- * - The returned array may be empty if no new events are generated as a result of processing.
30
- * - Implementations should ensure proper error handling and event validation.
41
+ * Implementation considerations:
42
+ * - Ensure proper error handling and event validation
43
+ * - Implement appropriate retry logic for transient failures
44
+ * - Use the provided OpenTelemetry configuration for tracing
45
+ * - Consider performance implications for long-running operations
46
+ * - Maintain idempotency where appropriate
47
+ * - Document any specific requirements for event schemas
48
+ *
49
+ * The method should handle observability concerns by:
50
+ * - Creating appropriate spans for tracing
51
+ * - Recording relevant attributes and events
52
+ * - Properly handling span lifecycle (creation and completion)
53
+ * - Propagating context appropriately
31
54
  */
32
- abstract execute(event: ArvoEvent): Promise<ArvoEvent[]>;
55
+ abstract execute(event: ArvoEvent, opentelemetry: ExecutionOpenTelemetryConfiguration): Promise<ArvoEvent[]>;
33
56
  /**
34
57
  * Provides the schema for system error events.
35
58
  *
@@ -0,0 +1,33 @@
1
+ import { Tracer } from '@opentelemetry/api';
2
+ /**
3
+ * Configuration options for OpenTelemetry integration in execution context.
4
+ *
5
+ * This type defines how tracing should be configured and inherited within
6
+ * the execution pipeline.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const config: ExecutionOpenTelemetryConfiguration = {
11
+ * inheritFrom: 'event',
12
+ * tracer: new OpenTelemetry.Tracer('service-name')
13
+ * };
14
+ * ```
15
+ */
16
+ export type ExecutionOpenTelemetryConfiguration = {
17
+ /**
18
+ * Specifies the context from which to inherit OpenTelemetry context.
19
+ *
20
+ * @property {('event' | 'execution')} inheritFrom
21
+ * - 'event': Inherits context from the event that triggered the execution
22
+ * - 'execution': Inherits context from the parent execution context
23
+ */
24
+ inheritFrom: 'event' | 'execution';
25
+ /**
26
+ * Optional OpenTelemetry tracer instance to use for creating spans.
27
+ * If not provided, a default tracer may be used depending on the implementation.
28
+ *
29
+ * @property {Tracer} [tracer]
30
+ * @see {@link https://open-telemetry.github.io/opentelemetry-js/classes/_opentelemetry_api.Tracer.html OpenTelemetry Tracer}
31
+ */
32
+ tracer?: Tracer;
33
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +1,8 @@
1
1
  import { ArvoContract, ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind, ResolveArvoContractRecord } from 'arvo-core';
2
2
  import { IArvoEventHandler } from './types';
3
- import { SpanKind, Tracer } from '@opentelemetry/api';
3
+ import { SpanKind } from '@opentelemetry/api';
4
4
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
5
+ import { ExecutionOpenTelemetryConfiguration } from '../AbstractArvoEventHandler/types';
5
6
  /**
6
7
  * Represents an event handler for Arvo contracts.
7
8
  *
@@ -45,6 +46,8 @@ export default class ArvoEventHandler<TContract extends ArvoContract> extends Ab
45
46
  * Executes the event handler for a given event.
46
47
  *
47
48
  * @param event - The event to handle.
49
+ * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
50
+ * and context inheritance settings.
48
51
  * @returns A promise that resolves to an array of resulting ArvoEvents.
49
52
  *
50
53
  * @remarks
@@ -88,10 +91,7 @@ export default class ArvoEventHandler<TContract extends ArvoContract> extends Ab
88
91
  * - Propagates trace context to output events
89
92
  * - Handles error cases and sets appropriate span status
90
93
  */
91
- execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>, opentelemetry?: {
92
- inheritFrom: 'event' | 'execution';
93
- tracer?: Tracer;
94
- }): Promise<ArvoEvent[]>;
94
+ execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>, opentelemetry?: ExecutionOpenTelemetryConfiguration): Promise<ArvoEvent[]>;
95
95
  /**
96
96
  * Provides the schema for system error events.
97
97
  *
@@ -114,6 +114,8 @@ var ArvoEventHandler = /** @class */ (function (_super) {
114
114
  * Executes the event handler for a given event.
115
115
  *
116
116
  * @param event - The event to handle.
117
+ * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
118
+ * and context inheritance settings.
117
119
  * @returns A promise that resolves to an array of resulting ArvoEvents.
118
120
  *
119
121
  * @remarks
@@ -189,14 +191,14 @@ var ArvoEventHandler = /** @class */ (function (_super) {
189
191
  eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
190
192
  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 () {
191
193
  var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
192
- var _a, _b, _c;
193
- return __generator(this, function (_d) {
194
- switch (_d.label) {
194
+ var _a, _b, _c, _d;
195
+ return __generator(this, function (_e) {
196
+ switch (_e.label) {
195
197
  case 0:
196
198
  otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
197
- _d.label = 1;
199
+ _e.label = 1;
198
200
  case 1:
199
- _d.trys.push([1, 3, 4, 5]);
201
+ _e.trys.push([1, 3, 4, 5]);
200
202
  span.setStatus({ code: api_1.SpanStatusCode.OK });
201
203
  Object.entries(event.otelAttributes).forEach(function (_a) {
202
204
  var key = _a[0], value = _a[1];
@@ -211,7 +213,7 @@ var ArvoEventHandler = /** @class */ (function (_super) {
211
213
  source: this.source,
212
214
  })];
213
215
  case 2:
214
- _handleOutput = _d.sent();
216
+ _handleOutput = _e.sent();
215
217
  if (!_handleOutput)
216
218
  return [2 /*return*/, []];
217
219
  outputs = [];
@@ -221,15 +223,14 @@ var ArvoEventHandler = /** @class */ (function (_super) {
221
223
  else {
222
224
  outputs = [_handleOutput];
223
225
  }
224
- return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function () {
225
- var args = [];
226
- for (var _i = 0; _i < arguments.length; _i++) {
227
- args[_i] = arguments[_i];
228
- }
229
- return eventFactory.emits.apply(eventFactory, args);
226
+ return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function (param, extension) {
227
+ var _a;
228
+ return eventFactory.emits(param, extension, {
229
+ tracer: (_a = opentelemetry.tracer) !== null && _a !== void 0 ? _a : OpenTelemetry_1.ArvoEventHandlerTracer,
230
+ });
230
231
  })];
231
232
  case 3:
232
- error_1 = _d.sent();
233
+ error_1 = _e.sent();
233
234
  (0, arvo_core_1.exceptionToSpan)(error_1);
234
235
  span.setStatus({
235
236
  code: api_1.SpanStatusCode.ERROR,
@@ -246,7 +247,7 @@ var ArvoEventHandler = /** @class */ (function (_super) {
246
247
  traceparent: (_a = otelSpanHeaders.traceparent) !== null && _a !== void 0 ? _a : undefined,
247
248
  tracestate: (_b = otelSpanHeaders.tracestate) !== null && _b !== void 0 ? _b : undefined,
248
249
  accesscontrol: (_c = event.accesscontrol) !== null && _c !== void 0 ? _c : undefined,
249
- }, {});
250
+ }, {}, { tracer: (_d = opentelemetry.tracer) !== null && _d !== void 0 ? _d : OpenTelemetry_1.ArvoEventHandlerTracer });
250
251
  Object.entries(result.otelAttributes).forEach(function (_a) {
251
252
  var key = _a[0], value = _a[1];
252
253
  return span.setAttribute("to_emit.0.".concat(key), value);
@@ -1,8 +1,9 @@
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, Tracer } from '@opentelemetry/api';
4
+ import { SpanKind } from '@opentelemetry/api';
5
5
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
6
+ import { ExecutionOpenTelemetryConfiguration } from '../AbstractArvoEventHandler/types';
6
7
  /**
7
8
  * ArvoEventRouter class handles routing of ArvoEvents to appropriate event handlers.
8
9
  */
@@ -48,6 +49,8 @@ export declare class ArvoEventRouter extends AbstractArvoEventHandler {
48
49
  * Executes the routing process for a given ArvoEvent.
49
50
  *
50
51
  * @param event - The ArvoEvent to be processed and routed.
52
+ * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
53
+ * and context inheritance settings.
51
54
  * @returns A Promise that resolves to an array of ArvoEvents.
52
55
  *
53
56
  * @remarks
@@ -89,10 +92,7 @@ export declare class ArvoEventRouter extends AbstractArvoEventHandler {
89
92
  * - Execution units are tracked for both successful executions and errors.
90
93
  * - The router's default execution units are used for error events.
91
94
  */
92
- execute(event: ArvoEvent, opentelemetry?: {
93
- inheritFrom: 'event' | 'execution';
94
- tracer?: Tracer;
95
- }): Promise<ArvoEvent[]>;
95
+ execute(event: ArvoEvent, opentelemetry?: ExecutionOpenTelemetryConfiguration): Promise<ArvoEvent[]>;
96
96
  /**
97
97
  * Provides the schema for system error events.
98
98
  *
@@ -123,6 +123,8 @@ var ArvoEventRouter = /** @class */ (function (_super) {
123
123
  * Executes the routing process for a given ArvoEvent.
124
124
  *
125
125
  * @param event - The ArvoEvent to be processed and routed.
126
+ * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
127
+ * and context inheritance settings.
126
128
  * @returns A Promise that resolves to an array of ArvoEvents.
127
129
  *
128
130
  * @remarks
@@ -236,12 +238,11 @@ var ArvoEventRouter = /** @class */ (function (_super) {
236
238
  })];
237
239
  case 3:
238
240
  error_1 = _a.sent();
239
- return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, "sys.".concat(this.source, ".error"), this.source, event, this.executionunits, function () {
240
- var args = [];
241
- for (var _i = 0; _i < arguments.length; _i++) {
242
- args[_i] = arguments[_i];
243
- }
244
- return arvo_core_1.createArvoEvent.apply(void 0, args);
241
+ return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, "sys.".concat(this.source, ".error"), this.source, event, this.executionunits, function (param, extension) {
242
+ var _a;
243
+ return (0, arvo_core_1.createArvoEvent)(param, extension, {
244
+ tracer: (_a = opentelemetry.tracer) !== null && _a !== void 0 ? _a : OpenTelemetry_1.ArvoEventHandlerTracer,
245
+ });
245
246
  })];
246
247
  case 4:
247
248
  span.end();
@@ -1,7 +1,8 @@
1
- import { SpanKind, Tracer } from '@opentelemetry/api';
1
+ import { SpanKind } from '@opentelemetry/api';
2
2
  import { ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
3
3
  import { IMultiArvoEventHandler } from './types';
4
4
  import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
5
+ import { ExecutionOpenTelemetryConfiguration } from '../AbstractArvoEventHandler/types';
5
6
  /**
6
7
  * Represents a Multi ArvoEvent handler that can process multiple event types.
7
8
  *
@@ -41,6 +42,8 @@ export default class MultiArvoEventHandler extends AbstractArvoEventHandler {
41
42
  * Executes the event handler for a given event.
42
43
  *
43
44
  * @param event - The event to handle.
45
+ * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
46
+ * and context inheritance settings.
44
47
  * @returns A promise that resolves to an array of resulting ArvoEvents.
45
48
  *
46
49
  * @remarks
@@ -90,10 +93,7 @@ export default class MultiArvoEventHandler extends AbstractArvoEventHandler {
90
93
  * - If they don't match, an error is thrown with a descriptive message.
91
94
  * - This ensures that the handler only processes events intended for it.
92
95
  */
93
- execute(event: ArvoEvent, opentelemetry?: {
94
- inheritFrom: 'event' | 'execution';
95
- tracer?: Tracer;
96
- }): Promise<ArvoEvent[]>;
96
+ execute(event: ArvoEvent, opentelemetry?: ExecutionOpenTelemetryConfiguration): Promise<ArvoEvent[]>;
97
97
  /**
98
98
  * Provides the schema for system error events.
99
99
  *
@@ -101,6 +101,8 @@ var MultiArvoEventHandler = /** @class */ (function (_super) {
101
101
  * Executes the event handler for a given event.
102
102
  *
103
103
  * @param event - The event to handle.
104
+ * @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
105
+ * and context inheritance settings.
104
106
  * @returns A promise that resolves to an array of resulting ArvoEvents.
105
107
  *
106
108
  * @remarks
@@ -211,21 +213,19 @@ var MultiArvoEventHandler = /** @class */ (function (_super) {
211
213
  else {
212
214
  outputs = [_handlerOutput];
213
215
  }
214
- return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function () {
215
- var args = [];
216
- for (var _i = 0; _i < arguments.length; _i++) {
217
- args[_i] = arguments[_i];
218
- }
219
- return arvo_core_1.createArvoEvent.apply(void 0, args);
216
+ return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function (param, extension) {
217
+ var _a;
218
+ return (0, arvo_core_1.createArvoEvent)(param, extension, {
219
+ tracer: (_a = opentelemetry.tracer) !== null && _a !== void 0 ? _a : OpenTelemetry_1.ArvoEventHandlerTracer,
220
+ });
220
221
  })];
221
222
  case 3:
222
223
  error_1 = _a.sent();
223
- return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, "sys.".concat(this.source, ".error"), this.source, event, this.executionunits, function () {
224
- var args = [];
225
- for (var _i = 0; _i < arguments.length; _i++) {
226
- args[_i] = arguments[_i];
227
- }
228
- return arvo_core_1.createArvoEvent.apply(void 0, args);
224
+ return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, "sys.".concat(this.source, ".error"), this.source, event, this.executionunits, function (param, extension) {
225
+ var _a;
226
+ return (0, arvo_core_1.createArvoEvent)(param, extension, {
227
+ tracer: (_a = opentelemetry.tracer) !== null && _a !== void 0 ? _a : OpenTelemetry_1.ArvoEventHandlerTracer,
228
+ });
229
229
  })];
230
230
  case 4:
231
231
  span.end();
package/dist/index.d.ts CHANGED
@@ -11,4 +11,5 @@ 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
+ import { ExecutionOpenTelemetryConfiguration } from './AbstractArvoEventHandler/types';
15
+ 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, ExecutionOpenTelemetryConfiguration, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
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.16",
52
+ "arvo-core": "^1.1.17",
53
53
  "uuid": "^10.0.0",
54
54
  "zod": "^3.23.8"
55
55
  }