@ngn-net/nestjs-telescope 0.3.7 → 0.3.9

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.7",
4
- "builtAt": "2026-06-13T11:07:43.039Z"
3
+ "version": "0.3.9",
4
+ "builtAt": "2026-06-13T11:13:25.789Z"
5
5
  }
@@ -6,6 +6,7 @@ export declare class RabbitMQWatcher implements OnModuleInit {
6
6
  constructor(telescope: TelescopeService);
7
7
  onModuleInit(): void;
8
8
  private patchAmqpConnection;
9
- private patchAmqplibChannel;
9
+ private patchAmqplibConnect;
10
+ private instrumentChannel;
10
11
  private sanitizeMessage;
11
12
  }
@@ -22,7 +22,7 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
22
22
  }
23
23
  onModuleInit() {
24
24
  this.patchAmqpConnection();
25
- this.patchAmqplibChannel();
25
+ this.patchAmqplibConnect();
26
26
  }
27
27
  patchAmqpConnection() {
28
28
  try {
@@ -50,63 +50,82 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
50
50
  }).catch(() => { });
51
51
  return result;
52
52
  };
53
+ this.logger.log('Patched AmqpConnection.publish');
54
+ }
55
+ catch (e) {
56
+ this.logger.warn('Failed to patch AmqpConnection.publish: ' + (e?.message || e));
53
57
  }
54
- catch { }
55
58
  }
56
- patchAmqplibChannel() {
59
+ patchAmqplibConnect() {
57
60
  try {
58
61
  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)
62
+ if (!amqplib || typeof amqplib.connect !== 'function') {
63
+ this.logger.warn('amqplib.connect not found');
66
64
  return;
65
+ }
66
+ const originalConnect = amqplib.connect;
67
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);
73
- const startTime = Date.now();
74
- let parsed;
75
- try {
76
- parsed = JSON.parse(msg.content.toString());
77
- }
78
- catch {
79
- parsed = msg.content.toString();
80
- }
81
- self.telescope.record({
82
- type: entry_type_enum_1.EntryType.RABBITMQ,
83
- content: {
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(),
93
- },
94
- }).catch(() => { });
95
- return onMessage(msg);
96
- };
97
- handlerMap.set(queue, wrappedOnMessage);
98
- return originalConsume.call(this, queue, wrappedOnMessage, options);
68
+ let patched = false;
69
+ amqplib.connect = async function (...args) {
70
+ const conn = await originalConnect.apply(this, args);
71
+ if (conn && typeof conn.createChannel === 'function') {
72
+ const originalCreateChannel = conn.createChannel.bind(conn);
73
+ conn.createChannel = async function (...channelArgs) {
74
+ const channel = await originalCreateChannel(...channelArgs);
75
+ if (channel && typeof channel.consume === 'function') {
76
+ self.instrumentChannel(channel);
77
+ self.logger.log('Patched amqplib channel.consume');
78
+ }
79
+ return channel;
80
+ };
81
+ }
82
+ return conn;
99
83
  };
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);
106
- };
107
- }
84
+ patched = true;
85
+ this.logger.log('Patched amqplib.connect');
86
+ }
87
+ catch (e) {
88
+ this.logger.warn('Failed to patch amqplib.connect: ' + (e?.message || e));
108
89
  }
109
- catch { }
90
+ }
91
+ instrumentChannel(channel) {
92
+ const self = this;
93
+ if (channel.__telescopePatched)
94
+ return;
95
+ channel.__telescopePatched = true;
96
+ const originalConsume = channel.consume;
97
+ if (!originalConsume)
98
+ return;
99
+ channel.consume = function (queue, onMessage, options) {
100
+ self.logger.log(`Intercepting consume for queue: ${queue}`);
101
+ const wrappedOnMessage = (msg) => {
102
+ if (!msg || !msg.content)
103
+ return onMessage(msg);
104
+ self.logger.log(`Incoming message on queue: ${queue}`);
105
+ const startTime = Date.now();
106
+ let parsed;
107
+ try {
108
+ parsed = JSON.parse(msg.content.toString());
109
+ }
110
+ catch {
111
+ parsed = msg.content.toString();
112
+ }
113
+ self.telescope.record({
114
+ type: entry_type_enum_1.EntryType.RABBITMQ,
115
+ content: {
116
+ direction: 'incoming',
117
+ queue,
118
+ exchange: msg.fields?.exchange || '',
119
+ routingKey: msg.fields?.routingKey || queue,
120
+ message: self.sanitizeMessage(parsed),
121
+ deliveryTag: msg.fields?.deliveryTag,
122
+ timestamp: new Date().toISOString(),
123
+ },
124
+ }).catch(() => { });
125
+ return onMessage(msg);
126
+ };
127
+ return originalConsume.call(this, queue, wrappedOnMessage, options);
128
+ };
110
129
  }
111
130
  sanitizeMessage(msg) {
112
131
  if (!msg)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },