arvo-event-handler 1.0.4 → 1.0.6

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.
@@ -38,9 +38,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var arvo_core_1 = require("arvo-core");
40
40
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
41
- var OpenTelemetry_1 = require("../OpenTelemetry");
42
41
  var api_1 = require("@opentelemetry/api");
43
42
  var utils_1 = require("../utils");
43
+ var utils_2 = require("../OpenTelemetry/utils");
44
44
  /**
45
45
  * Represents an event handler for Arvo contracts.
46
46
  *
@@ -136,27 +136,16 @@ var ArvoEventHandler = /** @class */ (function () {
136
136
  */
137
137
  ArvoEventHandler.prototype.execute = function (event) {
138
138
  return __awaiter(this, void 0, void 0, function () {
139
- var spanName, spanOptions, span, inheritedContext, eventFactory;
140
- var _a;
139
+ var span, eventFactory;
141
140
  var _this = this;
142
- return __generator(this, function (_b) {
143
- switch (_b.label) {
141
+ return __generator(this, function (_a) {
142
+ switch (_a.label) {
144
143
  case 0:
145
- spanName = "ArvoEventHandler<".concat(this.contract.uri, ">.execute<").concat(event.type, ">");
146
- spanOptions = {
144
+ span = (0, utils_2.createSpanFromEvent)("ArvoEventHandler<".concat(this.contract.uri, ">.execute<").concat(event.type, ">"), event, {
147
145
  kind: this.openTelemetrySpanKind,
148
- attributes: (_a = {},
149
- _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = this.openInferenceSpanKind,
150
- _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = this.arvoExecutionSpanKind,
151
- _a),
152
- };
153
- if (event.traceparent) {
154
- inheritedContext = (0, OpenTelemetry_1.extractContext)(event.traceparent, event.tracestate);
155
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
156
- }
157
- else {
158
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
159
- }
146
+ openInference: this.openInferenceSpanKind,
147
+ arvoExecution: this.arvoExecutionSpanKind
148
+ });
160
149
  eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
161
150
  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 () {
162
151
  var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
@@ -228,7 +217,7 @@ var ArvoEventHandler = /** @class */ (function () {
228
217
  }
229
218
  });
230
219
  }); })];
231
- case 1: return [2 /*return*/, _b.sent()];
220
+ case 1: return [2 /*return*/, _a.sent()];
232
221
  }
233
222
  });
234
223
  });
@@ -142,17 +142,17 @@ var ArvoEventRouter = /** @class */ (function () {
142
142
  arvoExecution: this.arvoExecutionSpanKind,
143
143
  });
144
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 () {
145
- var otelSpanHeaders, newEvent, results, error_1, result;
145
+ var otelSpanHeaders, newEvent, results, error_1;
146
146
  var _this = this;
147
- var _a, _b, _c, _d;
148
- return __generator(this, function (_e) {
149
- switch (_e.label) {
147
+ var _a;
148
+ return __generator(this, function (_b) {
149
+ switch (_b.label) {
150
150
  case 0:
151
151
  otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
152
152
  newEvent = (0, utils_2.deleteOtelHeaders)(event);
153
- _e.label = 1;
153
+ _b.label = 1;
154
154
  case 1:
155
- _e.trys.push([1, 3, 4, 5]);
155
+ _b.trys.push([1, 3, 4, 5]);
156
156
  span.setStatus({ code: api_1.SpanStatusCode.OK });
157
157
  if (!(0, utils_1.isNullOrUndefined)(this.source) && newEvent.to !== this.source) {
158
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 ")));
@@ -162,7 +162,7 @@ var ArvoEventRouter = /** @class */ (function () {
162
162
  }
163
163
  return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent)];
164
164
  case 2:
165
- results = _e.sent();
165
+ results = _b.sent();
166
166
  return [2 /*return*/, results.map(function (event) {
167
167
  var _a;
168
168
  return new arvo_core_1.ArvoEvent({
@@ -183,37 +183,14 @@ var ArvoEventRouter = /** @class */ (function () {
183
183
  }, event.data, event.cloudevent.extensions);
184
184
  })];
185
185
  case 3:
186
- error_1 = _e.sent();
187
- (0, arvo_core_1.exceptionToSpan)(error_1);
188
- span.setStatus({
189
- code: api_1.SpanStatusCode.ERROR,
190
- message: error_1.message,
191
- });
192
- Object.entries(event.otelAttributes).forEach(function (_a) {
193
- var key = _a[0], value = _a[1];
194
- return span.setAttribute("to_process.0.".concat(key), value);
195
- });
196
- result = (0, arvo_core_1.createArvoEvent)({
197
- type: "sys.arvo.event.router.error",
198
- source: (_a = this.source) !== null && _a !== void 0 ? _a : "arvo.event.router",
199
- subject: event.subject,
200
- // The system error must always got back to
201
- // the source
202
- to: event.source,
203
- executionunits: this.executionunits,
204
- traceparent: (_b = otelSpanHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined,
205
- tracestate: (_c = otelSpanHeaders.tracestate) !== null && _c !== void 0 ? _c : undefined,
206
- data: {
207
- errorName: error_1.name,
208
- errorMessage: error_1.message,
209
- errorStack: (_d = error_1.stack) !== null && _d !== void 0 ? _d : null,
210
- },
211
- });
212
- Object.entries(result.otelAttributes).forEach(function (_a) {
213
- var key = _a[0], value = _a[1];
214
- return span.setAttribute("to_emit.0.".concat(key), value);
215
- });
216
- return [2 /*return*/, [result]];
186
+ error_1 = _b.sent();
187
+ return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, "sys.arvo.event.router.error", (_a = this.source) !== null && _a !== void 0 ? _a : "arvo.event.router", event, this.executionunits, function () {
188
+ var args = [];
189
+ for (var _i = 0; _i < arguments.length; _i++) {
190
+ args[_i] = arguments[_i];
191
+ }
192
+ return arvo_core_1.createArvoEvent.apply(void 0, args);
193
+ })];
217
194
  case 4:
218
195
  span.end();
219
196
  return [7 /*endfinally*/];
@@ -144,15 +144,14 @@ var MultiArvoEventHandler = /** @class */ (function () {
144
144
  arvoExecution: this.arvoExecutionSpanKind,
145
145
  });
146
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 () {
147
- var otelSpanHeaders, _handlerOutput, outputs, error_1, result;
148
- var _a, _b, _c;
149
- return __generator(this, function (_d) {
150
- switch (_d.label) {
147
+ var otelSpanHeaders, _handlerOutput, outputs, error_1;
148
+ return __generator(this, function (_a) {
149
+ switch (_a.label) {
151
150
  case 0:
152
151
  otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
153
- _d.label = 1;
152
+ _a.label = 1;
154
153
  case 1:
155
- _d.trys.push([1, 3, 4, 5]);
154
+ _a.trys.push([1, 3, 4, 5]);
156
155
  span.setStatus({ code: api_1.SpanStatusCode.OK });
157
156
  Object.entries(event.otelAttributes).forEach(function (_a) {
158
157
  var key = _a[0], value = _a[1];
@@ -166,7 +165,7 @@ var MultiArvoEventHandler = /** @class */ (function () {
166
165
  source: this.source,
167
166
  })];
168
167
  case 2:
169
- _handlerOutput = _d.sent();
168
+ _handlerOutput = _a.sent();
170
169
  if (!_handlerOutput)
171
170
  return [2 /*return*/, []];
172
171
  outputs = [];
@@ -184,31 +183,14 @@ var MultiArvoEventHandler = /** @class */ (function () {
184
183
  return arvo_core_1.createArvoEvent.apply(void 0, args);
185
184
  })];
186
185
  case 3:
187
- error_1 = _d.sent();
188
- (0, arvo_core_1.exceptionToSpan)(error_1);
189
- span.setStatus({
190
- code: api_1.SpanStatusCode.ERROR,
191
- message: error_1.message,
192
- });
193
- result = (0, arvo_core_1.createArvoEvent)({
194
- type: "sys.".concat(this.source, ".error"),
195
- source: this.source,
196
- subject: event.subject,
197
- to: event.source,
198
- executionunits: this.executionunits,
199
- traceparent: (_a = otelSpanHeaders.traceparent) !== null && _a !== void 0 ? _a : undefined,
200
- tracestate: (_b = otelSpanHeaders.tracestate) !== null && _b !== void 0 ? _b : undefined,
201
- data: {
202
- errorName: error_1.name,
203
- errorMessage: error_1.message,
204
- errorStack: (_c = error_1.stack) !== null && _c !== void 0 ? _c : null,
205
- },
206
- });
207
- Object.entries(result.otelAttributes).forEach(function (_a) {
208
- var key = _a[0], value = _a[1];
209
- return span.setAttribute("to_emit.0.".concat(key), value);
210
- });
211
- return [2 /*return*/, [result]];
186
+ error_1 = _a.sent();
187
+ return [2 /*return*/, (0, utils_1.createHandlerErrorOutputEvent)(error_1, otelSpanHeaders, "sys.".concat(this.source, ".error"), this.source, event, this.executionunits, function () {
188
+ var args = [];
189
+ for (var _i = 0; _i < arguments.length; _i++) {
190
+ args[_i] = arguments[_i];
191
+ }
192
+ return arvo_core_1.createArvoEvent.apply(void 0, args);
193
+ })];
212
194
  case 4:
213
195
  span.end();
214
196
  return [7 /*endfinally*/];
package/dist/utils.d.ts CHANGED
@@ -58,3 +58,10 @@ export declare function coalesceOrDefault<T>(values: (T | null | undefined)[], _
58
58
  export declare const eventHandlerOutputEventCreator: (events: Array<ArvoEventHandlerFunctionOutput<any> | MultiArvoEventHandlerFunctionOutput>, otelSpanHeaders: OpenTelemetryHeaders, source: string, originalEvent: ArvoEvent, handlerExectionUnits: number, factory: (param: CreateArvoEvent<any, any> & {
59
59
  to: string;
60
60
  }, extensions?: Record<string, string | number | boolean>) => ArvoEvent<any, any, any>) => ArvoEvent<any, any, any>[];
61
+ export declare const createHandlerErrorOutputEvent: (error: Error, otelSpanHeaders: OpenTelemetryHeaders, type: string, source: string, originalEvent: ArvoEvent, handlerExectionUnits: number, factory: (param: CreateArvoEvent<any, any> & {
62
+ to: string;
63
+ }, extensions?: Record<string, string | number | boolean>) => ArvoEvent<any, any, any>) => ArvoEvent<{
64
+ errorName: string;
65
+ errorMessage: string;
66
+ errorStack: string | null;
67
+ }, Record<string, string | number | boolean | null>, string>[];
package/dist/utils.js CHANGED
@@ -22,11 +22,12 @@ var __rest = (this && this.__rest) || function (s, e) {
22
22
  return t;
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.eventHandlerOutputEventCreator = void 0;
25
+ exports.createHandlerErrorOutputEvent = exports.eventHandlerOutputEventCreator = void 0;
26
26
  exports.isNullOrUndefined = isNullOrUndefined;
27
27
  exports.getValueOrDefault = getValueOrDefault;
28
28
  exports.coalesce = coalesce;
29
29
  exports.coalesceOrDefault = coalesceOrDefault;
30
+ var arvo_core_1 = require("arvo-core");
30
31
  var api_1 = require("@opentelemetry/api");
31
32
  /**
32
33
  * Checks if the item is null or undefined.
@@ -116,3 +117,32 @@ var eventHandlerOutputEventCreator = function (events, otelSpanHeaders, source,
116
117
  });
117
118
  };
118
119
  exports.eventHandlerOutputEventCreator = eventHandlerOutputEventCreator;
120
+ var createHandlerErrorOutputEvent = function (error, otelSpanHeaders, type, source, originalEvent, handlerExectionUnits, factory) {
121
+ var _a, _b, _c, _d;
122
+ (0, arvo_core_1.exceptionToSpan)(error);
123
+ (_a = api_1.trace.getActiveSpan()) === null || _a === void 0 ? void 0 : _a.setStatus({
124
+ code: api_1.SpanStatusCode.ERROR,
125
+ message: error.message,
126
+ });
127
+ var result = (0, arvo_core_1.createArvoEvent)({
128
+ type: type,
129
+ source: source,
130
+ subject: originalEvent.subject,
131
+ to: originalEvent.source,
132
+ executionunits: handlerExectionUnits,
133
+ traceparent: (_b = otelSpanHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined,
134
+ tracestate: (_c = otelSpanHeaders.tracestate) !== null && _c !== void 0 ? _c : undefined,
135
+ data: {
136
+ errorName: error.name,
137
+ errorMessage: error.message,
138
+ errorStack: (_d = error.stack) !== null && _d !== void 0 ? _d : null,
139
+ },
140
+ });
141
+ Object.entries(result.otelAttributes).forEach(function (_a) {
142
+ var _b;
143
+ var key = _a[0], value = _a[1];
144
+ return (_b = api_1.trace.getActiveSpan()) === null || _b === void 0 ? void 0 : _b.setAttribute("to_emit.0.".concat(key), value);
145
+ });
146
+ return [result];
147
+ };
148
+ exports.createHandlerErrorOutputEvent = createHandlerErrorOutputEvent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
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": {