arvo-event-handler 1.0.2 → 1.0.4

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
@@ -15,7 +15,7 @@
15
15
  ## [1.0.0] - 2024-09-10
16
16
 
17
17
  - First version rrelease
18
+
18
19
  ## [1.0.2] - 2024-09-10
19
20
 
20
21
  - Added ArvoEventRouter as a mechanism to group ArvoEventHandlers
21
-
package/README.md CHANGED
@@ -55,7 +55,6 @@ There 2 main types of event handlers in Arvo event driven system
55
55
  - [ArvoEventRouter](src/ArvoEventRouter/README.md) is designed to route ArvoEvents to appropriate ArvoEventHandlers. It provides a centralized mechanism for managing and executing multiple event handlers based on event types.
56
56
  - [MultiArvoEventHandler](src/MultiArvoEventHandler/README.md) is a flexible and powerful event handling class designed to process multiple event types across different ArvoContracts. This handler offers greater versatility compared to the more specialized `ArvoEventHandler`, as it's not bound to a specific contract or event type.
57
57
 
58
-
59
58
  ## Getting Started
60
59
 
61
60
  To start using Arvo Event Handlers in your project:
@@ -75,7 +74,7 @@ import {
75
74
  createArvoEventHandler,
76
75
  createMultiArvoEventHandler,
77
76
  createArvoEventRouter,
78
- } from 'arvo-event-handler'
77
+ } from 'arvo-event-handler';
79
78
  ```
80
79
 
81
80
  - Begin defining your events, contracts, and handlers using the provided classes.
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,17 +35,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
36
  }
48
37
  };
49
- var __rest = (this && this.__rest) || function (s, e) {
50
- var t = {};
51
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
- t[p] = s[p];
53
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
- t[p[i]] = s[p[i]];
57
- }
58
- return t;
59
- };
60
38
  Object.defineProperty(exports, "__esModule", { value: true });
61
39
  var arvo_core_1 = require("arvo-core");
62
40
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
@@ -86,7 +64,7 @@ var ArvoEventHandler = /** @class */ (function () {
86
64
  * If no source is provided, it defaults to the contract's accepted event type.
87
65
  */
88
66
  function ArvoEventHandler(param) {
89
- var _a, _b, _c;
67
+ var _a, _b, _c, _d, _e, _f, _g;
90
68
  this.openInferenceSpanKind = arvo_core_1.OpenInferenceSpanKind.CHAIN;
91
69
  this.arvoExecutionSpanKind = arvo_core_1.ArvoExecutionSpanKind.EVENT_HANDLER;
92
70
  this.openTelemetrySpanKind = api_1.SpanKind.INTERNAL;
@@ -101,13 +79,13 @@ var ArvoEventHandler = /** @class */ (function () {
101
79
  throw new Error("The provided 'source' is not a valid string. Error: ".concat(error.message));
102
80
  }
103
81
  }
104
- this.source = param.source || this.contract.accepts.type;
82
+ this.source = (_a = param.source) !== null && _a !== void 0 ? _a : this.contract.accepts.type;
105
83
  this.arvoExecutionSpanKind =
106
- ((_a = param.spanKind) === null || _a === void 0 ? void 0 : _a.arvoExecution) || this.arvoExecutionSpanKind;
84
+ (_c = (_b = param.spanKind) === null || _b === void 0 ? void 0 : _b.arvoExecution) !== null && _c !== void 0 ? _c : this.arvoExecutionSpanKind;
107
85
  this.openInferenceSpanKind =
108
- ((_b = param.spanKind) === null || _b === void 0 ? void 0 : _b.openInference) || this.openInferenceSpanKind;
86
+ (_e = (_d = param.spanKind) === null || _d === void 0 ? void 0 : _d.openInference) !== null && _e !== void 0 ? _e : this.openInferenceSpanKind;
109
87
  this.openTelemetrySpanKind =
110
- ((_c = param.spanKind) === null || _c === void 0 ? void 0 : _c.openTelemetry) || this.openTelemetrySpanKind;
88
+ (_g = (_f = param.spanKind) === null || _f === void 0 ? void 0 : _f.openTelemetry) !== null && _g !== void 0 ? _g : this.openTelemetrySpanKind;
111
89
  }
112
90
  /**
113
91
  * Executes the event handler for a given event.
@@ -182,7 +160,6 @@ var ArvoEventHandler = /** @class */ (function () {
182
160
  eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
183
161
  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 () {
184
162
  var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
185
- var _this = this;
186
163
  return __generator(this, function (_a) {
187
164
  switch (_a.label) {
188
165
  case 0:
@@ -214,19 +191,12 @@ var ArvoEventHandler = /** @class */ (function () {
214
191
  else {
215
192
  outputs = [_handleOutput];
216
193
  }
217
- return [2 /*return*/, outputs.map(function (output, index) {
218
- var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
219
- var result = eventFactory.emits(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject,
220
- // The user should be able to override the `to` field
221
- // If that is not present then the 'redirectto' field
222
- // is referred to. Then, after all else, 'source' field
223
- // is used as a form of reply.
224
- to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, _this.executionunits) }), __extensions);
225
- Object.entries(result.otelAttributes).forEach(function (_a) {
226
- var key = _a[0], value = _a[1];
227
- return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
228
- });
229
- return result;
194
+ return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function () {
195
+ var args = [];
196
+ for (var _i = 0; _i < arguments.length; _i++) {
197
+ args[_i] = arguments[_i];
198
+ }
199
+ return eventFactory.emits.apply(eventFactory, args);
230
200
  })];
231
201
  case 3:
232
202
  error_1 = _a.sent();
@@ -43,7 +43,6 @@ export type ArvoEventHandlerFunction<TContract extends ArvoContract> = (params:
43
43
  export interface IArvoEventHandler<TContract extends ArvoContract> {
44
44
  /**
45
45
  * An override source for emitted events.
46
- * @deprecated This field is deprecated and should be used with caution.
47
46
  * @remarks
48
47
  * When provided, this value will be used as the source for emitted events
49
48
  * instead of the `contract.accepts.type`. Use this very carefully as it may
@@ -40,9 +40,9 @@ exports.ArvoEventRouter = void 0;
40
40
  var arvo_core_1 = require("arvo-core");
41
41
  var utils_1 = require("../utils");
42
42
  var api_1 = require("@opentelemetry/api");
43
- var OpenTelemetry_1 = require("../OpenTelemetry");
44
43
  var utils_2 = require("./utils");
45
44
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
45
+ var utils_3 = require("../OpenTelemetry/utils");
46
46
  /**
47
47
  * ArvoEventRouter class handles routing of ArvoEvents to appropriate event handlers.
48
48
  */
@@ -130,40 +130,29 @@ var ArvoEventRouter = /** @class */ (function () {
130
130
  */
131
131
  ArvoEventRouter.prototype.execute = function (event) {
132
132
  return __awaiter(this, void 0, void 0, function () {
133
- var spanName, spanOptions, span, inheritedContext;
134
- var _a;
133
+ var span;
135
134
  var _this = this;
136
- var _b;
137
- return __generator(this, function (_c) {
138
- switch (_c.label) {
135
+ var _a;
136
+ return __generator(this, function (_b) {
137
+ switch (_b.label) {
139
138
  case 0:
140
- spanName = "ArvoEventRouter.source<".concat((_b = this.source) !== null && _b !== void 0 ? _b : 'arvo.event.router', ">.execute<").concat(event.type, ">");
141
- spanOptions = {
139
+ span = (0, utils_3.createSpanFromEvent)("ArvoEventRouter.source<".concat((_a = this.source) !== null && _a !== void 0 ? _a : 'arvo.event.router', ">.execute<").concat(event.type, ">"), event, {
142
140
  kind: this.openTelemetrySpanKind,
143
- attributes: (_a = {},
144
- _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = this.openInferenceSpanKind,
145
- _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = this.arvoExecutionSpanKind,
146
- _a),
147
- };
148
- if (event.traceparent) {
149
- inheritedContext = (0, OpenTelemetry_1.extractContext)(event.traceparent, event.tracestate);
150
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
151
- }
152
- else {
153
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
154
- }
141
+ openInference: this.openInferenceSpanKind,
142
+ arvoExecution: this.arvoExecutionSpanKind,
143
+ });
155
144
  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 () {
156
145
  var otelSpanHeaders, newEvent, results, error_1, result;
157
146
  var _this = this;
158
- var _a, _b;
159
- return __generator(this, function (_c) {
160
- switch (_c.label) {
147
+ var _a, _b, _c, _d;
148
+ return __generator(this, function (_e) {
149
+ switch (_e.label) {
161
150
  case 0:
162
151
  otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
163
152
  newEvent = (0, utils_2.deleteOtelHeaders)(event);
164
- _c.label = 1;
153
+ _e.label = 1;
165
154
  case 1:
166
- _c.trys.push([1, 3, 4, 5]);
155
+ _e.trys.push([1, 3, 4, 5]);
167
156
  span.setStatus({ code: api_1.SpanStatusCode.OK });
168
157
  if (!(0, utils_1.isNullOrUndefined)(this.source) && newEvent.to !== this.source) {
169
158
  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 ")));
@@ -173,7 +162,7 @@ var ArvoEventRouter = /** @class */ (function () {
173
162
  }
174
163
  return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent)];
175
164
  case 2:
176
- results = _c.sent();
165
+ results = _e.sent();
177
166
  return [2 /*return*/, results.map(function (event) {
178
167
  var _a;
179
168
  return new arvo_core_1.ArvoEvent({
@@ -194,7 +183,7 @@ var ArvoEventRouter = /** @class */ (function () {
194
183
  }, event.data, event.cloudevent.extensions);
195
184
  })];
196
185
  case 3:
197
- error_1 = _c.sent();
186
+ error_1 = _e.sent();
198
187
  (0, arvo_core_1.exceptionToSpan)(error_1);
199
188
  span.setStatus({
200
189
  code: api_1.SpanStatusCode.ERROR,
@@ -206,18 +195,18 @@ var ArvoEventRouter = /** @class */ (function () {
206
195
  });
207
196
  result = (0, arvo_core_1.createArvoEvent)({
208
197
  type: "sys.arvo.event.router.error",
209
- source: this.source || "arvo.event.router",
198
+ source: (_a = this.source) !== null && _a !== void 0 ? _a : "arvo.event.router",
210
199
  subject: event.subject,
211
200
  // The system error must always got back to
212
201
  // the source
213
202
  to: event.source,
214
203
  executionunits: this.executionunits,
215
- traceparent: (_a = otelSpanHeaders.traceparent) !== null && _a !== void 0 ? _a : undefined,
216
- tracestate: (_b = otelSpanHeaders.tracestate) !== null && _b !== void 0 ? _b : undefined,
204
+ traceparent: (_b = otelSpanHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined,
205
+ tracestate: (_c = otelSpanHeaders.tracestate) !== null && _c !== void 0 ? _c : undefined,
217
206
  data: {
218
207
  errorName: error_1.name,
219
208
  errorMessage: error_1.message,
220
- errorStack: error_1.stack || null,
209
+ errorStack: (_d = error_1.stack) !== null && _d !== void 0 ? _d : null,
221
210
  },
222
211
  });
223
212
  Object.entries(result.otelAttributes).forEach(function (_a) {
@@ -232,7 +221,7 @@ var ArvoEventRouter = /** @class */ (function () {
232
221
  }
233
222
  });
234
223
  }); })];
235
- case 1: return [2 /*return*/, _c.sent()];
224
+ case 1: return [2 /*return*/, _b.sent()];
236
225
  }
237
226
  });
238
227
  });
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,23 +35,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
36
  }
48
37
  };
49
- var __rest = (this && this.__rest) || function (s, e) {
50
- var t = {};
51
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
- t[p] = s[p];
53
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
- t[p[i]] = s[p[i]];
57
- }
58
- return t;
59
- };
60
38
  Object.defineProperty(exports, "__esModule", { value: true });
61
39
  var api_1 = require("@opentelemetry/api");
62
40
  var arvo_core_1 = require("arvo-core");
63
41
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
64
- var OpenTelemetry_1 = require("../OpenTelemetry");
65
42
  var utils_1 = require("../utils");
43
+ var utils_2 = require("../OpenTelemetry/utils");
66
44
  /**
67
45
  * Represents a Multi ArvoEvent handler that can process multiple event types.
68
46
  *
@@ -155,37 +133,26 @@ var MultiArvoEventHandler = /** @class */ (function () {
155
133
  */
156
134
  MultiArvoEventHandler.prototype.execute = function (event) {
157
135
  return __awaiter(this, void 0, void 0, function () {
158
- var spanName, spanOptions, span, inheritedContext;
159
- var _a;
136
+ var span;
160
137
  var _this = this;
161
- return __generator(this, function (_b) {
162
- switch (_b.label) {
138
+ return __generator(this, function (_a) {
139
+ switch (_a.label) {
163
140
  case 0:
164
- spanName = "MutliArvoEventHandler.source<".concat(this.source, ">.execute<").concat(event.type, ">");
165
- spanOptions = {
141
+ span = (0, utils_2.createSpanFromEvent)("MutliArvoEventHandler.source<".concat(this.source, ">.execute<").concat(event.type, ">"), event, {
166
142
  kind: this.openTelemetrySpanKind,
167
- attributes: (_a = {},
168
- _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = this.openInferenceSpanKind,
169
- _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = this.arvoExecutionSpanKind,
170
- _a),
171
- };
172
- if (event.traceparent) {
173
- inheritedContext = (0, OpenTelemetry_1.extractContext)(event.traceparent, event.tracestate);
174
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
175
- }
176
- else {
177
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
178
- }
143
+ openInference: this.openInferenceSpanKind,
144
+ arvoExecution: this.arvoExecutionSpanKind,
145
+ });
179
146
  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
147
  var otelSpanHeaders, _handlerOutput, outputs, error_1, result;
181
- var _this = this;
182
- return __generator(this, function (_a) {
183
- switch (_a.label) {
148
+ var _a, _b, _c;
149
+ return __generator(this, function (_d) {
150
+ switch (_d.label) {
184
151
  case 0:
185
152
  otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
186
- _a.label = 1;
153
+ _d.label = 1;
187
154
  case 1:
188
- _a.trys.push([1, 3, 4, 5]);
155
+ _d.trys.push([1, 3, 4, 5]);
189
156
  span.setStatus({ code: api_1.SpanStatusCode.OK });
190
157
  Object.entries(event.otelAttributes).forEach(function (_a) {
191
158
  var key = _a[0], value = _a[1];
@@ -199,7 +166,7 @@ var MultiArvoEventHandler = /** @class */ (function () {
199
166
  source: this.source,
200
167
  })];
201
168
  case 2:
202
- _handlerOutput = _a.sent();
169
+ _handlerOutput = _d.sent();
203
170
  if (!_handlerOutput)
204
171
  return [2 /*return*/, []];
205
172
  outputs = [];
@@ -209,22 +176,15 @@ var MultiArvoEventHandler = /** @class */ (function () {
209
176
  else {
210
177
  outputs = [_handlerOutput];
211
178
  }
212
- return [2 /*return*/, outputs.map(function (output, index) {
213
- var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
214
- var result = (0, arvo_core_1.createArvoEvent)(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject,
215
- // The user should be able to override the `to` field
216
- // If that is not present then the 'redirectto' field
217
- // is referred to. Then, after all else, 'source' field
218
- // is used as a form of reply.
219
- to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, _this.executionunits) }), __extensions);
220
- Object.entries(result.otelAttributes).forEach(function (_a) {
221
- var key = _a[0], value = _a[1];
222
- return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
223
- });
224
- return result;
179
+ return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function () {
180
+ var args = [];
181
+ for (var _i = 0; _i < arguments.length; _i++) {
182
+ args[_i] = arguments[_i];
183
+ }
184
+ return arvo_core_1.createArvoEvent.apply(void 0, args);
225
185
  })];
226
186
  case 3:
227
- error_1 = _a.sent();
187
+ error_1 = _d.sent();
228
188
  (0, arvo_core_1.exceptionToSpan)(error_1);
229
189
  span.setStatus({
230
190
  code: api_1.SpanStatusCode.ERROR,
@@ -234,16 +194,14 @@ var MultiArvoEventHandler = /** @class */ (function () {
234
194
  type: "sys.".concat(this.source, ".error"),
235
195
  source: this.source,
236
196
  subject: event.subject,
237
- // The system error must always got back to
238
- // the source
239
197
  to: event.source,
240
198
  executionunits: this.executionunits,
241
- traceparent: otelSpanHeaders.traceparent || undefined,
242
- tracestate: otelSpanHeaders.tracestate || undefined,
199
+ traceparent: (_a = otelSpanHeaders.traceparent) !== null && _a !== void 0 ? _a : undefined,
200
+ tracestate: (_b = otelSpanHeaders.tracestate) !== null && _b !== void 0 ? _b : undefined,
243
201
  data: {
244
202
  errorName: error_1.name,
245
203
  errorMessage: error_1.message,
246
- errorStack: error_1.stack || null,
204
+ errorStack: (_c = error_1.stack) !== null && _c !== void 0 ? _c : null,
247
205
  },
248
206
  });
249
207
  Object.entries(result.otelAttributes).forEach(function (_a) {
@@ -258,7 +216,7 @@ var MultiArvoEventHandler = /** @class */ (function () {
258
216
  }
259
217
  });
260
218
  }); })];
261
- case 1: return [2 /*return*/, _b.sent()];
219
+ case 1: return [2 /*return*/, _a.sent()];
262
220
  }
263
221
  });
264
222
  });
@@ -12,7 +12,7 @@ exports.ArvoEventHandlerTracer = api_1.trace.getTracer(pkg.name, pkg.version);
12
12
  var extractContext = function (traceparent, tracestate) {
13
13
  var extractedContext = api_1.propagation.extract(api_1.context.active(), {
14
14
  traceparent: traceparent,
15
- tracestate: tracestate || undefined,
15
+ tracestate: tracestate !== null && tracestate !== void 0 ? tracestate : undefined,
16
16
  });
17
17
  return extractedContext;
18
18
  };
@@ -1,4 +1,11 @@
1
+ import { Span, SpanKind } from '@opentelemetry/api';
2
+ import { ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
1
3
  export declare function getPackageInfo(): {
2
4
  name: string;
3
5
  version: string;
4
6
  };
7
+ export declare const createSpanFromEvent: (spanName: string, event: ArvoEvent, spanKinds: {
8
+ kind: SpanKind;
9
+ openInference: OpenInferenceSpanKind;
10
+ arvoExecution: ArvoExecutionSpanKind;
11
+ }) => Span;
@@ -23,9 +23,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createSpanFromEvent = void 0;
26
27
  exports.getPackageInfo = getPackageInfo;
28
+ var arvo_core_1 = require("arvo-core");
27
29
  var fs = __importStar(require("fs"));
28
30
  var path = __importStar(require("path"));
31
+ var _1 = require(".");
29
32
  function getPackageInfo() {
30
33
  try {
31
34
  // Read the package.json file
@@ -42,3 +45,23 @@ function getPackageInfo() {
42
45
  return { name: 'Unknown', version: 'Unknown' };
43
46
  }
44
47
  }
48
+ var createSpanFromEvent = function (spanName, event, spanKinds) {
49
+ var _a;
50
+ var spanOptions = {
51
+ kind: spanKinds.kind,
52
+ attributes: (_a = {},
53
+ _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = spanKinds.openInference,
54
+ _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = spanKinds.arvoExecution,
55
+ _a),
56
+ };
57
+ var span;
58
+ if (event.traceparent) {
59
+ var inheritedContext = (0, _1.extractContext)(event.traceparent, event.tracestate);
60
+ span = _1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
61
+ }
62
+ else {
63
+ span = _1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
64
+ }
65
+ return span;
66
+ };
67
+ exports.createSpanFromEvent = createSpanFromEvent;
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { ArvoEvent, CreateArvoEvent, OpenTelemetryHeaders } from "arvo-core";
2
+ import { ArvoEventHandlerFunctionOutput } from "./ArvoEventHandler/types";
3
+ import { MultiArvoEventHandlerFunctionOutput } from "./MultiArvoEventHandler/types";
1
4
  /**
2
5
  * Checks if the item is null or undefined.
3
6
  *
@@ -41,3 +44,17 @@ export declare function coalesce<T>(...values: (T | null | undefined)[]): T | un
41
44
  * console.log(result); // Output: 'default'
42
45
  */
43
46
  export declare function coalesceOrDefault<T>(values: (T | null | undefined)[], _default: NonNullable<T>): NonNullable<T>;
47
+ /**
48
+ * Creates ArvoEvents from event handler output.
49
+ *
50
+ * @param events - An array of event handler function outputs.
51
+ * @param otelSpanHeaders - OpenTelemetry headers for tracing.
52
+ * @param source - The source of the event.
53
+ * @param originalEvent - The original ArvoEvent that triggered the handler.
54
+ * @param handlerExectionUnits - The number of execution units for the handler.
55
+ * @param factory - A function to create ArvoEvents.
56
+ * @returns An array of ArvoEvents created from the handler output.
57
+ */
58
+ export declare const eventHandlerOutputEventCreator: (events: Array<ArvoEventHandlerFunctionOutput<any> | MultiArvoEventHandlerFunctionOutput>, otelSpanHeaders: OpenTelemetryHeaders, source: string, originalEvent: ArvoEvent, handlerExectionUnits: number, factory: (param: CreateArvoEvent<any, any> & {
59
+ to: string;
60
+ }, extensions?: Record<string, string | number | boolean>) => ArvoEvent<any, any, any>) => ArvoEvent<any, any, any>[];
package/dist/utils.js CHANGED
@@ -1,9 +1,33 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
2
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.eventHandlerOutputEventCreator = void 0;
3
26
  exports.isNullOrUndefined = isNullOrUndefined;
4
27
  exports.getValueOrDefault = getValueOrDefault;
5
28
  exports.coalesce = coalesce;
6
29
  exports.coalesceOrDefault = coalesceOrDefault;
30
+ var api_1 = require("@opentelemetry/api");
7
31
  /**
8
32
  * Checks if the item is null or undefined.
9
33
  *
@@ -65,3 +89,30 @@ function coalesce() {
65
89
  function coalesceOrDefault(values, _default) {
66
90
  return getValueOrDefault(coalesce.apply(void 0, values), _default);
67
91
  }
92
+ /**
93
+ * Creates ArvoEvents from event handler output.
94
+ *
95
+ * @param events - An array of event handler function outputs.
96
+ * @param otelSpanHeaders - OpenTelemetry headers for tracing.
97
+ * @param source - The source of the event.
98
+ * @param originalEvent - The original ArvoEvent that triggered the handler.
99
+ * @param handlerExectionUnits - The number of execution units for the handler.
100
+ * @param factory - A function to create ArvoEvents.
101
+ * @returns An array of ArvoEvents created from the handler output.
102
+ */
103
+ var eventHandlerOutputEventCreator = function (events, otelSpanHeaders, source, originalEvent, handlerExectionUnits, factory) {
104
+ return events.map(function (item, index) {
105
+ var __extensions = item.__extensions, handlerResult = __rest(item, ["__extensions"]);
106
+ var result = factory(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: source, subject: originalEvent.subject,
107
+ // prioritise returned 'to', 'redirectto' and then
108
+ // 'source'
109
+ to: coalesceOrDefault([handlerResult.to, originalEvent.redirectto], originalEvent.source), executionunits: coalesce(handlerResult.executionunits, handlerExectionUnits) }), __extensions);
110
+ Object.entries(result.otelAttributes).forEach(function (_a) {
111
+ var _b;
112
+ var key = _a[0], value = _a[1];
113
+ return (_b = api_1.trace.getActiveSpan()) === null || _b === void 0 ? void 0 : _b.setAttribute("to_emit.".concat(index, ".").concat(key), value);
114
+ });
115
+ return result;
116
+ });
117
+ };
118
+ exports.eventHandlerOutputEventCreator = eventHandlerOutputEventCreator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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": {