@midwayjs/mqtt 4.0.0-beta.9 → 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
@@ -79,12 +79,39 @@ let MidwayMQTTFramework = class MidwayMQTTFramework extends core_1.BaseFramework
79
79
  ctx.topic = topic;
80
80
  ctx.packet = packet;
81
81
  ctx.message = message;
82
- const fn = await this.applyMiddleware(async (ctx) => {
83
- const instance = await ctx.requestContext.getAsync(ClzProvider);
84
- // eslint-disable-next-line prefer-spread
85
- return await instance['subscribe'].call(instance, ctx);
82
+ const traceService = this.applicationContext.get(core_1.MidwayTraceService);
83
+ const traceMetaResolver = this.configurationOptions?.tracing
84
+ ?.meta;
85
+ const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
86
+ const traceExtractor = this.configurationOptions?.tracing
87
+ ?.extractor;
88
+ const packetPropertiesDefault = packet?.properties?.userProperties || {};
89
+ const packetProperties = typeof traceExtractor === 'function'
90
+ ? traceExtractor({ ctx, request: packet, custom: { topic } })
91
+ : packetPropertiesDefault;
92
+ return await traceService.runWithEntrySpan(`mqtt ${topic}`, {
93
+ enable: traceEnabled,
94
+ carrier: packetProperties ?? packetPropertiesDefault,
95
+ attributes: {
96
+ 'midway.protocol': 'mqtt',
97
+ 'midway.mqtt.topic': topic,
98
+ },
99
+ meta: traceMetaResolver,
100
+ metaArgs: {
101
+ ctx,
102
+ carrier: packetProperties ?? packetPropertiesDefault,
103
+ request: packet,
104
+ custom: {
105
+ topic,
106
+ },
107
+ },
108
+ }, async () => {
109
+ const fn = await this.applyMiddleware(async (ctx) => {
110
+ const instance = await ctx.requestContext.getAsync(ClzProvider);
111
+ return await instance['subscribe'].call(instance, ctx);
112
+ });
113
+ return await fn(ctx);
86
114
  });
87
- return await fn(ctx);
88
115
  });
89
116
  await consumer.subscribeAsync(subscribeOptions.topicObject, subscribeOptions.opts);
90
117
  return consumer;
package/dist/service.d.ts CHANGED
@@ -1,11 +1,18 @@
1
- import { ILogger, ServiceFactory, ServiceFactoryConfigOption } from '@midwayjs/core';
1
+ import { ILogger, ServiceFactory, ServiceFactoryConfigOption, MidwayTraceService } from '@midwayjs/core';
2
2
  import { type IClientOptions, MqttClient } from 'mqtt';
3
3
  export declare class MqttProducerFactory extends ServiceFactory<MqttClient> {
4
4
  logger: ILogger;
5
5
  pubConfig: ServiceFactoryConfigOption<IClientOptions>;
6
+ traceService: MidwayTraceService;
7
+ traceEnabled: boolean;
8
+ traceInjector: (args: {
9
+ request?: unknown;
10
+ custom?: Record<string, unknown>;
11
+ }) => any;
6
12
  getName(): string;
7
13
  init(): Promise<void>;
8
14
  protected createClient(config: any, clientName: any): Promise<MqttClient>;
15
+ private bindTraceContext;
9
16
  destroyClient(producer: MqttClient, name: string): Promise<void>;
10
17
  }
11
18
  export declare class DefaultMqttProducer implements MqttClient {
package/dist/service.js CHANGED
@@ -15,6 +15,9 @@ const mqtt_1 = require("mqtt");
15
15
  let MqttProducerFactory = class MqttProducerFactory extends core_1.ServiceFactory {
16
16
  logger;
17
17
  pubConfig;
18
+ traceService;
19
+ traceEnabled;
20
+ traceInjector;
18
21
  getName() {
19
22
  return 'mqtt';
20
23
  }
@@ -27,6 +30,7 @@ let MqttProducerFactory = class MqttProducerFactory extends core_1.ServiceFactor
27
30
  return new Promise(resolve => {
28
31
  const client = (0, mqtt_1.connect)(config);
29
32
  client.on('connect', () => {
33
+ this.bindTraceContext(client);
30
34
  this.logger.info('[midway-mqtt] producer: %s is connect', clientName);
31
35
  resolve(client);
32
36
  });
@@ -35,6 +39,38 @@ let MqttProducerFactory = class MqttProducerFactory extends core_1.ServiceFactor
35
39
  });
36
40
  });
