@ngn-net/nestjs-telescope 0.3.6 → 0.3.7

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,5 +1,5 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.3.6",
4
- "builtAt": "2026-06-13T10:59:23.325Z"
3
+ "version": "0.3.7",
4
+ "builtAt": "2026-06-13T11:07:43.039Z"
5
5
  }
@@ -6,5 +6,6 @@ export declare class RabbitMQWatcher implements OnModuleInit {
6
6
  constructor(telescope: TelescopeService);
7
7
  onModuleInit(): void;
8
8
  private patchAmqpConnection;
9
+ private patchAmqplibChannel;
9
10
  private sanitizeMessage;
10
11
  }
@@ -22,93 +22,87 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
22
22
  }
23
23
  onModuleInit() {
24
24
  this.patchAmqpConnection();
25
+ this.patchAmqplibChannel();
25
26
  }
26
27
  patchAmqpConnection() {
27
28
  try {
28
29
  const { AmqpConnection } = require('@golevelup/nestjs-rabbitmq');
29
30
  if (!AmqpConnection || !AmqpConnection.prototype)
30
31
  return;
31
- const self = this;
32
- // Patch publish for outgoing messages
33
32
  const originalPublish = AmqpConnection.prototype.publish;
34
- if (originalPublish) {
35
- AmqpConnection.prototype.publish = async function (exchange, routingKey, msg, ...args) {
33
+ if (!originalPublish)
34
+ return;
35
+ const self = this;
36
+ AmqpConnection.prototype.publish = async function (exchange, routingKey, msg, ...args) {
37
+ const startTime = Date.now();
38
+ const result = await originalPublish.call(this, exchange, routingKey, msg, ...args);
39
+ const duration = Date.now() - startTime;
40
+ self.telescope.record({
41
+ type: entry_type_enum_1.EntryType.RABBITMQ,
42
+ content: {
43
+ direction: 'outgoing',
44
+ exchange,
45
+ routingKey,
46
+ message: self.sanitizeMessage(msg),
47
+ duration,
48
+ status: 'sent',
49
+ },
50
+ }).catch(() => { });
51
+ return result;
52
+ };
53
+ }
54
+ catch { }
55
+ }
56
+ patchAmqplibChannel() {
57
+ try {
58
+ const amqplib = require('amqplib');
59
+ if (!amqplib)
60
+ return;
61
+ const Channel = amqplib.Channel;
62
+ if (!Channel || !Channel.prototype)
63
+ return;
64
+ const originalConsume = Channel.prototype.consume;
65
+ if (!originalConsume)
66
+ return;
67
+ const self = this;
68
+ const handlerMap = new Map();
69
+ Channel.prototype.consume = function (queue, onMessage, options) {
70
+ const wrappedOnMessage = (msg) => {
71
+ if (!msg || !msg.content)
72
+ return onMessage(msg);
36
73
  const startTime = Date.now();
37
- const result = await originalPublish.call(this, exchange, routingKey, msg, ...args);
38
- const duration = Date.now() - startTime;
74
+ let parsed;
75
+ try {
76
+ parsed = JSON.parse(msg.content.toString());
77
+ }
78
+ catch {
79
+ parsed = msg.content.toString();
80
+ }
39
81
  self.telescope.record({
40
82
  type: entry_type_enum_1.EntryType.RABBITMQ,
41
83
  content: {
42
- direction: 'outgoing',
43
- exchange,
44
- routingKey,
45
- message: self.sanitizeMessage(msg),
46
- duration,
47
- status: 'sent',
84
+ direction: 'incoming',
85
+ queue,
86
+ exchange: msg.fields?.exchange || '',
87
+ routingKey: msg.fields?.routingKey || queue,
88
+ message: self.sanitizeMessage(parsed),
89
+ deliveryTag: msg.fields?.deliveryTag,
90
+ redelivered: msg.fields?.redelivered,
91
+ consumerTag: msg.fields?.consumerTag,
92
+ timestamp: new Date().toISOString(),
48
93
  },
49
94
  }).catch(() => { });
50
- return result;
95
+ return onMessage(msg);
51
96
  };
52
- }
53
- // Patch consume for incoming messages
54
- const originalConsume = AmqpConnection.prototype.consume;
55
- if (originalConsume) {
56
- AmqpConnection.prototype.consume = async function (queue, handler, options) {
57
- const wrappedHandler = async (msg, ...rest) => {
58
- const startTime = Date.now();
59
- const content = msg?.content ? msg.content.toString() : msg;
60
- let parsed;
61
- try {
62
- parsed = JSON.parse(content);
63
- }
64
- catch {
65
- parsed = content;
66
- }
67
- self.telescope.record({
68
- type: entry_type_enum_1.EntryType.RABBITMQ,
69
- content: {
70
- direction: 'incoming',
71
- queue,
72
- exchange: options?.exchange || '',
73
- routingKey: options?.routingKey || queue,
74
- message: self.sanitizeMessage(parsed),
75
- timestamp: new Date().toISOString(),
76
- },
77
- }).catch(() => { });
78
- try {
79
- const result = await handler(msg, ...rest);
80
- const duration = Date.now() - startTime;
81
- self.telescope.record({
82
- type: entry_type_enum_1.EntryType.RABBITMQ,
83
- content: {
84
- direction: 'incoming_response',
85
- queue,
86
- exchange: options?.exchange || '',
87
- routingKey: options?.routingKey || queue,
88
- duration,
89
- status: 'completed',
90
- },
91
- }).catch(() => { });
92
- return result;
93
- }
94
- catch (err) {
95
- const duration = Date.now() - startTime;
96
- self.telescope.record({
97
- type: entry_type_enum_1.EntryType.RABBITMQ,
98
- content: {
99
- direction: 'incoming_response',
100
- queue,
101
- exchange: options?.exchange || '',
102
- routingKey: options?.routingKey || queue,
103
- duration,
104
- status: 'failed',
105
- error: err.message || String(err),
106
- },
107
- }).catch(() => { });
108
- throw err;
109
- }
110
- };
111
- return originalConsume.call(this, queue, wrappedHandler, options);
97
+ handlerMap.set(queue, wrappedOnMessage);
98
+ return originalConsume.call(this, queue, wrappedOnMessage, options);
99
+ };
100
+ // Also patch cancel to clean up
101
+ const originalCancel = Channel.prototype.cancel;
102
+ if (originalCancel) {
103
+ Channel.prototype.cancel = function (consumerTag) {
104
+ handlerMap.delete(consumerTag);
105
+ return originalCancel.call(this, consumerTag);
112
106
  };
113
107
  }
114
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },