@midwayjs/rabbitmq 4.0.0-beta.8 → 4.0.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/framework.js CHANGED
@@ -14,9 +14,11 @@ let MidwayRabbitMQFramework = class MidwayRabbitMQFramework extends core_1.BaseF
14
14
  return this.configService.getConfiguration('rabbitmq');
15
15
  }
16
16
  async applicationInitialize(options) {
17
+ const traceService = this.applicationContext.get(core_1.MidwayTraceService);
17
18
  // Create a connection manager
18
19
  this.app = new mq_1.RabbitMQServer({
19
20
  logger: this.logger,
21
+ traceService,
20
22
  ...this.configurationOptions,
21
23
  });
22
24
  }
@@ -55,26 +57,58 @@ let MidwayRabbitMQFramework = class MidwayRabbitMQFramework extends core_1.BaseF
55
57
  return channelWrapper.ack(data);
56
58
  },
57
59
  };
58
- this.app.createAnonymousContext(ctx);
59
- const isPassed = await this.app
60
- .getFramework()
61
- .runGuard(ctx, module, listenerOptions.propertyKey);
62
- if (!isPassed) {
63
- throw new core_1.MidwayInvokeForbiddenError(listenerOptions.propertyKey, module);
64
- }
65
- const ins = await ctx.requestContext.getAsync(module);
66
- const fn = await this.applyMiddleware(async (ctx) => {
67
- return await ins[listenerOptions.propertyKey].call(ins, data);
68
- });
69
- try {
70
- const result = await fn(ctx);
71
- if (result) {
72
- return channelWrapper.ack(data);
60
+ const traceService = this.applicationContext.get(core_1.MidwayTraceService);
61
+ const traceMetaResolver = this.configurationOptions
62
+ ?.tracing?.meta;
63
+ const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
64
+ const traceExtractor = this.configurationOptions?.tracing
65
+ ?.extractor;
66
+ const headersDefault = data?.properties?.headers ?? {};
67
+ const headers = typeof traceExtractor === 'function'
68
+ ? traceExtractor({
69
+ ctx,
70
+ request: data,
71
+ custom: { queueName: listenerOptions.queueName },
72
+ })
73
+ : headersDefault;
74
+ await traceService.runWithEntrySpan(`rabbitmq ${listenerOptions.queueName}`, {
75
+ enable: traceEnabled,
76
+ carrier: headers ?? headersDefault,
77
+ attributes: {
78
+ 'midway.protocol': 'rabbitmq',
79
+ 'midway.rabbitmq.queue': listenerOptions.queueName,
80
+ },
81
+ meta: traceMetaResolver,
82
+ metaArgs: {
83
+ ctx,
84
+ carrier: headers ?? headersDefault,
85
+ request: data,
86
+ custom: {
87
+ queueName: listenerOptions.queueName,
88
+ },
89
+ },
90
+ }, async () => {
91
+ this.app.createAnonymousContext(ctx);
92
+ const isPassed = await this.app
93
+ .getFramework()
94
+ .runGuard(ctx, module, listenerOptions.propertyKey);
95
+ if (!isPassed) {
96
+ throw new core_1.MidwayInvokeForbiddenError(listenerOptions.propertyKey, module);
73
97
  }
74
- }
75
- catch (error) {
76
- this.logger.error(error);
77
- }
98
+ const ins = await ctx.requestContext.getAsync(module);
99
+ const fn = await this.applyMiddleware(async (ctx) => {
100
+ return await ins[listenerOptions.propertyKey].call(ins, data);
101
+ });
102
+ try {
103
+ const result = await fn(ctx);
104
+ if (result) {
105
+ return channelWrapper.ack(data);
106
+ }
107
+ }
108
+ catch (error) {
109
+ this.logger.error(error);
110
+ }
111
+ });
78
112
  });
79
113
  }
80
114
  }
package/dist/mq.d.ts CHANGED
@@ -4,17 +4,24 @@
4
4
  import * as amqp from 'amqp-connection-manager';
5
5
  import { IRabbitMQApplication } from './interface';
6
6
  import { ConsumeMessage } from 'amqplib/properties';
7
- import { RabbitMQListenerOptions, ILogger } from '@midwayjs/core';
7
+ import { RabbitMQListenerOptions, ILogger, MidwayTraceService } from '@midwayjs/core';
8
8
  import type { Channel } from 'amqplib';
9
9
  import { EventEmitter } from 'events';
