@reventlessdev/reventless-core 3.0.0-alpha.327 → 3.0.0-alpha.329

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/package.json +8 -8
  3. package/src/CommandSent.res +22 -0
  4. package/src/CommandSent.res.mjs +22 -0
  5. package/src/Projection.res +26 -1
  6. package/src/Projection.res.mjs +19 -1
  7. package/src/RowChange.res +37 -0
  8. package/src/RowChange.res.mjs +62 -0
  9. package/src/components/Api/GraphQL_FragmentGenerator.res +22 -0
  10. package/src/components/Api/GraphQL_FragmentGenerator.res.mjs +20 -4
  11. package/src/components/Api/GraphQL_SchemaInspector.res.mjs +2 -2
  12. package/src/components/AutomationSlice/AutomationSlice_Callback.res +1 -0
  13. package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +4 -2
  14. package/src/components/CommandGenerator/CommandGenerator_Callback.res +2 -1
  15. package/src/components/CommandGenerator/CommandGenerator_Callback.res.mjs +1 -1
  16. package/src/components/CommandTopic/CommandTopic.res.mjs +6 -0
  17. package/src/components/CommandTopic/CommandTopic_Helpers.res +44 -2
  18. package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +69 -11
  19. package/src/components/EventMapper/EventMapper_Callback.res +8 -2
  20. package/src/components/EventMapper/EventMapper_Callback.res.mjs +3 -2
  21. package/src/components/ExtensionPoint/ExtensionPoint_Callback.res +5 -1
  22. package/src/components/ExtensionPoint/ExtensionPoint_Callback.res.mjs +2 -1
  23. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res +4 -0
  24. package/src/components/InboundTranslationSlice/InboundTranslationSlice_Callback.res.mjs +3 -1
  25. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +4 -0
  26. package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +4 -2
  27. package/src/components/QueryDb/QueryDb_Operations.res +2 -0
  28. package/src/components/QueryDb/QueryDb_Operations.res.mjs +3 -0
  29. package/src/components/Task/Task_Builder.res +5 -3
  30. package/src/components/Task/Task_Builder.res.mjs +2 -1
  31. package/src/plugin/component/Plugin_Callback.res +6 -2
  32. package/src/plugin/component/Plugin_Callback.res.mjs +3 -2
  33. package/src/util/Metrics.res +151 -16
  34. package/src/util/Metrics.res.mjs +123 -3
  35. package/tests/api/GraphQL_FragmentGeneratorTest.res.mjs +2 -2
  36. package/tests/api/IdentitySemanticTest.res.mjs +1 -1
  37. package/tests/commandgenerator/CommandGeneratorCallbackTest.res +33 -0
  38. package/tests/commandgenerator/CommandGeneratorCallbackTest.res.mjs +51 -0
  39. package/tests/commandtopic/CommandOutcomeLineTest.res +121 -0
  40. package/tests/commandtopic/CommandOutcomeLineTest.res.mjs +149 -0
  41. package/tests/querydb/RowChangeTest.res +87 -0
  42. package/tests/querydb/RowChangeTest.res.mjs +157 -0
  43. package/tests/util/MetricsTest.res +72 -0
  44. package/tests/util/MetricsTest.res.mjs +108 -0
@@ -46,11 +46,56 @@ function clearCommandOutcome() {
46
46
  commandOutcomeHooks.contents = [];
47
47
  }
48
48
 
