arvo-event-handler 1.1.25 → 2.0.1
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 +4 -0
- package/dist/ArvoEventHandler/helpers.d.ts +4 -2
- package/dist/ArvoEventHandler/helpers.js +4 -2
- package/dist/ArvoEventHandler/index.d.ts +2 -3
- package/dist/ArvoEventHandler/index.js +39 -22
- package/dist/ArvoEventHandler/types.d.ts +7 -5
- package/dist/ArvoEventRouter/index.d.ts +1 -3
- package/dist/ArvoEventRouter/index.js +13 -11
- package/dist/ArvoEventRouter/types.d.ts +1 -3
- package/dist/MultiArvoEventHandler/index.js +3 -3
- package/dist/OpenTelemetry/index.js +1 -3
- package/dist/OpenTelemetry/utils.d.ts +0 -4
- package/dist/OpenTelemetry/utils.js +4 -46
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -19,8 +19,10 @@ import ArvoEventHandler from '.';
|
|
|
19
19
|
* const myHandler = createArvoEventHandler({
|
|
20
20
|
* contract: myContract,
|
|
21
21
|
* executionunits: 100,
|
|
22
|
-
* handler:
|
|
23
|
-
*
|
|
22
|
+
* handler: {
|
|
23
|
+
* '0.0.1': async ({ event }) => {
|
|
24
|
+
* // Handler implementation
|
|
25
|
+
* }
|
|
24
26
|
* }
|
|
25
27
|
* });
|
|
26
28
|
* ```
|
|
@@ -23,8 +23,10 @@ var _1 = __importDefault(require("."));
|
|
|
23
23
|
* const myHandler = createArvoEventHandler({
|
|
24
24
|
* contract: myContract,
|
|
25
25
|
* executionunits: 100,
|
|
26
|
-
* handler:
|
|
27
|
-
*
|
|
26
|
+
* handler: {
|
|
27
|
+
* '0.0.1': async ({ event }) => {
|
|
28
|
+
* // Handler implementation
|
|
29
|
+
* }
|
|
28
30
|
* }
|
|
29
31
|
* });
|
|
30
32
|
* ```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ArvoContract, ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind
|
|
1
|
+
import { ArvoContract, ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
|
|
2
2
|
import { IArvoEventHandler } from './types';
|
|
3
3
|
import { SpanKind } from '@opentelemetry/api';
|
|
4
4
|
import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
|
|
@@ -50,7 +50,6 @@ export default class ArvoEventHandler<TContract extends ArvoContract> extends Ab
|
|
|
50
50
|
* and context inheritance settings. Default is inherit from event and internal tracer
|
|
51
51
|
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
52
52
|
*
|
|
53
|
-
* @remarks
|
|
54
53
|
* This method performs the following steps:
|
|
55
54
|
* 1. Creates an OpenTelemetry span for the execution.
|
|
56
55
|
* 2. Validates the input event against the contract.
|
|
@@ -91,7 +90,7 @@ export default class ArvoEventHandler<TContract extends ArvoContract> extends Ab
|
|
|
91
90
|
* - Propagates trace context to output events
|
|
92
91
|
* - Handles error cases and sets appropriate span status
|
|
93
92
|
*/
|
|
94
|
-
execute(event: ArvoEvent
|
|
93
|
+
execute(event: ArvoEvent, opentelemetry?: OpenTelemetryConfig): Promise<ArvoEvent[]>;
|
|
95
94
|
/**
|
|
96
95
|
* Provides the schema for system error events.
|
|
97
96
|
*
|
|
@@ -24,8 +24,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
24
24
|
});
|
|
25
25
|
};
|
|
26
26
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
-
return g =
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
28
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
29
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
30
|
function step(op) {
|
|
31
31
|
if (f) throw new TypeError("Generator is already executing.");
|
|
@@ -101,13 +101,19 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
101
101
|
throw new Error("The provided 'source' is not a valid string. Error: ".concat(error.message));
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
_this.source = (_a = param.source) !== null && _a !== void 0 ? _a : _this.contract.
|
|
104
|
+
_this.source = (_a = param.source) !== null && _a !== void 0 ? _a : _this.contract.type;
|
|
105
105
|
_this.arvoExecutionSpanKind =
|
|
106
106
|
(_c = (_b = param.spanKind) === null || _b === void 0 ? void 0 : _b.arvoExecution) !== null && _c !== void 0 ? _c : _this.arvoExecutionSpanKind;
|
|
107
107
|
_this.openInferenceSpanKind =
|
|
108
108
|
(_e = (_d = param.spanKind) === null || _d === void 0 ? void 0 : _d.openInference) !== null && _e !== void 0 ? _e : _this.openInferenceSpanKind;
|
|
109
109
|
_this.openTelemetrySpanKind =
|
|
110
110
|
(_g = (_f = param.spanKind) === null || _f === void 0 ? void 0 : _f.openTelemetry) !== null && _g !== void 0 ? _g : _this.openTelemetrySpanKind;
|
|
111
|
+
for (var _i = 0, _h = Object.keys(_this.contract.versions); _i < _h.length; _i++) {
|
|
112
|
+
var contractVersions = _h[_i];
|
|
113
|
+
if (!_this._handler[contractVersions]) {
|
|
114
|
+
throw new Error("The event handler for contract (uri=".concat(_this.contract.uri, ") must contain a handler for version ").concat(contractVersions, "."));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
111
117
|
return _this;
|
|
112
118
|
}
|
|
113
119
|
/**
|
|
@@ -118,7 +124,6 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
118
124
|
* and context inheritance settings. Default is inherit from event and internal tracer
|
|
119
125
|
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
120
126
|
*
|
|
121
|
-
* @remarks
|
|
122
127
|
* This method performs the following steps:
|
|
123
128
|
* 1. Creates an OpenTelemetry span for the execution.
|
|
124
129
|
* 2. Validates the input event against the contract.
|
|
@@ -161,7 +166,7 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
161
166
|
*/
|
|
162
167
|
ArvoEventHandler.prototype.execute = function (event, opentelemetry) {
|
|
163
168
|
return __awaiter(this, void 0, void 0, function () {
|
|
164
|
-
var span
|
|
169
|
+
var span;
|
|
165
170
|
var _this = this;
|
|
166
171
|
return __generator(this, function (_a) {
|
|
167
172
|
switch (_a.label) {
|
|
@@ -174,34 +179,44 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
174
179
|
arvoExecution: this.arvoExecutionSpanKind,
|
|
175
180
|
},
|
|
176
181
|
event: event,
|
|
177
|
-
opentelemetryConfig: opentelemetry
|
|
182
|
+
opentelemetryConfig: opentelemetry,
|
|
178
183
|
});
|
|
179
|
-
eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
|
|
180
184
|
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 () {
|
|
181
|
-
var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
|
|
182
|
-
var _a, _b, _c, _d;
|
|
183
|
-
return __generator(this, function (
|
|
184
|
-
switch (
|
|
185
|
+
var otelSpanHeaders, parsedDataSchema, handlerContract, inputEventValidation, _handleOutput, outputs, eventFactory_1, error_1, eventFactory, result;
|
|
186
|
+
var _a, _b, _c, _d, _e;
|
|
187
|
+
return __generator(this, function (_f) {
|
|
188
|
+
switch (_f.label) {
|
|
185
189
|
case 0:
|
|
186
190
|
otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
|
|
187
|
-
|
|
191
|
+
_f.label = 1;
|
|
188
192
|
case 1:
|
|
189
|
-
|
|
193
|
+
_f.trys.push([1, 3, 4, 5]);
|
|
190
194
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
195
|
+
if (this.contract.type !== event.type) {
|
|
196
|
+
throw new Error("Invalid event type='".concat(event.type, "' is provide to handler for type='").concat(this.contract.type, "'"));
|
|
197
|
+
}
|
|
198
|
+
parsedDataSchema = (0, arvo_core_1.parseEventDataSchema)(event);
|
|
199
|
+
if (!(parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version)) {
|
|
200
|
+
(0, arvo_core_1.logToSpan)({
|
|
201
|
+
level: 'WARNING',
|
|
202
|
+
message: "Unable to resolve the event version from dataschema \"".concat(event.dataschema, "\". Defaulting to the latest version."),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
handlerContract = this.contract.version((_a = parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version) !== null && _a !== void 0 ? _a : 'latest');
|
|
191
206
|
Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
192
207
|
var key = _a[0], value = _a[1];
|
|
193
208
|
return span.setAttribute("to_process.0.".concat(key), value);
|
|
194
209
|
});
|
|
195
|
-
inputEventValidation =
|
|
210
|
+
inputEventValidation = handlerContract.accepts.schema.safeParse(event.data);
|
|
196
211
|
if (inputEventValidation.error) {
|
|
197
212
|
throw new Error("Invalid event payload: ".concat(inputEventValidation.error));
|
|
198
213
|
}
|
|
199
|
-
return [4 /*yield*/, this._handler({
|
|
214
|
+
return [4 /*yield*/, this._handler[handlerContract.version]({
|
|
200
215
|
event: event,
|
|
201
216
|
source: this.source,
|
|
202
217
|
})];
|
|
203
218
|
case 2:
|
|
204
|
-
_handleOutput =
|
|
219
|
+
_handleOutput = _f.sent();
|
|
205
220
|
if (!_handleOutput)
|
|
206
221
|
return [2 /*return*/, []];
|
|
207
222
|
outputs = [];
|
|
@@ -211,14 +226,16 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
211
226
|
else {
|
|
212
227
|
outputs = [_handleOutput];
|
|
213
228
|
}
|
|
229
|
+
eventFactory_1 = (0, arvo_core_1.createArvoEventFactory)(handlerContract);
|
|
214
230
|
return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function (param, extension) {
|
|
215
231
|
var _a;
|
|
216
|
-
return
|
|
232
|
+
return eventFactory_1.emits(param, extension, {
|
|
217
233
|
tracer: (_a = opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.tracer) !== null && _a !== void 0 ? _a : (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
|
|
218
234
|
});
|
|
219
235
|
})];
|
|
220
236
|
case 3:
|
|
221
|
-
error_1 =
|
|
237
|
+
error_1 = _f.sent();
|
|
238
|
+
eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract.version('any'));
|
|
222
239
|
(0, arvo_core_1.exceptionToSpan)(error_1);
|
|
223
240
|
span.setStatus({
|
|
224
241
|
code: api_1.SpanStatusCode.ERROR,
|
|
@@ -232,10 +249,10 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
232
249
|
to: event.source,
|
|
233
250
|
error: error_1,
|
|
234
251
|
executionunits: this.executionunits,
|
|
235
|
-
traceparent: (
|
|
236
|
-
tracestate: (
|
|
237
|
-
accesscontrol: (
|
|
238
|
-
}, {}, { tracer: (
|
|
252
|
+
traceparent: (_b = otelSpanHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined,
|
|
253
|
+
tracestate: (_c = otelSpanHeaders.tracestate) !== null && _c !== void 0 ? _c : undefined,
|
|
254
|
+
accesscontrol: (_d = event.accesscontrol) !== null && _d !== void 0 ? _d : undefined,
|
|
255
|
+
}, {}, { tracer: (_e = opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.tracer) !== null && _e !== void 0 ? _e : (0, OpenTelemetry_1.fetchOpenTelemetryTracer)() });
|
|
239
256
|
Object.entries(result.otelAttributes).forEach(function (_a) {
|
|
240
257
|
var key = _a[0], value = _a[1];
|
|
241
258
|
return span.setAttribute("to_emit.0.".concat(key), value);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { SpanKind } from '@opentelemetry/api';
|
|
2
|
-
import { ArvoContract, ArvoEvent,
|
|
2
|
+
import { ArvoContract, ArvoEvent, CreateArvoEvent, OpenInferenceSpanKind, ArvoExecutionSpanKind, VersionedArvoContract, ArvoSemanticVersion } from 'arvo-core';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
/**
|
|
5
5
|
* Represents the input for an ArvoEvent handler function.
|
|
6
6
|
* @template TAccepts - The type of ArvoContractRecord that the handler accepts.
|
|
7
7
|
*/
|
|
8
|
-
export type ArvoEventHandlerFunctionInput<TContract extends ArvoContract
|
|
8
|
+
export type ArvoEventHandlerFunctionInput<TContract extends VersionedArvoContract<ArvoContract, ArvoSemanticVersion>> = {
|
|
9
9
|
/** The ArvoEvent object. */
|
|
10
|
-
event: ArvoEvent<
|
|
10
|
+
event: ArvoEvent<z.infer<TContract['accepts']['schema']>, Record<string, any>, TContract['accepts']['type']>;
|
|
11
11
|
/** The source field data of the handler */
|
|
12
12
|
source: string;
|
|
13
13
|
};
|
|
@@ -15,7 +15,7 @@ export type ArvoEventHandlerFunctionInput<TContract extends ArvoContract> = {
|
|
|
15
15
|
* Represents the output of an ArvoEvent handler function.
|
|
16
16
|
* @template TContract - The type of ArvoContract that the handler is associated with.
|
|
17
17
|
*/
|
|
18
|
-
export type ArvoEventHandlerFunctionOutput<TContract extends ArvoContract
|
|
18
|
+
export type ArvoEventHandlerFunctionOutput<TContract extends VersionedArvoContract<ArvoContract, ArvoSemanticVersion>> = {
|
|
19
19
|
[K in keyof TContract['emits']]: Omit<CreateArvoEvent<z.infer<TContract['emits'][K]>, K & string>, 'subject' | 'source' | 'executionunits' | 'traceparent' | 'tracestate'> & {
|
|
20
20
|
/**
|
|
21
21
|
* An optional override for the execution units of this specific event.
|
|
@@ -33,7 +33,9 @@ export type ArvoEventHandlerFunctionOutput<TContract extends ArvoContract> = {
|
|
|
33
33
|
* Defines the structure of an ArvoEvent handler function.
|
|
34
34
|
* @template TContract - The type of ArvoContract that the handler is associated with.
|
|
35
35
|
*/
|
|
36
|
-
export type ArvoEventHandlerFunction<TContract extends ArvoContract> =
|
|
36
|
+
export type ArvoEventHandlerFunction<TContract extends ArvoContract> = {
|
|
37
|
+
[V in ArvoSemanticVersion & keyof TContract['versions']]: (params: ArvoEventHandlerFunctionInput<VersionedArvoContract<TContract, V>>) => Promise<Array<ArvoEventHandlerFunctionOutput<VersionedArvoContract<TContract, V>>> | ArvoEventHandlerFunctionOutput<VersionedArvoContract<TContract, V>> | void>;
|
|
38
|
+
};
|
|
37
39
|
/**
|
|
38
40
|
* Interface for an ArvoEvent handler.
|
|
39
41
|
* @template T - The type of the contract (defaults to string).
|
|
@@ -12,7 +12,6 @@ export declare class ArvoEventRouter extends AbstractArvoEventHandler {
|
|
|
12
12
|
private readonly _source;
|
|
13
13
|
/**
|
|
14
14
|
* The source name of the router.
|
|
15
|
-
* @returns {string} The router's source name.
|
|
16
15
|
*
|
|
17
16
|
* @remarks
|
|
18
17
|
* The router attempts to match the `event.to` field with this value.
|
|
@@ -23,7 +22,6 @@ export declare class ArvoEventRouter extends AbstractArvoEventHandler {
|
|
|
23
22
|
get source(): string;
|
|
24
23
|
/**
|
|
25
24
|
* A list of all available event handlers to be used by the router.
|
|
26
|
-
* @property {ArvoEventHandler<ArvoContract>[]} handlers
|
|
27
25
|
*/
|
|
28
26
|
readonly handlers: ArvoEventHandler<ArvoContract>[];
|
|
29
27
|
/**
|
|
@@ -40,7 +38,7 @@ export declare class ArvoEventRouter extends AbstractArvoEventHandler {
|
|
|
40
38
|
readonly openTelemetrySpanKind: SpanKind;
|
|
41
39
|
/**
|
|
42
40
|
* Creates an instance of ArvoEventRouter.
|
|
43
|
-
* @param
|
|
41
|
+
* @param param - The parameters for initializing the router
|
|
44
42
|
* @throws {Error} If there are duplicate handlers for the same event type or the
|
|
45
43
|
* source in an invalid string
|
|
46
44
|
*/
|
|
@@ -24,8 +24,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
24
24
|
});
|
|
25
25
|
};
|
|
26
26
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
-
return g =
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
28
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
29
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
30
|
function step(op) {
|
|
31
31
|
if (f) throw new TypeError("Generator is already executing.");
|
|
@@ -69,7 +69,7 @@ var ArvoEventRouter = /** @class */ (function (_super) {
|
|
|
69
69
|
__extends(ArvoEventRouter, _super);
|
|
70
70
|
/**
|
|
71
71
|
* Creates an instance of ArvoEventRouter.
|
|
72
|
-
* @param
|
|
72
|
+
* @param param - The parameters for initializing the router
|
|
73
73
|
* @throws {Error} If there are duplicate handlers for the same event type or the
|
|
74
74
|
* source in an invalid string
|
|
75
75
|
*/
|
|
@@ -91,11 +91,11 @@ var ArvoEventRouter = /** @class */ (function (_super) {
|
|
|
91
91
|
_this.executionunits = param.executionunits;
|
|
92
92
|
for (var _i = 0, _a = _this.handlers; _i < _a.length; _i++) {
|
|
93
93
|
var handler = _a[_i];
|
|
94
|
-
if (_this.handlersMap[handler.contract.
|
|
95
|
-
var existingHandler = _this.handlersMap[handler.contract.
|
|
96
|
-
throw new Error((0, arvo_core_1.cleanString)("\n Duplicate handlers for event.type=".concat(handler.contract.
|
|
94
|
+
if (_this.handlersMap[handler.contract.type]) {
|
|
95
|
+
var existingHandler = _this.handlersMap[handler.contract.type];
|
|
96
|
+
throw new Error((0, arvo_core_1.cleanString)("\n Duplicate handlers for event.type=".concat(handler.contract.type, " found. There are same 'contract.accept.types' in \n contracts 'uri=").concat(existingHandler.contract.uri, "' and 'uri=").concat(handler.contract.uri, "'. This router does not support handlers\n with the same 'contract.accept.type'.\n ")));
|
|
97
97
|
}
|
|
98
|
-
_this.handlersMap[handler.contract.
|
|
98
|
+
_this.handlersMap[handler.contract.type] = handler;
|
|
99
99
|
}
|
|
100
100
|
Object.freeze(_this.handlers);
|
|
101
101
|
Object.freeze(_this.handlersMap);
|
|
@@ -104,7 +104,6 @@ var ArvoEventRouter = /** @class */ (function (_super) {
|
|
|
104
104
|
Object.defineProperty(ArvoEventRouter.prototype, "source", {
|
|
105
105
|
/**
|
|
106
106
|
* The source name of the router.
|
|
107
|
-
* @returns {string} The router's source name.
|
|
108
107
|
*
|
|
109
108
|
* @remarks
|
|
110
109
|
* The router attempts to match the `event.to` field with this value.
|
|
@@ -182,7 +181,7 @@ var ArvoEventRouter = /** @class */ (function (_super) {
|
|
|
182
181
|
arvoExecution: this.arvoExecutionSpanKind,
|
|
183
182
|
},
|
|
184
183
|
event: event,
|
|
185
|
-
opentelemetryConfig: opentelemetry
|
|
184
|
+
opentelemetryConfig: opentelemetry,
|
|
186
185
|
});
|
|
187
186
|
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 () {
|
|
188
187
|
var otelSpanHeaders, newEvent, results, error_1;
|
|
@@ -202,9 +201,12 @@ var ArvoEventRouter = /** @class */ (function (_super) {
|
|
|
202
201
|
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 ")));
|
|
203
202
|
}
|
|
204
203
|
if (!this.handlersMap[newEvent.type]) {
|
|
205
|
-
throw new Error((0, arvo_core_1.cleanString)("\n Invalid event (type=".concat(newEvent.type, "). No valid handler \n <handler[*].contract.
|
|
204
|
+
throw new Error((0, arvo_core_1.cleanString)("\n Invalid event (type=".concat(newEvent.type, "). No valid handler \n <handler[*].contract.type> found in the router.\n ")));
|
|
206
205
|
}
|
|
207
|
-
return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent, {
|
|
206
|
+
return [4 /*yield*/, this.handlersMap[newEvent.type].execute(newEvent, {
|
|
207
|
+
inheritFrom: 'execution',
|
|
208
|
+
tracer: (_a = opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.tracer) !== null && _a !== void 0 ? _a : (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
|
|
209
|
+
})];
|
|
208
210
|
case 2:
|
|
209
211
|
results = _b.sent();
|
|
210
212
|
return [2 /*return*/, results.map(function (event) {
|
|
@@ -33,14 +33,12 @@ export interface IArvoEventRouter {
|
|
|
33
33
|
/**
|
|
34
34
|
* A list of all available event handlers to be used by the router.
|
|
35
35
|
*
|
|
36
|
-
* @property {ArvoEventHandler<ArvoContract>[]} handlers
|
|
37
|
-
*
|
|
38
36
|
* @remarks
|
|
39
37
|
* This array contains instances of `ArvoEventHandler<ArvoContract>` which define
|
|
40
38
|
* how different types of events should be processed. The router will use these
|
|
41
39
|
* handlers to manage incoming events and generate appropriate responses or actions.
|
|
42
40
|
*/
|
|
43
|
-
handlers: ArvoEventHandler<ArvoContract<any, any, any
|
|
41
|
+
handlers: ArvoEventHandler<ArvoContract<any, any, any>>[];
|
|
44
42
|
/**
|
|
45
43
|
* The OpenTelemetry span kind attributes for the handler
|
|
46
44
|
* executor.
|
|
@@ -24,8 +24,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
24
24
|
});
|
|
25
25
|
};
|
|
26
26
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
-
return g =
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
28
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
29
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
30
|
function step(op) {
|
|
31
31
|
if (f) throw new TypeError("Generator is already executing.");
|
|
@@ -167,7 +167,7 @@ var MultiArvoEventHandler = /** @class */ (function (_super) {
|
|
|
167
167
|
arvoExecution: this.arvoExecutionSpanKind,
|
|
168
168
|
},
|
|
169
169
|
event: event,
|
|
170
|
-
opentelemetryConfig: opentelemetry
|
|
170
|
+
opentelemetryConfig: opentelemetry,
|
|
171
171
|
});
|
|
172
172
|
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
173
|
var otelSpanHeaders, _handlerOutput, outputs, error_1;
|
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fetchOpenTelemetryTracer = void 0;
|
|
4
4
|
var api_1 = require("@opentelemetry/api");
|
|
5
|
-
var utils_1 = require("./utils");
|
|
6
5
|
/**
|
|
7
6
|
* Returns a tracer instance for the ArvoEventHandler package.
|
|
8
7
|
*/
|
|
9
8
|
var fetchOpenTelemetryTracer = function () {
|
|
10
|
-
|
|
11
|
-
return api_1.trace.getTracer(pkg.name, pkg.version);
|
|
9
|
+
return api_1.trace.getTracer('arvo-instrumentation');
|
|
12
10
|
};
|
|
13
11
|
exports.fetchOpenTelemetryTracer = fetchOpenTelemetryTracer;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { Span, SpanKind, Tracer, Context } from '@opentelemetry/api';
|
|
2
2
|
import { ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
|
|
3
3
|
import { ICreateOtelSpan } from './types';
|
|
4
|
-
export declare function getPackageInfo(defaultName: string): {
|
|
5
|
-
name: string;
|
|
6
|
-
version: string;
|
|
7
|
-
};
|
|
8
4
|
export declare const extractContext: (traceparent: string, tracestate: string | null) => Context;
|
|
9
5
|
/**
|
|
10
6
|
* Creates an OpenTelemetry span from an ArvoEvent, facilitating distributed tracing in the Arvo system.
|
|
@@ -1,51 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.createOtelSpan = exports.createSpanFromEvent = exports.extractContext = void 0;
|
|
27
|
-
exports.getPackageInfo = getPackageInfo;
|
|
28
4
|
var api_1 = require("@opentelemetry/api");
|
|
29
5
|
var arvo_core_1 = require("arvo-core");
|
|
30
|
-
var fs = __importStar(require("fs"));
|
|
31
|
-
var path = __importStar(require("path"));
|
|
32
6
|
var _1 = require(".");
|
|
33
|
-
function getPackageInfo(defaultName) {
|
|
34
|
-
try {
|
|
35
|
-
// Read the package.json file
|
|
36
|
-
var packageJsonPath = path.resolve(__dirname, '../../package.json');
|
|
37
|
-
var packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8');
|
|
38
|
-
// Parse the JSON content
|
|
39
|
-
var packageJson = JSON.parse(packageJsonContent);
|
|
40
|
-
// Extract name and version
|
|
41
|
-
var name_1 = packageJson.name, version = packageJson.version;
|
|
42
|
-
return { name: name_1, version: version };
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
console.error('Error reading package.json:', error);
|
|
46
|
-
return { name: defaultName, version: 'Unknown' };
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
7
|
// Helper function to extract context from traceparent and tracestate
|
|
50
8
|
var extractContext = function (traceparent, tracestate) {
|
|
51
9
|
var extractedContext = api_1.propagation.extract(api_1.context.active(), {
|
|
@@ -158,7 +116,7 @@ var createOtelSpan = function (_a) {
|
|
|
158
116
|
var spanName = _a.spanName, spanKinds = _a.spanKinds, event = _a.event, opentelemetryConfig = _a.opentelemetryConfig;
|
|
159
117
|
opentelemetryConfig = opentelemetryConfig !== null && opentelemetryConfig !== void 0 ? opentelemetryConfig : {
|
|
160
118
|
inheritFrom: 'event',
|
|
161
|
-
tracer: null
|
|
119
|
+
tracer: null,
|
|
162
120
|
};
|
|
163
121
|
var spanOptions = {
|
|
164
122
|
kind: spanKinds.kind,
|
|
@@ -168,8 +126,8 @@ var createOtelSpan = function (_a) {
|
|
|
168
126
|
_b),
|
|
169
127
|
};
|
|
170
128
|
var tracer = (_c = opentelemetryConfig.tracer) !== null && _c !== void 0 ? _c : (0, _1.fetchOpenTelemetryTracer)();
|
|
171
|
-
return opentelemetryConfig.inheritFrom ===
|
|
172
|
-
(0, exports.createSpanFromEvent)(spanName, event, spanKinds, tracer)
|
|
173
|
-
tracer.startSpan(spanName, spanOptions);
|
|
129
|
+
return opentelemetryConfig.inheritFrom === 'event'
|
|
130
|
+
? (0, exports.createSpanFromEvent)(spanName, event, spanKinds, tracer)
|
|
131
|
+
: tracer.startSpan(spanName, spanOptions);
|
|
174
132
|
};
|
|
175
133
|
exports.createOtelSpan = createOtelSpan;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arvo-event-handler",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
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": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@opentelemetry/api": "^1.9.0",
|
|
52
52
|
"@opentelemetry/core": "^1.27.0",
|
|
53
|
-
"arvo-core": "^
|
|
53
|
+
"arvo-core": "^2.0.9",
|
|
54
54
|
"uuid": "^10.0.0",
|
|
55
55
|
"zod": "^3.23.8"
|
|
56
56
|
}
|