@midwayjs/kafka 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
@@ -85,14 +85,42 @@ let MidwayKafkaFramework = class MidwayKafkaFramework extends core_1.BaseFramewo
85
85
  ...resourceInitializeConfig.consumerRunConfig,
86
86
  };
87
87
  runConfig[runMethod] = async (payload) => {
88
- const ctx = this.app.createAnonymousContext();
89
- const fn = await this.applyMiddleware(async (ctx) => {
90
- ctx.payload = payload;
91
- ctx.consumer = consumer;
92
- const instance = await ctx.requestContext.getAsync(ClzProvider);
93
- return await instance[runMethod].call(instance, payload, ctx);
88
+ const traceService = this.applicationContext.get(core_1.MidwayTraceService);
89
+ const traceMetaResolver = this.configurationOptions?.tracing
90
+ ?.meta;
91
+ const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
92
+ const traceExtractor = this.configurationOptions?.tracing
93
+ ?.extractor;
94
+ const headersDefault = payload?.message?.headers ?? {};
95
+ const headers = typeof traceExtractor === 'function'
96
+ ? traceExtractor({ request: payload, custom: { runMethod } })
97
+ : headersDefault;
98
+ return await traceService.runWithEntrySpan(`kafka ${payload?.topic ?? 'consumer'}`, {
99
+ enable: traceEnabled,
100
+ carrier: headers ?? headersDefault,
101
+ attributes: {
102
+ 'midway.protocol': 'kafka',
103
+ 'midway.kafka.topic': payload?.topic,
104
+ },
105
+ meta: traceMetaResolver,
106
+ metaArgs: {
107
+ carrier: headers ?? headersDefault,
108
+ request: payload,
109
+ custom: {
110
+ topic: payload?.topic,
111
+ runMethod,
112
+ },
113
+ },
114
+ }, async () => {
115
+ const ctx = this.app.createAnonymousContext();
116
+ const fn = await this.applyMiddleware(async (ctx) => {
117
+ ctx.payload = payload;
118
+ ctx.consumer = consumer;
119
+ const instance = await ctx.requestContext.getAsync(ClzProvider);
120
+ return await instance[runMethod].call(instance, payload, ctx);
121
+ });
122
+ return await fn(ctx);
94
123
  });
95
- return await fn(ctx);
96
124
  };
97
125
  return runConfig;
98
126
  },
@@ -1,4 +1,4 @@
1
- import { IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction, ServiceFactoryConfigOption } from "@midwayjs/core";
1
+ import { IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction, ServiceFactoryConfigOption } from '@midwayjs/core';
2
2
  import { AdminConfig, Consumer, ConsumerConfig, ConsumerRunConfig, ConsumerSubscribeTopic, ConsumerSubscribeTopics, EachBatchHandler, EachBatchPayload, EachMessageHandler, EachMessagePayload, Kafka, KafkaConfig, ProducerConfig } from 'kafkajs';
3
3
  export interface IKafkaApplication {
4
4
  }
package/dist/service.d.ts CHANGED
@@ -1,12 +1,19 @@
1
- import { ILogger, ServiceFactory, ServiceFactoryConfigOption } from '@midwayjs/core';
1
+ import { ILogger, ServiceFactory, ServiceFactoryConfigOption, MidwayTraceService } from '@midwayjs/core';
2
2
  import { Producer, Admin } from 'kafkajs';
3
3
  import { IMidwayKafkaAdminInitOptions, IMidwayKafkaProducerInitOptions } from './interface';
4
4
  export declare class KafkaProducerFactory extends ServiceFactory<Producer> {
5
5
  logger: ILogger;
6
6
  pubConfig: ServiceFactoryConfigOption<IMidwayKafkaProducerInitOptions>;
7
+ traceService: MidwayTraceService;
8
+ traceEnabled: boolean;
9
+ traceInjector: (args: {
10
+ request?: unknown;
11
+ custom?: Record<string, unknown>;
12
+ }) => any;
7
13
  getName(): string;
8
14
  init(): Promise<void>;
9
15
  protected createClient(config: IMidwayKafkaProducerInitOptions, clientName: any): Promise<Producer>;
16
+ private bindTraceContext;
10
17
  destroyClient(producer: Producer, name: string): Promise<void>;
11
18
  destroy(): Promise<void>;
12
19
  }
package/dist/service.js CHANGED
@@ -16,6 +16,9 @@ const manager_1 = require("./manager");
16
16
  let KafkaProducerFactory = class KafkaProducerFactory extends core_1.ServiceFactory {
17
17
  logger;
18
18
  pubConfig;
19
+ traceService;
20
+ traceEnabled;
21
+ traceInjector;
19
22
  getName() {
20
23
  return 'kafka:producer';
21
24
  }
@@ -38,12 +41,59 @@ let KafkaProducerFactory = class KafkaProducerFactory extends core_1.ServiceFact
38
41
  manager_1.KafkaManager.getInstance().addKafkaInstance(kafkaInstanceRef, client);
39
42
  }
40
43
  const producer = client.producer(producerOptions);
44
+ this.bindTraceContext(producer);
41
45
  producer.on('producer.connect', () => {
42
46
  this.logger.info('[midway:kafka] producer: %s is connect', clientName);
43
47
  });
44
48
  await producer.connect();
45
49
  return producer;
46
50
  }