49
+ let outcomeLines = {
50
+ contents: true
51
+ };
52
+
53
+ function logOutcome(r) {
54
+ let match = r.outcome;
55
+ let match$1;
56
+ match$1 = match.TAG === "OutcomeAccepted" ? [
57
+ "command accepted",
58
+ [
59
+ [
60
+ "outcome",
61
+ "accepted"
62
+ ],
63
+ [
64
+ "eventCount",
65
+ match.eventCount
66
+ ]
67
+ ].concat(Stdlib_Option.mapOr(match.entityId, [], e => [[
68
+ "entityId",
69
+ e
70
+ ]]))
71
+ ] : [
72
+ "command rejected",
73
+ [
74
+ [
75
+ "outcome",
76
+ "rejected"
77
+ ],
78
+ [
79
+ "errorCode",
80
+ match.errorCode
81
+ ],
82
+ [
83
+ "cause",
84
+ refusalCauseToGraphQL(match.cause)
85
+ ]
86
+ ]
87
+ ];
88
+ Effect.runSync(EffectLogger$ReventlessCore.logInfo(`CommandTopic(` + r.component + `)`, Object.fromEntries([[
89
+ "component",
90
+ r.component
91
+ ]].concat(match$1[1])), {
92
+ msgId: r.reference,
93
+ correlationId: r.correlationId,
94
+ causationId: r.causationId
95
+ }, match$1[0]));
96
+ }
97
+
49
98
  function fireCommandOutcome(component, reference, correlationId, causationId, outcome) {
50
- let hooks = commandOutcomeHooks.contents;
51
- if (hooks.length === 0) {
52
- return;
53
- }
54
99
  let report = {
55
100
  component: component,
56
101
  reference: reference,
@@ -58,15 +103,26 @@ function fireCommandOutcome(component, reference, correlationId, causationId, ou
58
103
  causationId: causationId,
59
104
  outcome: outcome
60
105
  };
61
- hooks.forEach(param => {
106
+ if (outcomeLines.contents) {
62
107
  try {
63
- param.hook(report);
64
- return;
65
- } catch (raw_err) {
66
- let err = Primitive_exceptions.internalToException(raw_err);
67
- return Effect.runSync(EffectLogger$ReventlessCore.logError(`CommandTopic(` + component + `)`, undefined, undefined, `command-outcome hook threw (ignored): ` + Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(err), Stdlib_JsExn.message), "unknown")));
108
+ logOutcome(report);
109
+ } catch (exn) {
110
+
68
111
  }
69
- });
112
+ }
113
+ let hooks = commandOutcomeHooks.contents;
114
+ if (hooks.length !== 0) {
115
+ hooks.forEach(param => {
116
+ try {
117
+ param.hook(report);
118
+ return;
119
+ } catch (raw_err) {
120
+ let err = Primitive_exceptions.internalToException(raw_err);
121
+ return Effect.runSync(EffectLogger$ReventlessCore.logError(`CommandTopic(` + component + `)`, undefined, undefined, `command-outcome hook threw (ignored): ` + Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(err), Stdlib_JsExn.message), "unknown")));
122
+ }
123
+ });
124
+ return;
125
+ }
70
126
  }
71
127
 
72
128
  let acceptedResultChannel = {
@@ -295,6 +351,8 @@ export {
295
351
  nextOutcomeHookId,
296
352
  registerCommandOutcome,
297
353
  clearCommandOutcome,
354
+ outcomeLines,
355
+ logOutcome,
298
356
  fireCommandOutcome,
299
357
  acceptedResultChannel,
300
358
  rejectedResultChannel,
@@ -236,7 +236,10 @@ module MakeCounterHandler = (
236
236
  )
237
237
  ->Effect.flatMap(((publisherEntries, _)) =>
238
238
  Effect.promise(() => publisherEntries)->Effect.flatMap(
239
- entries => Effect.promise(() => Ops.publishJsons(entries)),
239
+ entries =>
240
+ Effect.promise(
241
+ () => CommandSent.announcing(~comp="EventMapper", Ops.publishJsons)(entries),
242
+ ),
240
243
  )
241
244
  )
242
245
  )
@@ -334,7 +337,10 @@ module MakeEventCollectorHandler = (Ops: EventCollectorOps): EventCollectorHandl
334
337
  ->Effect.flatMap(
335
338
  _ =>
336
339
  Effect.promise(() => publisherEntries)->Effect.flatMap(
337
- entries => Effect.promise(() => Ops.publishJsons(entries)),
340
+ entries =>
341
+ Effect.promise(
342
+ () => CommandSent.announcing(~comp="EventMapper", Ops.publishJsons)(entries),
343
+ ),
338
344
  ),
339
345
  )
340
346
  })
@@ -16,6 +16,7 @@ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_excep
16
16
  import * as Message$ReventlessCore from "../../Message.res.mjs";
17
17
  import * as LogFormat$ReventlessCore from "../../util/LogFormat.res.mjs";
18
18
  import * as Util_Error$ReventlessCore from "../../util/Util_Error.res.mjs";
19
+ import * as CommandSent$ReventlessCore from "../../CommandSent.res.mjs";
19
20
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
20
21
 