37
41
  }
42
+ bindTraceContext(client) {
43
+ const injectCarrier = (topic, options) => {
44
+ const publishOptions = options ?? {};
45
+ if (!publishOptions.properties) {
46
+ publishOptions.properties = {};
47
+ }
48
+ const configuredCarrier = typeof this.traceInjector === 'function'
49
+ ? this.traceInjector({
50
+ request: publishOptions,
51
+ custom: {
52
+ topic,
53
+ },
54
+ })
55
+ : undefined;
56
+ publishOptions.properties.userProperties =
57
+ configuredCarrier ?? publishOptions.properties.userProperties ?? {};
58
+ if (this.traceEnabled !== false) {
59
+ this.traceService.injectContext(publishOptions.properties.userProperties);
60
+ }
61
+ return publishOptions;
62
+ };
63
+ const rawPublish = client.publish.bind(client);
64
+ client.publish = (topic, message, options, callback) => {
65
+ return rawPublish(topic, message, injectCarrier(topic, options), callback);
66
+ };
67
+ if (typeof client.publishAsync === 'function') {
68
+ const rawPublishAsync = client.publishAsync.bind(client);
69
+ client.publishAsync = (topic, message, options) => {
70
+ return rawPublishAsync(topic, message, injectCarrier(topic, options));
71
+ };
72
+ }
73
+ }
38
74
  async destroyClient(producer, name) {
39
75
  await producer.endAsync();
40
76
  this.logger.info('[midway-mqtt] producer: %s is close', name);
@@ -49,6 +85,18 @@ __decorate([
49
85
  (0, core_1.Config)('mqtt.pub'),
50
86
  __metadata("design:type", Object)
51
87
  ], MqttProducerFactory.prototype, "pubConfig", void 0);
88
+ __decorate([
89
+ (0, core_1.Inject)(),
90
+ __metadata("design:type", core_1.MidwayTraceService)
91
+ ], MqttProducerFactory.prototype, "traceService", void 0);
92
+ __decorate([
93
+ (0, core_1.Config)('mqtt.tracing.enable'),
94
+ __metadata("design:type", Boolean)
95
+ ], MqttProducerFactory.prototype, "traceEnabled", void 0);
96
+ __decorate([
97
+ (0, core_1.Config)('mqtt.tracing.injector'),
98
+ __metadata("design:type", Function)
99
+ ], MqttProducerFactory.prototype, "traceInjector", void 0);
52
100
  __decorate([
53
101
  (0, core_1.Init)(),
54
102
  __metadata("design:type", Function),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mqtt",
3
- "version": "4.0.0-beta.9",
3
+ "version": "4.0.0",
4
4
  "description": "Midway Framework for mqtt",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -23,11 +23,11 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/core": "^4.0.0-beta.9",
27
- "@midwayjs/mock": "^4.0.0-beta.9"
26
+ "@midwayjs/core": "^4.0.0",
27
+ "@midwayjs/mock": "^4.0.0"
28
28
  },
29
29
  "dependencies": {
30
- "mqtt": "5.14.1"
30
+ "mqtt": "5.15.0"
31
31
  },
32
32
  "author": "Harry Chen <czy88840616@gmail.com>",
33
33
  "repository": {
@@ -37,5 +37,5 @@
37
37
  "engines": {
38
38
  "node": ">=20"
39
39
  },
40
- "gitHead": "771e83974ef9f3d78c296e4ee0c8c68db44f6b31"
40
+ "gitHead": "014f32c23ebc1d5ac21777c76be2fd373ce992d8"
41
41
  }