@entifix/amqp 0.10.1 → 0.11.0

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.
@@ -1,7 +1,7 @@
1
1
  import type { WiringRegistry } from '@entifix/business';
2
2
  import { ShutdownRegistryTag, WiringRegistryTag } from '@entifix/business';
3
3
  import { type EventBus, EventBusTag, type Subscription } from '@entifix/transactions';
4
- import { Duration, Effect, Layer } from 'effect';
4
+ import { Context, Duration, Effect, Layer } from 'effect';
5
5
  import { AmqpChannelTag, type AmqpConnector } from '../amqp-connection/amqp-connection.js';
6
6
  /**
7
7
  * The queue a subscription binds.
@@ -37,11 +37,30 @@ export declare const ATTEMPTS_HEADER = "x-entifix-attempts";
37
37
  /** How long a transient failure waits before its copy goes back on the queue. */
38
38
  export type RedeliveryBackoff = (attempt: number) => Duration.Duration;
39
39
  /**
40
- * Doubling from 100 ms, capped at two seconds. The wait holds the delivery —
41
- * the queue is `prefetch(1)`, so it pauses this consumer — which is the point:
42
- * a dependency that is down is not helped by asking it again at once.
40
+ * A backoff doubling from `baseMs`, capped at `capMs`. The wait holds the
41
+ * delivery — the queue is `prefetch(1)`, so it pauses this consumer — which is
42
+ * the point: a dependency that is down is not helped by asking it again at
43
+ * once.
43
44
  */
45
+ export declare const exponentialRedeliveryBackoff: (dials: {
46
+ readonly baseMs: number;
47
+ readonly capMs: number;
48
+ }) => RedeliveryBackoff;
49
+ /** Doubling from 100 ms, capped at two seconds. */
44
50
  export declare const DEFAULT_REDELIVERY_BACKOFF: RedeliveryBackoff;
51
+ declare const RedeliveryBackoffTag_base: Context.ServiceClass<RedeliveryBackoffTag, "RedeliveryBackoffTag", RedeliveryBackoff>;
52
+ /**
53
+ * The redelivery backoff {@link AmqpEventBusLayer} uses, when a composition
54
+ * provides one; {@link DEFAULT_REDELIVERY_BACKOFF} otherwise.
55
+ *
56
+ * A tag rather than a layer option because the dials it is sized from are
57
+ * runtime configuration a host reads at boot, after the layer value already
58
+ * exists. Read with `Effect.serviceOption`, so a host that never sizes it
59
+ * provides nothing — which also means nothing forces one that meant to, and
60
+ * the spec pins both arms.
61
+ */
62
+ export declare class RedeliveryBackoffTag extends RedeliveryBackoffTag_base {
63
+ }
45
64
  /**
46
65
  * RabbitMQ-backed {@link EventBus}. Events go out as `event` envelopes on the
47
66
  * shared topic exchange — the adapter owns the wire framing, so the port stays
@@ -83,4 +102,5 @@ export declare const makeAmqpEventBus: (connector: AmqpConnector, wiring: Wiring
83
102
  * document reports the bindings this very process made.
84
103
  */
85
104
  export declare const AmqpEventBusLayer: Layer.Layer<EventBusTag, never, AmqpChannelTag | ShutdownRegistryTag | WiringRegistryTag>;
105
+ export {};
86
106
  //# sourceMappingURL=amqp-event-bus.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"amqp-event-bus.d.ts","sourceRoot":"","sources":["../../src/adapters/amqp-event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAO3E,OAAO,EACL,KAAK,QAAQ,EACb,WAAW,EACX,KAAK,YAAY,EAClB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAEjD,OAAO,EACL,cAAc,EACd,KAAK,aAAa,EAGnB,MAAM,uCAAuC,CAAC;AAuE/C;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,YAAY,GAAI,cAAc,YAAY,KAAG,MAG1B,CAAC;AAsDjC;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,eAAe,uBAAuB,CAAC;AASpD,iFAAiF;AACjF,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,QAAQ,CAAC,QAAQ,CAAC;AAEvE;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,EAAE,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AG,iBAEuE,CAAC;AAE9E,mDAAmD;AACnD,eAAO,MAAM,0BAA0B,EAAE,iBACoB,CAAC;;AAE9D;;;;;;;;;GASG;AACH,qBAAa,oBAAqB,SAAQ,yBAGf;CAAG;AAqF9B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,gBAAgB,GAC3B,WAAW,aAAa,EACxB,QAAQ,cAAc,EACtB,UAAS;IAAE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,iBAAiB,CAAA;CAAO,KAC/D;IAAE,GAAG,EAAE,QAAQ,CAAC;IAAC,eAAe,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;CAsKvD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,2FA0B7B,CAAC"}
@@ -1,10 +1,10 @@
1
1
  import { _ as _extends } from "@swc/helpers/_/_extends";
2
2
  import { ShutdownRegistryTag, WiringRegistryTag } from '@entifix/business';
3
- import { isFailure, makeEventEnvelope, readEventEnvelope, Unavailable } from '@entifix/core';
3
+ import { decodeEvent, encodeEvent, isFailure, Unavailable } from '@entifix/core';
4
4
  import { EventBusTag } from '@entifix/transactions';
5
- import { Duration, Effect, Layer } from 'effect';
5
+ import { Context, Duration, Effect, Layer, Option } from 'effect';
6
6
  import { AmqpChannelTag, EVENTS_DLX, EVENTS_EXCHANGE } from '../amqp-connection/amqp-connection.js';
7
- import { recordConsumed, recordFailed, recordPublished, recordPublishFailure } from './bus-metrics.js';
7
+ import { recordConsumed, recordFailed, recordPublished, recordPublishFailure, recordQuarantined } from './bus-metrics.js';
8
8
  /** Appended to a work queue's name to make the queue that holds its failures. */ const QUARANTINE_SUFFIX = '.quarantine';
