rocketmq-client-remoting-nodejs 0.0.0-stage → 0.0.1
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.en.md +428 -0
- package/README.md +424 -2
- package/dist/client/admin.d.ts +229 -0
- package/dist/client/admin.js +2462 -0
- package/dist/client/allocate.d.ts +48 -0
- package/dist/client/allocate.js +365 -0
- package/dist/client/backpressure.d.ts +23 -0
- package/dist/client/backpressure.js +95 -0
- package/dist/client/consumer.d.ts +294 -0
- package/dist/client/consumer.js +3095 -0
- package/dist/client/consumer_result.d.ts +129 -0
- package/dist/client/consumer_result.js +138 -0
- package/dist/client/consumer_stats.d.ts +69 -0
- package/dist/client/consumer_stats.js +258 -0
- package/dist/client/exception.d.ts +39 -0
- package/dist/client/exception.js +36 -0
- package/dist/client/hook.d.ts +145 -0
- package/dist/client/hook.js +171 -0
- package/dist/client/latency.d.ts +61 -0
- package/dist/client/latency.js +239 -0
- package/dist/client/lite_pull_consumer.d.ts +151 -0
- package/dist/client/lite_pull_consumer.js +1096 -0
- package/dist/client/mq_client.d.ts +188 -0
- package/dist/client/mq_client.js +1678 -0
- package/dist/client/mq_client_manager.d.ts +21 -0
- package/dist/client/mq_client_manager.js +129 -0
- package/dist/client/offset_store.d.ts +45 -0
- package/dist/client/offset_store.js +389 -0
- package/dist/client/open_tracing_hook.d.ts +52 -0
- package/dist/client/open_tracing_hook.js +241 -0
- package/dist/client/pop_api.d.ts +28 -0
- package/dist/client/pop_api.js +224 -0
- package/dist/client/pop_consumer.d.ts +23 -0
- package/dist/client/pop_consumer.js +719 -0
- package/dist/client/pop_process_queue.d.ts +15 -0
- package/dist/client/pop_process_queue.js +51 -0
- package/dist/client/process_queue.d.ts +49 -0
- package/dist/client/process_queue.js +261 -0
- package/dist/client/produce_accumulator.d.ts +75 -0
- package/dist/client/produce_accumulator.js +288 -0
- package/dist/client/producer.d.ts +231 -0
- package/dist/client/producer.js +1774 -0
- package/dist/client/pull_api.d.ts +26 -0
- package/dist/client/pull_api.js +264 -0
- package/dist/client/pull_consumer.d.ts +119 -0
- package/dist/client/pull_consumer.js +876 -0
- package/dist/client/pull_schedule_service.d.ts +71 -0
- package/dist/client/pull_schedule_service.js +424 -0
- package/dist/client/request_reply.d.ts +61 -0
- package/dist/client/request_reply.js +232 -0
- package/dist/client/retry_message.d.ts +2 -0
- package/dist/client/retry_message.js +27 -0
- package/dist/client/send_result.d.ts +57 -0
- package/dist/client/send_result.js +70 -0
- package/dist/client/top_addressing.d.ts +20 -0
- package/dist/client/top_addressing.js +75 -0
- package/dist/client/trace_context.d.ts +61 -0
- package/dist/client/trace_context.js +373 -0
- package/dist/client/trace_dispatcher.d.ts +31 -0
- package/dist/client/trace_dispatcher.js +236 -0
- package/dist/client/trace_hook.d.ts +23 -0
- package/dist/client/trace_hook.js +228 -0
- package/dist/client/traceparent.d.ts +9 -0
- package/dist/client/traceparent.js +86 -0
- package/dist/common/boundary_type.d.ts +7 -0
- package/dist/common/boundary_type.js +16 -0
- package/dist/common/compress.d.ts +28 -0
- package/dist/common/compress.js +512 -0
- package/dist/common/message.d.ts +129 -0
- package/dist/common/message.js +214 -0
- package/dist/common/messageClientIdSetter.d.ts +7 -0
- package/dist/common/messageClientIdSetter.js +48 -0
- package/dist/common/messageConst.d.ts +68 -0
- package/dist/common/messageConst.js +68 -0
- package/dist/common/messageDecoder.d.ts +40 -0
- package/dist/common/messageDecoder.js +504 -0
- package/dist/common/messageType.d.ts +11 -0
- package/dist/common/messageType.js +11 -0
- package/dist/common/message_accessor.d.ts +39 -0
- package/dist/common/message_accessor.js +135 -0
- package/dist/common/mixAll.d.ts +106 -0
- package/dist/common/mixAll.js +247 -0
- package/dist/common/recall_message_handle.d.ts +25 -0
- package/dist/common/recall_message_handle.js +98 -0
- package/dist/common/subscriptionData.d.ts +36 -0
- package/dist/common/subscriptionData.js +64 -0
- package/dist/common/sysflag.d.ts +129 -0
- package/dist/common/sysflag.js +96 -0
- package/dist/common/topic_config.d.ts +66 -0
- package/dist/common/topic_config.js +106 -0
- package/dist/common/topic_validator.d.ts +49 -0
- package/dist/common/topic_validator.js +115 -0
- package/dist/common/utilAll.d.ts +39 -0
- package/dist/common/utilAll.js +154 -0
- package/dist/common/validators.d.ts +25 -0
- package/dist/common/validators.js +80 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +36 -0
- package/dist/logging.d.ts +24 -0
- package/dist/logging.js +246 -0
- package/dist/remoting/acl.d.ts +16 -0
- package/dist/remoting/acl.js +39 -0
- package/dist/remoting/admin_body.d.ts +257 -0
- package/dist/remoting/admin_body.js +669 -0
- package/dist/remoting/bodies.d.ts +475 -0
- package/dist/remoting/bodies.js +1131 -0
- package/dist/remoting/client.d.ts +73 -0
- package/dist/remoting/client.js +591 -0
- package/dist/remoting/codes.d.ts +296 -0
- package/dist/remoting/codes.js +301 -0
- package/dist/remoting/exception.d.ts +45 -0
- package/dist/remoting/exception.js +58 -0
- package/dist/remoting/extra_info.d.ts +61 -0
- package/dist/remoting/extra_info.js +220 -0
- package/dist/remoting/headers.d.ts +758 -0
- package/dist/remoting/headers.js +1307 -0
- package/dist/remoting/heartbeat.d.ts +91 -0
- package/dist/remoting/heartbeat.js +227 -0
- package/dist/remoting/namespace.d.ts +14 -0
- package/dist/remoting/namespace.js +85 -0
- package/dist/remoting/pop_bodies.d.ts +58 -0
- package/dist/remoting/pop_bodies.js +214 -0
- package/dist/remoting/remotingCommand.d.ts +39 -0
- package/dist/remoting/remotingCommand.js +225 -0
- package/dist/remoting/route.d.ts +61 -0
- package/dist/remoting/route.js +288 -0
- package/dist/remoting/rpc_hooks.d.ts +31 -0
- package/dist/remoting/rpc_hooks.js +73 -0
- package/dist/remoting/serialize.d.ts +33 -0
- package/dist/remoting/serialize.js +372 -0
- package/dist/remoting/socks_proxy.d.ts +29 -0
- package/dist/remoting/socks_proxy.js +379 -0
- package/dist/remoting/static_topic.d.ts +49 -0
- package/dist/remoting/static_topic.js +244 -0
- package/dist/remoting/subscription.d.ts +65 -0
- package/dist/remoting/subscription.js +172 -0
- package/package.json +38 -4
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Message trace hooks (send / consume / transaction end / recall message).
|
|
2
|
+
//
|
|
3
|
+
// Contract highlights:
|
|
4
|
+
// - The BEFORE and AFTER halves of one event **must share the same TraceContext
|
|
5
|
+
// object** (its internal requestId is what the console uses to stitch the two
|
|
6
|
+
// halves together) — the hook stores it in the context's hookState.
|
|
7
|
+
// - On the consume side msgId uses UNIQ_KEY (Java MessageClientExt.getMsgId()
|
|
8
|
+
// parity), falling back to the broker OFFSET-form id when UNIQ_KEY is absent;
|
|
9
|
+
// the producer-side Pub trace likewise uses UNIQ_KEY.
|
|
10
|
+
// - Exceptions inside a hook must never bring delivery down: swallow them all.
|
|
11
|
+
import { ConsumeMessageHook, SendMessageHook, } from "./hook.js";
|
|
12
|
+
import { TraceContext, TraceType, TraceBean, DefaultTraceRegionId, AccessChannelLocal, traceBeanFromMessageExt, } from "./trace_context.js";
|
|
13
|
+
import { createUniqID } from "../common/messageClientIdSetter.js";
|
|
14
|
+
import { MessageConst } from "../common/messageConst.js";
|
|
15
|
+
import { RequestCode, ResponseCode } from "../remoting/codes.js";
|
|
16
|
+
import { RecallMessageRequestHeader } from "../remoting/headers.js";
|
|
17
|
+
import { NamespaceUtil } from "../remoting/namespace.js";
|
|
18
|
+
import { RecallMessageHandle } from "../common/recall_message_handle.js";
|
|
19
|
+
import { getLogger } from "../logging.js";
|
|
20
|
+
const logger = getLogger('client.trace_hook');
|
|
21
|
+
// SendMessageTraceHookImpl records Pub events. The method names follow the
|
|
22
|
+
// SendMessageHook sendMessageBefore/sendMessageAfter contract strictly — the
|
|
23
|
+
// producer send path iterates hookRegistry.sendMessageHooks and invokes exactly
|
|
24
|
+
// these two names directly.
|
|
25
|
+
export class SendMessageTraceHookImpl extends SendMessageHook {
|
|
26
|
+
dispatcher;
|
|
27
|
+
constructor(dispatcher) {
|
|
28
|
+
super();
|
|
29
|
+
this.dispatcher = dispatcher;
|
|
30
|
+
}
|
|
31
|
+
getHookName() { return 'SendMessageTraceHookImpl'; }
|
|
32
|
+
sendMessageBefore(ctx) {
|
|
33
|
+
try {
|
|
34
|
+
const context = new TraceContext();
|
|
35
|
+
context.traceType = TraceType.PUB;
|
|
36
|
+
context.timeStamp = Date.now();
|
|
37
|
+
context.isSuccess = true;
|
|
38
|
+
// Java's Pub context carries no accessChannel of its own: the dispatcher's
|
|
39
|
+
// channel (start(namesrv, accessChannel)) decides the topic. The encoder also
|
|
40
|
+
// reads this value for SubAfter's CLOUD wire shape, so it must be concrete.
|
|
41
|
+
context.accessChannel = this.dispatcher.accessChannel || AccessChannelLocal;
|
|
42
|
+
context.regionId = ctx.brokerAddr || DefaultTraceRegionId;
|
|
43
|
+
context.groupName = ctx.producerGroup || '';
|
|
44
|
+
const bean = new TraceBean();
|
|
45
|
+
const msg = ctx.message;
|
|
46
|
+
if (msg) {
|
|
47
|
+
bean.topic = msg.getTopic();
|
|
48
|
+
bean.tags = msg.getProperty(MessageConst.PROPERTY_TAGS) || '';
|
|
49
|
+
bean.keys = msg.getProperty(MessageConst.PROPERTY_KEYS) || '';
|
|
50
|
+
bean.storeHost = '';
|
|
51
|
+
bean.clientHost = '';
|
|
52
|
+
const body = msg.getBody();
|
|
53
|
+
bean.bodyLength = body ? body.length : 0;
|
|
54
|
+
bean.msgId = msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) || '';
|
|
55
|
+
}
|
|
56
|
+
context.traceBeans = [bean];
|
|
57
|
+
ctx.mqTraceContext = context;
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
logger.debug('trace sendBefore error: %s', e.message);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
sendMessageAfter(ctx) {
|
|
64
|
+
try {
|
|
65
|
+
const context = ctx.mqTraceContext;
|
|
66
|
+
if (!context || context.traceBeans.length === 0)
|
|
67
|
+
return;
|
|
68
|
+
const bean = context.traceBeans[0];
|
|
69
|
+
context.costTime = ctx.costTime || (Date.now() - context.timeStamp);
|
|
70
|
+
context.isSuccess = !!(ctx.sendResult && ctx.sendResult.sendStatus === 'SEND_OK');
|
|
71
|
+
if (ctx.sendResult) {
|
|
72
|
+
bean.msgId = ctx.sendResult.msgId || bean.msgId;
|
|
73
|
+
bean.offsetMsgId = ctx.sendResult.offsetMsgId || '';
|
|
74
|
+
context.regionId = ctx.sendResult.regionId || context.regionId;
|
|
75
|
+
if (ctx.sendResult.messageQueue) {
|
|
76
|
+
bean.topic = ctx.sendResult.messageQueue.getTopic() || bean.topic;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
this.dispatcher.append(context);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
logger.debug('trace sendAfter error: %s', e.message);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// ConsumeMessageTraceHookImpl records the SubBefore + SubAfter events. One
|
|
87
|
+
// batch consume produces one SubBefore record PER message (they share the
|
|
88
|
+
// timestamp/region/group/requestId) and one SubAfter record per message.
|
|
89
|
+
export class ConsumeMessageTraceHookImpl extends ConsumeMessageHook {
|
|
90
|
+
dispatcher;
|
|
91
|
+
constructor(dispatcher) {
|
|
92
|
+
super();
|
|
93
|
+
this.dispatcher = dispatcher;
|
|
94
|
+
}
|
|
95
|
+
getHookName() { return 'ConsumeMessageTraceHookImpl'; }
|
|
96
|
+
consumeMessageBefore(ctx) {
|
|
97
|
+
try {
|
|
98
|
+
const msgs = ctx.msgList || [];
|
|
99
|
+
if (msgs.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
const context = new TraceContext();
|
|
102
|
+
context.traceType = TraceType.SUB_BEFORE;
|
|
103
|
+
context.timeStamp = Date.now();
|
|
104
|
+
context.isSuccess = true;
|
|
105
|
+
// Java ConsumeMessageConcurrentlyService:410 stamps the facade's channel on the
|
|
106
|
+
// consume context; the hook forwards it (Java's SubBefore falls back to the
|
|
107
|
+
// dispatcher, but this port's encoder needs a concrete value).
|
|
108
|
+
context.accessChannel = ctx.accessChannel || this.dispatcher.accessChannel || AccessChannelLocal;
|
|
109
|
+
// Java ConsumeMessageTraceHookImpl:61 reads MSG_REGION off the message. A blank
|
|
110
|
+
// region makes AsyncTraceDispatcher skip the record (:316-318), so use the
|
|
111
|
+
// broker's default region when the property is absent.
|
|
112
|
+
const first = msgs[0];
|
|
113
|
+
const region = first && typeof first.getProperty === 'function'
|
|
114
|
+
? first.getProperty(MessageConst.PROPERTY_MSG_REGION) : null;
|
|
115
|
+
context.regionId = region || DefaultTraceRegionId;
|
|
116
|
+
context.groupName = ctx.consumerGroup || '';
|
|
117
|
+
// The request id joins SubBefore and SubAfter of one consume.
|
|
118
|
+
context.requestId = createUniqID();
|
|
119
|
+
context.traceBeans = msgs.map((m) => {
|
|
120
|
+
const bean = traceBeanFromMessageExt(m, false);
|
|
121
|
+
bean.clientHost = bean.clientHost || '';
|
|
122
|
+
return bean;
|
|
123
|
+
});
|
|
124
|
+
ctx.mqTraceContext = context;
|
|
125
|
+
// The SubBefore context is appended here (consumeMessageAfter only adds the
|
|
126
|
+
// SubAfter half). Without this line the SubBefore records would never
|
|
127
|
+
// reach the wire.
|
|
128
|
+
if (context.traceBeans.length > 0)
|
|
129
|
+
this.dispatcher.append(context);
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
logger.debug('trace consumeBefore error: %s', e.message);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
consumeMessageAfter(ctx) {
|
|
136
|
+
try {
|
|
137
|
+
const before = ctx.mqTraceContext;
|
|
138
|
+
if (!before || before.traceBeans.length === 0)
|
|
139
|
+
return;
|
|
140
|
+
const msgs = ctx.msgList || [];
|
|
141
|
+
const statusName = ctx.props ? ctx.props['ConsumeContextType'] : undefined;
|
|
142
|
+
const contextCode = consumeReturnTypeCode(statusName);
|
|
143
|
+
for (let i = 0; i < before.traceBeans.length && i < msgs.length; i++) {
|
|
144
|
+
const after = new TraceContext();
|
|
145
|
+
after.traceType = TraceType.SUB_AFTER;
|
|
146
|
+
after.requestId = before.requestId;
|
|
147
|
+
after.costTime = Date.now() - before.timeStamp;
|
|
148
|
+
after.isSuccess = ctx.success === true;
|
|
149
|
+
after.contextCode = contextCode;
|
|
150
|
+
// Java ConsumeMessageTraceHookImpl:102 copies the consume context's channel
|
|
151
|
+
// onto SubAfter (it decides both the topic and the CLOUD wire shape).
|
|
152
|
+
after.accessChannel = ctx.accessChannel || this.dispatcher.accessChannel || AccessChannelLocal;
|
|
153
|
+
after.regionId = before.regionId;
|
|
154
|
+
after.timeStamp = before.timeStamp;
|
|
155
|
+
after.groupName = before.groupName;
|
|
156
|
+
const bean = traceBeanFromMessageExt(msgs[i], false);
|
|
157
|
+
bean.retryTimes = msgs[i].getReconsumeTimes ? msgs[i].getReconsumeTimes() : 0;
|
|
158
|
+
after.traceBeans = [bean];
|
|
159
|
+
this.dispatcher.append(after);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
logger.debug('trace consumeAfter error: %s', e.message);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// consumeReturnTypeCode maps a ConsumeContextType name back to the ordinal
|
|
168
|
+
// stored in the SubAfter contextCode.
|
|
169
|
+
function consumeReturnTypeCode(name) {
|
|
170
|
+
switch (name) {
|
|
171
|
+
case 'TIME_OUT': return 1;
|
|
172
|
+
case 'EXCEPTION': return 2;
|
|
173
|
+
case 'RETURNNULL': return 3;
|
|
174
|
+
case 'FAILED': return 4;
|
|
175
|
+
default: return 0; // SUCCESS
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// DefaultRecallMessageTraceHook is the RPCHook attached to the remoting client
|
|
179
|
+
// together with the trace dispatcher (registered in the producer branch where
|
|
180
|
+
// trace is enabled). It watches the RECALL_MESSAGE rpc and appends a Recall
|
|
181
|
+
// trace entry for every answered request.
|
|
182
|
+
//
|
|
183
|
+
// Gated by the system property com.rocketmq.recall.default.trace.enable
|
|
184
|
+
// (**off by default**, must be turned on explicitly); the node side reads it from
|
|
185
|
+
// the environment variable of the same name.
|
|
186
|
+
export class DefaultRecallMessageTraceHook {
|
|
187
|
+
dispatcher;
|
|
188
|
+
enableDefaultTrace;
|
|
189
|
+
constructor(dispatcher) {
|
|
190
|
+
this.dispatcher = dispatcher;
|
|
191
|
+
const raw = process.env['com.rocketmq.recall.default.trace.enable'];
|
|
192
|
+
this.enableDefaultTrace = raw != null && (raw === 'true' || raw === '1' || raw === 'True');
|
|
193
|
+
}
|
|
194
|
+
// doBeforeRequest stays a no-op.
|
|
195
|
+
doBeforeRequest(_remoteAddr, _request) { }
|
|
196
|
+
doAfterResponse(remoteAddr, request, response) {
|
|
197
|
+
void remoteAddr;
|
|
198
|
+
try {
|
|
199
|
+
if (request == null || request.code !== RequestCode.RECALL_MESSAGE)
|
|
200
|
+
return;
|
|
201
|
+
if (!this.enableDefaultTrace || response == null || this.dispatcher == null)
|
|
202
|
+
return;
|
|
203
|
+
const ext = response.extFields || {};
|
|
204
|
+
const regionId = ext[MessageConst.PROPERTY_MSG_REGION];
|
|
205
|
+
if (regionId == null)
|
|
206
|
+
return;
|
|
207
|
+
const header = new RecallMessageRequestHeader();
|
|
208
|
+
header.fromExtFields(request.extFields || {});
|
|
209
|
+
const topic = NamespaceUtil.withoutNamespace(header.topic != null ? header.topic : '');
|
|
210
|
+
const group = NamespaceUtil.withoutNamespace(header.producerGroup != null ? header.producerGroup : '');
|
|
211
|
+
const handleV1 = RecallMessageHandle.decodeHandle(header.recallHandle);
|
|
212
|
+
const bean = new TraceBean();
|
|
213
|
+
bean.topic = topic;
|
|
214
|
+
bean.msgId = handleV1.messageId != null ? handleV1.messageId : '';
|
|
215
|
+
const context = new TraceContext();
|
|
216
|
+
context.regionId = regionId;
|
|
217
|
+
context.accessChannel = this.dispatcher.accessChannel || AccessChannelLocal;
|
|
218
|
+
context.traceBeans = [bean];
|
|
219
|
+
context.traceType = TraceType.RECALL;
|
|
220
|
+
context.groupName = group;
|
|
221
|
+
context.isSuccess = response.code === ResponseCode.SUCCESS;
|
|
222
|
+
this.dispatcher.append(context);
|
|
223
|
+
}
|
|
224
|
+
catch (e) {
|
|
225
|
+
// Everything is swallowed here too — a trace must never break the rpc path.
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Message } from '../common/message.js';
|
|
2
|
+
export declare const TRACE_CONTEXT_PROPERTY: string;
|
|
3
|
+
export declare const TRACE_STATE_PROPERTY = "tracestate";
|
|
4
|
+
export declare function generateTraceparent(): string;
|
|
5
|
+
export declare function isValidTraceparent(value: string): boolean;
|
|
6
|
+
export declare function childTraceparent(parent: string): string | null;
|
|
7
|
+
export declare function injectTraceContext(msg: Message): string;
|
|
8
|
+
export declare function extractTraceparent(msg: Message): string | null;
|
|
9
|
+
export declare function traceContextEnabledFromEnv(): boolean;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// W3C Trace Context (traceparent) propagation on messages, for integrations such
|
|
2
|
+
// as OpenTelemetry.
|
|
3
|
+
//
|
|
4
|
+
// traceparent: 00-<trace-id 32hex>-<parent-id 16hex>-<flags 2hex>
|
|
5
|
+
//
|
|
6
|
+
// Key rules:
|
|
7
|
+
//
|
|
8
|
+
// - trace-id and parent-id must not be all zeros; flags "00" (not sampled) is
|
|
9
|
+
// legal;
|
|
10
|
+
// - the producer injects a root span only while the message **does not yet
|
|
11
|
+
// carry a traceparent** — a context propagated from upstream always wins;
|
|
12
|
+
// - the consumer side performs EXTRACT so application code can attach its own
|
|
13
|
+
// child span.
|
|
14
|
+
//
|
|
15
|
+
// Enabled via setEnableTraceContext or the ROCKETMQ_TRACE_CONTEXT_ENABLE
|
|
16
|
+
// environment variable.
|
|
17
|
+
import { randomBytes } from 'node:crypto';
|
|
18
|
+
import { MessageConst } from "../common/messageConst.js";
|
|
19
|
+
export const TRACE_CONTEXT_PROPERTY = MessageConst.PROPERTY_TRACE_PARENT; // "traceparent"
|
|
20
|
+
export const TRACE_STATE_PROPERTY = 'tracestate';
|
|
21
|
+
const TRACE_CONTEXT_ENABLE_ENV = 'ROCKETMQ_TRACE_CONTEXT_ENABLE';
|
|
22
|
+
function traceRandomHex(chars) {
|
|
23
|
+
return randomBytes(chars / 2).toString('hex');
|
|
24
|
+
}
|
|
25
|
+
// generateTraceparent mints a root traceparent: version 00, random ids, flags
|
|
26
|
+
// 01 (sampled).
|
|
27
|
+
export function generateTraceparent() {
|
|
28
|
+
return `00-${traceRandomHex(32)}-${traceRandomHex(16)}-01`;
|
|
29
|
+
}
|
|
30
|
+
function isHex(s) {
|
|
31
|
+
return /^[0-9a-fA-F]+$/.test(s);
|
|
32
|
+
}
|
|
33
|
+
// isValidTraceparent checks the W3C syntax plus the "not all zeros" rule.
|
|
34
|
+
// Uppercase hex is accepted too (values forwarded from elsewhere are passed
|
|
35
|
+
// through unchanged).
|
|
36
|
+
export function isValidTraceparent(value) {
|
|
37
|
+
const parts = String(value).trim().split('-');
|
|
38
|
+
if (parts.length !== 4)
|
|
39
|
+
return false;
|
|
40
|
+
const [version, traceID, parentID, flags] = parts;
|
|
41
|
+
if (version !== '00' && !(version.length === 2 && isHex(version)))
|
|
42
|
+
return false;
|
|
43
|
+
// Version ff is forbidden by the spec (and reserved).
|
|
44
|
+
if (version === 'ff')
|
|
45
|
+
return false;
|
|
46
|
+
if (traceID.length !== 32 || parentID.length !== 16 || flags.length !== 2)
|
|
47
|
+
return false;
|
|
48
|
+
if (!isHex(traceID) || !isHex(parentID) || !isHex(flags))
|
|
49
|
+
return false;
|
|
50
|
+
if (/^0{32}$/.test(traceID) || /^0{16}$/.test(parentID))
|
|
51
|
+
return false;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
// childTraceparent derives a child span (same trace-id, fresh parent-id) from a
|
|
55
|
+
// valid parent. Returns null when the parent is malformed.
|
|
56
|
+
export function childTraceparent(parent) {
|
|
57
|
+
if (!isValidTraceparent(parent))
|
|
58
|
+
return null;
|
|
59
|
+
const parts = String(parent).trim().split('-');
|
|
60
|
+
return `00-${parts[1].toLowerCase()}-${traceRandomHex(16)}-01`;
|
|
61
|
+
}
|
|
62
|
+
// injectTraceContext writes a root traceparent into the message unless one is
|
|
63
|
+
// already present (caller-propagated contexts win) and returns the value now on
|
|
64
|
+
// the message.
|
|
65
|
+
export function injectTraceContext(msg) {
|
|
66
|
+
const existing = msg.getProperty(TRACE_CONTEXT_PROPERTY);
|
|
67
|
+
if (existing)
|
|
68
|
+
return existing;
|
|
69
|
+
const tp = generateTraceparent();
|
|
70
|
+
msg.putProperty(TRACE_CONTEXT_PROPERTY, tp);
|
|
71
|
+
return tp;
|
|
72
|
+
}
|
|
73
|
+
// extractTraceparent reads the traceparent off a consumed message. Returns
|
|
74
|
+
// null when the producer never injected one.
|
|
75
|
+
export function extractTraceparent(msg) {
|
|
76
|
+
if (!msg)
|
|
77
|
+
return null;
|
|
78
|
+
const v = msg.getProperty(TRACE_CONTEXT_PROPERTY);
|
|
79
|
+
return v ? v : null;
|
|
80
|
+
}
|
|
81
|
+
// traceContextEnabledFromEnv reads the ROCKETMQ_TRACE_CONTEXT_ENABLE
|
|
82
|
+
// environment variable.
|
|
83
|
+
export function traceContextEnabledFromEnv() {
|
|
84
|
+
const v = (process.env[TRACE_CONTEXT_ENABLE_ENV] || '').trim().toLowerCase();
|
|
85
|
+
return v === '1' || v === 'true' || v === 'yes';
|
|
86
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The wire value is the upper-case enum name ("LOWER"/"UPPER"); getType() is lenient — only
|
|
2
|
+
// "upper" (case-insensitive) maps to UPPER, everything else (null / blank / "lower") is LOWER.
|
|
3
|
+
export const BoundaryType = {
|
|
4
|
+
LOWER: 'LOWER',
|
|
5
|
+
UPPER: 'UPPER',
|
|
6
|
+
lowercaseName(value) {
|
|
7
|
+
return value.toLowerCase();
|
|
8
|
+
},
|
|
9
|
+
get_type(name) {
|
|
10
|
+
if (typeof name === 'string' && name.toLowerCase() === BoundaryType.UPPER.toLowerCase()) {
|
|
11
|
+
return BoundaryType.UPPER;
|
|
12
|
+
}
|
|
13
|
+
return BoundaryType.LOWER;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
export default BoundaryType;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare function lz4CompressBlock(data: Buffer): Buffer;
|
|
2
|
+
export declare function lz4DecompressBlock(data: Buffer): Buffer;
|
|
3
|
+
export declare function xxh32(data: Buffer, seed?: number): number;
|
|
4
|
+
export declare function lz4CompressFrame(data: Buffer): Buffer;
|
|
5
|
+
export declare function lz4DecompressFrame(data: Buffer): Buffer;
|
|
6
|
+
export declare function zstdCompressRaw(data: Buffer): Buffer;
|
|
7
|
+
export declare function zstdDecompressFrame(data: Buffer): Buffer;
|
|
8
|
+
declare class UnsupportedCompressionError extends Error {
|
|
9
|
+
}
|
|
10
|
+
export { UnsupportedCompressionError };
|
|
11
|
+
export declare function zstdCompress(data: Buffer, level?: number): Buffer;
|
|
12
|
+
export declare function zstdDecompress(data: Buffer): Buffer;
|
|
13
|
+
export declare function compressFor(data: Buffer, compressionType: number, level?: number): Buffer;
|
|
14
|
+
export declare function decompressFor(data: Buffer, compressionType: number): Buffer;
|
|
15
|
+
export declare function compressionTypeByName(name: string | number): number;
|
|
16
|
+
declare const _default: {
|
|
17
|
+
lz4CompressBlock: typeof lz4CompressBlock;
|
|
18
|
+
lz4DecompressBlock: typeof lz4DecompressBlock;
|
|
19
|
+
zstdCompressRaw: typeof zstdCompressRaw;
|
|
20
|
+
zstdDecompressFrame: typeof zstdDecompressFrame;
|
|
21
|
+
zstdCompress: typeof zstdCompress;
|
|
22
|
+
zstdDecompress: typeof zstdDecompress;
|
|
23
|
+
compressFor: typeof compressFor;
|
|
24
|
+
decompressFor: typeof decompressFor;
|
|
25
|
+
compressionTypeByName: typeof compressionTypeByName;
|
|
26
|
+
UnsupportedCompressionError: typeof UnsupportedCompressionError;
|
|
27
|
+
};
|
|
28
|
+
export default _default;
|