@diia-inhouse/diia-queue 14.2.57 → 14.3.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.
package/dist/constants.js CHANGED
@@ -3,8 +3,7 @@ var constants_default = {
3
3
  DEFAULT_ROUTING_KEY: "*",
4
4
  APP_ID: "Diia App",
5
5
  PROJECT_NAME: "diia",
6
- PORTAL_NAME: "portal",
7
- REPLY_TO_QUEUE_NAME: "amq.rabbitmq.reply-to"
6
+ PORTAL_NAME: "portal"
8
7
  };
9
8
  //#endregion
10
9
  export { constants_default as default };
package/dist/index.d.ts CHANGED
@@ -4,11 +4,11 @@ import { AmqpConfig, AmqpConnectionEventNames, ConnectOptions, ConnectionStatus,
4
4
  import { AmqpConnection } from "./providers/rabbitmq/amqpConnection.js";
5
5
  import { EventName, ExternalServiceConfig, InternalServiceConfig, QueueConfigByQueueName, QueueConfigType, QueueName, ServiceConfig, ServiceConfigByConfigType, ServiceRulesConfig, TopicConfig, TopicConfigByConfigType } from "./interfaces/queueConfig/configs.js";
6
6
  import { Arguments, ConnectionClientType, ConnectionList, DeclareOptions, ExportConfig, Headers, Message, MessageData, MessageProperties, NackOptions, QueueMessage, QueueMessageData, QueueMessageError, QueueMessageMetaData, RabbitMQConfig, RabbitMQConfigCustomParams, RabbitMQStatus } from "./interfaces/providers/rabbitmq/index.js";
7
- import { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult } from "./providers/rabbitmq/amqpPublisher.types.js";
7
+ import { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult, ReplyToQueueName } from "./providers/rabbitmq/amqpPublisher.types.js";
8
8
  import { MessageHandler } from "./interfaces/messageHandler.js";
9
9
  import { MessageBrokerServicesStatus, QueueConnectionType, QueueStatus, QueueStatusByType } from "./interfaces/queueStatus.js";
10
10
  import { QueueContext } from "./interfaces/queueContext.js";
11
- import { CommunicationDirection, CommunicationsTotalLabelsMap, LabelUnknown, Metrics, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, communicationsTotalLabelsMap } from "./interfaces/metrics/index.js";
11
+ import { CommunicationDirection, CommunicationsTotalLabelsMap, LabelUnknown, Metrics, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, unroutedMessagesLabelsMap } from "./interfaces/metrics/index.js";
12
12
  import { ExternalCommunicatorFailureResponse, ExternalCommunicatorResponse, ExternalCommunicatorResponseError, ExternalCommunicatorSuccessResponse, ReceiveDirectOps } from "./interfaces/externalCommunicator.js";
13
13
  import { EventBusListener, EventBusQueue, EventListeners, ExternalEventBusQueue, InternalQueueConfig, Listener, MessageBrokerServiceEventsListener, MessageBrokerServiceListener, QueueConfig, QueueConnectionConfig, ScheduledTasksQueue, TaskListener, TaskQueue } from "./interfaces/index.js";
14
14
  import { AmqpAsserter } from "./providers/rabbitmq/amqpAsserter.js";
@@ -24,4 +24,4 @@ import { Queue } from "./services/queue.js";
24
24
  import { ScheduledTask } from "./services/scheduledTask.js";
25
25
  import { Task } from "./services/task.js";
26
26
  import { CommonQueueDeps, ExternalQueueDeps, InternalQueueDeps, QueueDeps } from "./interfaces/deps.js";
27
- export { AmqpAsserter, AmqpConfig, AmqpConnection, AmqpConnectionEventNames, AmqpListener, AmqpPublisher, AmqpPublisherPublishOptions, Arguments, BaseQueueOptions, BindOptions, CommonQueueDeps, CommunicationDirection, CommunicationsTotalLabelsMap, ConnectOptions, ConnectionClientType, ConnectionList, ConnectionStatus, ConsumerOptions, DeclareOptions, DirectResponse, DirectResponseHeaders, EventBus, EventBusListener, EventBusQueue, EventListeners, EventMessageHandler, EventMessageValidator, EventName, ExchangeName, ExchangeOptions, ExchangeType, ExportConfig, ExternalCommunicator, ExternalCommunicatorFailureResponse, ExternalCommunicatorResponse, ExternalCommunicatorResponseError, ExternalCommunicatorSuccessResponse, ExternalEventBus, ExternalEventBusQueue, ExternalQueueDeps, ExternalServiceConfig, Headers, InternalQueueConfig, InternalQueueDeps, InternalServiceConfig, LabelUnknown, Listener, ListenerOptions, Message, MessageBrokerExternalServiceType, MessageBrokerExternalServiceTypes, MessageBrokerGeneralServiceType, MessageBrokerGeneralServiceTypes, MessageBrokerInternalServiceType, MessageBrokerInternalServiceTypes, MessageBrokerServiceConfig, MessageBrokerServiceEventsListener, MessageBrokerServiceListener, MessageBrokerServiceType, MessageBrokerServicesConfig, MessageBrokerServicesStatus, MessageData, MessageHandler, MessageHeaders, MessagePayload, MessageProperties, Metrics, NackOptions, PublishDirectMessageOptions, PublishDirectOptions, PublishMessageOptions, PublishOptions, PublisherOptions, PublishingResult, Queue, QueueConfig, QueueConfigByQueueName, QueueConfigType, QueueConnectionConfig, QueueConnectionType, QueueContext, QueueDeps, QueueMessage, QueueMessageData, QueueMessageError, QueueMessageMetaData, QueueName, QueueOptions, QueueStatus, QueueStatusByType, QueueTypes, RabbitMQConfig, RabbitMQConfigCustomParams, RabbitMQProvider, RabbitMQStatus, ReceiveDirectOps, ReconnectOptions, RecreateChannelOptions, RedeclareQueueOptions, ScheduledTask, ScheduledTasksQueue, ServiceConfig, ServiceConfigByConfigType, ServiceRulesConfig, SocketOptions, SubscribeOptions, Task, TaskListener, TaskQueue, TopicConfig, TopicConfigByConfigType, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, UnbindOptions, communicationsTotalLabelsMap, emptyMessageBrokerServiceConfig };
27
+ export { AmqpAsserter, AmqpConfig, AmqpConnection, AmqpConnectionEventNames, AmqpListener, AmqpPublisher, AmqpPublisherPublishOptions, Arguments, BaseQueueOptions, BindOptions, CommonQueueDeps, CommunicationDirection, CommunicationsTotalLabelsMap, ConnectOptions, ConnectionClientType, ConnectionList, ConnectionStatus, ConsumerOptions, DeclareOptions, DirectResponse, DirectResponseHeaders, EventBus, EventBusListener, EventBusQueue, EventListeners, EventMessageHandler, EventMessageValidator, EventName, ExchangeName, ExchangeOptions, ExchangeType, ExportConfig, ExternalCommunicator, ExternalCommunicatorFailureResponse, ExternalCommunicatorResponse, ExternalCommunicatorResponseError, ExternalCommunicatorSuccessResponse, ExternalEventBus, ExternalEventBusQueue, ExternalQueueDeps, ExternalServiceConfig, Headers, InternalQueueConfig, InternalQueueDeps, InternalServiceConfig, LabelUnknown, Listener, ListenerOptions, Message, MessageBrokerExternalServiceType, MessageBrokerExternalServiceTypes, MessageBrokerGeneralServiceType, MessageBrokerGeneralServiceTypes, MessageBrokerInternalServiceType, MessageBrokerInternalServiceTypes, MessageBrokerServiceConfig, MessageBrokerServiceEventsListener, MessageBrokerServiceListener, MessageBrokerServiceType, MessageBrokerServicesConfig, MessageBrokerServicesStatus, MessageData, MessageHandler, MessageHeaders, MessagePayload, MessageProperties, Metrics, NackOptions, PublishDirectMessageOptions, PublishDirectOptions, PublishMessageOptions, PublishOptions, PublisherOptions, PublishingResult, Queue, QueueConfig, QueueConfigByQueueName, QueueConfigType, QueueConnectionConfig, QueueConnectionType, QueueContext, QueueDeps, QueueMessage, QueueMessageData, QueueMessageError, QueueMessageMetaData, QueueName, QueueOptions, QueueStatus, QueueStatusByType, QueueTypes, RabbitMQConfig, RabbitMQConfigCustomParams, RabbitMQProvider, RabbitMQStatus, ReceiveDirectOps, ReconnectOptions, RecreateChannelOptions, RedeclareQueueOptions, ReplyToQueueName, ScheduledTask, ScheduledTasksQueue, ServiceConfig, ServiceConfigByConfigType, ServiceRulesConfig, SocketOptions, SubscribeOptions, Task, TaskListener, TaskQueue, TopicConfig, TopicConfigByConfigType, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, UnbindOptions, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, emptyMessageBrokerServiceConfig, unroutedMessagesLabelsMap };
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Arguments, ConnectionClientType, Headers, Message, NackOptions } from "./interfaces/providers/rabbitmq/index.js";
2
2
  import { AmqpConnectionEventNames, ConnectionStatus } from "./interfaces/providers/rabbitmq/amqpConnection.js";
