@ngn-net/nestjs-telescope 0.3.11 → 0.3.13

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.11",
4
- "builtAt": "2026-06-13T11:21:01.736Z"
3
+ "version": "0.3.13",
4
+ "builtAt": "2026-06-13T11:26:28.718Z"
5
5
  }
@@ -5,6 +5,4 @@ export declare class RabbitMQWatcher implements OnModuleInit {
5
5
  private readonly logger;
6
6
  constructor(telescope: TelescopeService);
7
7
  onModuleInit(): void;
8
- private patchExistingChannels;
9
- private patchAmqpConnectionPublish;
10
8
  }
@@ -42,78 +42,62 @@ function sanitizeMessage(msg) {
42
42
  }
43
43
  return msg;
44
44
  }
45
- function instrumentChannel(channel) {
46
- if (channel.__telescopePatched)
47
- return;
48
- channel.__telescopePatched = true;
49
- const originalConsume = channel.consume;
50
- if (!originalConsume)
51
- return;
52
- channel.consume = function (queue, onMessage, options) {
53
- const wrappedOnMessage = (msg) => {
54
- if (!msg || !msg.content)
55
- return onMessage(msg);
56
- if (telescopeRef) {
57
- let parsed;
58
- try {
59
- parsed = JSON.parse(msg.content.toString());
60
- }
61
- catch {
62
- parsed = msg.content.toString();
63
- }
64
- telescopeRef.record({
65
- type: entry_type_enum_1.EntryType.RABBITMQ,
66
- content: {
67
- direction: 'incoming',
68
- queue,
69
- exchange: msg.fields?.exchange || '',
70
- routingKey: msg.fields?.routingKey || queue,
71
- message: sanitizeMessage(parsed),
72
- deliveryTag: msg.fields?.deliveryTag,
73
- timestamp: new Date().toISOString(),
74
- },
75
- }).catch(() => { });
76
- }
77
- return onMessage(msg);
78
- };
79
- return originalConsume.call(this, queue, wrappedOnMessage, options);
80
- };
81
- }
82
- // Patch at file load time — intercept AmqpConnection.createChannel
45
+ // Patch at file load time
83
46
  try {
84
47
  const { AmqpConnection } = require('@golevelup/nestjs-rabbitmq');
85
48
  if (AmqpConnection && AmqpConnection.prototype) {
86
- const origCreateChannel = AmqpConnection.prototype.createChannel;
87
- if (origCreateChannel) {
88
- AmqpConnection.prototype.createChannel = async function (...args) {
89
- const channel = await origCreateChannel.apply(this, args);
90
- if (channel && typeof channel.consume === 'function') {
91
- instrumentChannel(channel);
92
- }
93
- return channel;
94
- };
95
- }
96
- // Also patch the internal init method that creates channels
97
- const origInit = AmqpConnection.prototype.init;
98
- if (origInit) {
99
- AmqpConnection.prototype.init = async function (...args) {
100
- const result = await origInit.apply(this, args);
101
- // After init, patch any existing channel
102
- if (this.channel && typeof this.channel.consume === 'function') {
103
- instrumentChannel(this.channel);
49
+ // Patch publish for outgoing
50
+ const originalPublish = AmqpConnection.prototype.publish;
51
+ if (originalPublish) {
52
+ AmqpConnection.prototype.publish = async function (exchange, routingKey, msg, ...args) {
53
+ const startTime = Date.now();
54
+ const result = await originalPublish.call(this, exchange, routingKey, msg, ...args);
55
+ const duration = Date.now() - startTime;
56
+ if (telescopeRef) {
57
+ telescopeRef.record({
58
+ type: entry_type_enum_1.EntryType.RABBITMQ,
59
+ content: {
60
+ direction: 'outgoing',
61
+ exchange,
62
+ routingKey,
63
+ message: sanitizeMessage(msg),
64
+ duration,
65
+ status: 'sent',
66
+ },
67
+ }).catch(() => { });
104
68
  }
105
69
  return result;
106
70
  };
107
71
  }
108
- // Patch the 'managedConnection' setter to catch channel creation
109
- const origConnect = AmqpConnection.prototype.connect;
110
- if (origConnect) {
111
- AmqpConnection.prototype.connect = async function (...args) {
112
- const result = await origConnect.apply(this, args);
113
- if (this.channel && typeof this.channel.consume === 'function') {
114
- instrumentChannel(this.channel);
115
- }
116
- return result;
72
+ // Patch subscribe for incoming
73
+ const originalSubscribe = AmqpConnection.prototype.subscribe;
74
+ if (originalSubscribe) {
75
+ AmqpConnection.prototype.subscribe = async function (queue, handler, options) {
76
+ const wrappedHandler = (msg) => {
77
+ if (msg && msg.content && telescopeRef) {
78
+ let parsed;
79
+ try {
80
+ parsed = JSON.parse(msg.content.toString());
81
+ }
82
+ catch {
83
+ parsed = msg.content.toString();
84
+ }
85
+ telescopeRef.record({
86
+ type: entry_type_enum_1.EntryType.RABBITMQ,
87
+ content: {
88
+ direction: 'incoming',
89
+ queue,
90
+ exchange: msg.fields?.exchange || '',
91
+ routingKey: msg.fields?.routingKey || queue,
92
+ message: sanitizeMessage(parsed),
93
+ deliveryTag: msg.fields?.deliveryTag,
94
+ timestamp: new Date().toISOString(),
95
+ },
96
+ }).catch(() => { });
97
+ }
98
+ return handler(msg);
99
+ };
100
+ return originalSubscribe.call(this, queue, wrappedHandler, options);
117
101
  };
118
102
  }
119
103
  }
@@ -127,55 +111,8 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
127
111
  }
128
112
  onModuleInit() {
129
113
  telescopeRef = this.telescope;
130
- this.patchAmqpConnectionPublish();
131
- this.patchExistingChannels();
132
114
  this.logger.log('RabbitMQWatcher initialized — incoming & outgoing messages will be recorded');
133
115
  }
134
- patchExistingChannels() {
135
- try {
136
- const { AmqpConnection } = require('@golevelup/nestjs-rabbitmq');
137
- if (!AmqpConnection)
138
- return;
139
- // Try to get the instance from the global scope
140
- // @golevelup/nestjs-rabbitmq stores it as a module provider
141
- const instances = AmqpConnection.__instances || [];
142
- for (const inst of instances) {
143
- if (inst?.channel && typeof inst.channel.consume === 'function') {
144
- instrumentChannel(inst.channel);
145
- }
146
- }
147
- }
148
- catch { }
149
- }
150
- patchAmqpConnectionPublish() {
151
- try {
152
- const { AmqpConnection } = require('@golevelup/nestjs-rabbitmq');
153
- if (!AmqpConnection || !AmqpConnection.prototype)
154
- return;
155
- const originalPublish = AmqpConnection.prototype.publish;
156
- if (!originalPublish)
157
- return;
158
- const self = this;
159
- AmqpConnection.prototype.publish = async function (exchange, routingKey, msg, ...args) {
160
- const startTime = Date.now();
161
- const result = await originalPublish.call(this, exchange, routingKey, msg, ...args);
162
- const duration = Date.now() - startTime;
163
- self.telescope.record({
164
- type: entry_type_enum_1.EntryType.RABBITMQ,
165
- content: {
166
- direction: 'outgoing',
167
- exchange,
168
- routingKey,
169
- message: sanitizeMessage(msg),
170
- duration,
171
- status: 'sent',
172
- },
173
- }).catch(() => { });
174
- return result;
175
- };
176
- }
177
- catch { }
178
- }
179
116
  };
180
117
  exports.RabbitMQWatcher = RabbitMQWatcher;
181
118
  exports.RabbitMQWatcher = RabbitMQWatcher = RabbitMQWatcher_1 = __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.3.11",
3
+ "version": "0.3.13",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },