@autofleet/rabbit 3.0.0 → 3.0.1-alpha10

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.d.ts CHANGED
@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
3
3
  import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
4
4
  import { ConfirmChannel, ConsumeMessage, Options, Replies } from 'amqplib';
5
5
  import { RedisConfig } from './lib/redis';
6
- import { TRACING_HEADER } from './lib/consts';
6
+ import { TRACING_HEADER, USER_TRACING_HEADER } from './lib/consts';
7
7
  import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache } from './lib/types';
8
8
  export interface IAfRabbitMq {
9
9
  ack: any;
@@ -44,6 +44,7 @@ declare class RabbitMq implements IAfRabbitMq {
44
44
  static getPublishOptions(customHeaders?: CustomMessageHeaders): {
45
45
  timestamp: number;
46
46
  headers: {
47
+ "x-af-user-id": any;
47
48
  "x-trace-id": any;
48
49
  redisTimestampValidationKey?: string | undefined;
49
50
  creationTimestamp: number;
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ const util_1 = require("util");
9
9
  const moment_1 = __importDefault(require("moment"));
10
10
  const redis_lock_1 = __importDefault(require("redis-lock"));
11
11
  const amqp_connection_manager_1 = require("amqp-connection-manager");
12
- const outbreak_1 = require("@autofleet/outbreak");
12
+ const zehut_1 = require("@autofleet/zehut");
13
13
  const logger_1 = __importDefault(require("./logger"));
14
14
  const rabbitError_1 = __importDefault(require("./lib/rabbitError"));
15
15
  const redis_1 = __importDefault(require("./lib/redis"));
@@ -124,12 +124,14 @@ class RabbitMq {
124
124
  }
125
125
  }
126
126
  static getPublishOptions(customHeaders = {}) {
127
- const trace = outbreak_1.getCurrentContext();
127
+ const trace = zehut_1.getCurrentPayload();
128
+ const user = trace?.context?.get(consts_1.USER_OBJECT);
128
129
  return {
129
130
  timestamp: moment_1.default().unix(),
130
131
  headers: {
131
132
  creationTimestamp: moment_1.default().valueOf(),
132
133
  ...customHeaders,
134
+ [consts_1.USER_TRACING_HEADER]: user?.id,
133
135
  [consts_1.TRACING_HEADER]: trace?.context?.get(consts_1.TRACING_HEADER),
134
136
  },
135
137
  };
@@ -319,7 +321,7 @@ class RabbitMq {
319
321
  async consumeFromRabbit(queue, callback, options) {
320
322
  const optionsWithDefaults = { ...consts_1.DEFAULT_OPTIONS, ...options };
321
323
  RabbitMq.validateName('queue', queue);
322
- const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, } = optionsWithDefaults;
324
+ const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace, } = optionsWithDefaults;
323
325
  if (useConsumeWithLock) {
324
326
  if (!this.redisLock) {
325
327
  throw new Error('Usage of consumeWithLock requires RedisInstance');
@@ -335,10 +337,23 @@ class RabbitMq {
335
337
  return null;
336
338
  }
337
339
  const traceId = msg.properties.headers[consts_1.TRACING_HEADER];
340
+ const userId = msg.properties.headers[consts_1.USER_TRACING_HEADER];
341
+ logger_1.default.info('rabbit: consume', { traceId, userId, enableRabbitTrace });
338
342
  if (traceId) {
339
- const trace = outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
343
+ const trace = zehut_1.newTrace(zehut_1.traceTypes.RABBIT);
340
344
  trace?.context?.set(consts_1.TRACING_HEADER, traceId);
341
345
  }
346
+ else if (userId && enableRabbitTrace) {
347
+ try {
348
+ await zehut_1.setRabbitTrace(userId);
349
+ }
350
+ catch (e) {
351
+ logger_1.default.error('rabbit: failed to setRabbitTrace', { userId, e });
352
+ }
353
+ }
354
+ if (auditContext) {
355
+ auditContext(queue);
356
+ }
342
357
  const parsedMessage = RabbitMq.parseMsg(msg);
343
358
  const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
344
359
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
@@ -2,6 +2,8 @@ export declare const DEFAULT_DEAD_TTL_TWO_DAYS: number;
2
2
  export declare const DEFAULT_LOCK_TIMEOUT: number;
3
3
  export declare const RETRY_HEADER = "x-retry-count";
4
4
  export declare const TRACING_HEADER = "x-trace-id";
5
+ export declare const USER_TRACING_HEADER = "x-af-user-id";
6
+ export declare const USER_OBJECT = "userObject";
5
7
  export declare const DEFAULT_USE_CONSUME_WITH_LOCK = false;
6
8
  export declare const CONNECTION_CREATED_CONST = "connectionCreated";
7
9
  export declare const DEFAULT_OPTIONS: {
@@ -10,4 +12,6 @@ export declare const DEFAULT_OPTIONS: {
10
12
  deadMessageTtl: number;
11
13
  lockTimeout: number;
12
14
  useConsumeWithLock: boolean;
15
+ auditContext: null;
16
+ enableRabbitTrace: boolean;
13
17
  };
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_OPTIONS = exports.CONNECTION_CREATED_CONST = exports.DEFAULT_USE_CONSUME_WITH_LOCK = exports.TRACING_HEADER = exports.RETRY_HEADER = exports.DEFAULT_LOCK_TIMEOUT = exports.DEFAULT_DEAD_TTL_TWO_DAYS = void 0;
3
+ exports.DEFAULT_OPTIONS = exports.CONNECTION_CREATED_CONST = exports.DEFAULT_USE_CONSUME_WITH_LOCK = exports.USER_OBJECT = exports.USER_TRACING_HEADER = exports.TRACING_HEADER = exports.RETRY_HEADER = exports.DEFAULT_LOCK_TIMEOUT = exports.DEFAULT_DEAD_TTL_TWO_DAYS = void 0;
4
4
  exports.DEFAULT_DEAD_TTL_TWO_DAYS = 60000 * 60 * 12;
5
5
  exports.DEFAULT_LOCK_TIMEOUT = 1000 * 5;
6
6
  exports.RETRY_HEADER = 'x-retry-count';
7
7
  exports.TRACING_HEADER = 'x-trace-id';
8
+ exports.USER_TRACING_HEADER = 'x-af-user-id';
9
+ exports.USER_OBJECT = 'userObject';
8
10
  exports.DEFAULT_USE_CONSUME_WITH_LOCK = false;
9
11
  exports.CONNECTION_CREATED_CONST = 'connectionCreated';
10
12
  exports.DEFAULT_OPTIONS = {
@@ -13,4 +15,6 @@ exports.DEFAULT_OPTIONS = {
13
15
  deadMessageTtl: exports.DEFAULT_DEAD_TTL_TWO_DAYS,
14
16
  lockTimeout: exports.DEFAULT_LOCK_TIMEOUT,
15
17
  useConsumeWithLock: exports.DEFAULT_USE_CONSUME_WITH_LOCK,
18
+ auditContext: null,
19
+ enableRabbitTrace: false,
16
20
  };
@@ -17,6 +17,8 @@ export interface ConsumeOptions {
17
17
  limit?: number;
18
18
  lockTimeout?: number;
19
19
  useConsumeWithLock?: boolean;
20
+ auditContext?: any;
21
+ enableRabbitTrace?: boolean;
20
22
  }
21
23
  export declare type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>;
22
24
  export declare type newChannelOpts = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.0.0",
3
+ "version": "3.0.1-alpha10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@autofleet/logger": "^1.2.8",
19
+ "@autofleet/zehut": "^3.0.1-alpha3",
19
20
  "@types/amqplib": "0.8.2",
20
21
  "@types/uuid": "^8.3.3",
21
22
  "amqp-connection-manager": "4.1.9",
@@ -26,9 +27,6 @@
26
27
  "redis-lock": "^0.1.4",
27
28
  "uuid": "^8.3.2"
28
29
  },
29
- "peerDependencies": {
30
- "@autofleet/outbreak": "^0.2.1"
31
- },
32
30
  "devDependencies": {
33
31
  "@types/jest": "^26.0.23",
34
32
  "@typescript-eslint/eslint-plugin": "^4.8.1",
package/src/index.ts CHANGED
@@ -8,7 +8,9 @@ import { AmqpConnectionManager, ChannelWrapper, connect } from 'amqp-connection-
8
8
  import {
9
9
  ConfirmChannel, ConsumeMessage, Options, Replies,
10
10
  } from 'amqplib';
11
- import { getCurrentContext, newTrace, traceTypes } from '@autofleet/outbreak';
11
+ import {
12
+ getCurrentPayload, newTrace, traceTypes, setRabbitTrace,
13
+ } from '@autofleet/zehut';
12
14
  import logger from './logger';
13
15
  import RabbitError from './lib/rabbitError';
14
16
  import getRedisInstance, { RedisConfig } from './lib/redis';
@@ -19,6 +21,8 @@ import {
19
21
  DEFAULT_OPTIONS,
20
22
  RETRY_HEADER,
21
23
  TRACING_HEADER,
24
+ USER_OBJECT,
25
+ USER_TRACING_HEADER,
22
26
  } from './lib/consts';
23
27
  import {
24
28
  CallbackFunction,
@@ -108,12 +112,14 @@ class RabbitMq implements IAfRabbitMq {
108
112
  }
109
113
 
110
114
  static getPublishOptions(customHeaders: CustomMessageHeaders = {}) {
111
- const trace = getCurrentContext();
115
+ const trace = getCurrentPayload();
116
+ const user = trace?.context?.get(USER_OBJECT);
112
117
  return {
113
118
  timestamp: moment().unix(),
114
119
  headers: {
115
120
  creationTimestamp: moment().valueOf(),
116
121
  ...customHeaders,
122
+ [USER_TRACING_HEADER]: user?.id,
117
123
  [TRACING_HEADER]: trace?.context?.get(TRACING_HEADER),
118
124
  },
119
125
  };
@@ -455,7 +461,7 @@ class RabbitMq implements IAfRabbitMq {
455
461
  const optionsWithDefaults = { ...DEFAULT_OPTIONS, ...options };
456
462
  RabbitMq.validateName('queue', queue);
457
463
  const {
458
- limit, deadMessageTtl, useConsumeWithLock, lockTimeout,
464
+ limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace,
459
465
  } = optionsWithDefaults;
460
466
  if (useConsumeWithLock) {
461
467
  if (!this.redisLock) {
@@ -475,9 +481,21 @@ class RabbitMq implements IAfRabbitMq {
475
481
  }
476
482
 
477
483
  const traceId = msg.properties.headers[TRACING_HEADER];
484
+ const userId = msg.properties.headers[USER_TRACING_HEADER];
485
+ logger.info('rabbit: consume', { traceId, userId, enableRabbitTrace });
478
486
  if (traceId) {
479
487
  const trace = newTrace(traceTypes.RABBIT);
480
488
  (trace as any)?.context?.set(TRACING_HEADER, traceId);
489
+ } else if (userId && enableRabbitTrace) {
490
+ try {
491
+ await setRabbitTrace(userId);
492
+ } catch (e) {
493
+ logger.error('rabbit: failed to setRabbitTrace', { userId, e });
494
+ }
495
+ }
496
+
497
+ if (auditContext) {
498
+ auditContext(queue);
481
499
  }
482
500
 
483
501
  const parsedMessage = RabbitMq.parseMsg(msg);
package/src/lib/consts.ts CHANGED
@@ -2,6 +2,8 @@ export const DEFAULT_DEAD_TTL_TWO_DAYS = 60000 * 60 * 12;
2
2
  export const DEFAULT_LOCK_TIMEOUT = 1000 * 5;
3
3
  export const RETRY_HEADER = 'x-retry-count';
4
4
  export const TRACING_HEADER = 'x-trace-id';
5
+ export const USER_TRACING_HEADER = 'x-af-user-id';
6
+ export const USER_OBJECT = 'userObject';
5
7
  export const DEFAULT_USE_CONSUME_WITH_LOCK = false;
6
8
  export const CONNECTION_CREATED_CONST = 'connectionCreated';
7
9
  export const DEFAULT_OPTIONS = {
@@ -10,4 +12,6 @@ export const DEFAULT_OPTIONS = {
10
12
  deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
11
13
  lockTimeout: DEFAULT_LOCK_TIMEOUT,
12
14
  useConsumeWithLock: DEFAULT_USE_CONSUME_WITH_LOCK,
15
+ auditContext: null,
16
+ enableRabbitTrace: false,
13
17
  };
package/src/lib/types.ts CHANGED
@@ -21,6 +21,8 @@ export interface ConsumeOptions {
21
21
  limit?: number;
22
22
  lockTimeout?: number;
23
23
  useConsumeWithLock?: boolean;
24
+ auditContext?: any;
25
+ enableRabbitTrace?: boolean;
24
26
  }
25
27
 
26
28
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>