@autofleet/rabbit 3.0.1-alpha9 → 3.0.1-beta1

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/index.js CHANGED
@@ -126,13 +126,14 @@ class RabbitMq {
126
126
  static getPublishOptions(customHeaders = {}) {
127
127
  const trace = zehut_1.getCurrentPayload();
128
128
  const user = trace?.context?.get(consts_1.USER_OBJECT);
129
+ const outbreakTrace = zehut_1.oubtreak.getCurrentContext();
129
130
  return {
130
131
  timestamp: moment_1.default().unix(),
131
132
  headers: {
132
133
  creationTimestamp: moment_1.default().valueOf(),
133
134
  ...customHeaders,
134
135
  [consts_1.USER_TRACING_HEADER]: user?.id,
135
- [consts_1.TRACING_HEADER]: trace?.context?.get(consts_1.TRACING_HEADER),
136
+ [consts_1.TRACING_HEADER]: outbreakTrace?.context?.get(consts_1.TRACING_HEADER),
136
137
  },
137
138
  };
138
139
  }
@@ -338,18 +339,25 @@ class RabbitMq {
338
339
  }
339
340
  const traceId = msg.properties.headers[consts_1.TRACING_HEADER];
340
341
  const userId = msg.properties.headers[consts_1.USER_TRACING_HEADER];
342
+ logger_1.default.info('rabbit: consume', { traceId, userId, enableRabbitTrace });
343
+ const parsedMessage = RabbitMq.parseMsg(msg);
344
+ const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
345
+ const trace = zehut_1.newTrace(zehut_1.traceTypes.RABBIT);
341
346
  if (traceId) {
342
- const trace = zehut_1.newTrace(zehut_1.traceTypes.RABBIT);
343
347
  trace?.context?.set(consts_1.TRACING_HEADER, traceId);
344
348
  }
345
- else if (userId && enableRabbitTrace) {
346
- await zehut_1.setRabbitTrace(userId);
349
+ if (userId && enableRabbitTrace) {
350
+ try {
351
+ await zehut_1.createOrSetRabbitTrace(trace, userId);
352
+ }
353
+ catch (e) {
354
+ logger_1.default.error('rabbit: failed to setRabbitTrace', { userId, e });
355
+ await this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }, msg, releaseLock)(msg);
356
+ }
347
357
  }
348
358
  if (auditContext) {
349
- auditContext(queue);
359
+ await auditContext(queue);
350
360
  }
351
- const parsedMessage = RabbitMq.parseMsg(msg);
352
- const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
353
361
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
354
362
  if (!shouldConsume) {
355
363
  await this.unlockRedisIfNeeded(releaseLock);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.0.1-alpha9",
3
+ "version": "3.0.1-beta1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@autofleet/logger": "^1.2.8",
19
- "@autofleet/zehut": "^3.0.1-alpha3",
19
+ "@autofleet/zehut": "^3.0.1-beta2",
20
20
  "@types/amqplib": "0.8.2",
21
21
  "@types/uuid": "^8.3.3",
22
22
  "amqp-connection-manager": "4.1.9",
package/src/index.ts CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  ConfirmChannel, ConsumeMessage, Options, Replies,
10
10
  } from 'amqplib';
11
11
  import {
12
- getCurrentPayload, newTrace, traceTypes, setRabbitTrace,
12
+ getCurrentPayload, newTrace, traceTypes, createOrSetRabbitTrace, oubtreak,
13
13
  } from '@autofleet/zehut';
14
14
  import logger from './logger';
15
15
  import RabbitError from './lib/rabbitError';
@@ -114,13 +114,14 @@ class RabbitMq implements IAfRabbitMq {
114
114
  static getPublishOptions(customHeaders: CustomMessageHeaders = {}) {
115
115
  const trace = getCurrentPayload();
116
116
  const user = trace?.context?.get(USER_OBJECT);
117
+ const outbreakTrace = oubtreak.getCurrentContext();
117
118
  return {
118
119
  timestamp: moment().unix(),
119
120
  headers: {
120
121
  creationTimestamp: moment().valueOf(),
121
122
  ...customHeaders,
122
123
  [USER_TRACING_HEADER]: user?.id,
123
- [TRACING_HEADER]: trace?.context?.get(TRACING_HEADER),
124
+ [TRACING_HEADER]: outbreakTrace?.context?.get(TRACING_HEADER),
124
125
  },
125
126
  };
126
127
  }
@@ -482,19 +483,25 @@ class RabbitMq implements IAfRabbitMq {
482
483
 
483
484
  const traceId = msg.properties.headers[TRACING_HEADER];
484
485
  const userId = msg.properties.headers[USER_TRACING_HEADER];
486
+ logger.info('rabbit: consume', { traceId, userId, enableRabbitTrace });
487
+ const parsedMessage = RabbitMq.parseMsg(msg);
488
+ const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
489
+ const trace = newTrace(traceTypes.RABBIT);
485
490
  if (traceId) {
486
- const trace = newTrace(traceTypes.RABBIT);
487
491
  (trace as any)?.context?.set(TRACING_HEADER, traceId);
488
- } else if (userId && enableRabbitTrace) {
489
- await setRabbitTrace(userId);
492
+ }
493
+ if (userId && enableRabbitTrace) {
494
+ try {
495
+ await createOrSetRabbitTrace(trace, userId);
496
+ } catch (e) {
497
+ logger.error('rabbit: failed to setRabbitTrace', { userId, e });
498
+ await this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }, msg, releaseLock)(msg);
499
+ }
490
500
  }
491
501
 
492
502
  if (auditContext) {
493
- auditContext(queue);
503
+ await auditContext(queue);
494
504
  }
495
-
496
- const parsedMessage = RabbitMq.parseMsg(msg);
497
- const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
498
505
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
499
506
  if (!shouldConsume) {
500
507
  await this.unlockRedisIfNeeded(releaseLock);