mercury-composable 4.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +195 -0
- package/dist/src/actuator.d.ts +36 -0
- package/dist/src/actuator.js +295 -0
- package/dist/src/bus.d.ts +110 -0
- package/dist/src/bus.js +386 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +72 -0
- package/dist/src/client.d.ts +48 -0
- package/dist/src/client.js +433 -0
- package/dist/src/config.d.ts +27 -0
- package/dist/src/config.js +161 -0
- package/dist/src/default-log-context.yaml +19 -0
- package/dist/src/envelope.d.ts +44 -0
- package/dist/src/envelope.js +227 -0
- package/dist/src/event-stream.d.ts +120 -0
- package/dist/src/event-stream.js +359 -0
- package/dist/src/exceptions.d.ts +18 -0
- package/dist/src/exceptions.js +25 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +21 -0
- package/dist/src/log-context.d.ts +24 -0
- package/dist/src/log-context.js +172 -0
- package/dist/src/log.d.ts +14 -0
- package/dist/src/log.js +113 -0
- package/dist/src/registry.d.ts +64 -0
- package/dist/src/registry.js +80 -0
- package/dist/src/server.d.ts +43 -0
- package/dist/src/server.js +343 -0
- package/dist/src/trace.d.ts +39 -0
- package/dist/src/trace.js +73 -0
- package/dist/src/version.d.ts +2 -0
- package/dist/src/version.js +2 -0
- package/package.json +55 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimalist telemetry: distributed trace context for polyglot functions.
|
|
3
|
+
*
|
|
4
|
+
* The engines carry trace_id / trace_path / cid inside the event envelope.
|
|
5
|
+
* The server installs them in an AsyncLocalStorage around each handler call;
|
|
6
|
+
* annotations ride back on the reply envelope's `annotations` field.
|
|
7
|
+
*/
|
|
8
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
9
|
+
// The business correlation-id rides an engine-managed envelope tag - never an
|
|
10
|
+
// envelope header - and is injected into the receiving function's input header
|
|
11
|
+
// copy as a read-only view at delivery (the engines' WorkerHandler contract).
|
|
12
|
+
export const MY_CID_TAG = 'my_cid';
|
|
13
|
+
export const MY_CORRELATION_ID = 'my_correlation_id';
|
|
14
|
+
// The engines' RPC round-trip marker tag: an RPC leg emits no trace dataset
|
|
15
|
+
// (its metrics fold into the caller's view), so the client stamps it on
|
|
16
|
+
// request() calls and the bus honors it at delivery.
|
|
17
|
+
export const RPC_TAG = 'rpc';
|
|
18
|
+
// Reserved application log-context tokens (the engines' LogContext contract):
|
|
19
|
+
// resolved live per log line; a developer cannot override them via
|
|
20
|
+
// updateContext. The output key names in app-log-context.yaml are the
|
|
21
|
+
// operator's choice - this set governs the template tokens and developer API.
|
|
22
|
+
export const RESERVED_CONTEXT_TOKENS = new Set(['cid', 'traceId', 'tracePath', 'spanId', 'parentSpanId', 'service', 'utc']);
|
|
23
|
+
const storage = new AsyncLocalStorage();
|
|
24
|
+
/** The trace context of the event being handled, if any. */
|
|
25
|
+
export function getTrace() {
|
|
26
|
+
return storage.getStore();
|
|
27
|
+
}
|
|
28
|
+
/** Attach an annotation to the current trace (returned on the reply envelope). */
|
|
29
|
+
export function annotateTrace(key, value) {
|
|
30
|
+
const info = storage.getStore();
|
|
31
|
+
if (info) {
|
|
32
|
+
info.annotations[String(key)] = value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Add (or remove, when value is null/undefined) a custom key-value in the
|
|
37
|
+
* application log context - the engines' PostOffice.updateContext twin.
|
|
38
|
+
*
|
|
39
|
+
* The key-value is rendered into the "context" block of structured log output
|
|
40
|
+
* (log.format json/compact) when the app-log-context feature is enabled.
|
|
41
|
+
* Unlike annotateTrace (which feeds the distributed-trace telemetry), this is
|
|
42
|
+
* a logging-only sink. No-op outside a hosted request.
|
|
43
|
+
*
|
|
44
|
+
* @throws Error if key is one of the reserved context tokens
|
|
45
|
+
*/
|
|
46
|
+
export function updateContext(key, value) {
|
|
47
|
+
if (RESERVED_CONTEXT_TOKENS.has(key)) {
|
|
48
|
+
throw new Error(`Cannot override reserved log context key '${key}' - ` +
|
|
49
|
+
`reserved keys are ${[...RESERVED_CONTEXT_TOKENS].sort((a, b) => a.localeCompare(b)).join(', ')}`);
|
|
50
|
+
}
|
|
51
|
+
const info = storage.getStore();
|
|
52
|
+
if (!info) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
info.customContext ??= {};
|
|
56
|
+
if (value === null || value === undefined) {
|
|
57
|
+
delete info.customContext[key];
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
info.customContext[key] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Run fn under a trace context - the python trace_context twin. Useful for
|
|
65
|
+
* callers outside a hosted function (batch jobs, tests) whose PostOffice
|
|
66
|
+
* calls should carry a trace: the client inherits the context into the
|
|
67
|
+
* outbound envelope, including the business correlation-id as the
|
|
68
|
+
* engine-managed my_cid tag.
|
|
69
|
+
*/
|
|
70
|
+
export function runWithTrace(info, fn) {
|
|
71
|
+
// undefined detaches: long-lived internal tasks run with a clean context
|
|
72
|
+
return storage.run(info, fn);
|
|
73
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mercury-composable",
|
|
3
|
+
"version": "4.12.1",
|
|
4
|
+
"description": "Lightweight Event-over-HTTP function host and client for Mercury Composable",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"author": "Accenture Technology",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Accenture/mercury-nodejs.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://accenture.github.io/mercury-nodejs",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Accenture/mercury-nodejs/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"event-driven",
|
|
18
|
+
"composable",
|
|
19
|
+
"microservices",
|
|
20
|
+
"event-over-http",
|
|
21
|
+
"mercury"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=20"
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/src/index.js",
|
|
27
|
+
"types": "./dist/src/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/src/index.d.ts",
|
|
31
|
+
"default": "./dist/src/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"bin": {
|
|
35
|
+
"mercury-serve": "dist/src/cli.js"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist/src",
|
|
39
|
+
"LICENSE",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p . && node -e \"require('node:fs').cpSync('src/default-log-context.yaml','dist/src/default-log-context.yaml')\"",
|
|
44
|
+
"test": "npm run build && node --test dist/test/*.test.js",
|
|
45
|
+
"prepack": "npm run build"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@msgpack/msgpack": "^3.0.0",
|
|
49
|
+
"yaml": "^2.5.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^22.0.0",
|
|
53
|
+
"typescript": "^5.6.0"
|
|
54
|
+
}
|
|
55
|
+
}
|