@usearazzo/runner 1.0.1-alpha.34 → 1.0.1-alpha.36
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 +20 -0
- package/README.md +6 -1
- package/dist/arazzo-runner.browser.js +2212 -1116
- package/dist/arazzo-runner.browser.min.js +1 -1
- package/package.json +78 -25
- package/src/errors/ExecutionError.cjs +2 -0
- package/src/errors/ExecutionError.mjs +2 -0
- package/src/executor/OpenAPIOperationExecutor.cjs +58 -47
- package/src/executor/OpenAPIOperationExecutor.mjs +58 -47
- package/src/executor/WorkflowExecutor.cjs +302 -160
- package/src/executor/WorkflowExecutor.mjs +302 -160
- package/src/index.cjs +14 -0
- package/src/index.mjs +2 -0
- package/src/observability/EventChannel.cjs +30 -20
- package/src/observability/EventChannel.mjs +30 -23
- package/src/observability/Redactor.cjs +7 -5
- package/src/observability/Redactor.mjs +7 -5
- package/src/observability/combineObservers.cjs +18 -0
- package/src/observability/combineObservers.mjs +12 -0
- package/src/observability/instrument.cjs +159 -0
- package/src/observability/instrument.mjs +144 -0
- package/src/observability/logging/LoggingObserver.cjs +177 -0
- package/src/observability/logging/LoggingObserver.mjs +172 -0
- package/src/observability/logging/index.cjs +13 -0
- package/src/observability/logging/index.mjs +1 -0
- package/src/observability/opentelemetry/ArazzoRunnerInstrumentation.cjs +321 -0
- package/src/observability/opentelemetry/ArazzoRunnerInstrumentation.mjs +313 -0
- package/src/observability/opentelemetry/index.cjs +13 -0
- package/src/observability/opentelemetry/index.mjs +1 -0
- package/src/observability/stream/EventStream.cjs +112 -0
- package/src/observability/stream/EventStream.mjs +107 -0
- package/src/observability/stream/index.cjs +13 -0
- package/src/observability/stream/index.mjs +1 -0
- package/src/registry/DocumentRegistry.cjs +14 -9
- package/src/registry/DocumentRegistry.mjs +14 -9
- package/src/vendor/swagger-client.cjs +1 -1
- package/src/vendor/swagger-client.mjs +1 -1
- package/src/version.cjs +9 -0
- package/src/version.mjs +3 -0
- package/types/observability/logging/LoggingObserver.d.cts +82 -0
- package/types/observability/logging/LoggingObserver.d.ts +82 -0
- package/types/observability/logging/index.d.cts +2 -0
- package/types/observability/logging/index.d.ts +2 -0
- package/types/observability/opentelemetry/ArazzoRunnerInstrumentation.d.cts +84 -0
- package/types/observability/opentelemetry/ArazzoRunnerInstrumentation.d.ts +84 -0
- package/types/observability/opentelemetry/index.d.cts +2 -0
- package/types/observability/opentelemetry/index.d.ts +2 -0
- package/types/observability/stream/EventStream.d.cts +68 -0
- package/types/observability/stream/EventStream.d.ts +68 -0
- package/types/observability/stream/index.d.cts +2 -0
- package/types/observability/stream/index.d.ts +2 -0
- package/types/runner.d.cts +3891 -0
- package/types/runner.d.ts +137 -40
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var _ramdaAdjunct = require("ramda-adjunct");
|
|
8
|
+
var _instrument = require("./instrument.cjs");
|
|
8
9
|
/**
|
|
9
10
|
* An event without its `timestamp`, which the channel stamps. Distributes over
|
|
10
11
|
* a union, so each member keeps its own discriminated shape.
|
|
@@ -17,33 +18,19 @@ var _ramdaAdjunct = require("ramda-adjunct");
|
|
|
17
18
|
*/
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
|
-
* Delivers a building block's events to its observer
|
|
21
|
+
* Delivers a building block's events to its observer, then to the
|
|
22
|
+
* process-wide observers {@link instrument} registered: stamps each with the
|
|
21
23
|
* block's clock, snapshots it, and hands it over synchronously, without
|
|
22
24
|
* awaiting anything.
|
|
23
25
|
*
|
|
24
|
-
* A block
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* unobserved run builds no events at all.
|
|
26
|
+
* A block emits only while {@link EventChannel.hasObservers}:
|
|
27
|
+
* `if (this.#events.hasObservers) this.#events.emit({...})`, so an unobserved
|
|
28
|
+
* run builds no events at all, like `diagnostics_channel`'s `hasSubscribers`.
|
|
28
29
|
* @internal
|
|
29
30
|
*/
|
|
30
31
|
class EventChannel {
|
|
31
32
|
static #DEFAULT_NOW = () => performance.now();
|
|
32
33
|
|
|
33
|
-
/**
|
|
34
|
-
* A channel to `options.observer`, or `undefined` when there is none.
|
|
35
|
-
*/
|
|
36
|
-
static open(options) {
|
|
37
|
-
const {
|
|
38
|
-
observer,
|
|
39
|
-
now
|
|
40
|
-
} = options;
|
|
41
|
-
return observer === undefined ? undefined : new EventChannel({
|
|
42
|
-
observer,
|
|
43
|
-
now
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
34
|
/**
|
|
48
35
|
* A frozen deep copy of the plain objects and arrays in `value`; anything
|
|
49
36
|
* else, an ApiDOM element above all, is shared as it is. The run keeps
|
|
@@ -76,6 +63,15 @@ class EventChannel {
|
|
|
76
63
|
this.#now = options.now ?? EventChannel.#DEFAULT_NOW;
|
|
77
64
|
}
|
|
78
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Whether anyone observes the channel: its own observer or a process-wide
|
|
68
|
+
* one. Read per event, so hooks registered mid-run apply from the next
|
|
69
|
+
* event on.
|
|
70
|
+
*/
|
|
71
|
+
get hasObservers() {
|
|
72
|
+
return this.#observer !== undefined || (0, _instrument.isInstrumented)();
|
|
73
|
+
}
|
|
74
|
+
|
|
79
75
|
/**
|
|
80
76
|
* Emits the event. `timestamp` is the clock reading the event describes,
|
|
81
77
|
* when the caller already took one (so a start/settled pair matches a
|
|
@@ -90,7 +86,21 @@ class EventChannel {
|
|
|
90
86
|
...Object.fromEntries(fields),
|
|
91
87
|
timestamp: timestamp ?? this.#now()
|
|
92
88
|
};
|
|
93
|
-
|
|
89
|
+
const snapshot = EventChannel.#snapshot(stamped, new WeakMap());
|
|
90
|
+
// process-wide observers see the event even when the block's own throws,
|
|
91
|
+
// so a buggy observer cannot leave a trace incomplete; the block's own
|
|
92
|
+
// error is the one that aborts what emitted the event
|
|
93
|
+
try {
|
|
94
|
+
this.#observer?.(snapshot);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
try {
|
|
97
|
+
(0, _instrument.notify)(snapshot);
|
|
98
|
+
} catch {
|
|
99
|
+
// the block's own observer failed first
|
|
100
|
+
}
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
(0, _instrument.notify)(snapshot);
|
|
94
104
|
}
|
|
95
105
|
}
|
|
96
106
|
var _default = exports.default = EventChannel;
|
|
@@ -1,44 +1,28 @@
|
|
|
1
1
|
import { isPlainObject } from 'ramda-adjunct';
|
|
2
|
-
|
|
2
|
+
import { isInstrumented, notify } from "./instrument.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* An event without its `timestamp`, which the channel stamps. Distributes over
|
|
5
5
|
* a union, so each member keeps its own discriminated shape.
|
|
6
6
|
* @internal
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
/**
|
|
10
9
|
* Options for the EventChannel.
|
|
11
10
|
* @internal
|
|
12
11
|
*/
|
|
13
|
-
|
|
14
12
|
/**
|
|
15
|
-
* Delivers a building block's events to its observer
|
|
13
|
+
* Delivers a building block's events to its observer, then to the
|
|
14
|
+
* process-wide observers {@link instrument} registered: stamps each with the
|
|
16
15
|
* block's clock, snapshots it, and hands it over synchronously, without
|
|
17
16
|
* awaiting anything.
|
|
18
17
|
*
|
|
19
|
-
* A block
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* unobserved run builds no events at all.
|
|
18
|
+
* A block emits only while {@link EventChannel.hasObservers}:
|
|
19
|
+
* `if (this.#events.hasObservers) this.#events.emit({...})`, so an unobserved
|
|
20
|
+
* run builds no events at all, like `diagnostics_channel`'s `hasSubscribers`.
|
|
23
21
|
* @internal
|
|
24
22
|
*/
|
|
25
23
|
class EventChannel {
|
|
26
24
|
static #DEFAULT_NOW = () => performance.now();
|
|
27
25
|
|
|
28
|
-
/**
|
|
29
|
-
* A channel to `options.observer`, or `undefined` when there is none.
|
|
30
|
-
*/
|
|
31
|
-
static open(options) {
|
|
32
|
-
const {
|
|
33
|
-
observer,
|
|
34
|
-
now
|
|
35
|
-
} = options;
|
|
36
|
-
return observer === undefined ? undefined : new EventChannel({
|
|
37
|
-
observer,
|
|
38
|
-
now
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
26
|
/**
|
|
43
27
|
* A frozen deep copy of the plain objects and arrays in `value`; anything
|
|
44
28
|
* else, an ApiDOM element above all, is shared as it is. The run keeps
|
|
@@ -71,6 +55,15 @@ class EventChannel {
|
|
|
71
55
|
this.#now = options.now ?? EventChannel.#DEFAULT_NOW;
|
|
72
56
|
}
|
|
73
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Whether anyone observes the channel: its own observer or a process-wide
|
|
60
|
+
* one. Read per event, so hooks registered mid-run apply from the next
|
|
61
|
+
* event on.
|
|
62
|
+
*/
|
|
63
|
+
get hasObservers() {
|
|
64
|
+
return this.#observer !== undefined || isInstrumented();
|
|
65
|
+
}
|
|
66
|
+
|
|
74
67
|
/**
|
|
75
68
|
* Emits the event. `timestamp` is the clock reading the event describes,
|
|
76
69
|
* when the caller already took one (so a start/settled pair matches a
|
|
@@ -85,7 +78,21 @@ class EventChannel {
|
|
|
85
78
|
...Object.fromEntries(fields),
|
|
86
79
|
timestamp: timestamp ?? this.#now()
|
|
87
80
|
};
|
|
88
|
-
|
|
81
|
+
const snapshot = EventChannel.#snapshot(stamped, new WeakMap());
|
|
82
|
+
// process-wide observers see the event even when the block's own throws,
|
|
83
|
+
// so a buggy observer cannot leave a trace incomplete; the block's own
|
|
84
|
+
// error is the one that aborts what emitted the event
|
|
85
|
+
try {
|
|
86
|
+
this.#observer?.(snapshot);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
try {
|
|
89
|
+
notify(snapshot);
|
|
90
|
+
} catch {
|
|
91
|
+
// the block's own observer failed first
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
notify(snapshot);
|
|
89
96
|
}
|
|
90
97
|
}
|
|
91
98
|
export default EventChannel;
|
|
@@ -41,7 +41,8 @@ var _mediaTypes = require("@swaggerexpert/media-types");
|
|
|
41
41
|
* - a request or response body that is JSON or a URL-encoded form, field by
|
|
42
42
|
* field, and any other body, or any object that is not plain data, whole.
|
|
43
43
|
*
|
|
44
|
-
* A
|
|
44
|
+
* A settled event's `error` is carried as it is, the error `execute` rejects
|
|
45
|
+
* with. A credential under a name no pattern matches is carried as it is. The
|
|
45
46
|
* masked value is `REDACTED`.
|
|
46
47
|
* @public
|
|
47
48
|
*/
|
|
@@ -69,10 +70,11 @@ class Redactor {
|
|
|
69
70
|
#walk(value, seen) {
|
|
70
71
|
if (typeof value === 'string') return URL.canParse(value) ? this.#url(value) : value;
|
|
71
72
|
if (typeof value !== 'object' || value === null) return value;
|
|
72
|
-
// an ApiDOM element (a selected action) is the document's own,
|
|
73
|
-
// holds no credential
|
|
74
|
-
//
|
|
75
|
-
|
|
73
|
+
// an ApiDOM element (a selected action) is the document's own, a date
|
|
74
|
+
// holds no credential, and an error is the one `execute` rejects with,
|
|
75
|
+
// unmasked; any other object that is not plain data cannot be walked
|
|
76
|
+
// field by field.
|
|
77
|
+
if ((0, _apidomDatamodel.isElement)(value) || value instanceof Date || value instanceof Error) return value;
|
|
76
78
|
if (!Array.isArray(value) && !(0, _ramdaAdjunct.isPlainObject)(value)) return Redactor.CENSOR;
|
|
77
79
|
// only the objects on the current path: a shared, acyclic one is walked
|
|
78
80
|
// again where it reappears, a cycle is cut.
|
|
@@ -36,7 +36,8 @@ import { parse as parseMediaType } from '@swaggerexpert/media-types';
|
|
|
36
36
|
* - a request or response body that is JSON or a URL-encoded form, field by
|
|
37
37
|
* field, and any other body, or any object that is not plain data, whole.
|
|
38
38
|
*
|
|
39
|
-
* A
|
|
39
|
+
* A settled event's `error` is carried as it is, the error `execute` rejects
|
|
40
|
+
* with. A credential under a name no pattern matches is carried as it is. The
|
|
40
41
|
* masked value is `REDACTED`.
|
|
41
42
|
* @public
|
|
42
43
|
*/
|
|
@@ -64,10 +65,11 @@ class Redactor {
|
|
|
64
65
|
#walk(value, seen) {
|
|
65
66
|
if (typeof value === 'string') return URL.canParse(value) ? this.#url(value) : value;
|
|
66
67
|
if (typeof value !== 'object' || value === null) return value;
|
|
67
|
-
// an ApiDOM element (a selected action) is the document's own,
|
|
68
|
-
// holds no credential
|
|
69
|
-
//
|
|
70
|
-
|
|
68
|
+
// an ApiDOM element (a selected action) is the document's own, a date
|
|
69
|
+
// holds no credential, and an error is the one `execute` rejects with,
|
|
70
|
+
// unmasked; any other object that is not plain data cannot be walked
|
|
71
|
+
// field by field.
|
|
72
|
+
if (isElement(value) || value instanceof Date || value instanceof Error) return value;
|
|
71
73
|
if (!Array.isArray(value) && !isPlainObject(value)) return Redactor.CENSOR;
|
|
72
74
|
// only the objects on the current path: a shared, acyclic one is walked
|
|
73
75
|
// again where it reappears, a cycle is cut.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* One observer that hands each event to every observer given, in order, so
|
|
9
|
+
* several consumers (a log, a trace, a stream) can watch the same run.
|
|
10
|
+
*
|
|
11
|
+
* Delivery stays synchronous. An observer that throws aborts the run with its
|
|
12
|
+
* error, as any observer does, and the observers after it miss that event.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const combineObservers = (...observers) => event => {
|
|
16
|
+
for (const observer of observers) observer(event);
|
|
17
|
+
};
|
|
18
|
+
var _default = exports.default = combineObservers;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One observer that hands each event to every observer given, in order, so
|
|
3
|
+
* several consumers (a log, a trace, a stream) can watch the same run.
|
|
4
|
+
*
|
|
5
|
+
* Delivery stays synchronous. An observer that throws aborts the run with its
|
|
6
|
+
* error, as any observer does, and the observers after it miss that event.
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
const combineObservers = (...observers) => event => {
|
|
10
|
+
for (const observer of observers) observer(event);
|
|
11
|
+
};
|
|
12
|
+
export default combineObservers;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.traced = exports.notify = exports.isInstrumented = exports.default = void 0;
|
|
8
|
+
var _ExecutionError = _interopRequireDefault(require("../errors/ExecutionError.cjs"));
|
|
9
|
+
/**
|
|
10
|
+
* A workflow invocation an {@link InstrumentationWrap} runs around: its
|
|
11
|
+
* `workflow:start`, its steps and its `workflow:settled`.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A step an {@link InstrumentationWrap} runs around: its `step:start`, its
|
|
17
|
+
* attempts and requests, and its `step:settled`.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What an {@link InstrumentationWrap} runs around.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Runs around each workflow invocation and each step, so what `run` does
|
|
28
|
+
* happens inside it: in the active context of a span it opened, for one. It
|
|
29
|
+
* must call `run`, and should settle once `run` settles; a second call runs
|
|
30
|
+
* nothing again. What it resolves or rejects with does not change the run:
|
|
31
|
+
* the run's own outcome stands, once the run settled, and the operation's
|
|
32
|
+
* settled event says what it was.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Hooks that apply to every runner in the process, on top of the observer
|
|
38
|
+
* each was given.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
// on globalThis, so the ESM and CommonJS builds of the runner, or two copies
|
|
43
|
+
// of it, share one registry
|
|
44
|
+
const KEY = Symbol.for('@usearazzo/runner/observability/instrument');
|
|
45
|
+
const registry = () => {
|
|
46
|
+
const scope = globalThis;
|
|
47
|
+
scope[KEY] ??= {
|
|
48
|
+
hooks: []
|
|
49
|
+
};
|
|
50
|
+
return scope[KEY];
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Applies `hooks` to every runner in the process, until the returned
|
|
55
|
+
* function removes them: the seam process-wide tooling, such as the
|
|
56
|
+
* OpenTelemetry instrumentation, is built on. A run already in progress
|
|
57
|
+
* picks hooks up at its next event or operation, and drops removed ones the
|
|
58
|
+
* same way: a removed hook misses the settled events of what it saw start.
|
|
59
|
+
* Hooks apply in the order they were added, the first wrap outermost.
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
const instrument = hooks => {
|
|
63
|
+
const {
|
|
64
|
+
hooks: registered
|
|
65
|
+
} = registry();
|
|
66
|
+
registered.push(hooks);
|
|
67
|
+
return () => {
|
|
68
|
+
const index = registered.indexOf(hooks);
|
|
69
|
+
if (index !== -1) registered.splice(index, 1);
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether any process-wide observer is registered.
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
const isInstrumented = () => registry().hooks.some(hooks => hooks.observer);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Hands the event to every process-wide observer.
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
exports.isInstrumented = isInstrumented;
|
|
84
|
+
const notify = event => {
|
|
85
|
+
// a copy: an observer that removes hooks must not skip the next one
|
|
86
|
+
for (const {
|
|
87
|
+
observer
|
|
88
|
+
} of [...registry().hooks]) observer?.(event);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Runs `work` inside every registered wrap, returning what `work` resolves
|
|
93
|
+
* to or rejecting with what it rejects with, whatever a wrap settles with,
|
|
94
|
+
* and only once `work` settled. A wrap that throws before running `work`
|
|
95
|
+
* aborts it; one that throws after is ignored.
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
exports.notify = notify;
|
|
99
|
+
const traced = async (describe, work) => {
|
|
100
|
+
const wraps = registry().hooks.flatMap(({
|
|
101
|
+
wrap
|
|
102
|
+
}) => wrap === undefined ? [] : [wrap]);
|
|
103
|
+
// described only when a wrap is registered: an uninstrumented run builds nothing
|
|
104
|
+
if (wraps.length === 0) return work();
|
|
105
|
+
const operation = describe();
|
|
106
|
+
let outcome;
|
|
107
|
+
let running;
|
|
108
|
+
let abandoned = false;
|
|
109
|
+
const unrun = () => new _ExecutionError.default(`an instrumentation wrap settled without running the ${operation.kind}`, {
|
|
110
|
+
workflowId: operation.workflowId,
|
|
111
|
+
...(operation.kind === 'step' ? {
|
|
112
|
+
stepId: operation.stepId
|
|
113
|
+
} : {}),
|
|
114
|
+
reason: _ExecutionError.default.InstrumentationWrap
|
|
115
|
+
});
|
|
116
|
+
// once, however many times a wrap calls it
|
|
117
|
+
const innermost = () => {
|
|
118
|
+
// a wrap calling it after the run gave up on it: running the work then
|
|
119
|
+
// would act for a run that already rejected
|
|
120
|
+
if (abandoned) return Promise.reject(unrun());
|
|
121
|
+
if (running === undefined) {
|
|
122
|
+
running = (async () => {
|
|
123
|
+
try {
|
|
124
|
+
outcome = {
|
|
125
|
+
value: await work()
|
|
126
|
+
};
|
|
127
|
+
} catch (error) {
|
|
128
|
+
outcome = {
|
|
129
|
+
error
|
|
130
|
+
};
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
})();
|
|
134
|
+
// handled here too: a wrap that settles later than the work must not
|
|
135
|
+
// leave its rejection unhandled, which would end the process
|
|
136
|
+
running.catch(() => undefined);
|
|
137
|
+
}
|
|
138
|
+
return running;
|
|
139
|
+
};
|
|
140
|
+
const run = wraps.reduceRight((next, wrap) => () => wrap(operation, next), innermost);
|
|
141
|
+
try {
|
|
142
|
+
await run();
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (running === undefined) {
|
|
145
|
+
abandoned = true;
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// a wrap that settled without waiting for the work: the work still decides
|
|
150
|
+
if (running !== undefined) await running.catch(() => undefined);
|
|
151
|
+
if (outcome === undefined) {
|
|
152
|
+
abandoned = true;
|
|
153
|
+
throw unrun();
|
|
154
|
+
}
|
|
155
|
+
if ('error' in outcome) throw outcome.error;
|
|
156
|
+
return outcome.value;
|
|
157
|
+
};
|
|
158
|
+
exports.traced = traced;
|
|
159
|
+
var _default = exports.default = instrument;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import ExecutionError from "../errors/ExecutionError.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* A workflow invocation an {@link InstrumentationWrap} runs around: its
|
|
4
|
+
* `workflow:start`, its steps and its `workflow:settled`.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* A step an {@link InstrumentationWrap} runs around: its `step:start`, its
|
|
9
|
+
* attempts and requests, and its `step:settled`.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* What an {@link InstrumentationWrap} runs around.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Runs around each workflow invocation and each step, so what `run` does
|
|
18
|
+
* happens inside it: in the active context of a span it opened, for one. It
|
|
19
|
+
* must call `run`, and should settle once `run` settles; a second call runs
|
|
20
|
+
* nothing again. What it resolves or rejects with does not change the run:
|
|
21
|
+
* the run's own outcome stands, once the run settled, and the operation's
|
|
22
|
+
* settled event says what it was.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Hooks that apply to every runner in the process, on top of the observer
|
|
27
|
+
* each was given.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
// on globalThis, so the ESM and CommonJS builds of the runner, or two copies
|
|
31
|
+
// of it, share one registry
|
|
32
|
+
const KEY = Symbol.for('@usearazzo/runner/observability/instrument');
|
|
33
|
+
const registry = () => {
|
|
34
|
+
const scope = globalThis;
|
|
35
|
+
scope[KEY] ??= {
|
|
36
|
+
hooks: []
|
|
37
|
+
};
|
|
38
|
+
return scope[KEY];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Applies `hooks` to every runner in the process, until the returned
|
|
43
|
+
* function removes them: the seam process-wide tooling, such as the
|
|
44
|
+
* OpenTelemetry instrumentation, is built on. A run already in progress
|
|
45
|
+
* picks hooks up at its next event or operation, and drops removed ones the
|
|
46
|
+
* same way: a removed hook misses the settled events of what it saw start.
|
|
47
|
+
* Hooks apply in the order they were added, the first wrap outermost.
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
const instrument = hooks => {
|
|
51
|
+
const {
|
|
52
|
+
hooks: registered
|
|
53
|
+
} = registry();
|
|
54
|
+
registered.push(hooks);
|
|
55
|
+
return () => {
|
|
56
|
+
const index = registered.indexOf(hooks);
|
|
57
|
+
if (index !== -1) registered.splice(index, 1);
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether any process-wide observer is registered.
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
export const isInstrumented = () => registry().hooks.some(hooks => hooks.observer);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Hands the event to every process-wide observer.
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
export const notify = event => {
|
|
72
|
+
// a copy: an observer that removes hooks must not skip the next one
|
|
73
|
+
for (const {
|
|
74
|
+
observer
|
|
75
|
+
} of [...registry().hooks]) observer?.(event);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Runs `work` inside every registered wrap, returning what `work` resolves
|
|
80
|
+
* to or rejecting with what it rejects with, whatever a wrap settles with,
|
|
81
|
+
* and only once `work` settled. A wrap that throws before running `work`
|
|
82
|
+
* aborts it; one that throws after is ignored.
|
|
83
|
+
* @internal
|
|
84
|
+
*/
|
|
85
|
+
export const traced = async (describe, work) => {
|
|
86
|
+
const wraps = registry().hooks.flatMap(({
|
|
87
|
+
wrap
|
|
88
|
+
}) => wrap === undefined ? [] : [wrap]);
|
|
89
|
+
// described only when a wrap is registered: an uninstrumented run builds nothing
|
|
90
|
+
if (wraps.length === 0) return work();
|
|
91
|
+
const operation = describe();
|
|
92
|
+
let outcome;
|
|
93
|
+
let running;
|
|
94
|
+
let abandoned = false;
|
|
95
|
+
const unrun = () => new ExecutionError(`an instrumentation wrap settled without running the ${operation.kind}`, {
|
|
96
|
+
workflowId: operation.workflowId,
|
|
97
|
+
...(operation.kind === 'step' ? {
|
|
98
|
+
stepId: operation.stepId
|
|
99
|
+
} : {}),
|
|
100
|
+
reason: ExecutionError.InstrumentationWrap
|
|
101
|
+
});
|
|
102
|
+
// once, however many times a wrap calls it
|
|
103
|
+
const innermost = () => {
|
|
104
|
+
// a wrap calling it after the run gave up on it: running the work then
|
|
105
|
+
// would act for a run that already rejected
|
|
106
|
+
if (abandoned) return Promise.reject(unrun());
|
|
107
|
+
if (running === undefined) {
|
|
108
|
+
running = (async () => {
|
|
109
|
+
try {
|
|
110
|
+
outcome = {
|
|
111
|
+
value: await work()
|
|
112
|
+
};
|
|
113
|
+
} catch (error) {
|
|
114
|
+
outcome = {
|
|
115
|
+
error
|
|
116
|
+
};
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
})();
|
|
120
|
+
// handled here too: a wrap that settles later than the work must not
|
|
121
|
+
// leave its rejection unhandled, which would end the process
|
|
122
|
+
running.catch(() => undefined);
|
|
123
|
+
}
|
|
124
|
+
return running;
|
|
125
|
+
};
|
|
126
|
+
const run = wraps.reduceRight((next, wrap) => () => wrap(operation, next), innermost);
|
|
127
|
+
try {
|
|
128
|
+
await run();
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (running === undefined) {
|
|
131
|
+
abandoned = true;
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// a wrap that settled without waiting for the work: the work still decides
|
|
136
|
+
if (running !== undefined) await running.catch(() => undefined);
|
|
137
|
+
if (outcome === undefined) {
|
|
138
|
+
abandoned = true;
|
|
139
|
+
throw unrun();
|
|
140
|
+
}
|
|
141
|
+
if ('error' in outcome) throw outcome.error;
|
|
142
|
+
return outcome.value;
|
|
143
|
+
};
|
|
144
|
+
export default instrument;
|