3
+ import { ReplyToQueueName } from "./providers/rabbitmq/amqpPublisher.types.js";
3
4
  import { QueueConnectionType } from "./interfaces/queueStatus.js";
4
5
  import { ExchangeType, MessageBrokerExternalServiceTypes, MessageBrokerGeneralServiceTypes, MessageBrokerInternalServiceTypes, QueueTypes, emptyMessageBrokerServiceConfig } from "./interfaces/messageBrokerServiceConfig.js";
5
- import { CommunicationsTotalLabelsMap, LabelUnknown, Metrics, communicationsTotalLabelsMap } from "./interfaces/metrics/index.js";
6
+ import { CommunicationsTotalLabelsMap, LabelUnknown, Metrics, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, unroutedMessagesLabelsMap } from "./interfaces/metrics/index.js";
6
7
  import { QueueConfigType } from "./interfaces/queueConfig/configs.js";
7
8
  import "./interfaces/index.js";
8
9
  import { EventBus } from "./services/eventBus.js";
@@ -20,4 +21,4 @@ import { RabbitMQProvider } from "./providers/rabbitmq/index.js";
20
21
  import { Queue } from "./services/queue.js";
21
22
  import "./services/index.js";
22
23
  import "./providers/index.js";
23
- export { AmqpAsserter, AmqpConnection, AmqpConnectionEventNames, AmqpListener, AmqpPublisher, Arguments, CommunicationsTotalLabelsMap, ConnectionClientType, ConnectionStatus, EventBus, EventMessageHandler, EventMessageValidator, ExchangeType, ExternalCommunicator, ExternalEventBus, Headers, LabelUnknown, Message, MessageBrokerExternalServiceTypes, MessageBrokerGeneralServiceTypes, MessageBrokerInternalServiceTypes, Metrics, NackOptions, Queue, QueueConfigType, QueueConnectionType, QueueTypes, RabbitMQProvider, ScheduledTask, Task, communicationsTotalLabelsMap, emptyMessageBrokerServiceConfig };
24
+ export { AmqpAsserter, AmqpConnection, AmqpConnectionEventNames, AmqpListener, AmqpPublisher, Arguments, CommunicationsTotalLabelsMap, ConnectionClientType, ConnectionStatus, EventBus, EventMessageHandler, EventMessageValidator, ExchangeType, ExternalCommunicator, ExternalEventBus, Headers, LabelUnknown, Message, MessageBrokerExternalServiceTypes, MessageBrokerGeneralServiceTypes, MessageBrokerInternalServiceTypes, Metrics, NackOptions, Queue, QueueConfigType, QueueConnectionType, QueueTypes, RabbitMQProvider, ReplyToQueueName, ScheduledTask, Task, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, emptyMessageBrokerServiceConfig, unroutedMessagesLabelsMap };
@@ -3,11 +3,11 @@ import { ListenerOptions, PublishDirectMessageOptions, PublishDirectOptions, Pub
3
3
  import { AmqpConfig, AmqpConnectionEventNames, ConnectOptions, ConnectionStatus, ReconnectOptions, SocketOptions } from "./providers/rabbitmq/amqpConnection.js";
4
4
  import { EventName, ExternalServiceConfig, InternalServiceConfig, QueueConfigByQueueName, QueueConfigType, QueueName, ServiceConfig, ServiceConfigByConfigType, ServiceRulesConfig, TopicConfig, TopicConfigByConfigType } from "./queueConfig/configs.js";
5
5
  import { Arguments, ConnectionClientType, ConnectionList, DeclareOptions, ExportConfig, Headers, Message, MessageData, MessageProperties, NackOptions, QueueMessage, QueueMessageData, QueueMessageError, QueueMessageMetaData, RabbitMQConfig, RabbitMQConfigCustomParams, RabbitMQStatus } from "./providers/rabbitmq/index.js";
6
- import { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult } from "../providers/rabbitmq/amqpPublisher.types.js";
6
+ import { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult, ReplyToQueueName } from "../providers/rabbitmq/amqpPublisher.types.js";
7
7
  import { MessageHandler } from "./messageHandler.js";
8
8
  import { MessageBrokerServicesStatus, QueueConnectionType, QueueStatus, QueueStatusByType } from "./queueStatus.js";
9
9
  import { QueueContext } from "./queueContext.js";
10
- import { CommunicationDirection, CommunicationsTotalLabelsMap, LabelUnknown, Metrics, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, communicationsTotalLabelsMap } from "./metrics/index.js";
10
+ import { CommunicationDirection, CommunicationsTotalLabelsMap, LabelUnknown, Metrics, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, unroutedMessagesLabelsMap } from "./metrics/index.js";
11
11
  import { ExternalCommunicatorFailureResponse, ExternalCommunicatorResponse, ExternalCommunicatorResponseError, ExternalCommunicatorSuccessResponse, ReceiveDirectOps } from "./externalCommunicator.js";
12
12
  import { ValidationError } from "@diia-inhouse/errors";
13
13
  import { ValidationSchema } from "@diia-inhouse/validators";
@@ -1,5 +1,6 @@
1
1
  import "./providers/rabbitmq/index.js";
2
2
  import "./providers/rabbitmq/amqpConnection.js";
3
+ import "../providers/rabbitmq/amqpPublisher.types.js";
3
4
  import "./queueStatus.js";
4
5
  import "./messageBrokerServiceConfig.js";
5
6
  import "./metrics/index.js";
@@ -16,8 +16,15 @@ declare class CommunicationsTotalLabelsMap {
16
16
  direction: CommunicationDirection;
17
17
  }
18
18
  declare const communicationsTotalLabelsMap: (keyof CommunicationsTotalLabelsMap)[];
19
+ declare class UnroutedMessagesLabelsMap {
20
+ exchangeName: string;
21
+ event: string;
22
+ routingKey: string;
23
+ }
24
+ declare const unroutedMessagesLabelsMap: (keyof UnroutedMessagesLabelsMap)[];
19
25
  declare const Metrics: {
20
26
  readonly CommunicationsTotal: "diia_rabbitmq_communications_total";
27
+ readonly UnroutedMessagesTotal: "diia_rabbitmq_unrouted_messages_total";
21
28
  };
22
29
  //#endregion
23
- export { CommunicationDirection, CommunicationsTotalLabelsMap, LabelUnknown, Metrics, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, communicationsTotalLabelsMap };
30
+ export { CommunicationDirection, CommunicationsTotalLabelsMap, LabelUnknown, Metrics, TotalListenerChannelErrorsLabelsMap, TotalMessageHandlerErrorsLabelsMap, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, unroutedMessagesLabelsMap };
@@ -8,6 +8,15 @@ var CommunicationsTotalLabelsMap = class {
8
8
  direction = "inbound";
9
9
  };
10
10
  const communicationsTotalLabelsMap = Object.keys(new CommunicationsTotalLabelsMap());
11
- const Metrics = { CommunicationsTotal: "diia_rabbitmq_communications_total" };
11
+ var UnroutedMessagesLabelsMap = class {
12
+ exchangeName = LabelUnknown;
13
+ event = LabelUnknown;
14
+ routingKey = LabelUnknown;
15
+ };
16
+ const unroutedMessagesLabelsMap = Object.keys(new UnroutedMessagesLabelsMap());
17
+ const Metrics = {
18
+ CommunicationsTotal: "diia_rabbitmq_communications_total",
19
+ UnroutedMessagesTotal: "diia_rabbitmq_unrouted_messages_total"
20
+ };
12
21
  //#endregion
13
- export { CommunicationsTotalLabelsMap, LabelUnknown, Metrics, communicationsTotalLabelsMap };
22
+ export { CommunicationsTotalLabelsMap, LabelUnknown, Metrics, UnroutedMessagesLabelsMap, communicationsTotalLabelsMap, unroutedMessagesLabelsMap };
@@ -80,6 +80,7 @@ interface ExportConfig {
80
80
  declare const Headers: {
81
81
  readonly sentFrom: "sent-from";
82
82
  readonly handledBy: "handled-by";
83
+ readonly event: "event";
83
84
  };
84
85
  declare class NackOptions {
85
86
  /**
@@ -13,7 +13,8 @@ var Message = class {
13
13
  };
14
14
  const Headers = {
15
15
  sentFrom: "sent-from",
16
- handledBy: "handled-by"
16
+ handledBy: "handled-by",
17
+ event: "event"
17
18
  };
18
19
  var NackOptions = class {
19
20
  requeue;
@@ -1,5 +1,5 @@
1
1
  import { AmqpConnection } from "./rabbitmq/amqpConnection.js";
2
- import { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult } from "./rabbitmq/amqpPublisher.types.js";
2
+ import { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult, ReplyToQueueName } from "./rabbitmq/amqpPublisher.types.js";
3
3
  import { AmqpAsserter } from "./rabbitmq/amqpAsserter.js";
4
4
  import { AmqpListener } from "./rabbitmq/amqpListener.js";
5
5
  import { AmqpPublisher } from "./rabbitmq/amqpPublisher.js";
@@ -1,3 +1,4 @@
1
+ import "./rabbitmq/amqpPublisher.types.js";
1
2
  import "./rabbitmq/amqpAsserter.js";
2
3
  import "./rabbitmq/amqpConnection.js";
3
4
  import "./rabbitmq/amqpListener.js";
@@ -13,6 +13,7 @@ declare class AmqpListener {
13
13
  private readonly rabbitMQMetrics;
14
14
  private readonly queuesOptions;
15
15
  private readonly systemServiceName;
16
+ private readonly amqpMessageReader;
16
17
  private readonly infiniteRecreateChannelTriesCount;
17
18
  private readonly defaultRecreateChannelOptions;
18
19
  private readonly defaultPrefetchCount;
@@ -39,6 +40,7 @@ declare class AmqpListener {
39
40
  private parseMessage;
40
41
  private prepareResponse;
41
42
  private finishResponseProcessing;
43
+ private collectUnroutedMessage;
42
44
  private getConsumerOptions;
43
45
  private getQueueChannel;
44
46
  private saveConsumerTag;
@@ -1,7 +1,9 @@
1
1
  import { Headers, NackOptions } from "../../interfaces/providers/rabbitmq/index.js";
2
+ import "./amqpPublisher.types.js";
2
3
  import { LabelUnknown } from "../../interfaces/metrics/index.js";
3
4
  import "../../interfaces/index.js";
4
5
  import { totalListenerChannelErrorsMetric } from "../../metrics/index.js";
6
+ import { AmqpMessageReader } from "./amqpMessageReader.js";
5
7
  import { ApiError, ErrorType } from "@diia-inhouse/errors";
6
8
  import { setTimeout } from "node:timers/promises";
7
9
  //#region src/providers/rabbitmq/amqpListener.ts
@@ -11,6 +13,7 @@ var AmqpListener = class {
11
13
  rabbitMQMetrics;
12
14
  queuesOptions;
13
15
  systemServiceName;
16
+ amqpMessageReader = new AmqpMessageReader();
14
17
  infiniteRecreateChannelTriesCount = 0;
15
18
  defaultRecreateChannelOptions = {
16
19
  maxTries: 6,
@@ -80,6 +83,7 @@ var AmqpListener = class {
80
83
  const { consumerTag: preferredConsumerTag, prefetchCount = this.defaultPrefetchCount } = consumerOptions;
81
84
  await channel.prefetch(prefetchCount);
82
85
  const callback = this.onMessageCallback(queueName, channel, consumerOptions);
86
+ channel.on("return", (returned) => this.collectUnroutedMessage(returned));
83
87
  channel.on("error", async (err) => {
84
88
  totalListenerChannelErrorsMetric.increment({ queueName });
85
89
  this.logger.warn("Recreating listener channel because an error has been occurred", {
@@ -170,7 +174,7 @@ var AmqpListener = class {
170
174
  }
171
175
  }
172
176
  prepareResponse(message, messageData, channel) {
173
- const done = (data) => this.finishResponseProcessing(message, channel, data);
177
+ const done = (data) => this.finishResponseProcessing(message, channel, data, messageData.event);
174
178
  return {
175
179
  done,
176
180
  id: message?.properties.messageId,
@@ -181,21 +185,37 @@ var AmqpListener = class {
181
185
  }
182
186
  };
183
187
  }
184
- finishResponseProcessing(message, channel, data) {
188
+ finishResponseProcessing(message, channel, data, event) {
185
189
  const { replyTo, correlationId } = message.properties;
186
190
  if (data && replyTo && correlationId) {
187
191
  const json = JSON.stringify(data);
188
192
  const content = Buffer.from(json);
193
+ const routingKey = replyTo;
189
194
  const options = {
190
195
  correlationId,
191
- headers: { [Headers.handledBy]: this.systemServiceName }
196
+ ...routingKey.startsWith("amq.rabbitmq.reply-to") ? {} : { mandatory: true },
197
+ headers: {
198
+ [Headers.handledBy]: this.systemServiceName,
199
+ ...event ? { [Headers.event]: event } : {}
200
+ }
192
201
  };
193
- const exchangeName = "";
194
- const routingKey = replyTo;
195
- channel.publish(exchangeName, routingKey, content, options);
202
+ channel.publish("", routingKey, content, options);
196
203
  } else if (!data && replyTo && correlationId) this.logger.warn("Reply to and correlationId headers found but no reply data specified");
197
204
  this.ackMsg(channel, message);
198
205
  }
206
+ collectUnroutedMessage(message) {
207
+ try {
208
+ const unrouted = this.amqpMessageReader.readUnrouted(message);
209
+ if (!unrouted) return;
210
+ const { exchangeName, event, routingKey } = unrouted;
211
+ this.rabbitMQMetrics.collectUnroutedMessageMetric(exchangeName, event, routingKey);
212
+ } catch (err) {
213
+ this.logger.error("Error while collecting unrouted message", {
214
+ err,
215
+ message
216
+ });
217
+ }
218
+ }
199
219
  getConsumerOptions(queueName) {
200
220
  const consumerOptions = this.queueConsumerOptionsMap.get(queueName);
201
221
  if (!consumerOptions) {
@@ -0,0 +1,31 @@
1
+ import { Headers } from "../../interfaces/providers/rabbitmq/index.js";
2
+ import { ReplyToQueueName } from "./amqpPublisher.types.js";
3
+ //#region src/providers/rabbitmq/amqpMessageReader.ts
4
+ var AmqpMessageReader = class {
5
+ defaultExchangeName = "default";
6
+ readUnrouted(message) {
7
+ if (message.properties.headers?.["x-delay"] > 0) return;
8
+ return {
9
+ event: this.readEventName(message),
10
+ routingKey: this.readRoutingKey(message.fields.routingKey),
11
+ exchangeName: message.fields.exchange || this.defaultExchangeName
12
+ };
13
+ }
14
+ readRoutingKey(routingKey) {
15
+ if (!routingKey) return;
16
+ if (routingKey.startsWith("amq.rabbitmq.reply-to")) return ReplyToQueueName;
17
+ return routingKey;
18
+ }
19
+ readEventName(message) {
20
+ const headerEvent = message.properties.headers?.[Headers.event];
21
+ if (typeof headerEvent === "string" && headerEvent.length > 0) return headerEvent;
22
+ try {
23
+ const { event } = JSON.parse(message.content.toString());
24
+ return typeof event === "string" && event.length > 0 ? event : void 0;
25
+ } catch {
26
+ return;
27
+ }
28
+ }
29
+ };
30
+ //#endregion
31
+ export { AmqpMessageReader };
@@ -13,6 +13,7 @@ declare class AmqpPublisher {
13
13
  private readonly logger;
14
14
  private readonly rabbitMQMetrics;
15
15
  private readonly systemServiceName;
16
+ private readonly amqpMessageReader;
16
17
  private readonly defaultPublishOptions;
17
18
  private rpcChannel?;
18
19
  private regularChannel?;
@@ -28,6 +29,7 @@ declare class AmqpPublisher {
28
29
  getStatus(): ConnectionStatus;
29
30
  private createRegularChannel;
30
31
  private createRpcChannel;
32
+ private collectUnroutedMessage;
31
33
  private listenReplyToQueue;
32
34
  private publishMessage;
33
35
  private publishRequest;
@@ -1,17 +1,20 @@
1
1
  import { Headers } from "../../interfaces/providers/rabbitmq/index.js";
2
+ import { ReplyToQueueName } from "./amqpPublisher.types.js";
2
3
  import { LabelUnknown } from "../../interfaces/metrics/index.js";
3
4
  import constants_default from "../../constants.js";
5
+ import { AmqpMessageReader } from "./amqpMessageReader.js";
4
6
  import { ErrorType, ExternalCommunicatorError, InternalServerError } from "@diia-inhouse/errors";
5
7
  import { randomUUID } from "node:crypto";
6
8
  import { HttpStatusCode } from "@diia-inhouse/types";
7
9
  import { EventEmitter } from "node:events";
8
10
  //#region src/providers/rabbitmq/amqpPublisher.ts
9
- const { APP_ID, DEFAULT_ROUTING_KEY, REPLY_TO_QUEUE_NAME } = constants_default;
11
+ const { APP_ID, DEFAULT_ROUTING_KEY } = constants_default;
10
12
  var AmqpPublisher = class {
11
13
  connection;
12
14
  logger;
13
15
  rabbitMQMetrics;
14
16
  systemServiceName;
17
+ amqpMessageReader = new AmqpMessageReader();
15
18
  defaultPublishOptions = {
16
19
  contentEncoding: "utf-8",
17
20
  contentType: "application/json",
@@ -21,7 +24,7 @@ var AmqpPublisher = class {
21
24
  };
22
25
  rpcChannel;
23
26
  regularChannel;
24
- replyToQueueName = REPLY_TO_QUEUE_NAME;
27
+ replyToQueueName = ReplyToQueueName;
25
28
  eventEmitter;
26
29
  directResponseTimeout = 1e4;
27
30
  publishTimeout = 1e4;
@@ -33,7 +36,7 @@ var AmqpPublisher = class {
33
36
  this.systemServiceName = systemServiceName;
34
37
  this.publishTimeout = options.publishTimeout ?? this.publishTimeout;
35
38
  this.directResponseTimeout = options.directResponseTimeout ?? options.timeout ?? this.directResponseTimeout;
36
- this.replyToQueueName = options.replyToQueueName ?? REPLY_TO_QUEUE_NAME;
39
+ this.replyToQueueName = options.replyToQueueName ?? "amq.rabbitmq.reply-to";
37
40
  }
38
41
  async init() {
39
42
  await this.createRpcChannel();
@@ -100,14 +103,29 @@ var AmqpPublisher = class {
100
103
  }
101
104
  async createRegularChannel() {
102
105
  this.regularChannel = await this.connection.createChannel();
106
+ this.regularChannel?.on("return", (message) => this.collectUnroutedMessage(message));
103
107
  }
104
108
  async createRpcChannel() {
105
109
  this.rpcChannel = await this.connection.createChannel(this.replyToQueueName);
110
+ this.rpcChannel?.on("return", (message) => this.collectUnroutedMessage(message));
106
111
  this.rpcChannel?.on("error", async () => await this.createRpcChannel());
107
112
  this.eventEmitter = new EventEmitter();
108
113
  this.eventEmitter?.setMaxListeners(0);
109
114
  await this.rpcChannel?.consume(this.replyToQueueName, this.listenReplyToQueue.bind(this), { noAck: true });
110
115
  }
116
+ collectUnroutedMessage(message) {
117
+ try {
118
+ const unrouted = this.amqpMessageReader.readUnrouted(message);
119
+ if (!unrouted) return;
120
+ const { exchangeName, event, routingKey } = unrouted;
121
+ this.rabbitMQMetrics.collectUnroutedMessageMetric(exchangeName, event, routingKey);
122
+ } catch (err) {
123
+ this.logger.error("Error while collecting unrouted message", {
124
+ err,
125
+ message
126
+ });
127
+ }
128
+ }
111
129
  listenReplyToQueue(msg) {
112
130
  if (!msg) {
113
131
  this.logger.error("Consumed direct message is null");
@@ -3,6 +3,7 @@ import { Headers } from "../../interfaces/providers/rabbitmq/index.js";
3
3
  import { MessagePropertyHeaders, Options } from "amqplib";
4
4
 
5
5
  //#region src/providers/rabbitmq/amqpPublisher.types.d.ts
6
+ declare const ReplyToQueueName = "amq.rabbitmq.reply-to";
6
7
  type MessagePayload = unknown;
7
8
  interface MessageHeaders {
8
9
  traceId: string;
@@ -22,4 +23,4 @@ interface AmqpPublisherPublishOptions {
22
23
  custom: PublishMessageOptions;
23
24
  }
24
25
  //#endregion
25
- export { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult };
26
+ export { AmqpPublisherPublishOptions, DirectResponse, DirectResponseHeaders, MessageHeaders, MessagePayload, PublishingResult, ReplyToQueueName };
@@ -0,0 +1,4 @@
1
+ //#region src/providers/rabbitmq/amqpPublisher.types.ts
2
+ const ReplyToQueueName = "amq.rabbitmq.reply-to";
3
+ //#endregion
4
+ export { ReplyToQueueName };
@@ -8,7 +8,7 @@ import { AmqpAsserter } from "./amqpAsserter.js";
8
8
  import { AmqpConnection } from "./amqpConnection.js";
9
9
  import { AmqpListener } from "./amqpListener.js";
10
10
  import { AmqpPublisher } from "./amqpPublisher.js";
11
- import _ from "lodash";
11
+ import lodash from "lodash";
12
12
  import { randomUUID } from "node:crypto";
13
13
  import { EventEmitter } from "node:events";
14
14
  //#region src/providers/rabbitmq/index.ts
@@ -112,7 +112,7 @@ var RabbitMQProvider = class extends EventEmitter {
112
112
  };
113
113
  }
114
114
  async makeAMQPConnection(client) {
115
- return new AmqpConnection(this.rabbitmqConfig.connection, this.logger, this.rabbitmqConfig.reconnectOptions, _.merge({ clientProperties: { connectionClientType: client } }, this.rabbitmqConfig.socketOptions));
115
+ return new AmqpConnection(this.rabbitmqConfig.connection, this.logger, this.rabbitmqConfig.reconnectOptions, lodash.merge({ clientProperties: { connectionClientType: client } }, this.rabbitmqConfig.socketOptions));
116
116
  }
117
117
  async makeAMQPListener(queuesOptions) {
118
118
  return new AmqpListener(await this.getConnection("listener"), this.logger, this.rabbitMQMetricsService, queuesOptions, this.systemServiceName);
@@ -1,5 +1,5 @@
1
1
  import { collectEventBusListeners } from "../utils.js";
2
- import _ from "lodash";
2
+ import lodash from "lodash";
3
3
  //#region src/services/eventCommunicator.ts
4
4
  var EventCommunicator = class {
5
5
  logger;
@@ -33,8 +33,8 @@ var EventCommunicator = class {
33
33
  getUnicastListeners() {
34
34
  const listeners = [];
35
35
  const { queuesOptions, exchangesOptions } = this.queueProvider.getMessageBrokerServiceConfig();
36
- const queuesOptionsMap = _.keyBy(queuesOptions, "name");
37
- const exchangesOptionsMap = _.keyBy(exchangesOptions, "name");
36
+ const queuesOptionsMap = lodash.keyBy(queuesOptions, "name");
37
+ const exchangesOptionsMap = lodash.keyBy(exchangesOptions, "name");
38
38
  for (const eventListener of this.listenerList) {
39
39
  const { queueNames = [] } = eventListener;
40
40
  for (const queueName of queueNames) {
@@ -4,11 +4,13 @@ import { MetricsService } from "@diia-inhouse/diia-metrics";
4
4
 
5
5
  //#region src/services/metrics.d.ts
6
6
  declare class RabbitMQMetricsService {
7
- private metrics;
7
+ private readonly metrics;
8
8
  private readonly communicationsTotalMetric;
9
+ private readonly unroutedMessagesTotalMetric;
9
10
  constructor(metrics: MetricsService);
10
11
  collectResponseTotalMetric(startTime: bigint, route: string, source: string, destination: string, errorType?: ErrorType): void | never;
11
12
  collectCommunicationsTotalMetric(event: string, source: string, destination: string, direction: CommunicationDirection, queue?: string): void;
13
+ collectUnroutedMessageMetric(exchangeName: string, event?: string, routingKey?: string): void;
12
14
  collectRequestTotalMetric(startTime: bigint, route: string, source: string, destination: string, errorType?: ErrorType): void;
13
15
  }
14
16
  //#endregion
@@ -1,9 +1,10 @@
1
- import { Metrics, communicationsTotalLabelsMap } from "../interfaces/metrics/index.js";
1
+ import { Metrics, communicationsTotalLabelsMap, unroutedMessagesLabelsMap } from "../interfaces/metrics/index.js";
2
2
  import { Counter, RequestMechanism, RequestStatus } from "@diia-inhouse/diia-metrics";
3
3
  //#region src/services/metrics.ts
4
4
  var RabbitMQMetricsService = class {
5
5
  metrics;
6
6
  communicationsTotalMetric = new Counter(Metrics.CommunicationsTotal, communicationsTotalLabelsMap, "Total rabbitmq communications made by service");
7
+ unroutedMessagesTotalMetric = new Counter(Metrics.UnroutedMessagesTotal, unroutedMessagesLabelsMap, "Total rabbitmq messages returned because no queue was bound, excluding delayed messages the broker returns while they are still parked");
7
8
  constructor(metrics) {
8
9
  this.metrics = metrics;
9
10
  }
@@ -29,6 +30,14 @@ var RabbitMQMetricsService = class {
29
30
  };
30
31
  this.communicationsTotalMetric.increment(labels, 1);
31
32
  }
33
+ collectUnroutedMessageMetric(exchangeName, event, routingKey) {
34
+ const labels = {
35
+ exchangeName,
36
+ event: event || "unknown",
37
+ routingKey: routingKey || "unknown"
38
+ };
39
+ this.unroutedMessagesTotalMetric.increment(labels, 1);
40
+ }
32
41
  collectRequestTotalMetric(startTime, route, source, destination, errorType) {
33
42
  const delta = process.hrtime.bigint() - startTime;
34
43
  const labels = {
@@ -2,7 +2,7 @@ import { QueueTypes } from "../interfaces/messageBrokerServiceConfig.js";
2
2
  import "../interfaces/index.js";
3
3
  import constants_default from "../constants.js";
4
4
  import Communicator from "./communicator.js";
5
- import _ from "lodash";
5
+ import lodash from "lodash";
6
6
  //#region src/services/task.ts
7
7
  var Task = class extends Communicator {
8
8
  serviceName;
@@ -18,8 +18,8 @@ var Task = class extends Communicator {
18
18
  }
19
19
  async onInit() {
20
20
  const { queuesOptions, exchangesOptions } = await this.init();
21
- const queuesOptionsMap = _.keyBy(queuesOptions, "name");
22
- const exchangesOptionsMap = _.keyBy(exchangesOptions, "name");
21
+ const queuesOptionsMap = lodash.keyBy(queuesOptions, "name");
22
+ const exchangesOptionsMap = lodash.keyBy(exchangesOptions, "name");
23
23
  for (const task of this.taskList) this.taskNameExchangesOptionsMap.set(task.name, this.defineTaskExchangesOptions(task, queuesOptionsMap, exchangesOptionsMap));
24
24
  }
25
25
  async publish(taskName, payload, delay) {
@@ -44,8 +44,8 @@ var Task = class extends Communicator {
44
44
  }
45
45
  getProducerExchangesOptions() {
46
46
  const { queuesOptions, exchangesOptions } = this.queueProvider.getMessageBrokerServiceConfig();
47
- const queuesOptionsMap = _.keyBy(queuesOptions, "name");
48
- const exchangesOptionsMap = _.keyBy(exchangesOptions, "name");
47
+ const queuesOptionsMap = lodash.keyBy(queuesOptions, "name");
48
+ const exchangesOptionsMap = lodash.keyBy(exchangesOptions, "name");
49
49
  return this.taskList.flatMap((task) => this.defineTaskExchangesOptions(task, queuesOptionsMap, exchangesOptionsMap));
50
50
  }
51
51
  getMulticastListeners() {
@@ -54,8 +54,8 @@ var Task = class extends Communicator {
54
54
  getUnicastListeners() {
55
55
  const listeners = [];
56
56
  const { queuesOptions, exchangesOptions } = this.queueProvider.getMessageBrokerServiceConfig();
57
- const queuesOptionsMap = _.keyBy(queuesOptions, "name");
58
- const exchangesOptionsMap = _.keyBy(exchangesOptions, "name");
57
+ const queuesOptionsMap = lodash.keyBy(queuesOptions, "name");
58
+ const exchangesOptionsMap = lodash.keyBy(exchangesOptions, "name");
59
59
  for (const task of this.taskList) {
60
60
  const { queueNames = [], name: taskName, isDelayed = false } = task;
61
61
  if (queueNames.length === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diia-inhouse/diia-queue",
3
- "version": "14.2.57",
3
+ "version": "14.3.0",
4
4
  "description": "Package provide queue functionality",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "author": "diia-team",