@usearazzo/runner 1.0.1-alpha.33 → 1.0.1-alpha.35
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 +22 -0
- package/README.md +15 -1
- package/dist/arazzo-runner.browser.js +1516 -265
- package/dist/arazzo-runner.browser.min.js +1 -1
- package/package.json +64 -18
- package/src/ArazzoRunner.cjs +26 -3
- package/src/ArazzoRunner.mjs +26 -3
- package/src/client/OpenAPIOperationResponse.cjs +9 -0
- package/src/client/OpenAPIOperationResponse.mjs +9 -0
- package/src/document/APIDocument.cjs +53 -0
- package/src/document/APIDocument.mjs +53 -0
- package/src/errors/ExecutionError.cjs +2 -0
- package/src/errors/ExecutionError.mjs +2 -0
- package/src/executor/OpenAPIOperationExecutor.cjs +77 -6
- package/src/executor/OpenAPIOperationExecutor.mjs +77 -6
- package/src/executor/StepExecutor.cjs +16 -2
- package/src/executor/StepExecutor.mjs +16 -2
- package/src/executor/StepRetryRunner.cjs +5 -3
- package/src/executor/StepRetryRunner.mjs +5 -3
- package/src/executor/StepTransitionInterpreter.cjs +1 -1
- package/src/executor/StepTransitionInterpreter.mjs +1 -1
- package/src/executor/WorkflowCallStack.cjs +1 -1
- package/src/executor/WorkflowCallStack.mjs +1 -1
- package/src/executor/WorkflowExecutor.cjs +447 -89
- package/src/executor/WorkflowExecutor.mjs +447 -89
- package/src/index.cjs +21 -0
- package/src/index.mjs +4 -0
- package/src/observability/ArazzoRunnerEvent.cjs +5 -0
- package/src/observability/ArazzoRunnerEvent.mjs +1 -0
- package/src/observability/DocumentRegistryEvent.cjs +5 -0
- package/src/observability/DocumentRegistryEvent.mjs +1 -0
- package/src/observability/EventChannel.cjs +106 -0
- package/src/observability/EventChannel.mjs +98 -0
- package/src/observability/OpenAPIOperationEvent.cjs +5 -0
- package/src/observability/OpenAPIOperationEvent.mjs +1 -0
- package/src/observability/Redactor.cjs +215 -0
- package/src/observability/Redactor.mjs +210 -0
- package/src/observability/WorkflowExecutionEvent.cjs +5 -0
- package/src/observability/WorkflowExecutionEvent.mjs +1 -0
- 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 +27 -3
- package/src/registry/DocumentRegistry.mjs +27 -3
- package/src/registry/providers/ArazzoDocumentRegistryProvider.cjs +20 -0
- package/src/registry/providers/ArazzoDocumentRegistryProvider.mjs +21 -1
- package/src/registry/providers/DocumentRegistryProvider.cjs +14 -0
- package/src/registry/providers/DocumentRegistryProvider.mjs +14 -0
- package/src/registry/providers/OpenAPIDocumentRegistryProvider.cjs +27 -5
- package/src/registry/providers/OpenAPIDocumentRegistryProvider.mjs +27 -5
- package/src/version.cjs +9 -0
- package/src/version.mjs +3 -0
- package/types/observability/logging/LoggingObserver.d.ts +82 -0
- package/types/observability/logging/index.d.ts +2 -0
- package/types/observability/opentelemetry/ArazzoRunnerInstrumentation.d.ts +84 -0
- package/types/observability/opentelemetry/index.d.ts +2 -0
- package/types/observability/stream/EventStream.d.ts +68 -0
- package/types/observability/stream/index.d.ts +2 -0
- package/types/runner.d.ts +802 -5
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { isPlainObject } from 'ramda-adjunct';
|
|
2
|
+
import { isElement } from '@speclynx/apidom-datamodel';
|
|
3
|
+
import { parse as parseExpression } from '@swaggerexpert/arazzo-runtime-expression';
|
|
4
|
+
import { unescape as unescapeToken } from '@swaggerexpert/json-pointer';
|
|
5
|
+
import { parse as parseMediaType } from '@swaggerexpert/media-types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The parts of a parsed runtime expression that name what it reads.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A parsed media type, lowercased by the parser.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for the Redactor.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Masks credentials in events before they leave the process.
|
|
22
|
+
*
|
|
23
|
+
* Events carry what the run saw, credentials included, like the request a
|
|
24
|
+
* request interceptor receives. Pass every event through
|
|
25
|
+
* {@link Redactor.event} before logging, exporting or writing it.
|
|
26
|
+
*
|
|
27
|
+
* It masks by name, whatever the event:
|
|
28
|
+
*
|
|
29
|
+
* - the value of any field whose name matches a pattern: headers, query
|
|
30
|
+
* parameters, body and output fields, at any depth;
|
|
31
|
+
* - a criterion value whose runtime expression reads a matching name, in its
|
|
32
|
+
* base or in any token of its JSON Pointer (`$inputs.password`,
|
|
33
|
+
* `$steps.login.outputs.token#/value`);
|
|
34
|
+
* - in every absolute URL, as `URL` parses it, the userinfo and the matching
|
|
35
|
+
* query and fragment parameters;
|
|
36
|
+
* - a request or response body that is JSON or a URL-encoded form, field by
|
|
37
|
+
* field, and any other body, or any object that is not plain data, whole.
|
|
38
|
+
*
|
|
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
|
|
41
|
+
* masked value is `REDACTED`.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
class Redactor {
|
|
45
|
+
static CENSOR = 'REDACTED';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Elastic APM's `sanitizeFieldNames` defaults, with `*secret*` for its exact
|
|
49
|
+
* `secret` (which misses OAuth's `client_secret`), plus the signed-URL query
|
|
50
|
+
* parameters OpenTelemetry's HTTP conventions mask.
|
|
51
|
+
*/
|
|
52
|
+
static defaultFields = Object.freeze(['password', 'passwd', 'pwd', '*secret*', '*key', '*token*', '*session*', '*credit*', '*card*', '*auth*', 'set-cookie', '*principal*', 'pw', 'pass', 'connect.sid', 'cookie', 'AWSAccessKeyId', 'Signature', 'sig', 'X-Goog-Signature']);
|
|
53
|
+
#patterns;
|
|
54
|
+
#matches = new Map();
|
|
55
|
+
constructor(options = {}) {
|
|
56
|
+
this.#patterns = (options.fields ?? Redactor.defaultFields).map(Redactor.#compile);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A masked copy of the event.
|
|
61
|
+
*/
|
|
62
|
+
event(event) {
|
|
63
|
+
return this.#walk(event, new WeakSet());
|
|
64
|
+
}
|
|
65
|
+
#walk(value, seen) {
|
|
66
|
+
if (typeof value === 'string') return URL.canParse(value) ? this.#url(value) : value;
|
|
67
|
+
if (typeof value !== 'object' || value === null) return value;
|
|
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;
|
|
73
|
+
if (!Array.isArray(value) && !isPlainObject(value)) return Redactor.CENSOR;
|
|
74
|
+
// only the objects on the current path: a shared, acyclic one is walked
|
|
75
|
+
// again where it reappears, a cycle is cut.
|
|
76
|
+
if (seen.has(value)) return Redactor.CENSOR;
|
|
77
|
+
seen.add(value);
|
|
78
|
+
try {
|
|
79
|
+
return Array.isArray(value) ? value.map(item => this.#walk(item, seen)) : this.#fields(value, seen);
|
|
80
|
+
} finally {
|
|
81
|
+
seen.delete(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
#fields(object, seen) {
|
|
85
|
+
const redacted = {};
|
|
86
|
+
for (const [name, item] of Object.entries(object)) {
|
|
87
|
+
let masked;
|
|
88
|
+
if (this.#masks(name)) masked = Redactor.CENSOR;else if (name === 'body' && isPlainObject(object.headers)) {
|
|
89
|
+
masked = this.#body(item, object.headers, seen);
|
|
90
|
+
} else masked = this.#walk(item, seen);
|
|
91
|
+
// defined, not assigned, so an own `__proto__` key stays data
|
|
92
|
+
Object.defineProperty(redacted, name, {
|
|
93
|
+
value: masked,
|
|
94
|
+
enumerable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
configurable: true
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return redacted;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether a name matches a pattern. A runtime expression matches when any
|
|
104
|
+
* name it reads does.
|
|
105
|
+
*/
|
|
106
|
+
#masks(name) {
|
|
107
|
+
let matches = this.#matches.get(name);
|
|
108
|
+
if (matches === undefined) {
|
|
109
|
+
const names = name.startsWith('$') ? Redactor.#namesRead(name) : [name];
|
|
110
|
+
matches = names.some(candidate => this.#patterns.some(pattern => pattern.test(candidate)));
|
|
111
|
+
this.#matches.set(name, matches);
|
|
112
|
+
}
|
|
113
|
+
return matches;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The absolute URL with its userinfo and matching query and fragment
|
|
118
|
+
* parameters masked. A URL with nothing to mask comes back exactly as it
|
|
119
|
+
* was, not normalized.
|
|
120
|
+
*/
|
|
121
|
+
#url(value) {
|
|
122
|
+
const url = new URL(value);
|
|
123
|
+
let masked = false;
|
|
124
|
+
if (url.username !== '' || url.password !== '') {
|
|
125
|
+
url.username = Redactor.CENSOR;
|
|
126
|
+
url.password = Redactor.CENSOR;
|
|
127
|
+
masked = true;
|
|
128
|
+
}
|
|
129
|
+
const query = this.#params(url.search.slice(1));
|
|
130
|
+
if (query !== undefined) {
|
|
131
|
+
url.search = query;
|
|
132
|
+
masked = true;
|
|
133
|
+
}
|
|
134
|
+
const fragment = this.#params(url.hash.slice(1));
|
|
135
|
+
if (fragment !== undefined) {
|
|
136
|
+
url.hash = fragment;
|
|
137
|
+
masked = true;
|
|
138
|
+
}
|
|
139
|
+
return masked ? url.href : value;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A body masked by what its `Content-Type` says it is.
|
|
144
|
+
*/
|
|
145
|
+
#body(body, headers, seen) {
|
|
146
|
+
if (typeof body !== 'string' || body === '') return this.#walk(body, seen);
|
|
147
|
+
const mediaType = Redactor.#contentType(headers);
|
|
148
|
+
if (mediaType?.subtype === 'json' || mediaType?.suffix === 'json') {
|
|
149
|
+
try {
|
|
150
|
+
return JSON.stringify(this.#walk(JSON.parse(body), seen));
|
|
151
|
+
} catch {
|
|
152
|
+
return Redactor.CENSOR;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (mediaType?.type === 'application' && mediaType.subtype === 'x-www-form-urlencoded') {
|
|
156
|
+
return this.#params(body) ?? body;
|
|
157
|
+
}
|
|
158
|
+
return Redactor.CENSOR;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* URL-encoded `name=value` pairs with matching values masked, or
|
|
163
|
+
* `undefined` when none matches, so the caller keeps the text as it was.
|
|
164
|
+
*/
|
|
165
|
+
#params(text) {
|
|
166
|
+
let masked = false;
|
|
167
|
+
const params = [...new URLSearchParams(text)].map(([name, value]) => {
|
|
168
|
+
if (!this.#masks(name)) return [name, value];
|
|
169
|
+
masked = true;
|
|
170
|
+
return [name, Redactor.CENSOR];
|
|
171
|
+
});
|
|
172
|
+
return masked ? new URLSearchParams(params).toString() : undefined;
|
|
173
|
+
}
|
|
174
|
+
static #contentType(headers) {
|
|
175
|
+
const name = Object.keys(headers).find(key => key.toLowerCase() === 'content-type');
|
|
176
|
+
if (name === undefined) return undefined;
|
|
177
|
+
try {
|
|
178
|
+
return parseMediaType(headers[name], {
|
|
179
|
+
type: 'media-type'
|
|
180
|
+
}).tree;
|
|
181
|
+
} catch {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Every name a runtime expression reads: the header, query, path, input,
|
|
188
|
+
* output or component name, and each token of its JSON Pointer, e.g.
|
|
189
|
+
* `token` and `value` for `$steps.login.outputs.token#/value`. A string
|
|
190
|
+
* that does not parse as an expression is its own name.
|
|
191
|
+
*/
|
|
192
|
+
static #namesRead(expression) {
|
|
193
|
+
let tree;
|
|
194
|
+
try {
|
|
195
|
+
tree = parseExpression(expression).tree;
|
|
196
|
+
} catch {
|
|
197
|
+
return [expression];
|
|
198
|
+
}
|
|
199
|
+
if (tree === undefined) return [expression];
|
|
200
|
+
const reference = tree.source?.reference;
|
|
201
|
+
const tokens = (tree.jsonPointer ?? reference?.jsonPointer)?.referenceTokens ?? [];
|
|
202
|
+
const names = [reference?.token, reference?.name, tree.name, tree.outputName, tree.fieldName, tree.componentName, ...tokens.map(token => unescapeToken(token.value))];
|
|
203
|
+
return names.filter(name => name !== undefined);
|
|
204
|
+
}
|
|
205
|
+
static #compile(pattern) {
|
|
206
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
|
|
207
|
+
return new RegExp(`^${escaped}$`, 'i');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export default Redactor;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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;
|