51
+ bindTraceContext(producer) {
52
+ const injectHeaders = (headers, custom) => {
53
+ const configuredCarrier = typeof this.traceInjector === 'function'
54
+ ? this.traceInjector({
55
+ request: headers,
56
+ custom,
57
+ })
58
+ : undefined;
59
+ const carrier = configuredCarrier ?? headers ?? {};
60
+ if (this.traceEnabled !== false) {
61
+ this.traceService.injectContext(carrier);
62
+ }
63
+ return carrier;
64
+ };
65
+ const rawSend = producer.send.bind(producer);
66
+ producer.send = payload => {
67
+ const nextPayload = {
68
+ ...payload,
69
+ messages: (payload?.messages || []).map(message => ({
70
+ ...message,
71
+ headers: injectHeaders(message?.headers, {
72
+ sendMethod: 'send',
73
+ topic: payload?.topic,
74
+ }),
75
+ })),
76
+ };
77
+ return rawSend(nextPayload);
78
+ };
79
+ const rawSendBatch = producer.sendBatch.bind(producer);
80
+ producer.sendBatch = payload => {
81
+ const nextPayload = {
82
+ ...payload,
83
+ topicMessages: (payload?.topicMessages || []).map(topicMessage => ({
84
+ ...topicMessage,
85
+ messages: (topicMessage?.messages || []).map(message => ({
86
+ ...message,
87
+ headers: injectHeaders(message?.headers, {
88
+ sendMethod: 'sendBatch',
89
+ topic: topicMessage?.topic,
90
+ }),
91
+ })),
92
+ })),
93
+ };
94
+ return rawSendBatch(nextPayload);
95
+ };
96
+ }
47
97
  async destroyClient(producer, name) {
48
98
  await producer.disconnect();
49
99
  this.logger.info('[midway:kafka] producer: %s is close', name);
@@ -61,6 +111,18 @@ __decorate([
61
111
  (0, core_1.Config)('kafka.producer'),
62
112
  __metadata("design:type", Object)
63
113
  ], KafkaProducerFactory.prototype, "pubConfig", void 0);
114
+ __decorate([
115
+ (0, core_1.Inject)(),
116
+ __metadata("design:type", core_1.MidwayTraceService)
117
+ ], KafkaProducerFactory.prototype, "traceService", void 0);
118
+ __decorate([
119
+ (0, core_1.Config)('kafka.tracing.enable'),
120
+ __metadata("design:type", Boolean)
121
+ ], KafkaProducerFactory.prototype, "traceEnabled", void 0);
122
+ __decorate([
123
+ (0, core_1.Config)('kafka.tracing.injector'),
124
+ __metadata("design:type", Function)
125
+ ], KafkaProducerFactory.prototype, "traceInjector", void 0);
64
126
  __decorate([
65
127
  (0, core_1.Init)(),
66
128
  __metadata("design:type", Function),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/kafka",
3
- "version": "4.0.0-beta.9",
3
+ "version": "4.0.0",
4
4
  "description": "Midway Framework for kafka",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "homepage": "https://github.com/midwayjs/midway#readme",
35
35
  "devDependencies": {
36
- "@midwayjs/core": "^4.0.0-beta.9",
37
- "@midwayjs/mock": "^4.0.0-beta.9"
36
+ "@midwayjs/core": "^4.0.0",
37
+ "@midwayjs/mock": "^4.0.0"
38
38
  },
39
39
  "dependencies": {
40
40
  "kafkajs": "2.2.4"
@@ -42,5 +42,5 @@
42
42
  "engines": {
43
43
  "node": ">=20"
44
44
  },
45
- "gitHead": "771e83974ef9f3d78c296e4ee0c8c68db44f6b31"
45
+ "gitHead": "014f32c23ebc1d5ac21777c76be2fd373ce992d8"
46
46
  }