@palmetto/pubsub 2.2.2 → 2.2.4
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/dd-trace.wrapper.d.ts +34 -20
- package/dist/dd-trace.wrapper.js +11 -2
- package/dist/publisher.js +52 -60
- package/dist/rabbitmq/publisher.js +3 -3
- package/dist/subscriber.js +72 -80
- package/package.json +1 -1
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
interface Span {
|
|
2
|
-
/**
|
|
3
|
-
* Sets the end timestamp and finalizes Span state.
|
|
4
|
-
*
|
|
5
|
-
* With the exception of calls to Span.context() (which are always allowed),
|
|
6
|
-
* finish() must be the last call made to any span instance, and to do
|
|
7
|
-
* otherwise leads to undefined behavior.
|
|
8
|
-
*/
|
|
9
|
-
finish(): void;
|
|
10
2
|
/**
|
|
11
3
|
* Adds a single tag to the span. See `addTags()` for details.
|
|
12
4
|
*
|
|
@@ -16,19 +8,13 @@ interface Span {
|
|
|
16
8
|
setTag(key: string, value: unknown): this;
|
|
17
9
|
}
|
|
18
10
|
interface SpanOptions {
|
|
19
|
-
/**
|
|
20
|
-
* a parent SpanContext (or Span, for convenience) that the newly-started
|
|
21
|
-
* span will be the child of (per REFERENCE_CHILD_OF). If specified,
|
|
22
|
-
* `references` must be unspecified.
|
|
23
|
-
*/
|
|
24
|
-
childOf: Span | null;
|
|
25
11
|
/**
|
|
26
12
|
* set of key-value pairs which will be set as tags on the newly created
|
|
27
13
|
* Span. Ownership of the object is passed to the created span for
|
|
28
14
|
* efficiency reasons (the caller should not modify this object after
|
|
29
15
|
* calling startSpan).
|
|
30
16
|
*/
|
|
31
|
-
tags
|
|
17
|
+
tags?: {
|
|
32
18
|
[key: string]: unknown;
|
|
33
19
|
};
|
|
34
20
|
}
|
|
@@ -43,19 +29,47 @@ interface Scope {
|
|
|
43
29
|
*/
|
|
44
30
|
active(): Span | null;
|
|
45
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
|
+
}
|
|
46
43
|
interface Tracer {
|
|
47
44
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
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.
|
|
52
57
|
*/
|
|
53
|
-
|
|
58
|
+
trace<T>(name: string, options: TraceOptions & SpanOptions, fn: (span: Span) => Promise<T>): Promise<T>;
|
|
54
59
|
/**
|
|
55
60
|
* Returns a reference to the current scope.
|
|
56
61
|
*/
|
|
57
62
|
scope(): Scope;
|
|
58
63
|
}
|
|
59
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
|
+
*/
|
|
60
74
|
export declare function registerDdTrace(tracer: unknown): void;
|
|
61
75
|
export {};
|
package/dist/dd-trace.wrapper.js
CHANGED
|
@@ -20,8 +20,8 @@ class NoOpTracer {
|
|
|
20
20
|
this.noOpSpan = new NoOpSpan();
|
|
21
21
|
this.noOpScope = new NoOpScope();
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
return this.noOpSpan;
|
|
23
|
+
trace(_name, _options, fn) {
|
|
24
|
+
return fn(this.noOpSpan);
|
|
25
25
|
}
|
|
26
26
|
scope() {
|
|
27
27
|
return this.noOpScope;
|
|
@@ -31,6 +31,15 @@ let tracerInstance = new NoOpTracer();
|
|
|
31
31
|
function getTracer() {
|
|
32
32
|
return tracerInstance;
|
|
33
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
|
+
*/
|
|
34
43
|
function registerDdTrace(tracer) {
|
|
35
44
|
tracerInstance = tracer;
|
|
36
45
|
}
|
package/dist/publisher.js
CHANGED
|
@@ -54,71 +54,63 @@ class Publisher {
|
|
|
54
54
|
}
|
|
55
55
|
publish(config, message) {
|
|
56
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
const provider = this.getProvider(config);
|
|
67
|
-
const { schema, schemaId } = this.assertSchema(config);
|
|
68
|
-
if (!Array.isArray(message)) {
|
|
69
|
-
message = [message];
|
|
70
|
-
}
|
|
71
|
-
const jsons = message.map((msg) => {
|
|
72
|
-
if (!msg.id) {
|
|
73
|
-
msg.id = (0, uuid_1.v4)();
|
|
74
|
-
}
|
|
75
|
-
if (!msg.meta) {
|
|
76
|
-
msg.meta = {
|
|
77
|
-
createdAt: new Date().toISOString(),
|
|
78
|
-
publishedBy: "",
|
|
79
|
-
schemaId: schemaId,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
msg.meta.schemaId = schemaId;
|
|
57
|
+
yield (0, dd_trace_wrapper_js_1.getTracer)().trace("pubsub.publish", {
|
|
58
|
+
resource: `publish ${config.transport} ${config.name}`,
|
|
59
|
+
}, (span) => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
try {
|
|
61
|
+
const provider = this.getProvider(config);
|
|
62
|
+
const { schema, schemaId } = this.assertSchema(config);
|
|
63
|
+
if (!Array.isArray(message)) {
|
|
64
|
+
message = [message];
|
|
84
65
|
}
|
|
85
|
-
const
|
|
86
|
-
|
|
66
|
+
const jsons = message.map((msg) => {
|
|
67
|
+
if (!msg.id) {
|
|
68
|
+
msg.id = (0, uuid_1.v4)();
|
|
69
|
+
}
|
|
70
|
+
if (!msg.meta) {
|
|
71
|
+
msg.meta = {
|
|
72
|
+
createdAt: new Date().toISOString(),
|
|
73
|
+
publishedBy: "",
|
|
74
|
+
schemaId: schemaId,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
msg.meta.schemaId = schemaId;
|
|
79
|
+
}
|
|
80
|
+
const check = schema.safeEncode(msg);
|
|
81
|
+
if (!check.success) {
|
|
82
|
+
(0, message_logger_js_1.logMessage)({
|
|
83
|
+
note: "Publish message failed schema validation",
|
|
84
|
+
message: message,
|
|
85
|
+
level: "error",
|
|
86
|
+
logger: this.logger,
|
|
87
|
+
extra: Object.assign({ transport: provider.transport, name: config.name }, provider.enrichPublishedMesssageLog(config)),
|
|
88
|
+
});
|
|
89
|
+
throw new errors_js_1.SchemaValidationError(`Schema did not accept the published message: ${check.error.message}`);
|
|
90
|
+
}
|
|
91
|
+
const json = JSON.stringify(check.data);
|
|
92
|
+
return { json, data: check.data };
|
|
93
|
+
});
|
|
94
|
+
const start = (0, message_logger_js_1.startTiming)();
|
|
95
|
+
yield provider.publish(config, jsons.map((j) => j.json));
|
|
96
|
+
const duration = (0, message_logger_js_1.getDuration)(start);
|
|
97
|
+
jsons.forEach((msg) => {
|
|
87
98
|
(0, message_logger_js_1.logMessage)({
|
|
88
|
-
note: "
|
|
89
|
-
message:
|
|
90
|
-
level:
|
|
99
|
+
note: "Published message",
|
|
100
|
+
message: msg.data,
|
|
101
|
+
level: config.messageLogLevel,
|
|
91
102
|
logger: this.logger,
|
|
92
|
-
extra: Object.assign({ transport: provider.transport, name: config.name }, provider.enrichPublishedMesssageLog(config)),
|
|
103
|
+
extra: Object.assign({ transport: provider.transport, name: config.name, durationMs: duration }, provider.enrichPublishedMesssageLog(config)),
|
|
93
104
|
});
|
|
94
|
-
throw new errors_js_1.SchemaValidationError(`Schema did not accept the published message: ${check.error.message}`);
|
|
95
|
-
}
|
|
96
|
-
const json = JSON.stringify(check.data);
|
|
97
|
-
return { json, data: check.data };
|
|
98
|
-
});
|
|
99
|
-
const start = (0, message_logger_js_1.startTiming)();
|
|
100
|
-
yield provider.publish(config, jsons.map((j) => j.json));
|
|
101
|
-
const duration = (0, message_logger_js_1.getDuration)(start);
|
|
102
|
-
jsons.forEach((msg) => {
|
|
103
|
-
(0, message_logger_js_1.logMessage)({
|
|
104
|
-
note: "Published message",
|
|
105
|
-
message: msg.data,
|
|
106
|
-
level: config.messageLogLevel,
|
|
107
|
-
logger: this.logger,
|
|
108
|
-
extra: Object.assign({ transport: provider.transport, name: config.name, durationMs: duration }, provider.enrichPublishedMesssageLog(config)),
|
|
109
105
|
});
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
finally {
|
|
120
|
-
span.finish();
|
|
121
|
-
}
|
|
106
|
+
span.setTag("pubsub.duration_ms", duration);
|
|
107
|
+
span.setTag("pubsub.message_count", message.length);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
span.setTag("error", err);
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
}));
|
|
122
114
|
});
|
|
123
115
|
}
|
|
124
116
|
close() {
|
|
@@ -38,7 +38,7 @@ class RabbitMqPublisher {
|
|
|
38
38
|
var _a, _b, _c, _d;
|
|
39
39
|
const delayMs = this.connection.config.startupRetryDelayMs || 500;
|
|
40
40
|
const timeoutMs = this.connection.config.startupRetryTimeoutMs || 20000;
|
|
41
|
-
const timeoutAfter =
|
|
41
|
+
const timeoutAfter = Date.now() + timeoutMs;
|
|
42
42
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
43
43
|
while (true) {
|
|
44
44
|
try {
|
|
@@ -63,7 +63,7 @@ class RabbitMqPublisher {
|
|
|
63
63
|
return this.channel;
|
|
64
64
|
}
|
|
65
65
|
catch (err) {
|
|
66
|
-
if (
|
|
66
|
+
if (Date.now() >= timeoutAfter) {
|
|
67
67
|
throw err;
|
|
68
68
|
}
|
|
69
69
|
yield (0, utility_js_1.delay)(delayMs);
|
|
@@ -114,7 +114,7 @@ class RabbitMqPublisher {
|
|
|
114
114
|
return __awaiter(this, void 0, void 0, function* () {
|
|
115
115
|
const options = {
|
|
116
116
|
contentType: "application/json",
|
|
117
|
-
timestamp:
|
|
117
|
+
timestamp: Date.now(),
|
|
118
118
|
persistent: true,
|
|
119
119
|
};
|
|
120
120
|
const oks = yield Promise.all(messages.map((message) => sender(Buffer.from(message, "utf8"), options)));
|
package/dist/subscriber.js
CHANGED
|
@@ -56,86 +56,78 @@ class Subscriber {
|
|
|
56
56
|
if (!provider) {
|
|
57
57
|
throw new errors_js_1.MissingPubSubProviderError(`No provider configured for ${config.transport}`);
|
|
58
58
|
}
|
|
59
|
-
const transform = (jsonStr, context) =>
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
err,
|
|
132
|
-
};
|
|
133
|
-
this.events.emit("messageHandled", eventContext);
|
|
134
|
-
span.setTag("pubsub.success", false);
|
|
135
|
-
span.finish();
|
|
136
|
-
throw err;
|
|
137
|
-
}
|
|
138
|
-
});
|
|
59
|
+
const transform = (jsonStr, context) => {
|
|
60
|
+
return (0, dd_trace_wrapper_js_1.getTracer)().trace("pubsub.handleMessage", {
|
|
61
|
+
resource: `handle ${config.transport} ${config.name}`,
|
|
62
|
+
}, (span) => __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const start = (0, message_logger_js_1.startTiming)();
|
|
64
|
+
const jsonObject = JSON.parse(jsonStr);
|
|
65
|
+
const r = schema.safeDecode(jsonObject);
|
|
66
|
+
if (!r.success) {
|
|
67
|
+
const durationMs = (0, message_logger_js_1.getDuration)(start);
|
|
68
|
+
(0, message_logger_js_1.logMessage)({
|
|
69
|
+
note: "Subscribed message failed schema validation",
|
|
70
|
+
message: jsonObject,
|
|
71
|
+
level: "error",
|
|
72
|
+
logger: this.logger,
|
|
73
|
+
extra: Object.assign({ transport: provider.transport, name: config.name, durationMs }, provider.enrichHandledMesssageLog(config)),
|
|
74
|
+
});
|
|
75
|
+
const handledEventContext = {
|
|
76
|
+
message: jsonStr,
|
|
77
|
+
context,
|
|
78
|
+
config,
|
|
79
|
+
durationMs,
|
|
80
|
+
result: interfaces_js_1.MessageResult.Fail,
|
|
81
|
+
};
|
|
82
|
+
this.events.emit("schemaError", handledEventContext);
|
|
83
|
+
this.events.emit("messageHandled", handledEventContext);
|
|
84
|
+
span.setTag("error", r.error);
|
|
85
|
+
return interfaces_js_1.MessageResult.Fail;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const result = yield onMessage(r.data, context);
|
|
89
|
+
const durationMs = (0, message_logger_js_1.getDuration)(start);
|
|
90
|
+
const eventContext = {
|
|
91
|
+
message: jsonStr,
|
|
92
|
+
context,
|
|
93
|
+
config,
|
|
94
|
+
durationMs,
|
|
95
|
+
result,
|
|
96
|
+
};
|
|
97
|
+
(0, message_logger_js_1.logMessage)({
|
|
98
|
+
note: "Subscriber processed message",
|
|
99
|
+
message: r.data,
|
|
100
|
+
level: config.messageLogLevel,
|
|
101
|
+
logger: this.logger,
|
|
102
|
+
extra: Object.assign({ transport: provider.transport, name: config.name, durationMs,
|
|
103
|
+
result }, provider.enrichHandledMesssageLog(config)),
|
|
104
|
+
});
|
|
105
|
+
this.events.emit("messageHandled", eventContext);
|
|
106
|
+
span.setTag("pubsub.duration_ms", durationMs);
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
const durationMs = (0, message_logger_js_1.getDuration)(start);
|
|
111
|
+
(0, message_logger_js_1.logMessage)({
|
|
112
|
+
note: "Subscriber error when processing message",
|
|
113
|
+
message: r.data,
|
|
114
|
+
level: "error",
|
|
115
|
+
logger: this.logger,
|
|
116
|
+
extra: Object.assign(Object.assign({ transport: provider.transport, name: config.name, durationMs }, provider.enrichHandledMesssageLog(config)), { error: (0, create_log_error_payload_js_1.createLogErrorPayload)(err) }),
|
|
117
|
+
});
|
|
118
|
+
const eventContext = {
|
|
119
|
+
message: jsonStr,
|
|
120
|
+
context,
|
|
121
|
+
config,
|
|
122
|
+
durationMs,
|
|
123
|
+
err,
|
|
124
|
+
};
|
|
125
|
+
this.events.emit("messageHandled", eventContext);
|
|
126
|
+
span.setTag("error", r.error);
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
}));
|
|
130
|
+
};
|
|
139
131
|
this.logger.log(`Starting subscriber for ${config.transport}:${config.name}`);
|
|
140
132
|
subscribedMessage = new SubscribedMessage(yield provider.startSubscribe(config, transform));
|
|
141
133
|
this.subscribedMessages.set(config, subscribedMessage);
|