@palmetto/pubsub 2.2.5 → 2.2.7

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/README.md CHANGED
@@ -114,6 +114,10 @@ yarn add @palmetto/pubsub zod
114
114
 
115
115
  1. Register with dd-trace for improved trace logs
116
116
 
117
+ Datadog trace logs will include publisher and subscriber actions using the queue/job/exchange name.
118
+
119
+ ![alt text](dd-trace-sample.png)
120
+
117
121
  ```ts
118
122
  import tracer from "dd-trace";
119
123
  import { registerDdTrace } from "@palmetto/pubsub";
@@ -38,18 +38,23 @@ class BullMqPublisher {
38
38
  }
39
39
  publish(config, messages) {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
+ var _a, _b;
41
42
  const queue = yield this.getQueue(config);
42
- yield Promise.all(messages.map((message) => __awaiter(this, void 0, void 0, function* () {
43
- var _a, _b;
44
- (_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publishing message to ${queue.name} - ${message}`);
45
- yield queue.add(config.job || connection_js_1.BULLMQ_DEFAULTJOB, message, {
43
+ (_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, `Publishing ${messages.length} messages to ${queue.name}`);
44
+ const job = {
45
+ name: config.job || connection_js_1.BULLMQ_DEFAULTJOB,
46
+ opts: {
46
47
  attempts: config.retries,
47
48
  backoff: {
48
49
  type: "fixed",
49
50
  delay: config.retryDelay || 30000,
50
51
  },
51
- });
52
- })));
52
+ },
53
+ };
54
+ const jobs = messages.map((data) => {
55
+ return Object.assign(Object.assign({}, job), { data });
56
+ });
57
+ yield queue.addBulk(jobs);
53
58
  });
54
59
  }
55
60
  init(config) {
@@ -14,7 +14,7 @@ const interfaces_js_1 = require("../interfaces.js");
14
14
  const connection_js_1 = require("./connection.js");
15
15
  const lazy_load_js_1 = require("../lazy-load.js");
16
16
  const create_log_error_payload_js_1 = require("../create-log-error-payload.js");
17
- const dd_trace_wrapper_js_1 = require("../dd-trace.wrapper.js");
17
+ const dd_trace_api_1 = require("dd-trace-api");
18
18
  class SubscribedMessage {
19
19
  constructor(owner, worker, logger) {
20
20
  this.owner = owner;
@@ -65,7 +65,7 @@ class BullMqSubscriber {
65
65
  context: "BullMqSubscriber",
66
66
  });
67
67
  const worker = new BullMqPackage.Worker(config.name, (job) => __awaiter(this, void 0, void 0, function* () {
68
- return yield (0, dd_trace_wrapper_js_1.getTracer)().trace("pubsub.bullmq.handle", {
68
+ return yield dd_trace_api_1.tracer.trace("pubsub.bullmq.handle", {
69
69
  resource: `handle ${config.name}`,
70
70
  }, () => __awaiter(this, void 0, void 0, function* () {
71
71
  const context = {
package/dist/main.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export * from "./dd-trace.wrapper.js";
2
1
  export * from "./errors.js";
3
2
  export * from "./interfaces.js";
4
3
  export * from "./publisher.js";
package/dist/main.js CHANGED
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./dd-trace.wrapper.js"), exports);
18
17
  __exportStar(require("./errors.js"), exports);
19
18
  __exportStar(require("./interfaces.js"), exports);
20
19
  __exportStar(require("./publisher.js"), exports);
package/dist/publisher.js CHANGED
@@ -15,7 +15,7 @@ const uuid_1 = require("uuid");
15
15
  const crypto_1 = require("crypto");
16
16
  const errors_js_1 = require("./errors.js");
17
17
  const message_logger_js_1 = require("./message-logger.js");
18
- const dd_trace_wrapper_js_1 = require("./dd-trace.wrapper.js");
18
+ const dd_trace_api_1 = require("dd-trace-api");
19
19
  class Publisher {
20
20
  constructor(logger, providers) {
21
21
  var _a, _b;
@@ -54,7 +54,10 @@ class Publisher {
54
54
  }
55
55
  publish(config, message) {
56
56
  const messages = Array.isArray(message) ? message : [message];
57
- return (0, dd_trace_wrapper_js_1.getTracer)().trace("pubsub.publish", {
57
+ if (messages.length === 0) {
58
+ return Promise.resolve();
59
+ }
60
+ return dd_trace_api_1.tracer.trace("pubsub.publish", {
58
61
  resource: `publish ${config.transport} ${config.name}`,
59
62
  }, (span) => {
60
63
  return this.publishImpl(config, messages, span);
@@ -64,7 +67,7 @@ class Publisher {
64
67
  return __awaiter(this, void 0, void 0, function* () {
65
68
  const provider = this.getProvider(config);
66
69
  const { schema, schemaId } = this.assertSchema(config);
67
- const jsons = messages.map((msg) => {
70
+ const mappedMessages = messages.map((msg) => {
68
71
  if (!msg.id) {
69
72
  msg.id = (0, uuid_1.v4)();
70
73
  }
@@ -82,30 +85,38 @@ class Publisher {
82
85
  if (!check.success) {
83
86
  (0, message_logger_js_1.logMessage)({
84
87
  note: "Publish message failed schema validation",
85
- message: messages,
88
+ message: msg,
86
89
  level: "error",
87
90
  logger: this.logger,
88
91
  extra: Object.assign({ transport: provider.transport, name: config.name }, provider.enrichPublishedMesssageLog(config)),
89
92
  });
90
- throw new errors_js_1.SchemaValidationError(`Schema did not accept the published message: ${check.error.message}`);
93
+ return { json: JSON.stringify(msg), error: check.error };
91
94
  }
92
95
  const json = JSON.stringify(check.data);
93
96
  return { json, data: check.data };
94
97
  });
98
+ const errors = mappedMessages.filter((mm) => mm.error);
99
+ if (errors.length > 0) {
100
+ const errorMessage = errors.map((mm) => { var _a; return (_a = mm.error) === null || _a === void 0 ? void 0 : _a.message; }).join(", ");
101
+ throw new errors_js_1.SchemaValidationError(`Schema did not accept ${errors.length} of ${messages.length} message(s): ${errorMessage}`);
102
+ }
103
+ const jsons = mappedMessages.filter((j) => j.data).map((j) => j.json);
95
104
  const start = (0, message_logger_js_1.startTiming)();
96
- yield provider.publish(config, jsons.map((j) => j.json));
105
+ yield provider.publish(config, jsons);
97
106
  const duration = (0, message_logger_js_1.getDuration)(start);
98
- jsons.forEach((msg) => {
107
+ mappedMessages.forEach((msg) => {
99
108
  (0, message_logger_js_1.logMessage)({
100
109
  note: "Published message",
101
110
  message: msg.data,
102
111
  level: config.messageLogLevel,
103
112
  logger: this.logger,
104
- extra: Object.assign({ transport: provider.transport, name: config.name, durationMs: duration }, provider.enrichPublishedMesssageLog(config)),
113
+ extra: Object.assign({ transport: provider.transport, name: config.name, durationMs: duration, batchSize: messages.length }, provider.enrichPublishedMesssageLog(config)),
105
114
  });
106
115
  });
107
- span.setTag("pubsub.duration_ms", duration);
108
- span.setTag("pubsub.message_count", messages.length);
116
+ if (span) {
117
+ span.setTag("pubsub.duration_ms", duration);
118
+ span.setTag("pubsub.message_count", messages.length);
119
+ }
109
120
  });
110
121
  }
111
122
  close() {
@@ -15,7 +15,7 @@ const errors_js_1 = require("../errors.js");
15
15
  const config_js_1 = require("./config.js");
16
16
  const connection_js_1 = require("./connection.js");
17
17
  const create_log_error_payload_js_1 = require("../create-log-error-payload.js");
18
- const dd_trace_wrapper_js_1 = require("../dd-trace.wrapper.js");
18
+ const dd_trace_api_1 = require("dd-trace-api");
19
19
  class SubscribedMessage {
20
20
  constructor(owner, logger, config, onMessage, stop) {
21
21
  this.owner = owner;
@@ -123,7 +123,7 @@ class RabbitMqSubscriber {
123
123
  ? setupResult.dlQueueName
124
124
  : setupResult.queueName;
125
125
  const onRabbitMessage = (msg) => __awaiter(this, void 0, void 0, function* () {
126
- yield (0, dd_trace_wrapper_js_1.getTracer)().trace("pubsub.rabbit.consume", {
126
+ yield dd_trace_api_1.tracer.trace("pubsub.rabbit.consume", {
127
127
  resource: `consume ${config.transport} ${queueName}`,
128
128
  }, (span) => __awaiter(this, void 0, void 0, function* () {
129
129
  try {
@@ -135,7 +135,7 @@ class RabbitMqSubscriber {
135
135
  message: "Unexpected error handling RabbitMq message",
136
136
  error: (0, create_log_error_payload_js_1.createLogErrorPayload)(err),
137
137
  });
138
- span.setTag("error", err);
138
+ span === null || span === void 0 ? void 0 : span.setTag("error", err);
139
139
  try {
140
140
  channel.nack(msg, undefined, true);
141
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@palmetto/pubsub",
3
- "version": "2.2.5",
3
+ "version": "2.2.7",
4
4
  "main": "./dist/main.js",
5
5
  "scripts": {
6
6
  "lint": "yarn run -T eslint --fix ./src",
@@ -25,6 +25,7 @@
25
25
  "amqp-connection-manager": "^4.1.14",
26
26
  "amqplib": "^0.10.8",
27
27
  "bullmq": "^5.58.0",
28
+ "opentracing": "^0.14.7",
28
29
  "ts-node": "^10.9.2",
29
30
  "typescript": "^5.8.3",
30
31
  "vitest": "^3.2.4",
@@ -41,6 +42,7 @@
41
42
  "access": "public"
42
43
  },
43
44
  "dependencies": {
45
+ "dd-trace-api": "^1.0.0",
44
46
  "uuid": "^11.1.0"
45
47
  },
46
48
  "peerDependencies": {
@@ -1,75 +0,0 @@
1
- export interface Span {
2
- /**
3
- * Adds a single tag to the span. See `addTags()` for details.
4
- *
5
- * @param {string} key
6
- * @param {any} value
7
- */
8
- setTag(key: string, value: unknown): this;
9
- }
10
- interface SpanOptions {
11
- /**
12
- * set of key-value pairs which will be set as tags on the newly created
13
- * Span. Ownership of the object is passed to the created span for
14
- * efficiency reasons (the caller should not modify this object after
15
- * calling startSpan).
16
- */
17
- tags?: {
18
- [key: string]: unknown;
19
- };
20
- }
21
- /**
22
- * The Datadog Scope Manager. This is used for context propagation.
23
- */
24
- interface Scope {
25
- /**
26
- * Get the current active span or null if there is none.
27
- *
28
- * @returns {Span} The active span.
29
- */
30
- active(): Span | null;
31
- }
32
- interface TraceOptions {
33
- /**
34
- * The resource you are tracing. The resource name must not be longer than
35
- * 5000 characters.
36
- */
37
- resource?: string;
38
- /**
39
- * The type of request.
40
- */
41
- type?: string;
42
- }
43
- interface Tracer {
44
- /**
45
- * Instruments a function by automatically creating a span activated on its
46
- * scope.
47
- *
48
- * The span will automatically be finished when one of these conditions is
49
- * met:
50
- *
51
- * * The function returns a promise, in which case the span will finish when
52
- * the promise is resolved or rejected.
53
- * * The function takes a callback as its second parameter, in which case the
54
- * span will finish when that callback is called.
55
- * * The function doesn't accept a callback and doesn't return a promise, in
56
- * which case the span will finish at the end of the function execution.
57
- */
58
- trace<T>(name: string, options: TraceOptions & SpanOptions, fn: (span: Span) => Promise<T>): Promise<T>;
59
- /**
60
- * Returns a reference to the current scope.
61
- */
62
- scope(): Scope;
63
- }
64
- export declare function getTracer(): Tracer;
65
- /**
66
- * Registers the dd-trace tracer instance to be used by the pubsub package.
67
- *
68
- * Usage:
69
- * import trace from 'dd-trace';
70
- * registerDdTrace(trace.init());
71
- *
72
- * @param tracer the tracer instance
73
- */
74
- export declare function registerDdTrace(tracer: unknown): void;
75
- export {};
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTracer = getTracer;
4
- exports.registerDdTrace = registerDdTrace;
5
- class NoOpSpan {
6
- finish() {
7
- // no-op
8
- }
9
- setTag(_key, _value) {
10
- return this;
11
- }
12
- }
13
- class NoOpScope {
14
- active() {
15
- return null;
16
- }
17
- }
18
- class NoOpTracer {
19
- constructor() {
20
- this.noOpSpan = new NoOpSpan();
21
- this.noOpScope = new NoOpScope();
22
- }
23
- trace(_name, _options, fn) {
24
- return fn(this.noOpSpan);
25
- }
26
- scope() {
27
- return this.noOpScope;
28
- }
29
- }
30
- let tracerInstance = new NoOpTracer();
31
- function getTracer() {
32
- return tracerInstance;
33
- }
34
- /**
35
- * Registers the dd-trace tracer instance to be used by the pubsub package.
36
- *
37
- * Usage:
38
- * import trace from 'dd-trace';
39
- * registerDdTrace(trace.init());
40
- *
41
- * @param tracer the tracer instance
42
- */
43
- function registerDdTrace(tracer) {
44
- tracerInstance = tracer;
45
- }