@ngn-net/nestjs-telescope 0.3.6 → 0.3.8

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.8",
4
+ "builtAt": "2026-06-13T11:10:47.835Z"
5
5
  }
@@ -6,5 +6,7 @@ export declare class RabbitMQWatcher implements OnModuleInit {
6
6
  constructor(telescope: TelescopeService);
7
7
  onModuleInit(): void;
8
8
  private patchAmqpConnection;
9
+ private patchAmqplibConnect;
10
+ private instrumentChannel;
9
11
  private sanitizeMessage;
10
12
  }
@@ -22,98 +22,98 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
22
22
  }
23
23
  onModuleInit() {
24
24
  this.patchAmqpConnection();
25
+ this.patchAmqplibConnect();
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) {
36
- const startTime = Date.now();
37
- const result = await originalPublish.call(this, exchange, routingKey, msg, ...args);
38
- const duration = Date.now() - startTime;
39
- self.telescope.record({
40
- type: entry_type_enum_1.EntryType.RABBITMQ,
41
- content: {
42
- direction: 'outgoing',
43
- exchange,
44
- routingKey,
45
- message: self.sanitizeMessage(msg),
46
- duration,
47
- status: 'sent',
48
- },
49
- }).catch(() => { });
50
- return result;
51
- };
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;
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
+ patchAmqplibConnect() {
57
+ try {
58
+ const amqplib = require('amqplib');
59
+ if (!amqplib || typeof amqplib.connect !== 'function')
60
+ return;
61
+ const originalConnect = amqplib.connect;
62
+ const self = this;
63
+ amqplib.connect = async function (...args) {
64
+ const conn = await originalConnect.apply(this, args);
65
+ if (conn && typeof conn.createChannel === 'function') {
66
+ const originalCreateChannel = conn.createChannel.bind(conn);
67
+ conn.createChannel = async function (...channelArgs) {
68
+ const channel = await originalCreateChannel(...channelArgs);
69
+ if (channel && typeof channel.consume === 'function') {
70
+ self.instrumentChannel(channel);
109
71
  }
72
+ return channel;
110
73
  };
111
- return originalConsume.call(this, queue, wrappedHandler, options);
112
- };
113
- }
74
+ }
75
+ return conn;
76
+ };
114
77
  }
115
78
  catch { }
116
79
  }
80
+ instrumentChannel(channel) {
81
+ const self = this;
82
+ if (channel.__telescopePatched)
83
+ return;
84
+ channel.__telescopePatched = true;
85
+ const originalConsume = channel.consume;
86
+ if (!originalConsume)
87
+ return;
88
+ channel.consume = function (queue, onMessage, options) {
89
+ const wrappedOnMessage = (msg) => {
90
+ if (!msg || !msg.content)
91
+ return onMessage(msg);
92
+ const startTime = Date.now();
93
+ let parsed;
94
+ try {
95
+ parsed = JSON.parse(msg.content.toString());
96
+ }
97
+ catch {
98
+ parsed = msg.content.toString();
99
+ }
100
+ self.telescope.record({
101
+ type: entry_type_enum_1.EntryType.RABBITMQ,
102
+ content: {
103
+ direction: 'incoming',
104
+ queue,
105
+ exchange: msg.fields?.exchange || '',
106
+ routingKey: msg.fields?.routingKey || queue,
107
+ message: self.sanitizeMessage(parsed),
108
+ deliveryTag: msg.fields?.deliveryTag,
109
+ timestamp: new Date().toISOString(),
110
+ },
111
+ }).catch(() => { });
112
+ return onMessage(msg);
113
+ };
114
+ return originalConsume.call(this, queue, wrappedOnMessage, options);
115
+ };
116
+ }
117
117
  sanitizeMessage(msg) {
118
118
  if (!msg)
119
119
  return null;
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.8",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },