arvo-core 1.0.19 → 1.0.21

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -40,3 +40,7 @@
40
40
 
41
41
  - Fix a type bug in ArvoEvent creation
42
42
 
43
+ ## [1.0.21] - 2024-09-07
44
+
45
+ - Updated OpenTelemetry propagation and fixed propagation in the event factory
46
+
@@ -47,7 +47,7 @@ var uuid_1 = require("uuid");
47
47
  * );
48
48
  */
49
49
  var createArvoEvent = function (event, extensions, telemetry) {
50
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'createArvoEvent', {}, function (telemetryContext) {
50
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', "createArvoEvent<".concat(event.type, ">"), {}, function (telemetryContext) {
51
51
  if (event.datacontenttype &&
52
52
  event.datacontenttype !== schema_1.ArvoDataContentType) {
53
53
  var warning = (0, utils_1.cleanString)("\n Warning! The provided datacontenttype(=".concat(event.datacontenttype, ")\n is not ArvoEvent compatible (=").concat(schema_1.ArvoDataContentType, "). There may \n be some limited functionality.\n "));
@@ -54,12 +54,12 @@ var ArvoEventFactory = /** @class */ (function () {
54
54
  */
55
55
  ArvoEventFactory.prototype.accepts = function (event, extensions, telemetry) {
56
56
  var _this = this;
57
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'ContractualArvoEventFactory.accepts', {}, function () {
57
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', "ContractualArvoEventFactory<".concat(this.contract.uri, ">.accepts<").concat(this.contract.accepts.type, ">.create"), {}, function (telemetryContext) {
58
58
  var validationResult = _this.contract.validateInput(_this.contract.accepts.type, event.data);
59
59
  if (!validationResult.success) {
60
60
  throw new Error("Accept Event data validation failed: ".concat(validationResult.error.message));
61
61
  }
62
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { type: _this.contract.accepts.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri, data: validationResult.data }), extensions, telemetry);
62
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { type: _this.contract.accepts.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri, data: validationResult.data }), extensions, telemetryContext);
63
63
  });
64
64
  };
65
65
  /**
@@ -75,12 +75,12 @@ var ArvoEventFactory = /** @class */ (function () {
75
75
  */
76
76
  ArvoEventFactory.prototype.emits = function (event, extensions, telemetry) {
77
77
  var _this = this;
78
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'ContractualArvoEventFactory.emits', {}, function () {
78
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', "ContractualArvoEventFactory<".concat(this.contract.uri, ">.emits<").concat(event.type, ">.create"), {}, function (telemetryContext) {
79
79
  var validationResult = _this.contract.validateOutput(event.type, event.data);
80
80
  if (!validationResult.success) {
81
81
  throw new Error("Emit Event data validation failed: ".concat(validationResult.error.message));
82
82
  }
83
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri, data: validationResult.data }), extensions, telemetry);
83
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri, data: validationResult.data }), extensions, telemetryContext);
84
84
  });
85
85
  };
86
86
  /**
@@ -94,13 +94,13 @@ var ArvoEventFactory = /** @class */ (function () {
94
94
  */
95
95
  ArvoEventFactory.prototype.systemError = function (event, extensions, telemetry) {
96
96
  var _this = this;
97
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'ContractualArvoEventFactory.systemError', {}, function () {
97
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', "ContractualArvoEventFactory<".concat(this.contract.uri, ">.systemError<sys.").concat(this.contract.accepts.type, ".error>.create"), {}, function (telemetryContext) {
98
98
  var error = event.error, _events = __rest(event, ["error"]);
99
99
  return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { type: "sys.".concat(_this.contract.accepts.type, ".error"), data: {
100
100
  errorName: error.name,
101
101
  errorMessage: error.message,
102
102
  errorStack: error.stack || null,
103
- }, datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri }), extensions, telemetry);
103
+ }, datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri }), extensions, telemetryContext);
104
104
  });
105
105
  };
106
106
  return ArvoEventFactory;