9
9
  /**
10
10
  * How often the shutdown drain re-checks whether the handlers have finished.
@@ -120,10 +120,26 @@ const attemptsSoFar = (message)=>{
120
120
  return typeof value === 'number' && Number.isInteger(value) && value > 0 ? value : 0;
121
121
  };
122
122
  /**
123
- * Doubling from 100 ms, capped at two seconds. The wait holds the delivery —
124
- * the queue is `prefetch(1)`, so it pauses this consumer — which is the point:
125
- * a dependency that is down is not helped by asking it again at once.
126
- */ export const DEFAULT_REDELIVERY_BACKOFF = (attempt)=>Duration.millis(Math.min(100 * 2 ** (attempt - 1), 2000));
123
+ * A backoff doubling from `baseMs`, capped at `capMs`. The wait holds the
124
+ * delivery — the queue is `prefetch(1)`, so it pauses this consumer — which is
125
+ * the point: a dependency that is down is not helped by asking it again at
126
+ * once.
127
+ */ export const exponentialRedeliveryBackoff = (dials)=>(attempt)=>Duration.millis(Math.min(dials.baseMs * 2 ** (attempt - 1), dials.capMs));
128
+ /** Doubling from 100 ms, capped at two seconds. */ export const DEFAULT_REDELIVERY_BACKOFF = exponentialRedeliveryBackoff({
129
+ baseMs: 100,
130
+ capMs: 2000
131
+ });
132
+ /**
133
+ * The redelivery backoff {@link AmqpEventBusLayer} uses, when a composition
134
+ * provides one; {@link DEFAULT_REDELIVERY_BACKOFF} otherwise.
135
+ *
136
+ * A tag rather than a layer option because the dials it is sized from are
137
+ * runtime configuration a host reads at boot, after the layer value already
138
+ * exists. Read with `Effect.serviceOption`, so a host that never sizes it
139
+ * provides nothing — which also means nothing forces one that meant to, and
140
+ * the spec pins both arms.
141
+ */ export class RedeliveryBackoffTag extends Context.Service()('RedeliveryBackoffTag') {
142
+ }
127
143
  /**
128
144
  * Settles a delivery that failed.
129
145
  *
@@ -140,13 +156,13 @@ const attemptsSoFar = (message)=>{
140
156
  * that publish is refused it is nacked for the broker to redeliver, which is
141
157
  * the old behaviour and still better than losing it. A `broadcast` queue has no
142
158
  * quarantine and no ceiling, so its failures are dropped.
143
- */ const settleFailure = (channel, message, queue, subscription, failure, backoff)=>{
159
+ */ const settleFailure = (channel, message, queue, subscription, failure, backoff, tags)=>{
144
160
  if (subscription.mode === 'broadcast') {
145
161
  return Effect.sync(()=>channel.nack(message, false, false));
146
162
  }
147
163
  const attempts = failure.kind === 'transient' ? attemptsSoFar(message) + 1 : 0;
148
164
  if (failure.kind === 'poison' || attempts >= subscription.maxAttempts) {
149
- return Effect.sync(()=>quarantine(channel, message, queue, failure, attempts));
165
+ return Effect.sync(()=>quarantine(channel, message, queue, failure, attempts)).pipe(Effect.andThen(recordQuarantined(tags, failure.category)));
150
166
  }
151
167
  const wait = backoff(attempts);
152
168
  return (Duration.isZero(wait) ? Effect.void : Effect.sleep(wait)).pipe(Effect.andThen(Effect.sync(()=>{
@@ -220,16 +236,23 @@ const quarantine = (channel, message, queue, failure, attempts)=>{
220
236
  * making.
221
237
  */ let inFlight = 0;
222
238
  const bus = {
223
- publish: (event)=>Effect.tryPromise({
224
- try: ()=>connector.withChannel(async (channel)=>{
225
- channel.publish(EVENTS_EXCHANGE, event.name, Buffer.from(JSON.stringify(makeEventEnvelope(event))), {
226
- persistent: true
227
- });
228
- }),
229
- catch: (error)=>new Unavailable({
230
- dependency: 'amqp'
231
- })
232
- }).pipe(Effect.tap(()=>wiring.recordPublish(event.name).pipe(Effect.andThen(recordPublished(event.name)))), // Counted here and not in the `catch` above, which only builds the
239
+ // Encoded before the channel is touched: a message its registration
240
+ // refuses — no schema, no audience, no version — fails the publish as a
241
+ // protocol failure instead of reaching a consumer that would quarantine it.
242
+ publish: (event)=>Effect.flatMap(encodeEvent(event), (envelope)=>Effect.tryPromise({
243
+ try: ()=>connector.withChannel(async (channel)=>{
244
+ channel.publish(EVENTS_EXCHANGE, event.name, Buffer.from(JSON.stringify(envelope)), {
245
+ persistent: true
246
+ });
247
+ }),
248
+ catch: ()=>new Unavailable({
249
+ dependency: 'amqp'
250
+ })
251
+ })).pipe(// Recorded **after** the publish succeeds, so `GET /api/$service` reports
252
+ // what this process actually put on the exchange. Recording the intent
253
+ // instead would make the declared-vs-observed diff pass on a service whose
254
+ // every publish is failing.
255
+ Effect.tap(()=>wiring.recordPublish(event.name).pipe(Effect.andThen(recordPublished(event.name)))), // Counted here and not in the `catch` above, which only builds the
233
256
  // error: a rising publish-failure rate against a flat published rate
234
257
  // is what a broker outage looks like from the publisher's side, and
235
258
  // the outbox's own backlog is the confirmation.
@@ -271,20 +294,15 @@ const quarantine = (channel, message, queue, failure, attempts)=>{
271
294
  const settle = Effect.try({
272
295
  try: ()=>JSON.parse(message.content.toString()),
273
296
  catch: ()=>UNREADABLE
274
- }).pipe(Effect.flatMap((parsed)=>readEventEnvelope(parsed).pipe(Effect.mapError(classify))), // The handler carries no requirements (the subscriber closes over
297
+ }).pipe(Effect.flatMap((parsed)=>decodeEvent(parsed).pipe(Effect.mapError(classify))), // The handler carries no requirements (the subscriber closes over
275
298
  // its store), so it runs standalone.
276
299
  //
277
- // ⚠️ A handler's failure is classified by its category, not
278
- // treated as transient across the board. The transport
279
- // validates `meta` and deliberately not `data` (inventing an
280
- // opinion about a payload is how the bus would start knowing
281
- // about domains), so a consumer that decodes its own payload
282
- // is the *only* thing that can tell a malformed one from a bad
283
- // afternoon at the database — and it says so by failing with
284
- // a business or `protocol` failure, which quarantines at once
285
- // instead of spending `maxAttempts` on every message
286
- // behind it. Measured on `catalog.published`: a payload the
287
- // contract rejected used to take five deliveries to quarantine.
300
+ // ⚠️ `data` is decoded against the schema its event name
301
+ // registered, so a malformed payload is a `protocol` failure
302
+ // here — poison, quarantined at once — before any handler
303
+ // runs. A handler's own failure is classified by its category
304
+ // the same way: a business or `protocol` failure quarantines
305
+ // instead of spending `maxAttempts` on every message behind it.
288
306
  Effect.flatMap((event)=>handler(event).pipe(Effect.mapError(classify))));
289
307
  // Counted around the whole settle, so a shutdown waits for the ack
290
308
  // as well as the handler: cancelling a consumer stops *new*
@@ -297,7 +315,7 @@ const quarantine = (channel, message, queue, failure, attempts)=>{
297
315
  // inside the observability layer.
298
316
  Effect.matchEffect({
299
317
  onSuccess: ()=>recordConsumed(tags).pipe(Effect.andThen(Effect.sync(()=>channel.ack(message)))),
300
- onFailure: (failure)=>recordFailed(tags, failure.kind).pipe(Effect.andThen(settleFailure(channel, message, queue, subscription, failure, backoff)))
318
+ onFailure: (failure)=>recordFailed(tags, failure.kind).pipe(Effect.andThen(settleFailure(channel, message, queue, subscription, failure, backoff, tags)))
301
319
  }))).finally(()=>{
302
320
  inFlight -= 1;
303
321
  });
@@ -330,7 +348,10 @@ const quarantine = (channel, message, queue, failure, attempts)=>{
330
348
  const connector = yield* AmqpChannelTag;
331
349
  const wiring = yield* WiringRegistryTag;
332
350
  const shutdown = yield* ShutdownRegistryTag;
333
- const { bus, drainDeliveries } = makeAmqpEventBus(connector, wiring);
351
+ const redeliveryBackoff = yield* Effect.serviceOption(RedeliveryBackoffTag);
352
+ const { bus, drainDeliveries } = makeAmqpEventBus(connector, wiring, {
353
+ redeliveryBackoff: Option.getOrUndefined(redeliveryBackoff)
354
+ });
334
355
  // `stop-intake`, and both halves are it: cancelling stops the broker
335
356
  // handing over anything new, then the drain waits for the deliveries
336
357
  // already in progress. Registered here rather than in a service's `main.ts`
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/adapters/amqp-event-bus.ts"],"sourcesContent":["import type { WiringRegistry } from '@entifix/business';\nimport { ShutdownRegistryTag, WiringRegistryTag } from '@entifix/business';\nimport {\n isFailure,\n makeEventEnvelope,\n readEventEnvelope,\n Unavailable,\n} from '@entifix/core';\nimport {\n type EventBus,\n EventBusTag,\n type Subscription,\n} from '@entifix/transactions';\nimport type { Channel, ConsumeMessage } from 'amqplib';\nimport { Duration, Effect, Layer } from 'effect';\n\nimport {\n AmqpChannelTag,\n type AmqpConnector,\n EVENTS_DLX,\n EVENTS_EXCHANGE,\n} from '../amqp-connection/amqp-connection.js';\nimport {\n recordConsumed,\n recordFailed,\n recordPublished,\n recordPublishFailure,\n type SubscriptionTags,\n} from './bus-metrics.js';\n\n/** Appended to a work queue's name to make the queue that holds its failures. */\nconst QUARANTINE_SUFFIX = '.quarantine';\n\n/**\n * How often the shutdown drain re-checks whether the handlers have finished.\n *\n * A poll rather than a latch because a delivery is started from amqplib's own\n * callback, outside any fiber this module holds; the drain's own bound is the\n * shutdown hook's, so this interval only decides how promptly the process\n * notices it is free to go.\n */\nconst DELIVERY_POLL = Duration.millis(25);\n\n/**\n * Why a delivery did not succeed, which is the whole of the nack decision.\n *\n * `transient` is a handler that threw — a dependency briefly gone, a timeout —\n * and is worth another delivery. `poison` is a payload this process cannot\n * read, and is worth **none**: a message that cannot be deserialized never\n * becomes deserializable, so retrying it only spends the delivery budget of the\n * messages queued behind it. Conflating the two is what the adapter once did,\n * on a queue that discarded both.\n *\n * A *business* failure is neither, and never reaches here: that message was\n * handled successfully and produced its own `failed` event.\n */\ninterface DeliveryFailure {\n readonly kind: 'transient' | 'poison';\n /** The failure's code and category, carried onto a quarantined message. */\n readonly code: string;\n readonly category: string;\n}\n\n/**\n * The poison rule, by category and never by class\n * ([ADR 0013](../../../../../docs/adr/0013-failures-are-typed-values-decided-at-the-shell.md)):\n * an answer redelivered is the same answer, so every business failure is\n * poison, and so is `protocol` — bytes that did not decode will not decode on\n * the fifth try. The transient categories are redelivered, bounded by the\n * subscription's `maxAttempts` as the adapter counts them, and so is anything\n * uncategorised: not knowing what went wrong is not evidence that retrying is\n * pointless.\n */\nconst classify = (error: unknown): DeliveryFailure =>\n isFailure(error)\n ? {\n kind:\n error.family === 'business' || error.category === 'protocol'\n ? 'poison'\n : 'transient',\n code: error.code,\n category: error.category,\n }\n : { kind: 'transient', code: 'internal', category: 'internal' };\n\n/** A payload that is not JSON: the sender's fault, decided before any handler. */\nconst UNREADABLE: DeliveryFailure = {\n kind: 'poison',\n code: 'invalidEnvelope',\n category: 'invalid',\n};\n\n/**\n * The queue a subscription binds.\n *\n * `<subscribing slice>.<pattern>`, so the name says who is owed the messages in\n * it — readable in the broker's own UI, which is where someone looks when a\n * queue has depth. The slice is the *subscriber's*, never the publisher's:\n * `EventSourceTag` names the emitter, and one deployment hosts several slices\n * (a host may co-deploy two slices in one process), so\n * deriving from it would file a consumer's queue under whoever happened to\n * publish from the same process.\n *\n * `*` and `#` are spelled out because AMQP 0-9-1 restricts a queue name to\n * letters, digits, hyphen, underscore, period and colon. RabbitMQ is lenient in\n * practice, but the name also has to survive being a URL path segment in the\n * management API, and `%2A` in an operator's address bar is a poor trade for\n * three saved characters.\n */\nexport const queueNameFor = (subscription: Subscription): string =>\n `${subscription.slice}.${subscription.pattern\n .replaceAll('*', '_star_')\n .replaceAll('#', '_hash_')}`;\n\n/**\n * Declares the queue a subscription consumes and returns its name.\n *\n * A **work** queue is durable and quorum-backed, so it survives the consumer's\n * restart and accumulates while it is gone — the hop the outbox relay's\n * durability chain was missing. Its retry ceiling is the subscription's\n * `maxAttempts`, counted by the adapter in {@link ATTEMPTS_HEADER} because the\n * broker does not count a requeued nack (#130). The same number is the queue's\n * `x-delivery-limit`, which bounds what the broker *does* count — a redelivery\n * after a consumer crash.\n *\n * The quarantine is declared **first**: a message dead-lettered the instant the\n * work queue exists must have somewhere to land, and a `direct` exchange with\n * no matching binding drops what it routes.\n *\n * `x-delivery-limit` is immutable once a queue exists. Re-declaring with a\n * different `maxAttempts` fails `PRECONDITION_FAILED` and closes the channel,\n * and there is no safe automatic recovery — so changing the ceiling means\n * deleting the queue, which locally is `pnpm run <app>:dev:reset`.\n */\nconst declareQueue = async (\n channel: Channel,\n subscription: Subscription,\n): Promise<string> => {\n if (subscription.mode === 'broadcast') {\n // Connection-scoped and retained by nobody, which is right only when the\n // replicas differ from one another, as a per-connection push does.\n const { queue } = await channel.assertQueue('', { exclusive: true });\n return queue;\n }\n\n const queue = queueNameFor(subscription);\n const quarantine = `${queue}${QUARANTINE_SUFFIX}`;\n\n await channel.assertQueue(quarantine, {\n durable: true,\n arguments: { 'x-queue-type': 'quorum' },\n });\n await channel.bindQueue(quarantine, EVENTS_DLX, queue);\n await channel.assertQueue(queue, {\n durable: true,\n arguments: {\n 'x-queue-type': 'quorum',\n 'x-delivery-limit': subscription.maxAttempts,\n 'x-dead-letter-exchange': EVENTS_DLX,\n 'x-dead-letter-routing-key': queue,\n },\n });\n\n return queue;\n};\n\n/**\n * The adapter's own count of the attempts a message has failed, carried on the\n * copy it republishes (#130).\n *\n * The broker cannot be trusted with it. RabbitMQ 4 does not count a requeued\n * `basic.nack` toward a quorum queue's `x-delivery-limit` — measured on 4.3.5:\n * a queue limited to 2 delivered one message 8 times and never set\n * `x-delivery-count` — so a transient failure that never cleared was\n * redelivered without a ceiling and without a pause. `x-delivery-limit` stays\n * on the queue, where it still bounds the redeliveries a crashed consumer\n * causes, which the broker does count.\n */\nexport const ATTEMPTS_HEADER = 'x-entifix-attempts';\n\nconst attemptsSoFar = (message: ConsumeMessage): number => {\n const value: unknown = message.properties?.headers?.[ATTEMPTS_HEADER];\n return typeof value === 'number' && Number.isInteger(value) && value > 0\n ? value\n : 0;\n};\n\n/** How long a transient failure waits before its copy goes back on the queue. */\nexport type RedeliveryBackoff = (attempt: number) => Duration.Duration;\n\n/**\n * Doubling from 100 ms, capped at two seconds. The wait holds the delivery —\n * the queue is `prefetch(1)`, so it pauses this consumer — which is the point:\n * a dependency that is down is not helped by asking it again at once.\n */\nexport const DEFAULT_REDELIVERY_BACKOFF: RedeliveryBackoff = attempt =>\n Duration.millis(Math.min(100 * 2 ** (attempt - 1), 2_000));\n\n/**\n * Settles a delivery that failed.\n *\n * Poison on a `work` queue — and a transient failure that has spent the\n * subscription's `maxAttempts` — is republished to the dead-letter exchange\n * **with its reason**: `x-failure-code`, `x-failure-category` and the attempts\n * spent. It is then acked rather than nacked, because a nack cannot carry\n * headers, and a quarantine of messages nobody can say anything about is a\n * queue an operator has to replay blind. If that publish is refused the nack\n * still quarantines it, reasonless.\n *\n * A transient failure under the ceiling waits its backoff, goes back on its own\n * queue as a copy carrying the attempt count, and the original is acked. If\n * that publish is refused it is nacked for the broker to redeliver, which is\n * the old behaviour and still better than losing it. A `broadcast` queue has no\n * quarantine and no ceiling, so its failures are dropped.\n */\nconst settleFailure = (\n channel: Channel,\n message: ConsumeMessage,\n queue: string,\n subscription: Subscription,\n failure: DeliveryFailure,\n backoff: RedeliveryBackoff,\n): Effect.Effect<void> => {\n if (subscription.mode === 'broadcast') {\n return Effect.sync(() => channel.nack(message, false, false));\n }\n const attempts =\n failure.kind === 'transient' ? attemptsSoFar(message) + 1 : 0;\n if (failure.kind === 'poison' || attempts >= subscription.maxAttempts) {\n return Effect.sync(() =>\n quarantine(channel, message, queue, failure, attempts),\n );\n }\n const wait = backoff(attempts);\n return (Duration.isZero(wait) ? Effect.void : Effect.sleep(wait)).pipe(\n Effect.andThen(\n Effect.sync(() => {\n try {\n channel.publish('', queue, message.content, {\n ...message.properties,\n persistent: true,\n headers: {\n ...message.properties?.headers,\n [ATTEMPTS_HEADER]: attempts,\n },\n });\n channel.ack(message);\n } catch {\n channel.nack(message, false, true);\n }\n }),\n ),\n );\n};\n\nconst quarantine = (\n channel: Channel,\n message: ConsumeMessage,\n queue: string,\n failure: DeliveryFailure,\n attempts: number,\n): void => {\n try {\n channel.publish(EVENTS_DLX, queue, message.content, {\n ...message.properties,\n persistent: true,\n headers: {\n ...message.properties?.headers,\n 'x-failure-code': failure.code,\n 'x-failure-category': failure.category,\n ...(attempts > 0 ? { [ATTEMPTS_HEADER]: attempts } : {}),\n },\n });\n channel.ack(message);\n } catch {\n // Quarantined without its reason beats not quarantined.\n channel.nack(message, false, false);\n }\n};\n\n/**\n * RabbitMQ-backed {@link EventBus}. Events go out as `event` envelopes on the\n * shared topic exchange — the adapter owns the wire framing, so the port stays\n * event-typed.\n *\n * **The event's own name is the routing key**, which is what lets a subscriber\n * bind the exact pattern it declared in `tools/slices/*.slice.ts`.\n *\n * The queue a subscriber binds is its own choice of shape. A `work`\n * subscription gets a named durable queue that replicas share and that keeps\n * accumulating while the consumer is down; a `broadcast` one gets an exclusive\n * queue, where every replica sees every event and nothing\n * survives a restart. Choosing broadcast for a workload that is work is how an\n * event published during a rollout is dropped by the broker while the outbox\n * has already recorded it sent.\n *\n * Both methods go through the {@link AmqpConnector} rather than holding a\n * channel, because a channel does not survive the broker restarting. `publish`\n * asks for a live one per call, and `subscribe` registers its setup so the\n * connector can re-run it against the new channel after a reconnect. A durable\n * queue outlives that reconnect; the consumer bound to it does not.\n *\n * It returns the bus **and** its `drainDeliveries`, rather than only the port:\n * waiting for in-flight handlers is an adapter fact — it is this module that\n * runs them — and putting it on `EventBus` would make every implementation of\n * a framework-free port carry a shutdown concern it has no deliveries to drain.\n */\nexport const makeAmqpEventBus = (\n connector: AmqpConnector,\n wiring: WiringRegistry,\n options: { readonly redeliveryBackoff?: RedeliveryBackoff } = {},\n): { bus: EventBus; drainDeliveries: Effect.Effect<void> } => {\n const backoff = options.redeliveryBackoff ?? DEFAULT_REDELIVERY_BACKOFF;\n /**\n * Handlers started and not yet settled.\n *\n * Nothing tracked them before, so a SIGTERM killed them mid-flight and the\n * broker redelivered whatever was unacked — safe only while every consumer\n * happens to be idempotent, which is the assumption the command inbox stops\n * making.\n */\n let inFlight = 0;\n\n const bus: EventBus = {\n publish: event =>\n Effect.tryPromise({\n try: () =>\n connector.withChannel(async channel => {\n channel.publish(\n EVENTS_EXCHANGE,\n event.name,\n Buffer.from(JSON.stringify(makeEventEnvelope(event))),\n { persistent: true },\n );\n }),\n catch: error => new Unavailable({ dependency: 'amqp' }),\n // Recorded **after** the publish succeeds, so `GET /api/$service` reports\n // what this process actually put on the exchange. Recording the intent\n // instead would make the declared-vs-observed diff pass on a service whose\n // every publish is failing.\n }).pipe(\n Effect.tap(() =>\n wiring\n .recordPublish(event.name)\n .pipe(Effect.andThen(recordPublished(event.name))),\n ),\n // Counted here and not in the `catch` above, which only builds the\n // error: a rising publish-failure rate against a flat published rate\n // is what a broker outage looks like from the publisher's side, and\n // the outbox's own backlog is the confirmation.\n Effect.tapError(() => recordPublishFailure(event.name)),\n ),\n\n subscribe: (subscription, handler) =>\n Effect.tryPromise({\n try: () =>\n connector.addConsumer(async channel => {\n // One unacked message at a time: events for a transaction are then\n // folded serially (never concurrently), so an `accepted`/`completed`\n // pair can't race into two upserts.\n await channel.prefetch(1);\n const queue = await declareQueue(channel, subscription);\n await channel.bindQueue(\n queue,\n EVENTS_EXCHANGE,\n subscription.pattern,\n );\n // What the process **bound**, recorded at the point it bound it. The\n // registry deduplicates, so re-running this setup after a reconnect\n // does not double the entry.\n await Effect.runPromise(\n wiring.recordSubscription({\n slice: subscription.slice,\n pattern: subscription.pattern,\n mode: subscription.mode,\n queue,\n }),\n );\n // Resolved once per binding rather than per delivery: `queue` is the\n // durable name for a `work` subscription and the broker's generated\n // one for a `broadcast` queue, so it is only knowable here.\n const tags: SubscriptionTags = {\n queue,\n slice: subscription.slice,\n mode: subscription.mode,\n };\n const { consumerTag } = await channel.consume(\n queue,\n (message: ConsumeMessage | null) => {\n if (message === null) {\n return;\n }\n\n // Reading the payload and running the handler are separated on\n // purpose: they fail for different reasons and earn different\n // treatment. `JSON.parse` is inside the Effect because it throws\n // *synchronously* — outside, it escaped into amqplib's callback and\n // the message was never even nacked.\n const settle = Effect.try({\n try: () => JSON.parse(message.content.toString()) as unknown,\n catch: (): DeliveryFailure => UNREADABLE,\n }).pipe(\n Effect.flatMap(parsed =>\n readEventEnvelope(parsed).pipe(Effect.mapError(classify)),\n ),\n // The handler carries no requirements (the subscriber closes over\n // its store), so it runs standalone.\n //\n // ⚠️ A handler's failure is classified by its category, not\n // treated as transient across the board. The transport\n // validates `meta` and deliberately not `data` (inventing an\n // opinion about a payload is how the bus would start knowing\n // about domains), so a consumer that decodes its own payload\n // is the *only* thing that can tell a malformed one from a bad\n // afternoon at the database — and it says so by failing with\n // a business or `protocol` failure, which quarantines at once\n // instead of spending `maxAttempts` on every message\n // behind it. Measured on `catalog.published`: a payload the\n // contract rejected used to take five deliveries to quarantine.\n Effect.flatMap(event =>\n handler(event).pipe(Effect.mapError(classify)),\n ),\n );\n\n // Counted around the whole settle, so a shutdown waits for the ack\n // as well as the handler: cancelling a consumer stops *new*\n // deliveries and says nothing about this one.\n inFlight += 1;\n void Effect.runPromise(\n settle.pipe(\n // Effect's metric registry is a process-wide singleton, so an\n // increment inside this detached `runPromise` reaches the same\n // producer `NodeSdk` exports from. That is the only reason a\n // metric works here at all — nothing about this fiber is\n // inside the observability layer.\n Effect.matchEffect({\n onSuccess: () =>\n recordConsumed(tags).pipe(\n Effect.andThen(\n Effect.sync(() => channel.ack(message)),\n ),\n ),\n onFailure: failure =>\n recordFailed(tags, failure.kind).pipe(\n Effect.andThen(\n settleFailure(\n channel,\n message,\n queue,\n subscription,\n failure,\n backoff,\n ),\n ),\n ),\n }),\n ),\n ).finally(() => {\n inFlight -= 1;\n });\n },\n );\n return [consumerTag];\n }),\n catch: error => new Unavailable({ dependency: 'amqp' }),\n }),\n };\n\n return {\n bus,\n drainDeliveries: Effect.gen(function* () {\n while (inFlight > 0) {\n yield* Effect.sleep(DELIVERY_POLL);\n }\n }),\n };\n};\n\n/**\n * Provides {@link EventBusTag} from an {@link AmqpChannelTag}.\n *\n * It also takes {@link WiringRegistryTag}, which `makeServerLayer` provides once\n * per service — so the bus and `GET /api/$service` share one instance, and the\n * document reports the bindings this very process made.\n */\nexport const AmqpEventBusLayer = Layer.effect(\n EventBusTag,\n Effect.gen(function* () {\n const connector = yield* AmqpChannelTag;\n const wiring = yield* WiringRegistryTag;\n const shutdown = yield* ShutdownRegistryTag;\n const { bus, drainDeliveries } = makeAmqpEventBus(connector, wiring);\n\n // `stop-intake`, and both halves are it: cancelling stops the broker\n // handing over anything new, then the drain waits for the deliveries\n // already in progress. Registered here rather than in a service's `main.ts`\n // for the reason every registry in this repo exists — a service that gains\n // a bus gains its drain, with nothing to remember.\n yield* shutdown.register({\n name: 'amqp-consumers',\n phase: 'stop-intake',\n run: Effect.promise(() => connector.cancelConsumers()).pipe(\n Effect.andThen(drainDeliveries),\n ),\n });\n\n return bus;\n }),\n);\n"],"names":["ShutdownRegistryTag","WiringRegistryTag","isFailure","makeEventEnvelope","readEventEnvelope","Unavailable","EventBusTag","Duration","Effect","Layer","AmqpChannelTag","EVENTS_DLX","EVENTS_EXCHANGE","recordConsumed","recordFailed","recordPublished","recordPublishFailure","QUARANTINE_SUFFIX","DELIVERY_POLL","millis","classify","error","kind","family","category","code","UNREADABLE","queueNameFor","subscription","slice","pattern","replaceAll","declareQueue","channel","mode","queue","assertQueue","exclusive","quarantine","durable","arguments","bindQueue","maxAttempts","ATTEMPTS_HEADER","attemptsSoFar","message","value","properties","headers","Number","isInteger","DEFAULT_REDELIVERY_BACKOFF","attempt","Math","min","settleFailure","failure","backoff","sync","nack","attempts","wait","isZero","void","sleep","pipe","andThen","publish","content","persistent","ack","makeAmqpEventBus","connector","wiring","options","redeliveryBackoff","inFlight","bus","event","tryPromise","try","withChannel","name","Buffer","from","JSON","stringify","catch","dependency","tap","recordPublish","tapError","subscribe","handler","addConsumer","prefetch","runPromise","recordSubscription","tags","consumerTag","consume","settle","parse","toString","flatMap","parsed","mapError","matchEffect","onSuccess","onFailure","finally","drainDeliveries","gen","AmqpEventBusLayer","effect","shutdown","register","phase","run","promise","cancelConsumers"],"mappings":";AACA,SAASA,mBAAmB,EAAEC,iBAAiB,QAAQ,oBAAoB;AAC3E,SACEC,SAAS,EACTC,iBAAiB,EACjBC,iBAAiB,EACjBC,WAAW,QACN,gBAAgB;AACvB,SAEEC,WAAW,QAEN,wBAAwB;AAE/B,SAASC,QAAQ,EAAEC,MAAM,EAAEC,KAAK,QAAQ,SAAS;AAEjD,SACEC,cAAc,EAEdC,UAAU,EACVC,eAAe,QACV,wCAAwC;AAC/C,SACEC,cAAc,EACdC,YAAY,EACZC,eAAe,EACfC,oBAAoB,QAEf,mBAAmB;AAE1B,+EAA+E,GAC/E,MAAMC,oBAAoB;AAE1B;;;;;;;CAOC,GACD,MAAMC,gBAAgBX,SAASY,MAAM,CAAC;AAsBtC;;;;;;;;;CASC,GACD,MAAMC,WAAW,CAACC,QAChBnB,UAAUmB,SACN;QACEC,MACED,MAAME,MAAM,KAAK,cAAcF,MAAMG,QAAQ,KAAK,aAC9C,WACA;QACNC,MAAMJ,MAAMI,IAAI;QAChBD,UAAUH,MAAMG,QAAQ;IAC1B,IACA;QAAEF,MAAM;QAAa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AAiBC,WAAWC;IAE7D,qEAAqE;IACrE,qEAAqE;IACrE,4EAA4E;IAC5E,2EAA2E;IAC3E,mDAAmD;IACnD,OAAO0C,SAASC,QAAQ,CAAC;QACvBlC,MAAM;QACNmC,OAAO;QACPC,KAAK9G,OAAO+G,OAAO,CAAC,IAAM/C,UAAUgD,eAAe,IAAIvD,IAAI,CACzDzD,OAAO0D,OAAO,CAAC6C;IAEnB;IAEA,OAAOlC;AACT,IACA"}
1
+ {"version":3,"sources":["../../src/adapters/amqp-event-bus.ts"],"sourcesContent":["import type { WiringRegistry } from '@entifix/business';\nimport { ShutdownRegistryTag, WiringRegistryTag } from '@entifix/business';\nimport {\n decodeEvent,\n encodeEvent,\n isFailure,\n Unavailable,\n} from '@entifix/core';\nimport {\n type EventBus,\n EventBusTag,\n type Subscription,\n} from '@entifix/transactions';\nimport type { Channel, ConsumeMessage } from 'amqplib';\nimport { Context, Duration, Effect, Layer, Option } from 'effect';\n\nimport {\n AmqpChannelTag,\n type AmqpConnector,\n EVENTS_DLX,\n EVENTS_EXCHANGE,\n} from '../amqp-connection/amqp-connection.js';\nimport {\n recordConsumed,\n recordFailed,\n recordPublished,\n recordPublishFailure,\n recordQuarantined,\n type SubscriptionTags,\n} from './bus-metrics.js';\n\n/** Appended to a work queue's name to make the queue that holds its failures. */\nconst QUARANTINE_SUFFIX = '.quarantine';\n\n/**\n * How often the shutdown drain re-checks whether the handlers have finished.\n *\n * A poll rather than a latch because a delivery is started from amqplib's own\n * callback, outside any fiber this module holds; the drain's own bound is the\n * shutdown hook's, so this interval only decides how promptly the process\n * notices it is free to go.\n */\nconst DELIVERY_POLL = Duration.millis(25);\n\n/**\n * Why a delivery did not succeed, which is the whole of the nack decision.\n *\n * `transient` is a handler that threw — a dependency briefly gone, a timeout —\n * and is worth another delivery. `poison` is a payload this process cannot\n * read, and is worth **none**: a message that cannot be deserialized never\n * becomes deserializable, so retrying it only spends the delivery budget of the\n * messages queued behind it. Conflating the two is what the adapter once did,\n * on a queue that discarded both.\n *\n * A *business* failure is neither, and never reaches here: that message was\n * handled successfully and produced its own `failed` event.\n */\ninterface DeliveryFailure {\n readonly kind: 'transient' | 'poison';\n /** The failure's code and category, carried onto a quarantined message. */\n readonly code: string;\n readonly category: string;\n}\n\n/**\n * The poison rule, by category and never by class\n * ([ADR 0013](../../../../../docs/adr/0013-failures-are-typed-values-decided-at-the-shell.md)):\n * an answer redelivered is the same answer, so every business failure is\n * poison, and so is `protocol` — bytes that did not decode will not decode on\n * the fifth try. The transient categories are redelivered, bounded by the\n * subscription's `maxAttempts` as the adapter counts them, and so is anything\n * uncategorised: not knowing what went wrong is not evidence that retrying is\n * pointless.\n */\nconst classify = (error: unknown): DeliveryFailure =>\n isFailure(error)\n ? {\n kind:\n error.family === 'business' || error.category === 'protocol'\n ? 'poison'\n : 'transient',\n code: error.code,\n category: error.category,\n }\n : { kind: 'transient', code: 'internal', category: 'internal' };\n\n/** A payload that is not JSON: the sender's fault, decided before any handler. */\nconst UNREADABLE: DeliveryFailure = {\n kind: 'poison',\n code: 'invalidEnvelope',\n category: 'invalid',\n};\n\n/**\n * The queue a subscription binds.\n *\n * `<subscribing slice>.<pattern>`, so the name says who is owed the messages in\n * it — readable in the broker's own UI, which is where someone looks when a\n * queue has depth. The slice is the *subscriber's*, never the publisher's:\n * `EventSourceTag` names the emitter, and one deployment hosts several slices\n * (a host may co-deploy two slices in one process), so\n * deriving from it would file a consumer's queue under whoever happened to\n * publish from the same process.\n *\n * `*` and `#` are spelled out because AMQP 0-9-1 restricts a queue name to\n * letters, digits, hyphen, underscore, period and colon. RabbitMQ is lenient in\n * practice, but the name also has to survive being a URL path segment in the\n * management API, and `%2A` in an operator's address bar is a poor trade for\n * three saved characters.\n */\nexport const queueNameFor = (subscription: Subscription): string =>\n `${subscription.slice}.${subscription.pattern\n .replaceAll('*', '_star_')\n .replaceAll('#', '_hash_')}`;\n\n/**\n * Declares the queue a subscription consumes and returns its name.\n *\n * A **work** queue is durable and quorum-backed, so it survives the consumer's\n * restart and accumulates while it is gone — the hop the outbox relay's\n * durability chain was missing. Its retry ceiling is the subscription's\n * `maxAttempts`, counted by the adapter in {@link ATTEMPTS_HEADER} because the\n * broker does not count a requeued nack (#130). The same number is the queue's\n * `x-delivery-limit`, which bounds what the broker *does* count — a redelivery\n * after a consumer crash.\n *\n * The quarantine is declared **first**: a message dead-lettered the instant the\n * work queue exists must have somewhere to land, and a `direct` exchange with\n * no matching binding drops what it routes.\n *\n * `x-delivery-limit` is immutable once a queue exists. Re-declaring with a\n * different `maxAttempts` fails `PRECONDITION_FAILED` and closes the channel,\n * and there is no safe automatic recovery — so changing the ceiling means\n * deleting the queue, which locally is `pnpm run <app>:dev:reset`.\n */\nconst declareQueue = async (\n channel: Channel,\n subscription: Subscription,\n): Promise<string> => {\n if (subscription.mode === 'broadcast') {\n // Connection-scoped and retained by nobody, which is right only when the\n // replicas differ from one another, as a per-connection push does.\n const { queue } = await channel.assertQueue('', { exclusive: true });\n return queue;\n }\n\n const queue = queueNameFor(subscription);\n const quarantine = `${queue}${QUARANTINE_SUFFIX}`;\n\n await channel.assertQueue(quarantine, {\n durable: true,\n arguments: { 'x-queue-type': 'quorum' },\n });\n await channel.bindQueue(quarantine, EVENTS_DLX, queue);\n await channel.assertQueue(queue, {\n durable: true,\n arguments: {\n 'x-queue-type': 'quorum',\n 'x-delivery-limit': subscription.maxAttempts,\n 'x-dead-letter-exchange': EVENTS_DLX,\n 'x-dead-letter-routing-key': queue,\n },\n });\n\n return queue;\n};\n\n/**\n * The adapter's own count of the attempts a message has failed, carried on the\n * copy it republishes (#130).\n *\n * The broker cannot be trusted with it. RabbitMQ 4 does not count a requeued\n * `basic.nack` toward a quorum queue's `x-delivery-limit` — measured on 4.3.5:\n * a queue limited to 2 delivered one message 8 times and never set\n * `x-delivery-count` — so a transient failure that never cleared was\n * redelivered without a ceiling and without a pause. `x-delivery-limit` stays\n * on the queue, where it still bounds the redeliveries a crashed consumer\n * causes, which the broker does count.\n */\nexport const ATTEMPTS_HEADER = 'x-entifix-attempts';\n\nconst attemptsSoFar = (message: ConsumeMessage): number => {\n const value: unknown = message.properties?.headers?.[ATTEMPTS_HEADER];\n return typeof value === 'number' && Number.isInteger(value) && value > 0\n ? value\n : 0;\n};\n\n/** How long a transient failure waits before its copy goes back on the queue. */\nexport type RedeliveryBackoff = (attempt: number) => Duration.Duration;\n\n/**\n * A backoff doubling from `baseMs`, capped at `capMs`. The wait holds the\n * delivery — the queue is `prefetch(1)`, so it pauses this consumer — which is\n * the point: a dependency that is down is not helped by asking it again at\n * once.\n */\nexport const exponentialRedeliveryBackoff =\n (dials: {\n readonly baseMs: number;\n readonly capMs: number;\n }): RedeliveryBackoff =>\n attempt =>\n Duration.millis(Math.min(dials.baseMs * 2 ** (attempt - 1), dials.capMs));\n\n/** Doubling from 100 ms, capped at two seconds. */\nexport const DEFAULT_REDELIVERY_BACKOFF: RedeliveryBackoff =\n exponentialRedeliveryBackoff({ baseMs: 100, capMs: 2_000 });\n\n/**\n * The redelivery backoff {@link AmqpEventBusLayer} uses, when a composition\n * provides one; {@link DEFAULT_REDELIVERY_BACKOFF} otherwise.\n *\n * A tag rather than a layer option because the dials it is sized from are\n * runtime configuration a host reads at boot, after the layer value already\n * exists. Read with `Effect.serviceOption`, so a host that never sizes it\n * provides nothing — which also means nothing forces one that meant to, and\n * the spec pins both arms.\n */\nexport class RedeliveryBackoffTag extends Context.Service<\n RedeliveryBackoffTag,\n RedeliveryBackoff\n>()('RedeliveryBackoffTag') {}\n\n/**\n * Settles a delivery that failed.\n *\n * Poison on a `work` queue — and a transient failure that has spent the\n * subscription's `maxAttempts` — is republished to the dead-letter exchange\n * **with its reason**: `x-failure-code`, `x-failure-category` and the attempts\n * spent. It is then acked rather than nacked, because a nack cannot carry\n * headers, and a quarantine of messages nobody can say anything about is a\n * queue an operator has to replay blind. If that publish is refused the nack\n * still quarantines it, reasonless.\n *\n * A transient failure under the ceiling waits its backoff, goes back on its own\n * queue as a copy carrying the attempt count, and the original is acked. If\n * that publish is refused it is nacked for the broker to redeliver, which is\n * the old behaviour and still better than losing it. A `broadcast` queue has no\n * quarantine and no ceiling, so its failures are dropped.\n */\nconst settleFailure = (\n channel: Channel,\n message: ConsumeMessage,\n queue: string,\n subscription: Subscription,\n failure: DeliveryFailure,\n backoff: RedeliveryBackoff,\n tags: SubscriptionTags,\n): Effect.Effect<void> => {\n if (subscription.mode === 'broadcast') {\n return Effect.sync(() => channel.nack(message, false, false));\n }\n const attempts =\n failure.kind === 'transient' ? attemptsSoFar(message) + 1 : 0;\n if (failure.kind === 'poison' || attempts >= subscription.maxAttempts) {\n return Effect.sync(() =>\n quarantine(channel, message, queue, failure, attempts),\n ).pipe(Effect.andThen(recordQuarantined(tags, failure.category)));\n }\n const wait = backoff(attempts);\n return (Duration.isZero(wait) ? Effect.void : Effect.sleep(wait)).pipe(\n Effect.andThen(\n Effect.sync(() => {\n try {\n channel.publish('', queue, message.content, {\n ...message.properties,\n persistent: true,\n headers: {\n ...message.properties?.headers,\n [ATTEMPTS_HEADER]: attempts,\n },\n });\n channel.ack(message);\n } catch {\n channel.nack(message, false, true);\n }\n }),\n ),\n );\n};\n\nconst quarantine = (\n channel: Channel,\n message: ConsumeMessage,\n queue: string,\n failure: DeliveryFailure,\n attempts: number,\n): void => {\n try {\n channel.publish(EVENTS_DLX, queue, message.content, {\n ...message.properties,\n persistent: true,\n headers: {\n ...message.properties?.headers,\n 'x-failure-code': failure.code,\n 'x-failure-category': failure.category,\n ...(attempts > 0 ? { [ATTEMPTS_HEADER]: attempts } : {}),\n },\n });\n channel.ack(message);\n } catch {\n // Quarantined without its reason beats not quarantined.\n channel.nack(message, false, false);\n }\n};\n\n/**\n * RabbitMQ-backed {@link EventBus}. Events go out as `event` envelopes on the\n * shared topic exchange — the adapter owns the wire framing, so the port stays\n * event-typed.\n *\n * **The event's own name is the routing key**, which is what lets a subscriber\n * bind the exact pattern it declared in `tools/slices/*.slice.ts`.\n *\n * The queue a subscriber binds is its own choice of shape. A `work`\n * subscription gets a named durable queue that replicas share and that keeps\n * accumulating while the consumer is down; a `broadcast` one gets an exclusive\n * queue, where every replica sees every event and nothing\n * survives a restart. Choosing broadcast for a workload that is work is how an\n * event published during a rollout is dropped by the broker while the outbox\n * has already recorded it sent.\n *\n * Both methods go through the {@link AmqpConnector} rather than holding a\n * channel, because a channel does not survive the broker restarting. `publish`\n * asks for a live one per call, and `subscribe` registers its setup so the\n * connector can re-run it against the new channel after a reconnect. A durable\n * queue outlives that reconnect; the consumer bound to it does not.\n *\n * It returns the bus **and** its `drainDeliveries`, rather than only the port:\n * waiting for in-flight handlers is an adapter fact — it is this module that\n * runs them — and putting it on `EventBus` would make every implementation of\n * a framework-free port carry a shutdown concern it has no deliveries to drain.\n */\nexport const makeAmqpEventBus = (\n connector: AmqpConnector,\n wiring: WiringRegistry,\n options: { readonly redeliveryBackoff?: RedeliveryBackoff } = {},\n): { bus: EventBus; drainDeliveries: Effect.Effect<void> } => {\n const backoff = options.redeliveryBackoff ?? DEFAULT_REDELIVERY_BACKOFF;\n /**\n * Handlers started and not yet settled.\n *\n * Nothing tracked them before, so a SIGTERM killed them mid-flight and the\n * broker redelivered whatever was unacked — safe only while every consumer\n * happens to be idempotent, which is the assumption the command inbox stops\n * making.\n */\n let inFlight = 0;\n\n const bus: EventBus = {\n // Encoded before the channel is touched: a message its registration\n // refuses — no schema, no audience, no version — fails the publish as a\n // protocol failure instead of reaching a consumer that would quarantine it.\n publish: event =>\n Effect.flatMap(encodeEvent(event), envelope =>\n Effect.tryPromise({\n try: () =>\n connector.withChannel(async channel => {\n channel.publish(\n EVENTS_EXCHANGE,\n event.name,\n Buffer.from(JSON.stringify(envelope)),\n { persistent: true },\n );\n }),\n catch: () => new Unavailable({ dependency: 'amqp' }),\n }),\n ).pipe(\n // Recorded **after** the publish succeeds, so `GET /api/$service` reports\n // what this process actually put on the exchange. Recording the intent\n // instead would make the declared-vs-observed diff pass on a service whose\n // every publish is failing.\n Effect.tap(() =>\n wiring\n .recordPublish(event.name)\n .pipe(Effect.andThen(recordPublished(event.name))),\n ),\n // Counted here and not in the `catch` above, which only builds the\n // error: a rising publish-failure rate against a flat published rate\n // is what a broker outage looks like from the publisher's side, and\n // the outbox's own backlog is the confirmation.\n Effect.tapError(() => recordPublishFailure(event.name)),\n ),\n\n subscribe: (subscription, handler) =>\n Effect.tryPromise({\n try: () =>\n connector.addConsumer(async channel => {\n // One unacked message at a time: events for a transaction are then\n // folded serially (never concurrently), so an `accepted`/`completed`\n // pair can't race into two upserts.\n await channel.prefetch(1);\n const queue = await declareQueue(channel, subscription);\n await channel.bindQueue(\n queue,\n EVENTS_EXCHANGE,\n subscription.pattern,\n );\n // What the process **bound**, recorded at the point it bound it. The\n // registry deduplicates, so re-running this setup after a reconnect\n // does not double the entry.\n await Effect.runPromise(\n wiring.recordSubscription({\n slice: subscription.slice,\n pattern: subscription.pattern,\n mode: subscription.mode,\n queue,\n }),\n );\n // Resolved once per binding rather than per delivery: `queue` is the\n // durable name for a `work` subscription and the broker's generated\n // one for a `broadcast` queue, so it is only knowable here.\n const tags: SubscriptionTags = {\n queue,\n slice: subscription.slice,\n mode: subscription.mode,\n };\n const { consumerTag } = await channel.consume(\n queue,\n (message: ConsumeMessage | null) => {\n if (message === null) {\n return;\n }\n\n // Reading the payload and running the handler are separated on\n // purpose: they fail for different reasons and earn different\n // treatment. `JSON.parse` is inside the Effect because it throws\n // *synchronously* — outside, it escaped into amqplib's callback and\n // the message was never even nacked.\n const settle = Effect.try({\n try: () => JSON.parse(message.content.toString()) as unknown,\n catch: (): DeliveryFailure => UNREADABLE,\n }).pipe(\n Effect.flatMap(parsed =>\n decodeEvent(parsed).pipe(Effect.mapError(classify)),\n ),\n // The handler carries no requirements (the subscriber closes over\n // its store), so it runs standalone.\n //\n // ⚠️ `data` is decoded against the schema its event name\n // registered, so a malformed payload is a `protocol` failure\n // here — poison, quarantined at once — before any handler\n // runs. A handler's own failure is classified by its category\n // the same way: a business or `protocol` failure quarantines\n // instead of spending `maxAttempts` on every message behind it.\n Effect.flatMap(event =>\n handler(event).pipe(Effect.mapError(classify)),\n ),\n );\n\n // Counted around the whole settle, so a shutdown waits for the ack\n // as well as the handler: cancelling a consumer stops *new*\n // deliveries and says nothing about this one.\n inFlight += 1;\n void Effect.runPromise(\n settle.pipe(\n // Effect's metric registry is a process-wide singleton, so an\n // increment inside this detached `runPromise` reaches the same\n // producer `NodeSdk` exports from. That is the only reason a\n // metric works here at all — nothing about this fiber is\n // inside the observability layer.\n Effect.matchEffect({\n onSuccess: () =>\n recordConsumed(tags).pipe(\n Effect.andThen(\n Effect.sync(() => channel.ack(message)),\n ),\n ),\n onFailure: failure =>\n recordFailed(tags, failure.kind).pipe(\n Effect.andThen(\n settleFailure(\n channel,\n message,\n queue,\n subscription,\n failure,\n backoff,\n tags,\n ),\n ),\n ),\n }),\n ),\n ).finally(() => {\n inFlight -= 1;\n });\n },\n );\n return [consumerTag];\n }),\n catch: error => new Unavailable({ dependency: 'amqp' }),\n }),\n };\n\n return {\n bus,\n drainDeliveries: Effect.gen(function* () {\n while (inFlight > 0) {\n yield* Effect.sleep(DELIVERY_POLL);\n }\n }),\n };\n};\n\n/**\n * Provides {@link EventBusTag} from an {@link AmqpChannelTag}.\n *\n * It also takes {@link WiringRegistryTag}, which `makeServerLayer` provides once\n * per service — so the bus and `GET /api/$service` share one instance, and the\n * document reports the bindings this very process made.\n */\nexport const AmqpEventBusLayer = Layer.effect(\n EventBusTag,\n Effect.gen(function* () {\n const connector = yield* AmqpChannelTag;\n const wiring = yield* WiringRegistryTag;\n const shutdown = yield* ShutdownRegistryTag;\n const redeliveryBackoff = yield* Effect.serviceOption(RedeliveryBackoffTag);\n const { bus, drainDeliveries } = makeAmqpEventBus(connector, wiring, {\n redeliveryBackoff: Option.getOrUndefined(redeliveryBackoff),\n });\n\n // `stop-intake`, and both halves are it: cancelling stops the broker\n // handing over anything new, then the drain waits for the deliveries\n // already in progress. Registered here rather than in a service's `main.ts`\n // for the reason every registry in this repo exists — a service that gains\n // a bus gains its drain, with nothing to remember.\n yield* shutdown.register({\n name: 'amqp-consumers',\n phase: 'stop-intake',\n run: Effect.promise(() => connector.cancelConsumers()).pipe(\n Effect.andThen(drainDeliveries),\n ),\n });\n\n return bus;\n }),\n);\n"],"names":["ShutdownRegistryTag","WiringRegistryTag","decodeEvent","encodeEvent","isFailure","Unavailable","EventBusTag","Context","Duration","Effect","Layer","Option","AmqpChannelTag","EVENTS_DLX","EVENTS_EXCHANGE","recordConsumed","recordFailed","recordPublished","recordPublishFailure","recordQuarantined","QUARANTINE_SUFFIX","DELIVERY_POLL","millis","classify","error","kind","family","category","code","UNREADABLE","queueNameFor","subscription","slice","pattern","replaceAll","declareQueue","channel","mode","queue","assertQueue","exclusive","quarantine","durable","arguments","bindQueue","maxAttempts","ATTEMPTS_HEADER","attemptsSoFar","message","value","properties","headers","Number","isInteger","exponentialRedeliveryBackoff","dials","attempt","Math","min","baseMs","capMs","DEFAULT_REDELIVERY_BACKOFF","RedeliveryBackoffTag","Service","settleFailure","failure","backoff","tags","sync","nack","attempts","pipe","andThen","wait","isZero","void","sleep","publish","content","persistent","ack","makeAmqpEventBus","connector","wiring","options","redeliveryBackoff","inFlight","bus","event","flatMap","envelope","tryPromise","try","withChannel","name","Buffer","from","JSON","stringify","catch","dependency","tap","recordPublish","tapError","subscribe","handler","addConsumer","prefetch","runPromise","recordSubscription","consumerTag","consume","settle","parse","toString","parsed","mapError","matchEffect","onSuccess","onFailure","finally","drainDeliveries","gen","AmqpEventBusLayer","effect","shutdown","serviceOption","getOrUndefined","register","phase","run","promise","cancelConsumers"],"mappings":";AACA,SAASA,mBAAmB,EAAEC,iBAAiB,QAAQ,oBAAoB;AAC3E,SACEC,WAAW,EACXC,WAAW,EACXC,SAAS,EACTC,WAAW,QACN,gBAAgB;AACvB,SAEEC,WAAW,QAEN,wBAAwB;AAE/B,SAASC,OAAO,EAAEC,QAAQ,EAAEC,MAAM,EAAEC,KAAK,E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C;AACT,IACA"}
@@ -31,17 +31,26 @@ export declare const busPublishFailures: Metric.Counter<number>;
31
31
  /** Deliveries whose handler completed and were acked, by subscription. */
32
32
  export declare const busEventsConsumed: Metric.Counter<number>;
33
33
  /**
34
- * Deliveries that were nacked, by subscription and failure class.
34
+ * Deliveries that failed, by subscription and failure class.
35
35
  *
36
- * ⚠️ **This is not a dead-letter count, and cannot be.** `x-delivery-limit` is
37
- * what moves a message to `<queue>.quarantine`, and the broker does that without
38
- * telling the adapter — there is no in-process call site for it. A transient
39
- * failure on its fifth delivery is counted here exactly like its first. The
40
- * honest quarantine count on the publisher side is the outbox's; on this side,
41
- * a rising `failure="transient"` rate against a fixed `maxAttempts` is the
42
- * signal, and the quarantine queue's own depth is the confirmation.
36
+ * Every failed delivery, including the ones that go on to be retried, so a
37
+ * transient failure on its fifth attempt is counted here exactly like its
38
+ * first. How many messages were finally given up on is
39
+ * {@link busEventsQuarantined}.
43
40
  */
44
41
  export declare const busEventsFailed: Metric.Counter<number>;
42
+ /**
43
+ * Messages moved to `<queue>.quarantine`, by subscription and failure category.
44
+ *
45
+ * Possible since the adapter, not the broker, decides the ceiling (#130): every
46
+ * quarantine now goes through one call site in the adapter, so this is the
47
+ * dead-letter count `bus_events_failed_total` could never be. A poison message
48
+ * is counted on its first delivery, and a transient one when it has spent its
49
+ * subscription's `maxAttempts`. A redelivery the broker dead-letters after a
50
+ * consumer crash (`x-delivery-limit`) still bypasses the adapter, so the
51
+ * quarantine queue's depth stays the confirmation.
52
+ */
53
+ export declare const busEventsQuarantined: Metric.Counter<number>;
45
54
  /**
46
55
  * Identity of a subscription, as metric tags.
47
56
  *
@@ -70,4 +79,12 @@ export declare const recordConsumed: (tags: SubscriptionTags) => import("effect/
70
79
  * incomparable.
71
80
  */
72
81
  export declare const recordFailed: (tags: SubscriptionTags, failure: "transient" | "poison") => import("effect/Effect").Effect<void, never, never>;
82
+ /**
83
+ * Record one message moved to its quarantine queue.
84
+ *
85
+ * `category` rather than `code`: the categories are a closed set and the codes
86
+ * are not, and a metric attribute with an open set of values is a series per
87
+ * typo.
88
+ */
89
+ export declare const recordQuarantined: (tags: SubscriptionTags, category: string) => import("effect/Effect").Effect<void, never, never>;
73
90
  //# sourceMappingURL=bus-metrics.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bus-metrics.d.ts","sourceRoot":"","sources":["../../src/adapters/bus-metrics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC;;;;;;;;;;GAUG;AAEH;;;;;;GAMG;AAEH,kDAAkD;AAClD,eAAO,MAAM,kBAAkB,wBAE7B,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,wBAE7B,CAAC;AAEH,0EAA0E;AAC1E,eAAO,MAAM,iBAAiB,wBAE5B,CAAC;AAEH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,wBAI1B,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,gBAAgB,CAAC;CACxB;AAYD,+CAA+C;AAC/C,eAAO,MAAM,eAAe,GAAI,WAAW,MAAM,uDAI9C,CAAC;AAEJ,6CAA6C;AAC7C,eAAO,MAAM,oBAAoB,GAAI,WAAW,MAAM,uDAInD,CAAC;AAEJ,6DAA6D;AAC7D,eAAO,MAAM,cAAc,GAAI,MAAM,gBAAgB,uDACQ,CAAC;AAE9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GACvB,MAAM,gBAAgB,EACtB,SAAS,WAAW,GAAG,QAAQ,uDAK9B,CAAC"}
1
+ {"version":3,"file":"bus-metrics.d.ts","sourceRoot":"","sources":["../../src/adapters/bus-metrics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC;;;;;;;;;;GAUG;AAEH;;;;;;GAMG;AAEH,kDAAkD;AAClD,eAAO,MAAM,kBAAkB,wBAE7B,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,wBAE7B,CAAC;AAEH,0EAA0E;AAC1E,eAAO,MAAM,iBAAiB,wBAE5B,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,wBAI1B,CAAC;AAEH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,wBAOhC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,gBAAgB,CAAC;CACxB;AAYD,+CAA+C;AAC/C,eAAO,MAAM,eAAe,GAAI,WAAW,MAAM,uDAI9C,CAAC;AAEJ,6CAA6C;AAC7C,eAAO,MAAM,oBAAoB,GAAI,WAAW,MAAM,uDAInD,CAAC;AAEJ,6DAA6D;AAC7D,eAAO,MAAM,cAAc,GAAI,MAAM,gBAAgB,uDACQ,CAAC;AAE9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GACvB,MAAM,gBAAgB,EACtB,SAAS,WAAW,GAAG,QAAQ,uDAK9B,CAAC;AAEJ;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,MAAM,gBAAgB,EAAE,UAAU,MAAM,uDAMvE,CAAC"}
@@ -31,17 +31,27 @@ import { Metric } from 'effect';
31
31
  description: 'Deliveries acked after their handler completed.'
32
32
  });
33
33
  /**
34
- * Deliveries that were nacked, by subscription and failure class.
34
+ * Deliveries that failed, by subscription and failure class.
35
35
  *
36
- * ⚠️ **This is not a dead-letter count, and cannot be.** `x-delivery-limit` is
37
- * what moves a message to `<queue>.quarantine`, and the broker does that without
38
- * telling the adapter — there is no in-process call site for it. A transient
39
- * failure on its fifth delivery is counted here exactly like its first. The
40
- * honest quarantine count on the publisher side is the outbox's; on this side,
41
- * a rising `failure="transient"` rate against a fixed `maxAttempts` is the
42
- * signal, and the quarantine queue's own depth is the confirmation.
36
+ * Every failed delivery, including the ones that go on to be retried, so a
37
+ * transient failure on its fifth attempt is counted here exactly like its
38
+ * first. How many messages were finally given up on is
39
+ * {@link busEventsQuarantined}.
43
40
  */ export const busEventsFailed = Metric.counter('bus_events_failed_total', {
44
- description: 'Deliveries nacked, by subscription and failure class. Not a ' + 'dead-letter count — the broker dead-letters without telling the adapter.'
41
+ description: 'Deliveries whose handler or payload failed, by subscription and ' + 'failure class, retried or not.'
42
+ });
43
+ /**
44
+ * Messages moved to `<queue>.quarantine`, by subscription and failure category.
45
+ *
46
+ * Possible since the adapter, not the broker, decides the ceiling (#130): every
47
+ * quarantine now goes through one call site in the adapter, so this is the
48
+ * dead-letter count `bus_events_failed_total` could never be. A poison message
49
+ * is counted on its first delivery, and a transient one when it has spent its
50
+ * subscription's `maxAttempts`. A redelivery the broker dead-letters after a
51
+ * consumer crash (`x-delivery-limit`) still bypasses the adapter, so the
52
+ * quarantine queue's depth stays the confirmation.
53
+ */ export const busEventsQuarantined = Metric.counter('bus_events_quarantined_total', {
54
+ description: 'Messages the adapter moved to a quarantine queue, by subscription ' + 'and failure category.'
45
55
  });
46
56
  const withSubscription = (metric, tags)=>metric.pipe(Metric.withAttributes({
47
57
  queue: tags.queue
@@ -67,5 +77,14 @@ const withSubscription = (metric, tags)=>metric.pipe(Metric.withAttributes({
67
77
  */ export const recordFailed = (tags, failure)=>Metric.update(Metric.withAttributes(withSubscription(busEventsFailed, tags), {
68
78
  failure
69
79
  }), 1);
80
+ /**
81
+ * Record one message moved to its quarantine queue.
82
+ *
83
+ * `category` rather than `code`: the categories are a closed set and the codes
84
+ * are not, and a metric attribute with an open set of values is a series per
85
+ * typo.
86
+ */ export const recordQuarantined = (tags, category)=>Metric.update(Metric.withAttributes(withSubscription(busEventsQuarantined, tags), {
87
+ category
88
+ }), 1);
70
89
 
71
90
  //# sourceMappingURL=bus-metrics.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/adapters/bus-metrics.ts"],"sourcesContent":["import type { SubscriptionMode } from '@entifix/transactions';\nimport { Metric } from 'effect';\n\n/**\n * The bus half of the framework's first metric set; the outbox half is in\n * `@entifix/mongo`.\n *\n * Counters rather than logs because a quarantined message that is only ever a\n * log line is indistinguishable from a dropped one when the question is *how\n * many*. Nothing here needs plumbing: `NodeSdk` wires the metric reader through\n * `@effect/opentelemetry`'s `Metrics.layer`, whose producer reads **Effect's own\n * global registry**, so an increment in any fiber — including the detached one\n * the AMQP consumer settles a delivery in — is exported.\n */\n\n/**\n * ⚠️ These metric objects are **exported so a reader uses the same instance**.\n * An Effect metric's registry key includes its description, so\n * `Metric.counter('bus_events_published_total')` written out again — without the\n * description — addresses a *different* series that is always zero. That is a\n * silent way to write a dashboard query against nothing.\n */\n\n/** Events handed to the broker, by event name. */\nexport const busEventsPublished = Metric.counter('bus_events_published_total', {\n description: 'Events accepted by the broker, by event name.',\n});\n\n/**\n * Publishes the broker refused, by event name.\n *\n * Separate from the outbox's own failure count on purpose: this one says the\n * broker would not take it, the outbox's says how many attempts that entry has\n * spent. One is a rate, the other is a backlog.\n */\nexport const busPublishFailures = Metric.counter('bus_publish_failures_total', {\n description: 'Publishes the broker refused, by event name.',\n});\n\n/** Deliveries whose handler completed and were acked, by subscription. */\nexport const busEventsConsumed = Metric.counter('bus_events_consumed_total', {\n description: 'Deliveries acked after their handler completed.',\n});\n\n/**\n * Deliveries that were nacked, by subscription and failure class.\n *\n * ⚠️ **This is not a dead-letter count, and cannot be.** `x-delivery-limit` is\n * what moves a message to `<queue>.quarantine`, and the broker does that without\n * telling the adapter — there is no in-process call site for it. A transient\n * failure on its fifth delivery is counted here exactly like its first. The\n * honest quarantine count on the publisher side is the outbox's; on this side,\n * a rising `failure=\"transient\"` rate against a fixed `maxAttempts` is the\n * signal, and the quarantine queue's own depth is the confirmation.\n */\nexport const busEventsFailed = Metric.counter('bus_events_failed_total', {\n description:\n 'Deliveries nacked, by subscription and failure class. Not a ' +\n 'dead-letter count — the broker dead-letters without telling the adapter.',\n});\n\n/**\n * Identity of a subscription, as metric tags.\n *\n * `queue` is the durable work-queue name and so the only stable identity a\n * broadcast subscription does *not* have — its queue is anonymous and\n * regenerated per connection — which is why `slice` and `mode` are carried\n * beside it rather than derived from it.\n */\nexport interface SubscriptionTags {\n queue: string;\n slice: string;\n mode: SubscriptionMode;\n}\n\nconst withSubscription = (\n metric: Metric.Counter<number>,\n tags: SubscriptionTags,\n) =>\n metric.pipe(\n Metric.withAttributes({ queue: tags.queue }),\n Metric.withAttributes({ slice: tags.slice }),\n Metric.withAttributes({ mode: tags.mode }),\n );\n\n/** Record one event accepted by the broker. */\nexport const recordPublished = (eventName: string) =>\n Metric.update(\n Metric.withAttributes(busEventsPublished, { event: eventName }),\n 1,\n );\n\n/** Record one publish the broker refused. */\nexport const recordPublishFailure = (eventName: string) =>\n Metric.update(\n Metric.withAttributes(busPublishFailures, { event: eventName }),\n 1,\n );\n\n/** Record one delivery acked after its handler completed. */\nexport const recordConsumed = (tags: SubscriptionTags) =>\n Metric.update(withSubscription(busEventsConsumed, tags), 1);\n\n/**\n * Record one nacked delivery.\n *\n * `failure` is the class, never the event name: a poison message by definition\n * could not be parsed, so it has no `event.name` to attribute it to, and\n * dimensioning only the arm that has one would make the two counts\n * incomparable.\n */\nexport const recordFailed = (\n tags: SubscriptionTags,\n failure: 'transient' | 'poison',\n) =>\n Metric.update(\n Metric.withAttributes(withSubscription(busEventsFailed, tags), { failure }),\n 1,\n );\n"],"names":["Metric","busEventsPublished","counter","description","busPublishFailures","busEventsConsumed","busEventsFailed","withSubscription","metric","tags","pipe","withAttributes","queue","slice","mode","recordPublished","eventName","update","event","recordPublishFailure","recordConsumed","recordFailed","failure"],"mappings":"AACA,SAASA,MAAM,QAAQ,SA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,GACA"}
1
+ {"version":3,"sources":["../../src/adapters/bus-metrics.ts"],"sourcesContent":["import type { SubscriptionMode } from '@entifix/transactions';\nimport { Metric } from 'effect';\n\n/**\n * The bus half of the framework's first metric set; the outbox half is in\n * `@entifix/mongo`.\n *\n * Counters rather than logs because a quarantined message that is only ever a\n * log line is indistinguishable from a dropped one when the question is *how\n * many*. Nothing here needs plumbing: `NodeSdk` wires the metric reader through\n * `@effect/opentelemetry`'s `Metrics.layer`, whose producer reads **Effect's own\n * global registry**, so an increment in any fiber — including the detached one\n * the AMQP consumer settles a delivery in — is exported.\n */\n\n/**\n * ⚠️ These metric objects are **exported so a reader uses the same instance**.\n * An Effect metric's registry key includes its description, so\n * `Metric.counter('bus_events_published_total')` written out again — without the\n * description — addresses a *different* series that is always zero. That is a\n * silent way to write a dashboard query against nothing.\n */\n\n/** Events handed to the broker, by event name. */\nexport const busEventsPublished = Metric.counter('bus_events_published_total', {\n description: 'Events accepted by the broker, by event name.',\n});\n\n/**\n * Publishes the broker refused, by event name.\n *\n * Separate from the outbox's own failure count on purpose: this one says the\n * broker would not take it, the outbox's says how many attempts that entry has\n * spent. One is a rate, the other is a backlog.\n */\nexport const busPublishFailures = Metric.counter('bus_publish_failures_total', {\n description: 'Publishes the broker refused, by event name.',\n});\n\n/** Deliveries whose handler completed and were acked, by subscription. */\nexport const busEventsConsumed = Metric.counter('bus_events_consumed_total', {\n description: 'Deliveries acked after their handler completed.',\n});\n\n/**\n * Deliveries that failed, by subscription and failure class.\n *\n * Every failed delivery, including the ones that go on to be retried, so a\n * transient failure on its fifth attempt is counted here exactly like its\n * first. How many messages were finally given up on is\n * {@link busEventsQuarantined}.\n */\nexport const busEventsFailed = Metric.counter('bus_events_failed_total', {\n description:\n 'Deliveries whose handler or payload failed, by subscription and ' +\n 'failure class, retried or not.',\n});\n\n/**\n * Messages moved to `<queue>.quarantine`, by subscription and failure category.\n *\n * Possible since the adapter, not the broker, decides the ceiling (#130): every\n * quarantine now goes through one call site in the adapter, so this is the\n * dead-letter count `bus_events_failed_total` could never be. A poison message\n * is counted on its first delivery, and a transient one when it has spent its\n * subscription's `maxAttempts`. A redelivery the broker dead-letters after a\n * consumer crash (`x-delivery-limit`) still bypasses the adapter, so the\n * quarantine queue's depth stays the confirmation.\n */\nexport const busEventsQuarantined = Metric.counter(\n 'bus_events_quarantined_total',\n {\n description:\n 'Messages the adapter moved to a quarantine queue, by subscription ' +\n 'and failure category.',\n },\n);\n\n/**\n * Identity of a subscription, as metric tags.\n *\n * `queue` is the durable work-queue name and so the only stable identity a\n * broadcast subscription does *not* have — its queue is anonymous and\n * regenerated per connection — which is why `slice` and `mode` are carried\n * beside it rather than derived from it.\n */\nexport interface SubscriptionTags {\n queue: string;\n slice: string;\n mode: SubscriptionMode;\n}\n\nconst withSubscription = (\n metric: Metric.Counter<number>,\n tags: SubscriptionTags,\n) =>\n metric.pipe(\n Metric.withAttributes({ queue: tags.queue }),\n Metric.withAttributes({ slice: tags.slice }),\n Metric.withAttributes({ mode: tags.mode }),\n );\n\n/** Record one event accepted by the broker. */\nexport const recordPublished = (eventName: string) =>\n Metric.update(\n Metric.withAttributes(busEventsPublished, { event: eventName }),\n 1,\n );\n\n/** Record one publish the broker refused. */\nexport const recordPublishFailure = (eventName: string) =>\n Metric.update(\n Metric.withAttributes(busPublishFailures, { event: eventName }),\n 1,\n );\n\n/** Record one delivery acked after its handler completed. */\nexport const recordConsumed = (tags: SubscriptionTags) =>\n Metric.update(withSubscription(busEventsConsumed, tags), 1);\n\n/**\n * Record one nacked delivery.\n *\n * `failure` is the class, never the event name: a poison message by definition\n * could not be parsed, so it has no `event.name` to attribute it to, and\n * dimensioning only the arm that has one would make the two counts\n * incomparable.\n */\nexport const recordFailed = (\n tags: SubscriptionTags,\n failure: 'transient' | 'poison',\n) =>\n Metric.update(\n Metric.withAttributes(withSubscription(busEventsFailed, tags), { failure }),\n 1,\n );\n\n/**\n * Record one message moved to its quarantine queue.\n *\n * `category` rather than `code`: the categories are a closed set and the codes\n * are not, and a metric attribute with an open set of values is a series per\n * typo.\n */\nexport const recordQuarantined = (tags: SubscriptionTags, category: string) =>\n Metric.update(\n Metric.withAttributes(withSubscription(busEventsQuarantined, tags), {\n category,\n }),\n 1,\n );\n"],"names":["Metric","busEventsPublished","counter","description","busPublishFailures","busEventsConsumed","busEventsFailed","busEventsQuarantined","withSubscription","metric","tags","pipe","withAttributes","queue","slice","mode","recordPublished","eventName","update","event","recordPublishFailure","recordConsumed","recordFailed","failure","recordQuarantined","category"],"mappings":"AACA,SAASA,MAAM,QAAQ,SAAS;AAEhC;;;;;;;;;;CAUC,GAED;;;;;;CAMC,GAED,gDAAgD,GAChD,OAAO,MAAMC,qBAAqBD,OAAOE,OAAO,CAAC,8BAA8B;IAC7EC,aAAa;AACf,GAAG;AAEH;;;;;;CAMC,GACD,OAAO,MAAMC,qBAAqBJ,OAAOE,OAAO,CAAC,8BAA8B;IAC7EC,aAAa;AACf,GAAG;AAEH,wEAAwE,GACxE,OAAO,MAAME,oBAAoBL,OAAOE,OAAO,CAAC,6BAA6B;IAC3EC,aAAa;AACf,GAAG;AAEH;;;;;;;CAOC,GACD,OAAO,MAAMG,kBAAkBN,OAAOE,OAAO,CAAC,2BAA2B;IACvEC,aACE,qEACA;AACJ,GAAG;AAEH;;;;;;;;;;CAUC,GACD,OAAO,MAAMI,uBAAuBP,OAAOE,OAAO,CAChD,gCACA;IACEC,aACE,uEACA;AACJ,GACA;AAgBF,MAAMK,mBAAmB,CACvBC,QACAC,OAEAD,OAAOE,IAAI,CACTX,OAAOY,cAAc,CAAC;QAAEC,OAAOH,KAAKG,KAAK;IAAC,IAC1Cb,OAAOY,cAAc,CAAC;QAAEE,OAAOJ,KAAKI,KAAK;IAAC,IAC1Cd,OAAOY,cAAc,CAAC;QAAEG,MAAML,KAAKK,IAAI;IAAC;AAG5C,6CAA6C,GAC7C,OAAO,MAAMC,kBAAkB,CAACC,YAC9BjB,OAAOkB,MAAM,CACXlB,OAAOY,cAAc,CAACX,oBAAoB;QAAEkB,OAAOF;IAAU,IAC7D,GACA;AAEJ,2CAA2C,GAC3C,OAAO,MAAMG,uBAAuB,CAACH,YACnCjB,OAAOkB,MAAM,CACXlB,OAAOY,cAAc,CAACR,oBAAoB;QAAEe,OAAOF;IAAU,IAC7D,GACA;AAEJ,2DAA2D,GAC3D,OAAO,MAAMI,iBAAiB,CAACX,OAC7BV,OAAOkB,MAAM,CAACV,iBAAiBH,mBAAmBK,OAAO,GAAG;AAE9D;;;;;;;CAOC,GACD,OAAO,MAAMY,eAAe,CAC1BZ,MACAa,UAEAvB,OAAOkB,MAAM,CACXlB,OAAOY,cAAc,CAACJ,iBAAiBF,iBAAiBI,OAAO;QAAEa;IAAQ,IACzE,GACA;AAEJ;;;;;;CAMC,GACD,OAAO,MAAMC,oBAAoB,CAACd,MAAwBe,WACxDzB,OAAOkB,MAAM,CACXlB,OAAOY,cAAc,CAACJ,iBAAiBD,sBAAsBG,OAAO;QAClEe;IACF,IACA,GACA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@entifix/amqp",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "description": "RabbitMQ connection and health probe, and an optional event bus for @entifix/transactions with retry and quarantine.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -57,18 +57,18 @@
57
57
  "dependencies": {
58
58
  "@swc/helpers": "0.5.23",
59
59
  "amqplib": "2.2.0",
60
- "@entifix/business": "0.10.1",
61
- "@entifix/core": "0.10.1"
60
+ "@entifix/business": "0.11.0",
61
+ "@entifix/core": "0.11.0"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@types/amqplib": "0.10.8",
65
65
  "effect": "4.0.6",
66
- "@entifix/testing-unit": "0.10.1",
67
- "@entifix/transactions": "0.10.1"
66
+ "@entifix/testing-unit": "0.11.0",
67
+ "@entifix/transactions": "0.11.0"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "effect": "^4.0.6",
71
- "@entifix/transactions": "0.10.1"
71
+ "@entifix/transactions": "0.11.0"
72
72
  },
73
73
  "peerDependenciesMeta": {
74
74
  "@entifix/transactions": {