21
22
  function MakeCounterHandler(Target) {
@@ -183,7 +184,7 @@ function MakeCounterHandler(Target) {
183
184
  }
184
185
  }), param => {
185
186
  let publisherEntries = param[0];
186
- return Effect$1.flatMap(Effect$1.promise(() => publisherEntries), entries => Effect$1.promise(() => Ops.publishJsons(entries)));
187
+ return Effect$1.flatMap(Effect$1.promise(() => publisherEntries), entries => Effect$1.promise(() => CommandSent$ReventlessCore.announcing("EventMapper", Ops.publishJsons)(entries)));
187
188
  }));
188
189
  return {
189
190
  commonEventsHandler: commonEventsHandler,
@@ -218,7 +219,7 @@ function MakeEventCollectorHandler(Ops) {
218
219
  return Primitive_option.some(Effect$1.promise(() => Ops.addToCounterTarget(counterTarget)));
219
220
  }), {
220
221
  concurrency: "unbounded"
221
- }), param => {})), () => Effect$1.flatMap(Effect$1.promise(() => publisherEntries), entries => Effect$1.promise(() => Ops.publishJsons(entries))));
222
+ }), param => {})), () => Effect$1.flatMap(Effect$1.promise(() => publisherEntries), entries => Effect$1.promise(() => CommandSent$ReventlessCore.announcing("EventMapper", Ops.publishJsons)(entries))));
222
223
  }));
223
224
  return {
224
225
  handleJsonEvents: handleJsonEvents
@@ -59,7 +59,11 @@ module Make = (
59
59
  let result =
60
60
  Spec.publishToAggregates
61
61
  ->Dict.get(aggregateName)
62
- ->Option.map((publishJsons: CommandTopic.publishJsons) => publishJsons([cmdJson]))
62
+ ->Option.map((publishJsons: CommandTopic.publishJsons) =>
63
+ CommandSent.announcing(~comp=`ExtensionPoint(${MappingSpec.name})`, publishJsons)([
64
+ cmdJson,
65
+ ])
66
+ )
63
67
  ->Option.mapOr(
64
68
  () =>
65
69
  JsError.throwWithMessage(
@@ -6,6 +6,7 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
6
6
  import * as Effect$1 from "effect/Effect";
7
7
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
8
8
  import * as Util_Error$ReventlessCore from "../../util/Util_Error.res.mjs";
9
+ import * as CommandSent$ReventlessCore from "../../CommandSent.res.mjs";
9
10
  import * as ScheduleOps$ReventlessCore from "../../util/ScheduleOps.res.mjs";
10
11
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
11
12
 
@@ -27,7 +28,7 @@ function Make(Spec) {
27
28
  `ExtensionPoint: Error on publish command: ` + errMsg
28
29
  ];
29
30
  }, () => {
30
- let result = Stdlib_Option.mapOr(Stdlib_Option.map(Spec.publishToAggregates[aggregateName], publishJsons => publishJsons([cmdJson])), () => Stdlib_JsError.throwWithMessage(`ExtensionPoint.applyCommandAction: Aggregate ` + aggregateName + ` doesn't exist`), x => (() => x));
31
+ let result = Stdlib_Option.mapOr(Stdlib_Option.map(Spec.publishToAggregates[aggregateName], publishJsons => CommandSent$ReventlessCore.announcing(`ExtensionPoint(` + MappingSpec.name + `)`, publishJsons)([cmdJson])), () => Stdlib_JsError.throwWithMessage(`ExtensionPoint.applyCommandAction: Aggregate ` + aggregateName + ` doesn't exist`), x => (() => x));
31
32
  result();
32
33
  return {
33
34
  TAG: "Ok",
@@ -241,6 +241,10 @@ module Make = (
241
241
  ~caller: option<Reventless.Identity.t>=?,
242
242
  ~door: ReventlessInfra.InboundTranslationSlice.door=Mutation,
243
243
  ): ReventlessInfra.InboundTranslationSlice.receiveResult => {
244
+ let publishJsons = CommandSent.announcing(
245
+ ~comp=`InboundTranslationSlice(${Spec.name})`,
246
+ publishJsons,
247
+ )
244
248
  let requestId = Uuid.v4()
245
249
  let callerId = caller->Option.map(identity => identity.userId)
246
250
  let fail = (~errorCode=?, error) =>
@@ -19,6 +19,7 @@ import * as OwnerScope$Reventless from "@reventlessdev/reventless-spec/src/types
19
19
  import * as Message$ReventlessCore from "../../Message.res.mjs";
20
20
  import * as Authorization$Reventless from "@reventlessdev/reventless-spec/src/types/Authorization.res.mjs";
21
21
  import * as CommandOutcome$Reventless from "@reventlessdev/reventless-spec/src/components/CommandOutcome.res.mjs";
22
+ import * as CommandSent$ReventlessCore from "../../CommandSent.res.mjs";
22
23
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
23
24
  import * as ApiNoApiHelpers$ReventlessCore from "../Api/ApiNoApiHelpers.res.mjs";
24
25
  import * as InboundTranslationSlice$ReventlessInfra from "@reventlessdev/reventless-infra/src/components/InboundTranslationSlice.res.mjs";
@@ -192,6 +193,7 @@ function Make(Spec) {
192
193
  };
193
194
  let receive = async (publishJsons, inputJson, capabilities, caller, doorOpt) => {
194
195
  let door = doorOpt !== undefined ? doorOpt : "Mutation";
196
+ let publishJsons$1 = CommandSent$ReventlessCore.announcing(`InboundTranslationSlice(` + Spec.name + `)`, publishJsons);
195
197
  let requestId = Uuid.v4();
196
198
  let callerId = Stdlib_Option.map(caller, identity => identity.userId);
197
199
  let fail = (errorCode, error) => failWith(requestId, JSON.stringify(redactInput(inputJson, sensitive)), door, callerId, errorCode, error);
@@ -277,7 +279,7 @@ function Make(Spec) {
277
279
  return succeed([]);
278
280
  }
279
281
  try {
280
- await publishJsons(msgs);
282
+ await publishJsons$1(msgs);
281
283
  return succeed(pairs.map(param => param[0]));
282
284
  } catch (raw_exn$2) {
283
285
  let exn$2 = Primitive_exceptions.internalToException(raw_exn$2);
@@ -302,6 +302,10 @@ module Make = (
302
302
  publishJsons: ReventlessInfra.CommandTopic.publishJsons,
303
303
  ~capabilities: Reventless.Capabilities.t,
304
304
  ) => {
305
+ let publishJsons = CommandSent.announcing(
306
+ ~comp=`OutboundTranslationSlice(${Spec.name})`,
307
+ publishJsons,
308
+ )
305
309
  // Ids that ran out of budget during this pass, announced once at the end so
306
310
  // one item's abandonment cannot interleave with another's attempt.
307
311
  let newlyAbandoned: array<string> = []
@@ -12,6 +12,7 @@ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js
12
12
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
13
13
  import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
14
14
  import * as Message$ReventlessCore from "../../Message.res.mjs";
15
+ import * as CommandSent$ReventlessCore from "../../CommandSent.res.mjs";
15
16
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
16
17
 
17
18
  let todoStatusSchema = Sury.union([
@@ -157,6 +158,7 @@ function Make(Spec) {
157
158
  }
158
159
  };
159
160
  let phase2 = async (publishJsons, capabilities) => {
161
+ let publishJsons$1 = CommandSent$ReventlessCore.announcing(`OutboundTranslationSlice(` + Spec.name + `)`, publishJsons);
160
162
  let newlyAbandoned = [];
161
163
  Object.entries(todoItems).forEach(param => {
162
164
  let row = param[1];
@@ -284,7 +286,7 @@ function Make(Spec) {
284
286
  meta: msg_meta,
285
287
  commandJson: commandJson
286
288
  };
287
- await publishJsons([msg$1]);
289
+ await publishJsons$1([msg$1]);
288
290
  let newrecord$2 = {...row};
289
291
  newrecord$2.completedAt = new Date().toISOString();
290
292
  newrecord$2.status = "Completed";
@@ -335,7 +337,7 @@ function Make(Spec) {
335
337
  await prev;
336
338
  let row = todoItems[id];
337
339
  if (row !== undefined) {
338
- return await announceAbandonment(id, row, publishJsons);
340
+ return await announceAbandonment(id, row, publishJsons$1);
339
341
  }
340
342
  });
341
343
  };
@@ -129,6 +129,7 @@ module Make = (ReadModelSpec: Reventless.ReadModel.Spec, Ops: Ops) => {
129
129
  injectSubId(dict, state)
130
130
  injectCompositeIndexAttrs(dict)
131
131
  stampUnionMembers(dict)
132
+ RowChange.stampInto(dict)
132
133
  let json = JSON.Encode.object(dict)
133
134
  await Ops.jsonOps.save(id->ReadModelSpec.Id.toString, json, saveMode, ttl)
134
135
  | None =>
@@ -144,6 +145,7 @@ module Make = (ReadModelSpec: Reventless.ReadModel.Spec, Ops: Ops) => {
144
145
  injectSubId(dict, state)
145
146
  injectCompositeIndexAttrs(dict)
146
147
  stampUnionMembers(dict)
148
+ RowChange.stampInto(dict)
147
149
  let json = JSON.Encode.object(dict)
148
150
  Ok(batch->Array.concat([(id->ReadModelSpec.Id.toString, json, ttl)]))
149
151
  | None =>
@@ -10,6 +10,7 @@ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_excep
10
10
  import * as Logger$ReventlessCore from "../../util/Logger.res.mjs";
11
11
  import * as Message$ReventlessCore from "../../Message.res.mjs";
12
12
  import * as TaggedUnion$Reventless from "@reventlessdev/reventless-spec/src/components/TaggedUnion.res.mjs";
13
+ import * as RowChange$ReventlessCore from "../../RowChange.res.mjs";
13
14
 
14
15
  let log = Logger$ReventlessCore.fromEnv();
15
16
 
@@ -104,6 +105,7 @@ function Make(ReadModelSpec) {
104
105
  injectSubId(dict, state);
105
106
  injectCompositeIndexAttrs(dict);
106
107
  stampUnionMembers(dict);
108
+ RowChange$ReventlessCore.stampInto(dict);
107
109
  return await Ops.jsonOps.save(ReadModelSpec.Id.toString(id), dict, saveMode, ttl);
108
110
  } else {
109
111
  return {
@@ -130,6 +132,7 @@ function Make(ReadModelSpec) {
130
132
  injectSubId(dict, state);
131
133
  injectCompositeIndexAttrs(dict);
132
134
  stampUnionMembers(dict);
135
+ RowChange$ReventlessCore.stampInto(dict);
133
136
  return {
134
137
  TAG: "Ok",
135
138
  _0: batch.concat([[
@@ -140,9 +140,11 @@ module Make = (
140
140
  )
141
141
  )
142
142
 
143
- let publishCommands: Task.publishCommands = (aggregateName, cmdJsons) => {
144
- (publishToAggregates->Dict.get(aggregateName)->Option.getOrThrow)(cmdJsons)
145
- }
143
+ let publishCommands: Task.publishCommands = (aggregateName, cmdJsons) =>
144
+ CommandSent.announcing(
145
+ ~comp=`Task(${Spec.name})`,
146
+ publishToAggregates->Dict.get(aggregateName)->Option.getOrThrow,
147
+ )(cmdJsons)
146
148
 
147
149
  let config = Spec.setup(queryEngine, queryBucketName, opts)
148
150
 
@@ -11,6 +11,7 @@ import * as Task$ReventlessCore from "./Task.res.mjs";
11
11
  import * as Logger$ReventlessCore from "../../util/Logger.res.mjs";
12
12
  import * as Aggregate$ReventlessCore from "../Aggregate/Aggregate.res.mjs";
13
13
  import * as Component$ReventlessCore from "../Component.res.mjs";
14
+ import * as CommandSent$ReventlessCore from "../../CommandSent.res.mjs";
14
15
  import * as Util_Promise$ReventlessCore from "../../util/Util_Promise.res.mjs";
15
16
  import * as ComponentType$ReventlessCore from "../../ComponentType.res.mjs";
16
17
  import * as RuntimeHints$ReventlessInfra from "@reventlessdev/reventless-infra/src/types/RuntimeHints.res.mjs";
@@ -63,7 +64,7 @@ function Make(Spec) {
63
64
  return Pulumi.output("");
64
65
  }
65
66
  }));
66
- let publishCommands = (aggregateName, cmdJsons) => Stdlib_Option.getOrThrow(publishToAggregates[aggregateName], undefined)(cmdJsons);
67
+ let publishCommands = (aggregateName, cmdJsons) => CommandSent$ReventlessCore.announcing(`Task(` + Spec.name + `)`, Stdlib_Option.getOrThrow(publishToAggregates[aggregateName], undefined))(cmdJsons);
67
68
  let config = Spec.setup(queryEngine, queryBucketName, opts);
68
69
  let sideEffectHandler = Stdlib_Option.map(config.sideEffects, sideEffects => SpecificSideEffectHandler.make(extra$1, sideEffects, Aggregate$ReventlessCore.allEventTopics(allAggregates), allCommandTopics, undefined, queryEngine, scheduler, resourceNaming, undefined, undefined, undefined, opts));
69
70
  Stdlib_Option.forEach(sideEffectHandler, handler => Builder_Helpers$ReventlessCore.registerTaskSideEffectHandler(Component$ReventlessCore.operations(handler).apply(param => {}), SpecificSideEffectHandler.finish));
@@ -14,6 +14,10 @@ module type T = {
14
14
  }
15
15
 
16
16
  module Make = (Spec: Spec): T => {
17
+ // What this dispatcher logs is an event routed to an extension point or an extension, so its
18
+ // lines go by a contract's component name rather than the plugin's.
19
+ let comp = (id: string) => `ExtensionRouting(${id})`
20
+
17
21
  let warnedServices = ref(Belt.Set.String.empty)
18
22
 
19
23
  let knownServiceNames = () =>
@@ -44,7 +48,7 @@ module Make = (Spec: Spec): T => {
44
48
  warnedServices := warnedServices.contents->Belt.Set.String.add(serviceName)
45
49
  let known = knownServiceNames()->Array.join(", ")
46
50
  EffectLogger.logWarn(
47
- ~comp=`Plugin(${id})`,
51
+ ~comp=comp(id),
48
52
  `incoming event has service="${serviceName}" but no handler is registered for it — known services: [${known}]. Likely cause: an ExtensionPointMapping/Extension Delegate.name does not match the source's serviceName (DCB sources use "<plugin>DcbEventLog").`,
49
53
  )
50
54
  }
@@ -76,7 +80,7 @@ module Make = (Spec: Spec): T => {
76
80
  Spec.incomingExtensionEventHandlers->Dict.get(serviceName)->Option.isSome
77
81
  if isHandled {
78
82
  EffectLogger.logInfo(
79
- ~comp=`Plugin(${id})`,
83
+ ~comp=comp(id),
80
84
  `incoming event: ${LogFormat.eventSummary(eventJson')}`,
81
85
  )->Effect.zipRight(
82
86
  handleEventEffect(
@@ -9,6 +9,7 @@ import * as LogFormat$ReventlessCore from "../../util/LogFormat.res.mjs";
9
9
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
10
10
 
11
11
  function Make(Spec) {
12
+ let comp = id => `ExtensionRouting(` + id + `)`;
12
13
  let warnedServices = {
13
14
  contents: undefined
14
15
  };
@@ -27,7 +28,7 @@ function Make(Spec) {
27
28
  return Stdlib_Option.mapOr(Message$ReventlessCore.serviceNameOfMsg(eventJson$p), Effect.succeed(), serviceName => {
28
29
  let isHandled = Stdlib_Option.isSome(Spec.incomingConnectExtensionEventHandlers[serviceName]) || Stdlib_Option.isSome(Spec.outgoingExtensionPointEventHandlers[serviceName]) || Stdlib_Option.isSome(Spec.outgoingExtensionEventHandlers[serviceName]) || Stdlib_Option.isSome(Spec.incomingExtensionEventHandlers[serviceName]);
29
30
  if (isHandled) {
30
- return Effect.zipRight(EffectLogger$ReventlessCore.logInfo(`Plugin(` + id + `)`, undefined, undefined, `incoming event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)), Effect.zipRight(handleEventEffect(eventJson$p, Spec.incomingConnectExtensionEventHandlers), Effect.map(Effect.all([
31
+ return Effect.zipRight(EffectLogger$ReventlessCore.logInfo(comp(id), undefined, undefined, `incoming event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)), Effect.zipRight(handleEventEffect(eventJson$p, Spec.incomingConnectExtensionEventHandlers), Effect.map(Effect.all([
31
32
  handleEventEffect(eventJson$p, Spec.outgoingExtensionPointEventHandlers),
32
33
  handleEventEffect(eventJson$p, Spec.outgoingExtensionEventHandlers),
33
34
  handleEventEffect(eventJson$p, Spec.incomingExtensionEventHandlers)
@@ -40,7 +41,7 @@ function Make(Spec) {
40
41
  }
41
42
  warnedServices.contents = Belt_SetString.add(warnedServices.contents, serviceName);
42
43
  let known = knownServiceNames().join(", ");
43
- return EffectLogger$ReventlessCore.logWarn(`Plugin(` + id + `)`, undefined, undefined, `incoming event has service="` + serviceName + `" but no handler is registered for it — known services: [` + known + `]. Likely cause: an ExtensionPointMapping/Extension Delegate.name does not match the source's serviceName (DCB sources use "<plugin>DcbEventLog").`);
44
+ return EffectLogger$ReventlessCore.logWarn(comp(id), undefined, undefined, `incoming event has service="` + serviceName + `" but no handler is registered for it — known services: [` + known + `]. Likely cause: an ExtensionPointMapping/Extension Delegate.name does not match the source's serviceName (DCB sources use "<plugin>DcbEventLog").`);
44
45
  }
45
46
  });
46
47
  }));
@@ -1,15 +1,9 @@
1
- // Provider-neutral metric event emission.
2
- //
3
- // Emits one structured JSON line per occurrence — the same posture as `Logger`
4
- // (generic structured stdout, no provider vocabulary). Core does NOT know about
5
- // any provider's metric system: a collector/adapter turns these lines into real
6
- // metrics. On AWS a CloudWatch metric filter (defined in `reventless-aws`,
7
- // deploy-time) matches the discriminator field and extracts the `slice`
8
- // dimension + value; another provider can match the same generic shape.
9
- //
10
- // Suppressed outside a JSON sink so TTY dev runs stay clean. Used by the DCB
11
- // StateChangeSlice callback to surface append-retry / append-conflict rates per
12
- // slice (see docs/analysis/dcb-high-contention-handling.md §6).
1
+ // Provider-neutral metric emission. `emitCount` is the one call that emits a
2
+ // metric; where the count goes is the installed `Sink`. The default writes one
3
+ // JSON line per occurrence to a JSON log sink, which a deployed collector turns
4
+ // into a metric (on AWS a CloudWatch metric filter keys on `reventlessMetric`). A
5
+ // local platform installs `Counts.sink` instead: it counts in memory and writes
6
+ // nothing. See docs/plans/metrics-are-counted-not-logged.md.
13
7
 
14
8
  // Discriminator key a metric collector keys off. Stable — changing it is a
15
9
  // breaking change for any configured metric filter.
@@ -24,9 +18,150 @@ let metricLine = (~metric: string, ~slice: string, ~value: int=1): JSON.t =>
24
18
  ("unit", "Count"->JSON.Encode.string),
25
19
  ])->JSON.Encode.object
26
20
 
27
- // Emits a single count for `metric`, dimensioned by `slice`. No-op outside a
28
- // JSON sink. `value` defaults to 1 (one occurrence).
21
+ /** One occurrence of a metric, as a sink receives it. */
22
+ type sample = {
23
+ metric: string,
24
+ slice: string,
25
+ /** The plugin the slice belongs to, when the registry knows it. */
26
+ plugin?: string,
27
+ value: int,
28
+ unit: string,
29
+ /** In milliseconds since 1970. */
30
+ at: float,
31
+ }
32
+
33
+ module Sink = {
34
+ type t = sample => unit
35
+
36
+ /** The line a deployed collector reads, written only to a JSON log sink. */
37
+ let line: t = s =>
38
+ if Reventless.AnsiStyle.isJsonSink() {
39
+ Console.log(metricLine(~metric=s.metric, ~slice=s.slice, ~value=s.value)->JSON.stringify)
40
+ }
41
+
42
+ let current: ref<t> = ref(line)
43
+
44
+ /** Where every metric goes from now on, installed at a process's start. */
45
+ let install = (sink: t) => current := sink
46
+
47
+ /** Back to the line: for tests. */
48
+ let reset = () => current := line
49
+ }
50
+
51
+ // Emits a single count for `metric`, dimensioned by `slice`. `value` defaults to
52
+ // 1 (one occurrence).
29
53
  let emitCount = (~metric: string, ~slice: string, ~value: int=1): unit =>
30
- if Reventless.AnsiStyle.isJsonSink() {
31
- Console.log(metricLine(~metric, ~slice, ~value)->JSON.stringify)
54
+ Sink.current.contents({
55
+ metric,
56
+ slice,
57
+ plugin: ?Reventless.LogPrefix.lookup(slice),
58
+ value,
59
+ unit: "Count",
60
+ at: Date.now(),
61
+ })
62
+
63
+ /** Metrics counted in memory, per metric, plugin and slice: how often, the sum of
64
+ their values, the last time, and how many in each of the last 60 minutes. Bounded
65
+ by the number of slices, not of commands. */
66
+ module Counts = {
67
+ let minutes = 60
68
+ let minuteMs = 60_000.
69
+
70
+ type entry = {
71
+ metric: string,
72
+ plugin: string,
73
+ slice: string,
74
+ unit: string,
75
+ mutable count: int,
76
+ mutable sum: float,
77
+ mutable lastAt: float,
78
+ /** The counts of the last `minutes` minutes, oldest first; the last is `minute`'s. */
79
+ mutable perMinute: array<int>,
80
+ mutable minute: float,
81
+ }
82
+
83
+ type t = {entries: dict<entry>}
84
+
85
+ /** One count as a reader gets it. */
86
+ type snapshot = {
87
+ metric: string,
88
+ plugin: string,
89
+ slice: string,
90
+ unit: string,
91
+ count: int,
92
+ sum: float,
93
+ lastAt: float,
94
+ perMinute: array<int>,
95
+ }
96
+
97
+ let make = (): t => {entries: Dict.make()}
98
+
99
+ let clear = (t: t) => t.entries->Dict.keysToArray->Array.forEach(k => t.entries->Dict.delete(k))
100
+
101
+ let minuteOf = (at: float) => Math.floor(at /. minuteMs)
102
+
103
+ /** The buckets moved on to `minute`: the minutes since the last count are zero. */
104
+ let rolled = (perMinute: array<int>, ~from: float, ~to_: float): array<int> => {
105
+ let gap = Math.Int.min(minutes, (to_ -. from)->Float.toInt)
106
+ gap <= 0
107
+ ? perMinute
108
+ : perMinute
109
+ ->Array.slice(~start=gap, ~end=minutes)
110
+ ->Array.concat(Array.make(~length=gap, 0))
111
+ }
112
+
113
+ let add = (t: t, s: sample) => {
114
+ let plugin = s.plugin->Option.getOr("")
115
+ let key = `${s.metric}|${plugin}|${s.slice}`
116
+ let minute = minuteOf(s.at)
117
+ let e = switch t.entries->Dict.get(key) {
118
+ | Some(e) => e
119
+ | None =>
120
+ let e = {
121
+ metric: s.metric,
122
+ plugin,
123
+ slice: s.slice,
124
+ unit: s.unit,
125
+ count: 0,
126
+ sum: 0.,
127
+ lastAt: s.at,
128
+ perMinute: Array.make(~length=minutes, 0),
129
+ minute,
130
+ }
131
+ t.entries->Dict.set(key, e)
132
+ e
133
+ }
134
+ e.perMinute = rolled(e.perMinute, ~from=e.minute, ~to_=minute)
135
+ e.minute = Math.max(e.minute, minute)
136
+ let last = minutes - 1
137
+ e.perMinute->Array.setUnsafe(last, e.perMinute->Array.getUnsafe(last) + 1)
138
+ e.count = e.count + 1
139
+ e.sum = e.sum +. s.value->Int.toFloat
140
+ e.lastAt = Math.max(e.lastAt, s.at)
32
141
  }
142
+
143
+ /** The sink that counts into `t` and writes nothing. */
144
+ let sink = (t: t): Sink.t => s => add(t, s)
145
+
146
+ /** The counts at `now`, of the given plugins or of every one, ordered by plugin,
147
+ slice and metric. */
148
+ let read = (t: t, ~now: float, ~plugins: option<array<string>>=?): array<snapshot> =>
149
+ t.entries
150
+ ->Dict.valuesToArray
151
+ ->Array.filter(e => plugins->Option.mapOr(true, ps => ps->Array.includes(e.plugin)))
152
+ ->Array.map((e): snapshot => {
153
+ metric: e.metric,
154
+ plugin: e.plugin,
155
+ slice: e.slice,
156
+ unit: e.unit,
157
+ count: e.count,
158
+ sum: e.sum,
159
+ lastAt: e.lastAt,
160
+ perMinute: rolled(e.perMinute, ~from=e.minute, ~to_=minuteOf(now)),
161
+ })
162
+ ->Array.toSorted((a, b) =>
163
+ `${a.plugin}|${a.slice}|${a.metric}`->String.localeCompare(
164
+ `${b.plugin}|${b.slice}|${b.metric}`,
165
+ )
166
+ )
167
+ }