@@ -5,7 +5,7 @@ import { TelemetryCarrier, TelemetryLogLevel, TelemetryContext } from './types';
5
5
  * @param traceparent - The trace header string.
6
6
  * @returns The active context.
7
7
  */
8
- export declare const getTelemetryContext: (traceparent?: string | null) => Context;
8
+ export declare const getTelemetryContext: (carrier?: TelemetryCarrier) => Context;
9
9
  /**
10
10
  * Parses the context from a span and active context.
11
11
  * @param span - The span to parse the context from.
@@ -42,23 +42,27 @@ export declare const exceptionToSpan: (span: Span, level: TelemetryLogLevel, err
42
42
  * @template TReturn - The return type of the wrapped function.
43
43
  *
44
44
  * @param {TelemetryContext | string} telemetryContext - The OpenTelemetry context object or a tracer name.
45
+ * If a string is provided, it will be used as the tracer name to retrieve the tracer.
46
+ * If a `TelemetryContext` object is provided, it should contain the span, tracer, and carrier information.
45
47
  * @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.
48
+ * @param {SpanOptions} [spanOptions] - Optional configuration for the span, such as attributes, links, and sampling.
49
+ * @param {(telemetryContext: TelemetryContext, ...args: TArgs) => TReturn} wrappedFunction - The function to be executed within the new span.
50
+ * This function will receive the `TelemetryContext` object as its first argument, containing the current span, tracer, and carrier.
51
+ * The rest of the arguments will be passed as additional parameters to the wrapped function.
49
52
  * @param {ThisParameterType<TFunction>} [thisArg] - The 'this' context to be used when calling the wrapped function.
50
53
  * @param {...TArgs} args - The arguments to be passed to the wrapped function.
51
54
  *
52
55
  * @returns {TReturn} The result of the wrapped function execution.
53
56
  *
54
57
  * @throws {Error} Rethrows any error that occurs during the execution of the wrapped function.
58
+ * The error will be logged to the span with an 'ERROR' level, and the span status will be set to `SpanStatusCode.ERROR`.
55
59
  *
56
60
  * @example
57
61
  * // Using with TelemetryContext
58
62
  * const telemetryContext: TelemetryContext = {
59
63
  * span: currentSpan,
60
64
  * tracer: currentTracer,
61
- * context: { traceparent: 'traceparent-value', tracestate: 'tracestate-value' }
65
+ * carrier: { traceparent: 'traceparent-value', tracestate: 'tracestate-value' }
62
66
  * };
63
67
  * const result = createOtelSpan(
64
68
  * telemetryContext,
@@ -85,8 +89,7 @@ export declare const exceptionToSpan: (span: Span, level: TelemetryLogLevel, err
85
89
  * null,
86
90
  * '12345'
87
91
  * );
88
- */
89
- export declare const createOtelSpan: <TArgs extends unknown[], TReturn>(telemetryContext: TelemetryContext | string, spanName: string, spanOptions: SpanOptions | undefined, wrappedFunction: (telemetryContext: TelemetryContext, ...args: TArgs) => TReturn, thisArg?: ThisParameterType<typeof wrappedFunction>, ...args: TArgs) => TReturn;
92
+ */ export declare const createOtelSpan: <TArgs extends unknown[], TReturn>(telemetryContext: TelemetryContext | string, spanName: string, spanOptions: SpanOptions | undefined, wrappedFunction: (telemetryContext: TelemetryContext, ...args: TArgs) => TReturn, thisArg?: ThisParameterType<typeof wrappedFunction>, ...args: TArgs) => TReturn;
90
93
  /**
91
94
  * A constant representing a null or not applicable value in OpenTelemetry context.
92
95
  */
@@ -27,9 +27,9 @@ var api_1 = require("@opentelemetry/api");
27
27
  * @param traceparent - The trace header string.
28
28
  * @returns The active context.
29
29
  */