10
10
  export declare class RabbitMQServer extends EventEmitter implements IRabbitMQApplication {
11
11
  protected channelManagerSet: Set<Channel>;
12
12
  protected connection: amqp.AmqpConnectionManager;
13
13
  protected logger: ILogger;
14
+ protected traceService: MidwayTraceService;
15
+ protected traceEnabled: boolean;
16
+ protected traceInjector: (args: {
17
+ request?: unknown;
18
+ custom?: Record<string, unknown>;
19
+ }) => any;
14
20
  protected reconnectTime: any;
15
21
  constructor(options?: any);
16
22
  bindError(): void;
17
23
  createChannel(isConfirmChannel?: boolean): Promise<any>;
24
+ private bindTraceContext;
18
25
  connect(url: any, socketOptions: any): Promise<void>;
19
26
  createConsumer(listenerOptions: RabbitMQListenerOptions, listenerCallback: (msg: ConsumeMessage | null, channel: Channel, channelWrapper: any) => Promise<void>): Promise<void>;
20
27
  protected closeConnection(): Promise<void>;
package/dist/mq.js CHANGED
@@ -10,10 +10,16 @@ class RabbitMQServer extends events_1.EventEmitter {
10
10
  channelManagerSet = new Set();
11
11
  connection = null;
12
12
  logger;
13
+ traceService;
14
+ traceEnabled;
15
+ traceInjector;
13
16
  reconnectTime;
14
17
  constructor(options = {}) {
15
18
  super();
16
19
  this.logger = options.logger;
20
+ this.traceService = options.traceService;
21
+ this.traceEnabled = options.traceEnabled ?? options?.tracing?.enable;
22
+ this.traceInjector = options.traceInjector ?? options?.tracing?.injector;
17
23
  this.reconnectTime = options.reconnectTime ?? 10 * 1000;
18
24
  this.bindError();
19
25
  }
@@ -24,10 +30,54 @@ class RabbitMQServer extends events_1.EventEmitter {
24
30
  }
25
31
  createChannel(isConfirmChannel = false) {
26
32
  if (!isConfirmChannel) {
27
- return this.connection.connection.createChannel();
33
+ return this.connection.connection.createChannel().then(channel => {
34
+ this.bindTraceContext(channel);
35
+ return channel;
36
+ });
28
37
  }
29
38
  else {
30
- return this.connection.connection.createConfirmChannel();
39
+ return this.connection.connection.createConfirmChannel().then(channel => {
40
+ this.bindTraceContext(channel);
41
+ return channel;
42
+ });
43
+ }
44
+ }
45
+ bindTraceContext(channel) {
46
+ if (!channel || !this.traceService) {
47
+ return;
48
+ }
49
+ const injectHeaders = (options, custom) => {
50
+ const nextOptions = options ?? {};
51
+ const configuredCarrier = typeof this.traceInjector === 'function'
52
+ ? this.traceInjector({
53
+ request: nextOptions,
54
+ custom,
55
+ })
56
+ : undefined;
57
+ nextOptions.headers = configuredCarrier ?? nextOptions.headers ?? {};
58
+ if (this.traceEnabled !== false) {
59
+ this.traceService.injectContext(nextOptions.headers);
60
+ }
61
+ return nextOptions;
62
+ };
63
+ if (typeof channel.sendToQueue === 'function') {
64
+ const rawSendToQueue = channel.sendToQueue.bind(channel);
65
+ channel.sendToQueue = (queue, content, options) => {
66
+ return rawSendToQueue(queue, content, injectHeaders(options, {
67
+ method: 'sendToQueue',
68
+ queue,
69
+ }));
70
+ };
71
+ }
72
+ if (typeof channel.publish === 'function') {
73
+ const rawPublish = channel.publish.bind(channel);
74
+ channel.publish = (exchange, routingKey, content, options) => {
75
+ return rawPublish(exchange, routingKey, content, injectHeaders(options, {
76
+ method: 'publish',
77
+ exchange,
78
+ routingKey,
79
+ }));
80
+ };
31
81
  }
32
82
  }
33
83
  async connect(url, socketOptions) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/rabbitmq",
3
- "version": "4.0.0-beta.8",
3
+ "version": "4.0.0",
4
4
  "description": "Midway Framework for rabbitmq",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -25,12 +25,12 @@
25
25
  "license": "MIT",
26
26
  "devDependencies": {
27
27
  "@midwayjs/core": "workspace:^",
28
- "@types/amqplib": "0.10.6",
29
- "amqplib": "0.10.5",
30
- "fs-extra": "11.3.0"
28
+ "@types/amqplib": "0.10.8",
29
+ "amqplib": "0.10.9",
30
+ "fs-extra": "11.3.3"
31
31
  },
32
32
  "dependencies": {
33
- "@midwayjs/core": "^4.0.0-beta.8",
33
+ "@midwayjs/core": "^4.0.0",
34
34
  "amqp-connection-manager": "4.1.15"
35
35
  },
36
36
  "peerDependencies": {
@@ -45,5 +45,5 @@
45
45
  "engines": {
46
46
  "node": ">=20"
47
47
  },
48
- "gitHead": "355e55949fdd132b0bdcb4830222a0a027e92ded"
48
+ "gitHead": "014f32c23ebc1d5ac21777c76be2fd373ce992d8"
49
49
  }