arvo-core 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +5 -0
- package/LICENSE.md +7 -0
- package/README.md +46 -0
- package/dist/ArvoContract/helpers.d.ts +15 -0
- package/dist/ArvoContract/helpers.js +19 -0
- package/dist/ArvoContract/index.d.ts +80 -0
- package/dist/ArvoContract/index.js +122 -0
- package/dist/ArvoContract/types.d.ts +31 -0
- package/dist/ArvoContract/types.js +2 -0
- package/dist/ArvoContract/validators.d.ts +9 -0
- package/dist/ArvoContract/validators.js +21 -0
- package/dist/ArvoEvent/helpers.d.ts +40 -0
- package/dist/ArvoEvent/helpers.js +76 -0
- package/dist/ArvoEvent/index.d.ts +88 -0
- package/dist/ArvoEvent/index.js +126 -0
- package/dist/ArvoEvent/schema.d.ts +86 -0
- package/dist/ArvoEvent/schema.js +130 -0
- package/dist/ArvoEvent/types.d.ts +61 -0
- package/dist/ArvoEvent/types.js +2 -0
- package/dist/ArvoEvent/utils.d.ts +8 -0
- package/dist/ArvoEvent/utils.js +24 -0
- package/dist/OpenTelemetry/index.d.ts +93 -0
- package/dist/OpenTelemetry/index.js +179 -0
- package/dist/OpenTelemetry/types.d.ts +40 -0
- package/dist/OpenTelemetry/types.js +2 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +38 -0
- package/dist/utils.d.ts +50 -0
- package/dist/utils.js +79 -0
- package/generate-changelog.sh +29 -0
- package/package.json +53 -0
@@ -0,0 +1,126 @@
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
+
var schema_1 = require("./schema");
|
15
|
+
var OpenTelemetry_1 = require("../OpenTelemetry");
|
16
|
+
/**
|
17
|
+
* Represents an ArvoEvent, which extends the CloudEvent specification.
|
18
|
+
* @template TData - The type of the event data, extending ArvoEventData.
|
19
|
+
* @template TExtension - The type of additional extensions, extending CloudEventExtension.
|
20
|
+
*/
|
21
|
+
var ArvoEvent = /** @class */ (function () {
|
22
|
+
/**
|
23
|
+
* Creates an instance of ArvoEvent.
|
24
|
+
* @param context - The CloudEvent context along with Arvo and OpenTelemetry extensions.
|
25
|
+
* @param data - The event data.
|
26
|
+
* @param extensions - Optional additional extensions.
|
27
|
+
*/
|
28
|
+
function ArvoEvent(context, data, extensions) {
|
29
|
+
var cloudEventContext = schema_1.CloudEventContextSchema.parse(context);
|
30
|
+
this.id = cloudEventContext.id;
|
31
|
+
this.time = cloudEventContext.time;
|
32
|
+
this.type = cloudEventContext.type;
|
33
|
+
this.subject = cloudEventContext.subject;
|
34
|
+
this.source = cloudEventContext.source;
|
35
|
+
this.datacontenttype = cloudEventContext.datacontenttype;
|
36
|
+
this.specversion = cloudEventContext.specversion;
|
37
|
+
this.dataschema = cloudEventContext.dataschema;
|
38
|
+
this.data = schema_1.ArvoDataSchema.parse(data);
|
39
|
+
var arvoExtension = schema_1.ArvoExtensionSchema.parse(context);
|
40
|
+
var otelExtension = schema_1.OpenTelemetryExtensionSchema.parse(context);
|
41
|
+
this.extensions = __assign(__assign({}, (extensions
|
42
|
+
? schema_1.CloudEventExtensionSchema.parse(extensions)
|
43
|
+
: {})), { to: arvoExtension.to, accesscontrol: arvoExtension.accesscontrol, redirectto: arvoExtension.redirectto, executionunits: arvoExtension.executionunits, traceparent: otelExtension.traceparent, tracestate: otelExtension.tracestate });
|
44
|
+
if (this.datacontenttype === schema_1.ArvoDataContentType) {
|
45
|
+
if (!this.extensions.to) {
|
46
|
+
throw new Error("The ArvoEvent must have a non-empty 'to' field");
|
47
|
+
}
|
48
|
+
}
|
49
|
+
Object.freeze(this);
|
50
|
+
Object.freeze(this.extensions);
|
51
|
+
}
|
52
|
+
Object.defineProperty(ArvoEvent.prototype, "cloudevent", {
|
53
|
+
/**
|
54
|
+
* Gets the CloudEvent-specified default fields and extensions.
|
55
|
+
* @returns An object containing the base CloudEvent default fields and extensions.
|
56
|
+
* `default` fields are the standard CloudEvents attributes, which include:
|
57
|
+
* `extension` fields are additional attributes that provide extra context or functionality:
|
58
|
+
* - ArvoExtension: Arvo-specific extensions (to, accesscontrol, redirectto, executionunits)
|
59
|
+
* - OpenTelemetryExtension: OpenTelemetry-specific extensions (traceparent, tracestate)
|
60
|
+
* - TExtension: Any additional custom extensions
|
61
|
+
*/
|
62
|
+
get: function () {
|
63
|
+
return {
|
64
|
+
default: {
|
65
|
+
id: this.id,
|
66
|
+
source: this.source,
|
67
|
+
specversion: this.specversion,
|
68
|
+
type: this.type,
|
69
|
+
subject: this.subject,
|
70
|
+
datacontenttype: this.datacontenttype,
|
71
|
+
dataschema: this.dataschema,
|
72
|
+
data: this.data,
|
73
|
+
time: this.time,
|
74
|
+
},
|
75
|
+
extensions: this.extensions,
|
76
|
+
};
|
77
|
+
},
|
78
|
+
enumerable: false,
|
79
|
+
configurable: true
|
80
|
+
});
|
81
|
+
/**
|
82
|
+
* Converts the ArvoEvent to a JSON-serializable object.
|
83
|
+
* @returns A plain object representation of the ArvoEvent.
|
84
|
+
*/
|
85
|
+
ArvoEvent.prototype.toJSON = function () {
|
86
|
+
return __assign(__assign({}, this.cloudevent.default), this.extensions);
|
87
|
+
};
|
88
|
+
/**
|
89
|
+
* Converts the ArvoEvent to a JSON string.
|
90
|
+
* @param [spacing=0] - The number of spaces to use for indentation in the resulting JSON string.
|
91
|
+
* @returns A JSON string representation of the ArvoEvent.
|
92
|
+
*/
|
93
|
+
ArvoEvent.prototype.toString = function (spacing) {
|
94
|
+
if (spacing === void 0) { spacing = 0; }
|
95
|
+
return JSON.stringify(this.toJSON(), null, spacing);
|
96
|
+
};
|
97
|
+
Object.defineProperty(ArvoEvent.prototype, "otelAttributes", {
|
98
|
+
/**
|
99
|
+
* Gets OpenTelemetry attributes derived from the ArvoEvent.
|
100
|
+
* @returns An object containing OpenTelemetry attributes.
|
101
|
+
* The OpenTelemetry attributes for CloudEvents is as per
|
102
|
+
* the spec provided [here](https://opentelemetry.io/docs/specs/semconv/attributes-registry/cloudevents/)
|
103
|
+
* Additionally, the Arvo extension attributed are also returned
|
104
|
+
* as `cloudevents.arvo.event_*` fields
|
105
|
+
*/
|
106
|
+
get: function () {
|
107
|
+
return {
|
108
|
+
'cloudevents.event_id': this.id || OpenTelemetry_1.OTelNull,
|
109
|
+
'cloudevents.event_source': this.source || OpenTelemetry_1.OTelNull,
|
110
|
+
'cloudevents.event_spec_version': this.specversion || OpenTelemetry_1.OTelNull,
|
111
|
+
'cloudevents.event_subject': this.subject || OpenTelemetry_1.OTelNull,
|
112
|
+
'cloudevents.event_type': this.type || OpenTelemetry_1.OTelNull,
|
113
|
+
'cloudevents.event_time': this.time || OpenTelemetry_1.OTelNull,
|
114
|
+
'cloudevents.event_datacontenttype': this.datacontenttype || OpenTelemetry_1.OTelNull,
|
115
|
+
'cloudevents.event_dataschema': this.dataschema || OpenTelemetry_1.OTelNull,
|
116
|
+
'cloudevents.arvo.event_redirectto': this.extensions.redirectto || OpenTelemetry_1.OTelNull,
|
117
|
+
'cloudevents.arvo.event_to': this.extensions.to || OpenTelemetry_1.OTelNull,
|
118
|
+
'cloudevents.arvo.event_executionunits': this.extensions.executionunits || OpenTelemetry_1.OTelNull,
|
119
|
+
};
|
120
|
+
},
|
121
|
+
enumerable: false,
|
122
|
+
configurable: true
|
123
|
+
});
|
124
|
+
return ArvoEvent;
|
125
|
+
}());
|
126
|
+
exports.default = ArvoEvent;
|
@@ -0,0 +1,86 @@
|
|
1
|
+
import { z } from 'zod';
|
2
|
+
/**
|
3
|
+
* The content type for Arvo data in CloudEvents.
|
4
|
+
* This is the recommended content type for Arvo events.
|
5
|
+
*/
|
6
|
+
export declare const ArvoDataContentType = "application/cloudevents+json;charset=UTF-8;profile=arvo";
|
7
|
+
/**
|
8
|
+
* Zod schema for validating the core properties of a CloudEvent.
|
9
|
+
* This schema defines the structure and validation rules for the mandatory
|
10
|
+
* and optional attributes of a CloudEvent as per the CloudEvents specification.
|
11
|
+
*/
|
12
|
+
export declare const CloudEventContextSchema: z.ZodObject<{
|
13
|
+
id: z.ZodString;
|
14
|
+
time: z.ZodString;
|
15
|
+
source: z.ZodEffects<z.ZodString, string, string>;
|
16
|
+
specversion: z.ZodEffects<z.ZodString, "1.0", string>;
|
17
|
+
type: z.ZodString;
|
18
|
+
subject: z.ZodEffects<z.ZodString, string, string>;
|
19
|
+
datacontenttype: z.ZodDefault<z.ZodEffects<z.ZodString, string, string>>;
|
20
|
+
dataschema: z.ZodNullable<z.ZodEffects<z.ZodString, string, string>>;
|
21
|
+
}, "strip", z.ZodTypeAny, {
|
22
|
+
id: string;
|
23
|
+
time: string;
|
24
|
+
source: string;
|
25
|
+
specversion: "1.0";
|
26
|
+
type: string;
|
27
|
+
subject: string;
|
28
|
+
datacontenttype: string;
|
29
|
+
dataschema: string | null;
|
30
|
+
}, {
|
31
|
+
id: string;
|
32
|
+
time: string;
|
33
|
+
source: string;
|
34
|
+
specversion: string;
|
35
|
+
type: string;
|
36
|
+
subject: string;
|
37
|
+
dataschema: string | null;
|
38
|
+
datacontenttype?: string | undefined;
|
39
|
+
}>;
|
40
|
+
/**
|
41
|
+
* Zod schema for validating custom CloudEvent extensions.
|
42
|
+
* This schema allows for additional custom fields in the CloudEvent,
|
43
|
+
* following the CloudEvents specification for extension attributes.
|
44
|
+
*/
|
45
|
+
export declare const CloudEventExtensionSchema: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodBoolean, z.ZodNumber, z.ZodNull]>>;
|
46
|
+
/**
|
47
|
+
* Zod schema for validating the data payload of an Arvo event.
|
48
|
+
* The data must be a JSON serializable object.
|
49
|
+
*/
|
50
|
+
export declare const ArvoDataSchema: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodAny>, Record<string, any>, Record<string, any>>;
|
51
|
+
/**
|
52
|
+
* Zod schema for validating Arvo-specific extensions to the CloudEvent.
|
53
|
+
* This schema defines additional fields used by Arvo for event routing,
|
54
|
+
* access control, and execution metrics.
|
55
|
+
*/
|
56
|
+
export declare const ArvoExtensionSchema: z.ZodObject<{
|
57
|
+
to: z.ZodNullable<z.ZodEffects<z.ZodString, string, string>>;
|
58
|
+
accesscontrol: z.ZodNullable<z.ZodString>;
|
59
|
+
redirectto: z.ZodNullable<z.ZodEffects<z.ZodString, string, string>>;
|
60
|
+
executionunits: z.ZodNullable<z.ZodNumber>;
|
61
|
+
}, "strip", z.ZodTypeAny, {
|
62
|
+
to: string | null;
|
63
|
+
accesscontrol: string | null;
|
64
|
+
redirectto: string | null;
|
65
|
+
executionunits: number | null;
|
66
|
+
}, {
|
67
|
+
to: string | null;
|
68
|
+
accesscontrol: string | null;
|
69
|
+
redirectto: string | null;
|
70
|
+
executionunits: number | null;
|
71
|
+
}>;
|
72
|
+
/**
|
73
|
+
* Zod schema for validating OpenTelemetry extensions to the CloudEvent.
|
74
|
+
* This schema includes fields for distributed tracing as per the
|
75
|
+
* OpenTelemetry specification.
|
76
|
+
*/
|
77
|
+
export declare const OpenTelemetryExtensionSchema: z.ZodObject<{
|
78
|
+
traceparent: z.ZodNullable<z.ZodString>;
|
79
|
+
tracestate: z.ZodNullable<z.ZodString>;
|
80
|
+
}, "strip", z.ZodTypeAny, {
|
81
|
+
traceparent: string | null;
|
82
|
+
tracestate: string | null;
|
83
|
+
}, {
|
84
|
+
traceparent: string | null;
|
85
|
+
tracestate: string | null;
|
86
|
+
}>;
|
@@ -0,0 +1,130 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.OpenTelemetryExtensionSchema = exports.ArvoExtensionSchema = exports.ArvoDataSchema = exports.CloudEventExtensionSchema = exports.CloudEventContextSchema = exports.ArvoDataContentType = void 0;
|
4
|
+
var zod_1 = require("zod");
|
5
|
+
var utils_1 = require("../utils");
|
6
|
+
var utils_2 = require("./utils");
|
7
|
+
/**
|
8
|
+
* The content type for Arvo data in CloudEvents.
|
9
|
+
* This is the recommended content type for Arvo events.
|
10
|
+
*/
|
11
|
+
exports.ArvoDataContentType = 'application/cloudevents+json;charset=UTF-8;profile=arvo';
|
12
|
+
/**
|
13
|
+
* Zod schema for validating the core properties of a CloudEvent.
|
14
|
+
* This schema defines the structure and validation rules for the mandatory
|
15
|
+
* and optional attributes of a CloudEvent as per the CloudEvents specification.
|
16
|
+
*/
|
17
|
+
exports.CloudEventContextSchema = zod_1.z
|
18
|
+
.object({
|
19
|
+
id: zod_1.z
|
20
|
+
.string()
|
21
|
+
.min(1, 'ID must be a non-empty string')
|
22
|
+
.describe('Unique identifier of the event.'),
|
23
|
+
time: zod_1.z
|
24
|
+
.string()
|
25
|
+
.datetime({ offset: true })
|
26
|
+
.describe('Timestamp of when the occurrence happened. If the time of the occurrence cannot be determined then this attribute MAY be set to some other time (such as the current time) by the CloudEvents producer, however all producers for the same source MUST be consistent in this respect. In other words, either they all use the actual time of the occurrence or they all use the same algorithm to determine the value used.'),
|
27
|
+
source: zod_1.z
|
28
|
+
.string()
|
29
|
+
.min(1, 'Source must be a non-empty reference of the producer of the event')
|
30
|
+
.refine(utils_1.validateURI, 'The source must be a properly encoded URI')
|
31
|
+
.describe((0, utils_1.cleanString)("\n Identifies the context in which an event happened.\n It must be a valid URI that represents the event producer.\n ")),
|
32
|
+
specversion: zod_1.z
|
33
|
+
.string()
|
34
|
+
.min(1, 'Spec version must be a non-empty string')
|
35
|
+
.refine(function (val) { return val === '1.0'; }, "Spec version must be '1.0' for this version of the specification")
|
36
|
+
.describe((0, utils_1.cleanString)("\n The version of the CloudEvents specification which the event uses.\n Must be '1.0' for this version of the specification.\n ")),
|
37
|
+
type: zod_1.z
|
38
|
+
.string()
|
39
|
+
.min(1, 'Type must be a non-empty string')
|
40
|
+
.regex(/^[a-z0-9]+(\.[a-z0-9]+)+\.[a-z0-9]+$/, 'Type should be prefixed with a reverse-DNS name')
|
41
|
+
.describe((0, utils_1.cleanString)("\n Describes the type of event related to the originating occurrence.\n Should be prefixed with a reverse-DNS name.\n ")),
|
42
|
+
subject: zod_1.z
|
43
|
+
.string()
|
44
|
+
.min(1)
|
45
|
+
.describe((0, utils_1.cleanString)("\n Identifies the subject of the event in the context of the event producer. \n In case of Arvo, this MUST be Process Id.\n "))
|
46
|
+
.refine(utils_1.validateURI, 'The subject must be a properly encoded URI'),
|
47
|
+
datacontenttype: zod_1.z
|
48
|
+
.string()
|
49
|
+
.min(1, 'Data content type must be a non-empty string')
|
50
|
+
.refine(function (val) {
|
51
|
+
return Boolean(val === null || val === void 0 ? void 0 : val.includes('application/cloudevents+json')) ||
|
52
|
+
Boolean(val === null || val === void 0 ? void 0 : val.includes('application/json'));
|
53
|
+
}, (0, utils_1.cleanString)("\n The content type must be a valid JSON e.g. it must contain \n 'application/cloudevents+json' or 'application/json'. \n Arvo recommends using '".concat(exports.ArvoDataContentType, "'\n ")))
|
54
|
+
.default(exports.ArvoDataContentType)
|
55
|
+
.describe('Content type of the data value. Must adhere to RFC 2046 format if present.'),
|
56
|
+
dataschema: zod_1.z
|
57
|
+
.string()
|
58
|
+
.min(1, 'Must be a non-empty string if present.')
|
59
|
+
.refine(utils_1.validateURI, 'The dataschema must be a properly encoded URI')
|
60
|
+
.nullable()
|
61
|
+
.describe('Identifies the schema that data adheres to'),
|
62
|
+
})
|
63
|
+
.describe((0, utils_1.cleanString)("\n Defines the structure and validation rules \n for the core properties of a CloudEvent.\n "));
|
64
|
+
/**
|
65
|
+
* Zod schema for validating custom CloudEvent extensions.
|
66
|
+
* This schema allows for additional custom fields in the CloudEvent,
|
67
|
+
* following the CloudEvents specification for extension attributes.
|
68
|
+
*/
|
69
|
+
exports.CloudEventExtensionSchema = zod_1.z
|
70
|
+
.record(zod_1.z.string().regex(/^[a-z0-9]+$/, (0, utils_1.cleanString)("\n In compliance with CloudEvent specs, the extension keys must contain only \n lowercase letters and numbers, with no spaces or other characters \n ")), zod_1.z
|
71
|
+
.union([zod_1.z.string(), zod_1.z.boolean(), zod_1.z.number(), zod_1.z.null()])
|
72
|
+
.describe('The CloudEvent extension can only contain a number, boolean, string or null'))
|
73
|
+
.describe('Schema for custom CloudEvent extensions. Allows for additional custom fields in the CloudEvent.');
|
74
|
+
/**
|
75
|
+
* Zod schema for validating the data payload of an Arvo event.
|
76
|
+
* The data must be a JSON serializable object.
|
77
|
+
*/
|
78
|
+
exports.ArvoDataSchema = zod_1.z
|
79
|
+
.record(zod_1.z.string(), zod_1.z.any())
|
80
|
+
.refine(utils_2.isJSONSerializable, 'The Arvo data object must be a JSON serializable object')
|
81
|
+
.describe('A JSON serialisable object as ArvoEvent payload data');
|
82
|
+
/**
|
83
|
+
* Zod schema for validating Arvo-specific extensions to the CloudEvent.
|
84
|
+
* This schema defines additional fields used by Arvo for event routing,
|
85
|
+
* access control, and execution metrics.
|
86
|
+
*/
|
87
|
+
exports.ArvoExtensionSchema = zod_1.z
|
88
|
+
.object({
|
89
|
+
to: zod_1.z
|
90
|
+
.string()
|
91
|
+
.min(1, 'Must be a non empty string')
|
92
|
+
.refine(utils_1.validateURI, "The 'to' must be a properly encoded URI")
|
93
|
+
.nullable()
|
94
|
+
.describe((0, utils_1.cleanString)("\n This field defined the consumer machine of the event. It's value can\n be the same as the type field or can be different. This is a metadata \n field in event routing specifies initial recipients or topics. \n\n For successful events, it should be determined \n by handler definition, redirectto, or source. For errors, it should be \n set to the error destination or event's source.\n ")),
|
95
|
+
accesscontrol: zod_1.z
|
96
|
+
.string()
|
97
|
+
.min(1, 'Must be a non empty string, if defined')
|
98
|
+
.nullable()
|
99
|
+
.describe((0, utils_1.cleanString)("\n Defines the access controls for the event. This field may contain one of the following:\n 1. A UserID: A valid UUID representing a user who has access to the event.\n 2. An encrypted string: A base64-encoded encrypted string containing access control information.\n 3. Key-value pairs: A semicolon-separated list of key-value pairs, where each pair is separated by a colon.\n Example: \"role:admin;department:finance;clearance:top-secret\"\n\n This field is used to determine who or what can perform actions on or read the event.\n It can be used for fine-grained access control, allowing different levels of access\n based on user roles, departments, or other custom criteria.\n\n If no access controls are needed, this field can be set to null.\n\n Examples:\n - \"123e4567-e89b-12d3-a456-426614174000\" (UserID)\n - \"ZW5jcnlwdGVkX2FjY2Vzc19jb250cm9sX2RhdGE=\" (Encrypted string)\n - \"role:editor;project:alpha;team:backend\" (Key-value pairs)\n ")),
|
100
|
+
redirectto: zod_1.z
|
101
|
+
.string()
|
102
|
+
.min(1, 'Must be a non empty string, if defined')
|
103
|
+
.refine(utils_1.validateURI, "The 'redirectto' must be a properly encoded URI")
|
104
|
+
.nullable()
|
105
|
+
.describe((0, utils_1.cleanString)("\n This is a metadata field for events, indicating alternative recipients \n or destinations. It enables dynamic routing and complex workflows. \n\n For successful events, it's set by handlers; for errors, it's \n null to prevent automatic redirection.\n ")),
|
106
|
+
executionunits: zod_1.z
|
107
|
+
.number()
|
108
|
+
.nullable()
|
109
|
+
.describe((0, utils_1.cleanString)("\n This data field represents the cost associated with \n generating this specific cloudevent. It serves as a metric \n to track and measure the financial impact of event generation\n within cloud-based systems or applications.\n ")),
|
110
|
+
})
|
111
|
+
.describe('Schema for Arvo-specific extensions to the CloudEvent.');
|
112
|
+
/**
|
113
|
+
* Zod schema for validating OpenTelemetry extensions to the CloudEvent.
|
114
|
+
* This schema includes fields for distributed tracing as per the
|
115
|
+
* OpenTelemetry specification.
|
116
|
+
*/
|
117
|
+
exports.OpenTelemetryExtensionSchema = zod_1.z
|
118
|
+
.object({
|
119
|
+
traceparent: zod_1.z
|
120
|
+
.string()
|
121
|
+
.min(1, 'Must be a non empty string, if defined')
|
122
|
+
.nullable()
|
123
|
+
.describe((0, utils_1.cleanString)("\n The traceparent header is part of the OpenTelemetry specification. \n It contains trace context information, including trace ID, parent \n span ID, and trace flags, enabling distributed tracing across \n services and systems. \n ")),
|
124
|
+
tracestate: zod_1.z
|
125
|
+
.string()
|
126
|
+
.min(1, 'Must be a non empty string, if defined')
|
127
|
+
.nullable()
|
128
|
+
.describe((0, utils_1.cleanString)("\n The tracestate header in OpenTelemetry is used to convey vendor-specific \n trace information across service boundaries. It allows for custom \n key-value pairs to be propagated alongside the traceparent header in \n distributed tracing scenarios. \n ")),
|
129
|
+
})
|
130
|
+
.describe((0, utils_1.cleanString)("\n Distributed tracing extension as per\n https://github.com/cloudevents/spec/blob/main/cloudevents/extensions/distributed-tracing.md \n "));
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import { z } from 'zod';
|
2
|
+
import { CloudEventContextSchema, ArvoExtensionSchema, CloudEventExtensionSchema, ArvoDataSchema, OpenTelemetryExtensionSchema } from './schema';
|
3
|
+
/**
|
4
|
+
* Represents the core properties of a CloudEvent.
|
5
|
+
*/
|
6
|
+
export type CloudEventContext = z.infer<typeof CloudEventContextSchema>;
|
7
|
+
/**
|
8
|
+
* Represents custom CloudEvent extensions.
|
9
|
+
*/
|
10
|
+
export type CloudEventExtension = z.infer<typeof CloudEventExtensionSchema>;
|
11
|
+
/**
|
12
|
+
* Represents the data payload of an ArvoEvent.
|
13
|
+
*/
|
14
|
+
export type ArvoEventData = z.infer<typeof ArvoDataSchema>;
|
15
|
+
/**
|
16
|
+
* Represents Arvo-specific extensions to the CloudEvent.
|
17
|
+
*/
|
18
|
+
export type ArvoExtension = z.infer<typeof ArvoExtensionSchema>;
|
19
|
+
/**
|
20
|
+
* Represents OpenTelemetry-specific extensions to the CloudEvent.
|
21
|
+
*/
|
22
|
+
export type OpenTelemetryExtension = z.infer<typeof OpenTelemetryExtensionSchema>;
|
23
|
+
/**
|
24
|
+
* Represents the input parameters for creating an ArvoEvent.
|
25
|
+
* @template TData - The type of the event data, extending ArvoEventData.
|
26
|
+
*/
|
27
|
+
export type CreateArvoEvent<TData extends ArvoEventData> = {
|
28
|
+
/** Unique identifier of the event. Must be a non-empty string. If not provided, a UUID will be generated. */
|
29
|
+
id?: string;
|
30
|
+
/** Timestamp of when the occurrence happened. Must be in ISO 8601 format with timezone offset. */
|
31
|
+
time?: string;
|
32
|
+
/** Identifies the context in which an event happened. Must be a valid URI representing the event producer. */
|
33
|
+
source: string;
|
34
|
+
/** The version of the CloudEvents specification used. Must be '1.0' for this version. */
|
35
|
+
specversion?: '1.0';
|
36
|
+
/** Describes the type of event. Should be prefixed with a reverse-DNS name. */
|
37
|
+
type: string;
|
38
|
+
/** Identifies the subject of the event. For Arvo, this must be the Process Id. */
|
39
|
+
subject: string;
|
40
|
+
/** Content type of the data value. Must include 'application/cloudevents+json' or
|
41
|
+
* 'application/json'. For an ArvoEvent, it is set automatically to
|
42
|
+
* 'application/cloudevents+json;charset=UTF-8;profile=arvo'
|
43
|
+
*/
|
44
|
+
datacontenttype?: string;
|
45
|
+
/** Identifies the schema that data adheres to. Must be a valid URI if present. */
|
46
|
+
dataschema?: string;
|
47
|
+
/** The event payload. This payload must be JSON serializable. */
|
48
|
+
data: TData;
|
49
|
+
/** Defines the consumer machine of the event. Used for event routing. Must be a valid URI if present. */
|
50
|
+
to?: string;
|
51
|
+
/** Defines access controls for the event. Can be a UserID, encrypted string, or key-value pairs. */
|
52
|
+
accesscontrol?: string;
|
53
|
+
/** Indicates alternative recipients or destinations for events. Must be a valid URI if present. */
|
54
|
+
redirectto?: string;
|
55
|
+
/** Represents the cost associated with generating the cloudevent. */
|
56
|
+
executionunits?: number;
|
57
|
+
/** Contains trace context information for distributed tracing (OpenTelemetry). */
|
58
|
+
traceparent?: string;
|
59
|
+
/** Conveys vendor-specific trace information across service boundaries (OpenTelemetry). */
|
60
|
+
tracestate?: string;
|
61
|
+
};
|
@@ -0,0 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* Checks if an object is JSON serializable.
|
3
|
+
*
|
4
|
+
* @param obj - The object to check for JSON serializability.
|
5
|
+
* @returns A boolean indicating whether the object is JSON serializable.
|
6
|
+
* @throws {Error} If the object is not JSON serializable.
|
7
|
+
*/
|
8
|
+
export declare const isJSONSerializable: (obj: unknown) => boolean;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isJSONSerializable = void 0;
|
4
|
+
/**
|
5
|
+
* Checks if an object is JSON serializable.
|
6
|
+
*
|
7
|
+
* @param obj - The object to check for JSON serializability.
|
8
|
+
* @returns A boolean indicating whether the object is JSON serializable.
|
9
|
+
* @throws {Error} If the object is not JSON serializable.
|
10
|
+
*/
|
11
|
+
var isJSONSerializable = function (obj) {
|
12
|
+
try {
|
13
|
+
var serialized = JSON.stringify(obj);
|
14
|
+
if (serialized === '{}' && Object.keys(obj).length > 0) {
|
15
|
+
return false;
|
16
|
+
}
|
17
|
+
var parsed = JSON.parse(serialized);
|
18
|
+
return JSON.stringify(obj) === JSON.stringify(parsed);
|
19
|
+
}
|
20
|
+
catch (_a) {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
};
|
24
|
+
exports.isJSONSerializable = isJSONSerializable;
|
@@ -0,0 +1,93 @@
|
|
1
|
+
import { Context, Span, SpanOptions } from '@opentelemetry/api';
|
2
|
+
import { TelemetryCarrier, TelemetryLogLevel, TelemetryContext } from './types';
|
3
|
+
/**
|
4
|
+
* Retrieves the active context based on the provided trace header.
|
5
|
+
* @param traceparent - The trace header string.
|
6
|
+
* @returns The active context.
|
7
|
+
*/
|
8
|
+
export declare const getTelemetryContext: (traceparent?: string | null) => Context;
|
9
|
+
/**
|
10
|
+
* Parses the context from a span and active context.
|
11
|
+
* @param span - The span to parse the context from.
|
12
|
+
* @param activeContext - The active context (optional, defaults to the current active context).
|
13
|
+
* @returns The parsed telemetry context.
|
14
|
+
*/
|
15
|
+
export declare const getTelemetryCarrier: (span: Span, activeContext?: Context) => TelemetryCarrier;
|
16
|
+
/**
|
17
|
+
* Logs a message to a span with additional parameters.
|
18
|
+
* @param span - The span to log the message to.
|
19
|
+
* @param params - The parameters for the log message.
|
20
|
+
* @param params.level - The log level.
|
21
|
+
* @param params.message - The log message.
|
22
|
+
*/
|
23
|
+
export declare const logToSpan: (span: Span, params: {
|
24
|
+
level: TelemetryLogLevel;
|
25
|
+
message: string;
|
26
|
+
}) => void;
|
27
|
+
/**
|
28
|
+
* Logs an exception to a span and sets exception-related attributes.
|
29
|
+
* @param span - The span to log the exception to.
|
30
|
+
* @param level - The log level for the exception.
|
31
|
+
* @param error - The error object to be logged.
|
32
|
+
*/
|
33
|
+
export declare const exceptionToSpan: (span: Span, level: TelemetryLogLevel, error: Error) => void;
|
34
|
+
/**
|
35
|
+
* Creates a new OpenTelemetry span and executes the provided function within its context.
|
36
|
+
*
|
37
|
+
* This function enhances tracing by creating a new span, executing the given function within
|
38
|
+
* that span's context, and properly handling any errors that may occur. It also ensures that
|
39
|
+
* the wrapped function has access to the current span.
|
40
|
+
*
|
41
|
+
* @template TArgs - The type of the arguments array for the wrapped function.
|
42
|
+
* @template TReturn - The return type of the wrapped function.
|
43
|
+
*
|
44
|
+
* @param {TelemetryContext | string} telemetryContext - The OpenTelemetry context object or a tracer name.
|
45
|
+
* @param {string} spanName - The name of the span to be created.
|
46
|
+
* @param {SpanOptions} [spanOptions] - Optional configuration for the span.
|
47
|
+
* @param {(currentSpan: Span, ...args: TArgs) => TReturn} wrappedFunction - The function to be executed within the new span.
|
48
|
+
* This function will receive the current span as its first argument.
|
49
|
+
* @param {ThisParameterType<TFunction>} [thisArg] - The 'this' context to be used when calling the wrapped function.
|
50
|
+
* @param {...TArgs} args - The arguments to be passed to the wrapped function.
|
51
|
+
*
|
52
|
+
* @returns {TReturn} The result of the wrapped function execution.
|
53
|
+
*
|
54
|
+
* @throws {Error} Rethrows any error that occurs during the execution of the wrapped function.
|
55
|
+
*
|
56
|
+
* @example
|
57
|
+
* // Using with TelemetryContext
|
58
|
+
* const telemetryContext: TelemetryContext = {
|
59
|
+
* span: currentSpan,
|
60
|
+
* tracer: currentTracer,
|
61
|
+
* context: { traceparent: 'traceparent-value', tracestate: 'tracestate-value' }
|
62
|
+
* };
|
63
|
+
* const result = createOtelSpan(
|
64
|
+
* telemetryContext,
|
65
|
+
* 'ProcessOrder',
|
66
|
+
* { attributes: { orderId: '12345' } },
|
67
|
+
* (span, orderId) => {
|
68
|
+
* span.addEvent('Processing order');
|
69
|
+
* return processOrder(orderId);
|
70
|
+
* },
|
71
|
+
* null,
|
72
|
+
* '12345'
|
73
|
+
* );
|
74
|
+
*
|
75
|
+
* @example
|
76
|
+
* // Using with tracer name
|
77
|
+
* const result = createOtelSpan(
|
78
|
+
* 'OrderService',
|
79
|
+
* 'FetchOrderDetails',
|
80
|
+
* undefined,
|
81
|
+
* (span, orderId) => {
|
82
|
+
* span.setAttribute('orderId', orderId);
|
83
|
+
* return fetchOrderDetails(orderId);
|
84
|
+
* },
|
85
|
+
* null,
|
86
|
+
* '12345'
|
87
|
+
* );
|
88
|
+
*/
|
89
|
+
export declare const createOtelSpan: <TArgs extends unknown[], TReturn>(telemetryContext: TelemetryContext | string, spanName: string, spanOptions: SpanOptions | undefined, wrappedFunction: (currentSpan: Span, ...args: TArgs) => TReturn, thisArg?: ThisParameterType<typeof wrappedFunction>, ...args: TArgs) => TReturn;
|
90
|
+
/**
|
91
|
+
* A constant representing a null or not applicable value in OpenTelemetry context.
|
92
|
+
*/
|
93
|
+
export declare const OTelNull = "N/A";
|