@reventlessdev/reventless-core 3.0.0-alpha.325 → 3.0.0-alpha.327
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 +16 -0
- package/package.json +7 -7
- package/src/Message.res +46 -0
- package/src/Message.res.mjs +45 -0
- package/src/Projection.res +269 -131
- package/src/Projection.res.mjs +338 -224
- package/src/ViewChange.res +194 -0
- package/src/ViewChange.res.mjs +235 -0
- package/src/adapter/Runtime/AggregateRuntime_Builder_Common.res.mjs +5 -5
- package/src/adapter/Runtime/EventCollectorRuntime_Builder_Single.res.mjs +2 -2
- package/src/admin/Platform_Admin_Callback.res.mjs +1 -1
- package/src/components/Aggregate/Aggregate_Callback.res +93 -18
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +54 -32
- package/src/components/AutomationSlice/AutomationSlice_Builder.res.mjs +1 -1
- package/src/components/AutomationSlice/AutomationSlice_Callback.res +29 -9
- package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +35 -20
- package/src/components/CommandGenerator/CommandGenerator_Callback.res +47 -6
- package/src/components/CommandGenerator/CommandGenerator_Callback.res.mjs +41 -4
- package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
- package/src/components/CommandTopic/CommandTopic_Callback.res.mjs +1 -1
- package/src/components/CommandTopic/CommandTopic_Helpers.res +64 -26
- package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +37 -17
- package/src/components/CommandTopic/CommandTopic_Operations.res +2 -0
- package/src/components/CommandTopic/CommandTopic_Operations.res.mjs +2 -2
- package/src/components/Counter/Counter_Callback.res.mjs +3 -3
- package/src/components/Counter/Counter_Operations.res.mjs +4 -4
- package/src/components/DcbEventLog/DcbEventLog_Operations.res.mjs +3 -3
- package/src/components/EventLog/EventLog_Operations.res +1 -0
- package/src/components/EventLog/EventLog_Operations.res.mjs +3 -3
- package/src/components/EventMapper/EventMapper_Callback.res +4 -0
- package/src/components/EventMapper/EventMapper_Callback.res.mjs +13 -12
- package/src/components/EventTopic/EventTopic_Operations.res +2 -0
- package/src/components/EventTopic/EventTopic_Operations.res.mjs +2 -2
- package/src/components/Extension/Extension_Operations.res +4 -0
- package/src/components/Extension/Extension_Operations.res.mjs +8 -8
- package/src/components/ExtensionPoint/ExtensionPoint_Callback.res.mjs +2 -2
- package/src/components/ExtensionPoint/ExtensionPoint_Operations.res +2 -0
- package/src/components/ExtensionPoint/ExtensionPoint_Operations.res.mjs +9 -8
- package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res.mjs +1 -1
- package/src/components/InboundTranslationSlice/InboundTranslationSlice_Webhook.res.mjs +1 -1
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +7 -3
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +33 -12
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +30 -17
- package/src/components/ReadModel/ReadModel_Callback.res +24 -3
- package/src/components/ReadModel/ReadModel_Callback.res.mjs +15 -3
- package/src/components/SideEffectHandler/SideEffectHandler_Callback.res +2 -0
- package/src/components/SideEffectHandler/SideEffectHandler_Callback.res.mjs +6 -6
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +43 -7
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +38 -20
- package/src/components/StateViewSlice/StateViewSlice_Builder.res +16 -2
- package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +15 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res +35 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +12 -4
- package/src/plugin/component/Plugin_Callback.res.mjs +2 -2
- package/src/plugin/connect/PluginExtensionPoint_Plugin.res.mjs +6 -6
- package/src/util/CommandPublisher.res.mjs +9 -9
- package/src/util/EffectLogger.res +111 -42
- package/src/util/EffectLogger.res.mjs +72 -30
- package/src/util/FTPHandler.res.mjs +6 -6
- package/src/util/Logger.res +231 -35
- package/src/util/Logger.res.mjs +263 -42
- package/src/util/ScheduleOps.res.mjs +4 -4
- package/src/util/Util_AdapterRuntime.res.mjs +2 -2
- package/src/util/Util_Promise.res.mjs +1 -1
- package/src/util/Util_QueryDbRuntime.res.mjs +1 -1
- package/src/util/Validation.res.mjs +1 -1
- package/tests/commandgenerator/OwnerActingTest.res +3 -1
- package/tests/commandgenerator/OwnerStampingTest.res +65 -0
- package/tests/commandgenerator/OwnerStampingTest.res.mjs +62 -0
- package/tests/commandtopic/CommandOutcomeHookTest.res +99 -2
- package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +120 -0
- package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
- package/tests/logger/ComponentLineIdsTest.res +97 -0
- package/tests/logger/ComponentLineIdsTest.res.mjs +118 -0
- package/tests/logger/LogFormatTest.res.mjs +5 -5
- package/tests/logger/LoggerSinkTest.res +205 -0
- package/tests/logger/LoggerSinkTest.res.mjs +209 -0
- package/tests/projection/ViewChangeTest.res +173 -0
- package/tests/projection/ViewChangeTest.res.mjs +292 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Stream from "effect/Stream";
|
|
6
|
+
import * as Logger$ReventlessCore from "../../src/util/Logger.res.mjs";
|
|
7
|
+
import * as Runtime$ReventlessCore from "../../src/adapter/Runtime/Runtime.res.mjs";
|
|
8
|
+
import * as EffectLogger$ReventlessCore from "../../src/util/EffectLogger.res.mjs";
|
|
9
|
+
import * as AggregateFixtures$ReventlessCore from "../aggregate/AggregateFixtures.res.mjs";
|
|
10
|
+
|
|
11
|
+
async function capture(run) {
|
|
12
|
+
let lines = [];
|
|
13
|
+
let remove = Logger$ReventlessCore.addSink(r => {
|
|
14
|
+
lines.push(r);
|
|
15
|
+
});
|
|
16
|
+
await run();
|
|
17
|
+
remove();
|
|
18
|
+
return lines;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function field(r, k) {
|
|
22
|
+
return r.annotations[k];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function meta(msgId, correlationId, causationId) {
|
|
26
|
+
let newrecord = {...AggregateFixtures$ReventlessCore.testMeta};
|
|
27
|
+
newrecord.causationId = causationId;
|
|
28
|
+
newrecord.correlationId = correlationId;
|
|
29
|
+
newrecord.msgId = msgId;
|
|
30
|
+
return newrecord;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
globalThis.describe("the log call carries a message's ids", () => {
|
|
34
|
+
globalThis.test("a line on a fiber of its own keeps the ids it was given", async () => {
|
|
35
|
+
let lines = await capture(async () => Effect.runSync(EffectLogger$ReventlessCore.logInfo("Aggregate(Order)", undefined, EffectLogger$ReventlessCore.idsOf("o-1", "Order", meta("m-1", "flow-1", "e-0")), "deciding")));
|
|
36
|
+
let line = Stdlib_Option.getOrThrow(lines.find(r => r.message === "deciding"), undefined);
|
|
37
|
+
globalThis.expect([
|
|
38
|
+
"msgId",
|
|
39
|
+
"correlationId",
|
|
40
|
+
"causationId",
|
|
41
|
+
"entityId",
|
|
42
|
+
"entityType"
|
|
43
|
+
].map(k => line.annotations[k])).toEqual([
|
|
44
|
+
"m-1",
|
|
45
|
+
"flow-1",
|
|
46
|
+
"e-0",
|
|
47
|
+
"o-1",
|
|
48
|
+
"Order"
|
|
49
|
+
]);
|
|
50
|
+
});
|
|
51
|
+
globalThis.test("the message's flow wins over the one the invocation annotated", async () => {
|
|
52
|
+
let lines = await capture(async () => Effect.runSync(Runtime$ReventlessCore.annotateInvocation(EffectLogger$ReventlessCore.logInfo(undefined, undefined, EffectLogger$ReventlessCore.idsOf(undefined, undefined, meta("m-2", "flow-2", undefined)), "deciding"), "flow-of-the-batch", undefined, "Aggregate(Order)", undefined, undefined, undefined, undefined, undefined)));
|
|
53
|
+
let line = Stdlib_Option.getOrThrow(lines.find(r => r.message === "deciding"), undefined);
|
|
54
|
+
globalThis.expect(line.annotations["correlationId"]).toEqual("flow-2");
|
|
55
|
+
globalThis.expect(line.comp).toEqual("Aggregate(Order)");
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
globalThis.describe("component lines in a batch of several flows", () => {
|
|
60
|
+
globalThis.beforeEach(() => AggregateFixtures$ReventlessCore.mock.reset());
|
|
61
|
+
globalThis.test("each command's aggregate lines carry its own ids and its entity", async () => {
|
|
62
|
+
let item = (reference, flow) => {
|
|
63
|
+
let i = AggregateFixtures$ReventlessCore.makeTopicItem(undefined, reference, {
|
|
64
|
+
TAG: "Create",
|
|
65
|
+
name: "Widget"
|
|
66
|
+
});
|
|
67
|
+
let init = i.command;
|
|
68
|
+
return {
|
|
69
|
+
command: {
|
|
70
|
+
id: init.id,
|
|
71
|
+
meta: meta(`m-` + reference, flow, `e-` + reference),
|
|
72
|
+
command: init.command
|
|
73
|
+
},
|
|
74
|
+
reference: i.reference
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
let lines = await capture(async () => {
|
|
78
|
+
await Effect.runPromise(AggregateFixtures$ReventlessCore.TestHandler.handleCommands(Stream.fromIterable([
|
|
79
|
+
item("1", "flow-a"),
|
|
80
|
+
item("2", "flow-b")
|
|
81
|
+
])));
|
|
82
|
+
});
|
|
83
|
+
let deciding = lines.filter(r => r.message.startsWith("handling command"));
|
|
84
|
+
globalThis.expect(deciding.map(r => [
|
|
85
|
+
r.annotations["msgId"],
|
|
86
|
+
r.annotations["correlationId"]
|
|
87
|
+
])).toEqual([
|
|
88
|
+
[
|
|
89
|
+
"m-1",
|
|
90
|
+
"flow-a"
|
|
91
|
+
],
|
|
92
|
+
[
|
|
93
|
+
"m-2",
|
|
94
|
+
"flow-b"
|
|
95
|
+
]
|
|
96
|
+
]);
|
|
97
|
+
globalThis.expect(deciding.map(r => [
|
|
98
|
+
r.annotations["entityId"],
|
|
99
|
+
r.annotations["entityType"]
|
|
100
|
+
])).toEqual([
|
|
101
|
+
[
|
|
102
|
+
"agg-1",
|
|
103
|
+
"TestAggregate"
|
|
104
|
+
],
|
|
105
|
+
[
|
|
106
|
+
"agg-1",
|
|
107
|
+
"TestAggregate"
|
|
108
|
+
]
|
|
109
|
+
]);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
export {
|
|
114
|
+
capture,
|
|
115
|
+
field,
|
|
116
|
+
meta,
|
|
117
|
+
}
|
|
118
|
+
/* Not a pure module */
|
|
@@ -260,7 +260,7 @@ globalThis.describe("JSON sink", () => {
|
|
|
260
260
|
globalThis.expect(lines.length === 1 && Primitive_object.equal(fieldOf(line, "plugin"), "Billing")).toBe(true);
|
|
261
261
|
});
|
|
262
262
|
globalThis.test("dispatch annotation carries comp onto a handler line that passes none", () => {
|
|
263
|
-
let lines = captureLogs(() => Effect.runSync(Runtime$ReventlessCore.annotateInvocation(EffectLogger$ReventlessCore.logInfo(undefined, undefined, "projected"), "cid-1", "parent-1", "EventCollector(ProductsReadModel)", undefined, undefined, undefined, undefined, undefined)));
|
|
263
|
+
let lines = captureLogs(() => Effect.runSync(Runtime$ReventlessCore.annotateInvocation(EffectLogger$ReventlessCore.logInfo(undefined, undefined, undefined, "projected"), "cid-1", "parent-1", "EventCollector(ProductsReadModel)", undefined, undefined, undefined, undefined, undefined)));
|
|
264
264
|
let line = lines.length >= 1 ? lines[0] : "";
|
|
265
265
|
let ok = Primitive_object.equal(fieldOf(line, "comp"), "EventCollector(ProductsReadModel)") && Primitive_object.equal(fieldOf(line, "correlationId"), "cid-1") && Primitive_object.equal(fieldOf(line, "causationId"), "parent-1") && Primitive_object.equal(fieldOf(line, "message"), "projected");
|
|
266
266
|
globalThis.expect(ok).toBe(true);
|
|
@@ -274,12 +274,12 @@ globalThis.describe("JSON sink", () => {
|
|
|
274
274
|
globalThis.expect(lines.length === 1 && isCleanRecord(lines[0])).toBe(true);
|
|
275
275
|
});
|
|
276
276
|
globalThis.test("EffectLogger.logInfo emits clean JSON through install()", () => {
|
|
277
|
-
let lines = captureLogs(() => Effect.runSync(EffectLogger$ReventlessCore.logInfo("Aggregate(Product)", undefined, "replay done")));
|
|
277
|
+
let lines = captureLogs(() => Effect.runSync(EffectLogger$ReventlessCore.logInfo("Aggregate(Product)", undefined, undefined, "replay done")));
|
|
278
278
|
let allClean = lines.filter(l => !isCleanRecord(l)).length === 0;
|
|
279
279
|
globalThis.expect(lines.length >= 1 && allClean).toBe(true);
|
|
280
280
|
});
|
|
281
281
|
globalThis.test("EffectLogger.logInfo carries ~comp + ~detail as structured fields (no \\x00)", () => {
|
|
282
|
-
let lines = captureLogs(() => Effect.runSync(EffectLogger$ReventlessCore.logInfo("Aggregate(Product)", JSON.parse(`{"id":"p-1"}`), "added")));
|
|
282
|
+
let lines = captureLogs(() => Effect.runSync(EffectLogger$ReventlessCore.logInfo("Aggregate(Product)", JSON.parse(`{"id":"p-1"}`), undefined, "added")));
|
|
283
283
|
let ok = lines.some(line => {
|
|
284
284
|
let obj = Stdlib_JSON.Decode.object(JSON.parse(line));
|
|
285
285
|
if (obj !== undefined && Primitive_object.equal(Stdlib_Option.flatMap(obj["comp"], Stdlib_JSON.Decode.string), "Aggregate(Product)")) {
|
|
@@ -291,11 +291,11 @@ globalThis.describe("JSON sink", () => {
|
|
|
291
291
|
globalThis.expect(ok).toBe(true);
|
|
292
292
|
});
|
|
293
293
|
globalThis.test("Effect.annotateLogs(correlationId) surfaces as a top-level field", () => {
|
|
294
|
-
let lines = captureLogs(() => Effect.runSync(Effect.annotateLogs(EffectLogger$ReventlessCore.logInfo("Aggregate(Product)", undefined, "handling"), "correlationId", "c-123")));
|
|
294
|
+
let lines = captureLogs(() => Effect.runSync(Effect.annotateLogs(EffectLogger$ReventlessCore.logInfo("Aggregate(Product)", undefined, undefined, "handling"), "correlationId", "c-123")));
|
|
295
295
|
globalThis.expect(lines.some(line => Primitive_object.equal(fieldOf(line, "correlationId"), "c-123"))).toBe(true);
|
|
296
296
|
});
|
|
297
297
|
globalThis.test("Effect.annotateLogs(plugin) overrides registry resolution", () => {
|
|
298
|
-
let lines = captureLogs(() => Effect.runSync(Effect.annotateLogs(EffectLogger$ReventlessCore.logInfo("Aggregate(Unregistered)", undefined, "handling"), "plugin", "Ordering")));
|
|
298
|
+
let lines = captureLogs(() => Effect.runSync(Effect.annotateLogs(EffectLogger$ReventlessCore.logInfo("Aggregate(Unregistered)", undefined, undefined, "handling"), "plugin", "Ordering")));
|
|
299
299
|
globalThis.expect(lines.some(line => Primitive_object.equal(fieldOf(line, "plugin"), "Ordering"))).toBe(true);
|
|
300
300
|
});
|
|
301
301
|
globalThis.test("every JSON record carries an RFC 3339 time field", () => {
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// A registered sink receives every record that passed its level, as the JSON
|
|
2
|
+
// line prints it, and a level set while running decides what passes.
|
|
3
|
+
|
|
4
|
+
open JestGlobals
|
|
5
|
+
|
|
6
|
+
type consoleObj = {mutable log: string => unit, mutable error: string => unit}
|
|
7
|
+
@val external _console: consoleObj = "console"
|
|
8
|
+
|
|
9
|
+
let capture = (fn: unit => unit): (array<string>, array<string>) => {
|
|
10
|
+
let out: array<string> = []
|
|
11
|
+
let err: array<string> = []
|
|
12
|
+
let originalLog = _console.log
|
|
13
|
+
let originalError = _console.error
|
|
14
|
+
_console.log = s => out->Array.push(s)
|
|
15
|
+
_console.error = s => err->Array.push(s)
|
|
16
|
+
let result = try {
|
|
17
|
+
fn()
|
|
18
|
+
Ok()
|
|
19
|
+
} catch {
|
|
20
|
+
| e => Error(e)
|
|
21
|
+
}
|
|
22
|
+
_console.log = originalLog
|
|
23
|
+
_console.error = originalError
|
|
24
|
+
switch result {
|
|
25
|
+
| Ok() => (out, err)
|
|
26
|
+
| Error(e) => throw(e)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let collecting = (): (array<Logger.record>, unit => unit) => {
|
|
31
|
+
let got = []
|
|
32
|
+
let remove = Logger.addSink(r => got->Array.push(r))
|
|
33
|
+
(got, remove)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("Logger sinks", () => {
|
|
37
|
+
beforeAll(() => {
|
|
38
|
+
NodeProcess.env->Dict.set("REVENTLESS_LOG_FORMAT", "json")
|
|
39
|
+
Reventless.AnsiStyle.reload()
|
|
40
|
+
})
|
|
41
|
+
afterAll(() => {
|
|
42
|
+
NodeProcess.env->Dict.set("REVENTLESS_LOG_FORMAT", "text")
|
|
43
|
+
Reventless.AnsiStyle.reload()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
testSync("a sink's record has the JSON line's fields, field for field", () => {
|
|
47
|
+
let (got, remove) = collecting()
|
|
48
|
+
let detail = Dict.fromArray([("orderId", JSON.Encode.string("o-1"))])->JSON.Encode.object
|
|
49
|
+
let (lines, _) = capture(
|
|
50
|
+
() =>
|
|
51
|
+
Logger.emit(
|
|
52
|
+
~level=Warn,
|
|
53
|
+
~comp="Aggregate(Order)",
|
|
54
|
+
~plugin="Ordering",
|
|
55
|
+
~detail,
|
|
56
|
+
~annotations=Dict.fromArray([("correlationId", "c-1")]),
|
|
57
|
+
"refused",
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
remove()
|
|
61
|
+
expect(got->Array.length)->toBe(1)
|
|
62
|
+
expect(lines->Array.length)->toBe(1)
|
|
63
|
+
let record = got->Array.getUnsafe(0)
|
|
64
|
+
expect(lines->Array.getUnsafe(0)->JSON.parseOrThrow)->toEqual(record->Logger.recordToJson)
|
|
65
|
+
expect(record.plugin)->toEqual(Some("Ordering"))
|
|
66
|
+
expect(record.annotations->Dict.get("correlationId"))->toEqual(Some("c-1"))
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
testSync("the record's detail is redacted before a sink sees it", () => {
|
|
70
|
+
let (got, remove) = collecting()
|
|
71
|
+
let detail =
|
|
72
|
+
Dict.fromArray([
|
|
73
|
+
("name", Dict.fromArray([("$pii", JSON.Encode.string("Ada Secret"))])->JSON.Encode.object),
|
|
74
|
+
])->JSON.Encode.object
|
|
75
|
+
let _ = capture(() => Logger.emit(~level=Info, ~comp="Platform", ~detail, "joined"))
|
|
76
|
+
remove()
|
|
77
|
+
let printed = got->Array.getUnsafe(0)->Logger.recordToJson->JSON.stringify
|
|
78
|
+
expect(printed->String.includes("Ada Secret"))->toBe(false)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
testSync("a sink that throws is noted once, and the others still get every record", () => {
|
|
82
|
+
let removeBad = Logger.addSink(_ => throw(Failure("boom")))
|
|
83
|
+
let (got, remove) = collecting()
|
|
84
|
+
let (_, errors) = capture(
|
|
85
|
+
() => {
|
|
86
|
+
Logger.emit(~level=Info, ~comp="Platform", "one")
|
|
87
|
+
Logger.emit(~level=Info, ~comp="Platform", "two")
|
|
88
|
+
},
|
|
89
|
+
)
|
|
90
|
+
removeBad()
|
|
91
|
+
remove()
|
|
92
|
+
expect(got->Array.map(r => r.message))->toEqual(["one", "two"])
|
|
93
|
+
expect(errors->Array.filter(e => e->String.includes("log sink"))->Array.length)->toBe(1)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
testSync("a removed sink gets nothing more", () => {
|
|
97
|
+
let (got, remove) = collecting()
|
|
98
|
+
remove()
|
|
99
|
+
let _ = capture(() => Logger.emit(~level=Info, ~comp="Platform", "after"))
|
|
100
|
+
expect(got->Array.length)->toBe(0)
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
describe("Logger levels set while running", () => {
|
|
105
|
+
afterEach(() => Logger.clearLevels())
|
|
106
|
+
|
|
107
|
+
// LOG_LEVEL is read live; each case sets the base it needs.
|
|
108
|
+
let withLogLevel = (value: string, fn: unit => unit) => {
|
|
109
|
+
let before = NodeProcess.env->Dict.get("LOG_LEVEL")
|
|
110
|
+
NodeProcess.env->Dict.set("LOG_LEVEL", value)
|
|
111
|
+
fn()
|
|
112
|
+
switch before {
|
|
113
|
+
| Some(v) => NodeProcess.env->Dict.set("LOG_LEVEL", v)
|
|
114
|
+
| None => NodeProcess.env->Dict.delete("LOG_LEVEL")
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
testSync(
|
|
119
|
+
"the component's level wins over the plugin's, the plugin's over the default, the default over LOG_LEVEL",
|
|
120
|
+
() =>
|
|
121
|
+
withLogLevel(
|
|
122
|
+
"error",
|
|
123
|
+
() => {
|
|
124
|
+
Reventless.LogPrefix.registerComponentPlugin(
|
|
125
|
+
~componentName="Order",
|
|
126
|
+
~pluginName="Ordering",
|
|
127
|
+
)
|
|
128
|
+
let comp = "Aggregate(Order)"
|
|
129
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Error))
|
|
130
|
+
Logger.setLevel(Some(Logger.Warn))
|
|
131
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Warn))
|
|
132
|
+
Logger.setLevel(~plugin="Ordering", Some(Logger.Info))
|
|
133
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Info))
|
|
134
|
+
expect(Logger.minLevelFor(~comp="Aggregate(Unrelated)", ()))->toEqual(Some(Logger.Warn))
|
|
135
|
+
Logger.setLevel(~comp, Some(Logger.Debug))
|
|
136
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Debug))
|
|
137
|
+
// Cleared, the next wider scope applies again.
|
|
138
|
+
Logger.setLevel(~comp, None)
|
|
139
|
+
expect(Logger.minLevelFor(~comp, ()))->toEqual(Some(Logger.Info))
|
|
140
|
+
},
|
|
141
|
+
),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
testSync("a component of one plugin wins over the component everywhere", () => {
|
|
145
|
+
Logger.setLevel(~comp="Projection", Some(Logger.Warn))
|
|
146
|
+
Logger.setLevel(~plugin="Ordering", ~comp="Projection", Some(Logger.Debug))
|
|
147
|
+
expect(Logger.minLevelFor(~plugin="Ordering", ~comp="Projection", ()))->toEqual(
|
|
148
|
+
Some(Logger.Debug),
|
|
149
|
+
)
|
|
150
|
+
expect(Logger.minLevelFor(~plugin="Catalog", ~comp="Projection", ()))->toEqual(
|
|
151
|
+
Some(Logger.Warn),
|
|
152
|
+
)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
testSync("a line below its level never reaches a sink; raised, it does", () =>
|
|
156
|
+
withLogLevel(
|
|
157
|
+
"info",
|
|
158
|
+
() => {
|
|
159
|
+
let log = Logger.fromEnv()
|
|
160
|
+
let (got, remove) = collecting()
|
|
161
|
+
let _ = capture(
|
|
162
|
+
() => {
|
|
163
|
+
log.debug(~comp="Quiet", "not yet")
|
|
164
|
+
Logger.setLevel(~comp="Quiet", Some(Logger.Debug))
|
|
165
|
+
log.debug(~comp="Quiet", "now")
|
|
166
|
+
log.debug(~comp="Other", "still not")
|
|
167
|
+
Logger.setLevel(~comp="Loud", Some(Logger.Error))
|
|
168
|
+
log.warn(~comp="Loud", "dropped")
|
|
169
|
+
},
|
|
170
|
+
)
|
|
171
|
+
remove()
|
|
172
|
+
expect(got->Array.map(r => r.message))->toEqual(["now"])
|
|
173
|
+
},
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
testSync("the Effect helpers follow a component's level", () =>
|
|
178
|
+
withLogLevel(
|
|
179
|
+
"info",
|
|
180
|
+
() => {
|
|
181
|
+
let (got, remove) = collecting()
|
|
182
|
+
let _ = capture(
|
|
183
|
+
() => {
|
|
184
|
+
EffectLogger.logDebug(~comp="Quiet", "not yet")->Effect.runSync
|
|
185
|
+
Logger.setLevel(~comp="Quiet", Some(Logger.Debug))
|
|
186
|
+
EffectLogger.logDebug(~comp="Quiet", "now")->Effect.runSync
|
|
187
|
+
Logger.setLevel(~comp="Loud", Some(Logger.Error))
|
|
188
|
+
EffectLogger.logWarn(~comp="Loud", "dropped")->Effect.runSync
|
|
189
|
+
},
|
|
190
|
+
)
|
|
191
|
+
remove()
|
|
192
|
+
expect(got->Array.map(r => r.message))->toEqual(["now"])
|
|
193
|
+
},
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
testSync("levelSettings lists what was set", () => {
|
|
198
|
+
Logger.setLevel(Some(Logger.Warn))
|
|
199
|
+
Logger.setLevel(~plugin="Ordering", ~comp="Projection", Some(Logger.Debug))
|
|
200
|
+
expect(Logger.levelSettings())->toEqual([
|
|
201
|
+
{Logger.level: Logger.Warn},
|
|
202
|
+
{plugin: "Ordering", comp: "Projection", level: Logger.Debug},
|
|
203
|
+
])
|
|
204
|
+
})
|
|
205
|
+
})
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as AnsiStyle$Reventless from "@reventlessdev/reventless-spec/src/AnsiStyle.res.mjs";
|
|
6
|
+
import * as LogPrefix$Reventless from "@reventlessdev/reventless-spec/src/LogPrefix.res.mjs";
|
|
7
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
8
|
+
import * as Logger$ReventlessCore from "../../src/util/Logger.res.mjs";
|
|
9
|
+
import * as EffectLogger$ReventlessCore from "../../src/util/EffectLogger.res.mjs";
|
|
10
|
+
|
|
11
|
+
function capture(fn) {
|
|
12
|
+
let out = [];
|
|
13
|
+
let err = [];
|
|
14
|
+
let originalLog = console.log;
|
|
15
|
+
let originalError = console.error;
|
|
16
|
+
console.log = s => {
|
|
17
|
+
out.push(s);
|
|
18
|
+
};
|
|
19
|
+
console.error = s => {
|
|
20
|
+
err.push(s);
|
|
21
|
+
};
|
|
22
|
+
let result;
|
|
23
|
+
try {
|
|
24
|
+
fn();
|
|
25
|
+
result = {
|
|
26
|
+
TAG: "Ok",
|
|
27
|
+
_0: undefined
|
|
28
|
+
};
|
|
29
|
+
} catch (raw_e) {
|
|
30
|
+
let e = Primitive_exceptions.internalToException(raw_e);
|
|
31
|
+
result = {
|
|
32
|
+
TAG: "Error",
|
|
33
|
+
_0: e
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
console.log = originalLog;
|
|
37
|
+
console.error = originalError;
|
|
38
|
+
if (result.TAG === "Ok") {
|
|
39
|
+
return [
|
|
40
|
+
out,
|
|
41
|
+
err
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
throw result._0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function collecting() {
|
|
48
|
+
let got = [];
|
|
49
|
+
let remove = Logger$ReventlessCore.addSink(r => {
|
|
50
|
+
got.push(r);
|
|
51
|
+
});
|
|
52
|
+
return [
|
|
53
|
+
got,
|
|
54
|
+
remove
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
globalThis.describe("Logger sinks", () => {
|
|
59
|
+
globalThis.beforeAll(() => {
|
|
60
|
+
process.env["REVENTLESS_LOG_FORMAT"] = "json";
|
|
61
|
+
AnsiStyle$Reventless.reload();
|
|
62
|
+
});
|
|
63
|
+
globalThis.afterAll(() => {
|
|
64
|
+
process.env["REVENTLESS_LOG_FORMAT"] = "text";
|
|
65
|
+
AnsiStyle$Reventless.reload();
|
|
66
|
+
});
|
|
67
|
+
globalThis.test("a sink's record has the JSON line's fields, field for field", () => {
|
|
68
|
+
let match = collecting();
|
|
69
|
+
let got = match[0];
|
|
70
|
+
let detail = Object.fromEntries([[
|
|
71
|
+
"orderId",
|
|
72
|
+
"o-1"
|
|
73
|
+
]]);
|
|
74
|
+
let match$1 = capture(() => Logger$ReventlessCore.emit("Warn", "Aggregate(Order)", "Ordering", detail, Object.fromEntries([[
|
|
75
|
+
"correlationId",
|
|
76
|
+
"c-1"
|
|
77
|
+
]]), "refused"));
|
|
78
|
+
let lines = match$1[0];
|
|
79
|
+
match[1]();
|
|
80
|
+
globalThis.expect(got.length).toBe(1);
|
|
81
|
+
globalThis.expect(lines.length).toBe(1);
|
|
82
|
+
let record = got[0];
|
|
83
|
+
globalThis.expect(JSON.parse(lines[0])).toEqual(Logger$ReventlessCore.recordToJson(record));
|
|
84
|
+
globalThis.expect(record.plugin).toEqual("Ordering");
|
|
85
|
+
globalThis.expect(record.annotations["correlationId"]).toEqual("c-1");
|
|
86
|
+
});
|
|
87
|
+
globalThis.test("the record's detail is redacted before a sink sees it", () => {
|
|
88
|
+
let match = collecting();
|
|
89
|
+
let detail = Object.fromEntries([[
|
|
90
|
+
"name",
|
|
91
|
+
Object.fromEntries([[
|
|
92
|
+
"$pii",
|
|
93
|
+
"Ada Secret"
|
|
94
|
+
]])
|
|
95
|
+
]]);
|
|
96
|
+
capture(() => Logger$ReventlessCore.emit("Info", "Platform", undefined, detail, undefined, "joined"));
|
|
97
|
+
match[1]();
|
|
98
|
+
let printed = JSON.stringify(Logger$ReventlessCore.recordToJson(match[0][0]));
|
|
99
|
+
globalThis.expect(printed.includes("Ada Secret")).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
globalThis.test("a sink that throws is noted once, and the others still get every record", () => {
|
|
102
|
+
let removeBad = Logger$ReventlessCore.addSink(param => {
|
|
103
|
+
throw {
|
|
104
|
+
RE_EXN_ID: "Failure",
|
|
105
|
+
_1: "boom",
|
|
106
|
+
Error: new Error()
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
let match = collecting();
|
|
110
|
+
let match$1 = capture(() => {
|
|
111
|
+
Logger$ReventlessCore.emit("Info", "Platform", undefined, undefined, undefined, "one");
|
|
112
|
+
Logger$ReventlessCore.emit("Info", "Platform", undefined, undefined, undefined, "two");
|
|
113
|
+
});
|
|
114
|
+
removeBad();
|
|
115
|
+
match[1]();
|
|
116
|
+
globalThis.expect(match[0].map(r => r.message)).toEqual([
|
|
117
|
+
"one",
|
|
118
|
+
"two"
|
|
119
|
+
]);
|
|
120
|
+
globalThis.expect(match$1[1].filter(e => e.includes("log sink")).length).toBe(1);
|
|
121
|
+
});
|
|
122
|
+
globalThis.test("a removed sink gets nothing more", () => {
|
|
123
|
+
let match = collecting();
|
|
124
|
+
match[1]();
|
|
125
|
+
capture(() => Logger$ReventlessCore.emit("Info", "Platform", undefined, undefined, undefined, "after"));
|
|
126
|
+
globalThis.expect(match[0].length).toBe(0);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
globalThis.describe("Logger levels set while running", () => {
|
|
131
|
+
globalThis.afterEach(() => Logger$ReventlessCore.clearLevels());
|
|
132
|
+
let withLogLevel = (value, fn) => {
|
|
133
|
+
let before = process.env["LOG_LEVEL"];
|
|
134
|
+
process.env["LOG_LEVEL"] = value;
|
|
135
|
+
fn();
|
|
136
|
+
if (before !== undefined) {
|
|
137
|
+
process.env["LOG_LEVEL"] = before;
|
|
138
|
+
return;
|
|
139
|
+
} else {
|
|
140
|
+
return Stdlib_Dict.$$delete(process.env, "LOG_LEVEL");
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
globalThis.test("the component's level wins over the plugin's, the plugin's over the default, the default over LOG_LEVEL", () => withLogLevel("error", () => {
|
|
144
|
+
LogPrefix$Reventless.registerComponentPlugin("Order", "Ordering");
|
|
145
|
+
let comp = "Aggregate(Order)";
|
|
146
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Error");
|
|
147
|
+
Logger$ReventlessCore.setLevel(undefined, undefined, "Warn");
|
|
148
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Warn");
|
|
149
|
+
Logger$ReventlessCore.setLevel("Ordering", undefined, "Info");
|
|
150
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Info");
|
|
151
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, "Aggregate(Unrelated)", undefined)).toEqual("Warn");
|
|
152
|
+
Logger$ReventlessCore.setLevel(undefined, comp, "Debug");
|
|
153
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Debug");
|
|
154
|
+
Logger$ReventlessCore.setLevel(undefined, comp, undefined);
|
|
155
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor(undefined, comp, undefined)).toEqual("Info");
|
|
156
|
+
}));
|
|
157
|
+
globalThis.test("a component of one plugin wins over the component everywhere", () => {
|
|
158
|
+
Logger$ReventlessCore.setLevel(undefined, "Projection", "Warn");
|
|
159
|
+
Logger$ReventlessCore.setLevel("Ordering", "Projection", "Debug");
|
|
160
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor("Ordering", "Projection", undefined)).toEqual("Debug");
|
|
161
|
+
globalThis.expect(Logger$ReventlessCore.minLevelFor("Catalog", "Projection", undefined)).toEqual("Warn");
|
|
162
|
+
});
|
|
163
|
+
globalThis.test("a line below its level never reaches a sink; raised, it does", () => withLogLevel("info", () => {
|
|
164
|
+
let log = Logger$ReventlessCore.fromEnv();
|
|
165
|
+
let match = collecting();
|
|
166
|
+
capture(() => {
|
|
167
|
+
log.debug("Quiet", undefined, "not yet");
|
|
168
|
+
Logger$ReventlessCore.setLevel(undefined, "Quiet", "Debug");
|
|
169
|
+
log.debug("Quiet", undefined, "now");
|
|
170
|
+
log.debug("Other", undefined, "still not");
|
|
171
|
+
Logger$ReventlessCore.setLevel(undefined, "Loud", "Error");
|
|
172
|
+
log.warn("Loud", undefined, "dropped");
|
|
173
|
+
});
|
|
174
|
+
match[1]();
|
|
175
|
+
globalThis.expect(match[0].map(r => r.message)).toEqual(["now"]);
|
|
176
|
+
}));
|
|
177
|
+
globalThis.test("the Effect helpers follow a component's level", () => withLogLevel("info", () => {
|
|
178
|
+
let match = collecting();
|
|
179
|
+
capture(() => {
|
|
180
|
+
Effect.runSync(EffectLogger$ReventlessCore.logDebug("Quiet", undefined, undefined, "not yet"));
|
|
181
|
+
Logger$ReventlessCore.setLevel(undefined, "Quiet", "Debug");
|
|
182
|
+
Effect.runSync(EffectLogger$ReventlessCore.logDebug("Quiet", undefined, undefined, "now"));
|
|
183
|
+
Logger$ReventlessCore.setLevel(undefined, "Loud", "Error");
|
|
184
|
+
Effect.runSync(EffectLogger$ReventlessCore.logWarn("Loud", undefined, undefined, "dropped"));
|
|
185
|
+
});
|
|
186
|
+
match[1]();
|
|
187
|
+
globalThis.expect(match[0].map(r => r.message)).toEqual(["now"]);
|
|
188
|
+
}));
|
|
189
|
+
globalThis.test("levelSettings lists what was set", () => {
|
|
190
|
+
Logger$ReventlessCore.setLevel(undefined, undefined, "Warn");
|
|
191
|
+
Logger$ReventlessCore.setLevel("Ordering", "Projection", "Debug");
|
|
192
|
+
globalThis.expect(Logger$ReventlessCore.levelSettings()).toEqual([
|
|
193
|
+
{
|
|
194
|
+
level: "Warn"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
plugin: "Ordering",
|
|
198
|
+
comp: "Projection",
|
|
199
|
+
level: "Debug"
|
|
200
|
+
}
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
export {
|
|
206
|
+
capture,
|
|
207
|
+
collecting,
|
|
208
|
+
}
|
|
209
|
+
/* Not a pure module */
|