30
- var getTelemetryContext = function (traceparent) {
31
- if (traceparent) {
32
- return api_1.propagation.extract(api_1.context.active(), { traceparent: traceparent });
30
+ var getTelemetryContext = function (carrier) {
31
+ if (carrier && (carrier.traceparent || carrier.tracestate)) {
32
+ return api_1.propagation.extract(api_1.ROOT_CONTEXT, carrier);
33
33
  }
34
34
  return api_1.context.active();
35
35
  };
@@ -93,23 +93,27 @@ exports.exceptionToSpan = exceptionToSpan;
93
93
  * @template TReturn - The return type of the wrapped function.
94
94
  *
95
95
  * @param {TelemetryContext | string} telemetryContext - The OpenTelemetry context object or a tracer name.
96
+ * If a string is provided, it will be used as the tracer name to retrieve the tracer.
97
+ * If a `TelemetryContext` object is provided, it should contain the span, tracer, and carrier information.
96
98
  * @param {string} spanName - The name of the span to be created.
97
- * @param {SpanOptions} [spanOptions] - Optional configuration for the span.
98
- * @param {(currentSpan: Span, ...args: TArgs) => TReturn} wrappedFunction - The function to be executed within the new span.
99
- * This function will receive the current span as its first argument.
99
+ * @param {SpanOptions} [spanOptions] - Optional configuration for the span, such as attributes, links, and sampling.
100
+ * @param {(telemetryContext: TelemetryContext, ...args: TArgs) => TReturn} wrappedFunction - The function to be executed within the new span.
101
+ * This function will receive the `TelemetryContext` object as its first argument, containing the current span, tracer, and carrier.
102
+ * The rest of the arguments will be passed as additional parameters to the wrapped function.
100
103
  * @param {ThisParameterType<TFunction>} [thisArg] - The 'this' context to be used when calling the wrapped function.
101
104
  * @param {...TArgs} args - The arguments to be passed to the wrapped function.
102
105
  *
103
106
  * @returns {TReturn} The result of the wrapped function execution.
104
107
  *
105
108
  * @throws {Error} Rethrows any error that occurs during the execution of the wrapped function.
109
+ * The error will be logged to the span with an 'ERROR' level, and the span status will be set to `SpanStatusCode.ERROR`.
106
110
  *
107
111
  * @example
108
112
  * // Using with TelemetryContext
109
113
  * const telemetryContext: TelemetryContext = {
110
114
  * span: currentSpan,
111
115
  * tracer: currentTracer,
112
- * context: { traceparent: 'traceparent-value', tracestate: 'tracestate-value' }
116
+ * carrier: { traceparent: 'traceparent-value', tracestate: 'tracestate-value' }
113
117
  * };
114
118
  * const result = createOtelSpan(
115
119
  * telemetryContext,
@@ -136,19 +140,18 @@ exports.exceptionToSpan = exceptionToSpan;
136
140
  * null,
137
141
  * '12345'
138
142
  * );
139
- */
140
- var createOtelSpan = function (telemetryContext, spanName, spanOptions, wrappedFunction, thisArg) {
143
+ */ var createOtelSpan = function (telemetryContext, spanName, spanOptions, wrappedFunction, thisArg) {
141
144
  var args = [];
142
145
  for (var _i = 5; _i < arguments.length; _i++) {
143
146
  args[_i - 5] = arguments[_i];
144
147
  }
145
- var activeContext = api_1.context.active();
148
+ var activeContext = api_1.ROOT_CONTEXT;
146
149
  var activeTracer;
147
150
  if (typeof telemetryContext === 'string') {
148
151
  activeTracer = api_1.trace.getTracer(telemetryContext);
149
152
  }
150
153
  else {
151
- activeContext = (0, exports.getTelemetryContext)(telemetryContext.carrier.traceparent);
154
+ activeContext = (0, exports.getTelemetryContext)(telemetryContext.carrier);
152
155
  activeTracer = telemetryContext.tracer;
153
156
  }
154
157
  var newSpan = activeTracer.startSpan(spanName, spanOptions, activeContext);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-core",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "This core package contains all the core classes and components of the Arvo Event Driven System",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {