@moostjs/otel 0.4.6 → 0.4.8

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.mjs CHANGED
@@ -1,8 +1,12 @@
1
- import { trace, context, SpanStatusCode } from '@opentelemetry/api';
2
- import { useAsyncEventContext, eventContextHooks, useControllerContext, getConstructor } from 'moost';
1
+ import { trace, context, metrics } from '@opentelemetry/api';
2
+ import { useAsyncEventContext as useAsyncEventContext$1, ContextInjector, useControllerContext, getConstructor as getConstructor$2, replaceContextInjector } from 'moost';
3
+ import { randomUUID } from 'crypto';
4
+ import { AsyncLocalStorage } from 'node:async_hooks';
5
+ import { BatchSpanProcessor, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
3
6
 
7
+ const spanStack = [];
4
8
  function useOtelContext() {
5
- const store = useAsyncEventContext().store('otel');
9
+ const store = useAsyncEventContext$1().store('otel');
6
10
  const getSpan = () => store.get('span');
7
11
  const getSpanContext = () => {
8
12
  const span = getSpan();
@@ -20,23 +24,42 @@ function useOtelContext() {
20
24
  }
21
25
  return {};
22
26
  };
27
+ const pushSpan = (span) => {
28
+ const activeSpan = trace.getActiveSpan();
29
+ if (activeSpan) {
30
+ const origEnd = activeSpan.end;
31
+ Object.assign(activeSpan, {
32
+ end: (t) => {
33
+ popSpan();
34
+ const i = spanStack.indexOf(activeSpan);
35
+ if (i >= 0) {
36
+ spanStack.splice(i, 1);
37
+ }
38
+ origEnd.apply(span, [t]);
39
+ },
40
+ });
41
+ spanStack.push(activeSpan);
42
+ }
43
+ trace.setSpan(context.active(), span);
44
+ };
45
+ const popSpan = () => {
46
+ const span = spanStack.pop();
47
+ if (span) {
48
+ trace.setSpan(context.active(), span);
49
+ }
50
+ };
23
51
  return {
24
52
  trace,
25
53
  withChildSpan: (name, cb, opts) => {
26
- const parentSpan = getSpan();
27
- if (parentSpan) {
28
- const tracer = trace.getTracer('moost-tracer');
29
- const span = tracer.startSpan(name, opts);
30
- return () => context.with(trace.setSpan(context.active(), span), () => cb());
31
- }
32
- else {
33
- return cb();
34
- }
54
+ const tracer = trace.getTracer('moost-tracer');
55
+ const span = tracer.startSpan(name, opts);
56
+ return () => context.with(trace.setSpan(context.active(), span), cb);
35
57
  },
36
58
  getSpan,
37
59
  getSpanContext,
38
60
  getPropagationHeaders,
39
61
  registerSpan: (span) => store.set('span', span),
62
+ pushSpan,
40
63
  };
41
64
  }
42
65
  function useTrace() {
@@ -53,56 +76,1097 @@ function useOtelPropagation() {
53
76
  };
54
77
  }
55
78
 
56
- function enableOtelForMoost() {
57
- eventContextHooks.onStartEvent((eventType) => {
58
- if (eventType === 'init') {
59
- return;
79
+ const meter = metrics.getMeter('moost-meter');
80
+ const moostMetrics = {
81
+ httpRequestCount: meter.createCounter('http.server.requests', {
82
+ description: 'The number of HTTP requests received',
83
+ }),
84
+ httpResponseCount: meter.createCounter('http.server.responses', {
85
+ description: 'The number of HTTP responses sent',
86
+ }),
87
+ httpActiveRequests: meter.createUpDownCounter('http.server.active_requests', {
88
+ description: 'The number of active HTTP requests',
89
+ }),
90
+ httpRequestSize: meter.createHistogram('http.server.request.size', {
91
+ description: 'The size of HTTP request bodies',
92
+ }),
93
+ httpResponseSize: meter.createHistogram('http.server.response.size', {
94
+ description: 'The size of HTTP response bodies',
95
+ }),
96
+ httpErrorCount: meter.createCounter('http.server.errors', {
97
+ description: 'The number of HTTP requests that resulted in an error',
98
+ }),
99
+ moostEventCount: meter.createCounter('moost.events.count', {
100
+ description: 'The number of HTTP requests received',
101
+ }),
102
+ moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
103
+ description: 'The number of active HTTP requests',
104
+ }),
105
+ moostErrorCount: meter.createCounter('moost.events.errors', {
106
+ description: 'The number of HTTP requests that resulted in an error',
107
+ }),
108
+ };
109
+
110
+ const tracer = trace.getTracer('moost-tracer');
111
+ class SpanInjector extends ContextInjector {
112
+ with(name, attributes, cb) {
113
+ const fn = typeof attributes === 'function' ? attributes : cb;
114
+ const attrs = typeof attributes === 'object' ? attributes : undefined;
115
+ if (name === 'Event:start' && attrs?.eventType) {
116
+ return this.startEvent(name, attrs.eventType, fn);
117
+ }
118
+ else if (name !== 'Event:start') {
119
+ if (this.getIgnoreSpan()) {
120
+ return fn();
121
+ }
122
+ else {
123
+ const span = tracer.startSpan(name, {
124
+ attributes: attrs,
125
+ });
126
+ return this.withSpan(span, fn, {
127
+ rootSpan: false,
128
+ endSpan: true,
129
+ });
130
+ }
131
+ }
132
+ return fn();
133
+ }
134
+ patchRsponse() {
135
+ const res = this.getResponse();
136
+ if (res) {
137
+ const originalWriteHead = res.writeHead;
138
+ Object.assign(res, {
139
+ writeHead: (arg0, arg1, arg2) => {
140
+ res._statusCode = arg0;
141
+ const headers = typeof arg2 === 'object' ? arg2 : typeof arg1 === 'object' ? arg1 : undefined;
142
+ res._contentLength = headers?.['content-length'] ? Number(headers['content-length']) : 0;
143
+ return originalWriteHead.apply(res, [arg0, arg1, arg2]);
144
+ },
145
+ });
60
146
  }
147
+ }
148
+ startEvent(name, eventType, cb) {
61
149
  const { registerSpan } = useOtelContext();
62
- let span;
150
+ let span = trace.getActiveSpan();
63
151
  if (eventType === 'HTTP') {
64
- span = trace.getSpan(context.active());
152
+ this.patchRsponse();
65
153
  }
66
154
  else {
67
- const tracer = trace.getTracer('moost-tracer');
68
155
  span = tracer.startSpan(`${eventType} Event`);
69
156
  }
70
157
  if (span) {
71
158
  registerSpan(span);
159
+ return this.withSpan(span, cb, {
160
+ rootSpan: true,
161
+ endSpan: eventType !== 'HTTP',
162
+ });
72
163
  }
73
- });
74
- eventContextHooks.onEndEvent((eventType, abortReason) => {
75
- if (eventType === 'init') {
76
- return;
164
+ return cb();
165
+ }
166
+ getEventType() {
167
+ return useAsyncEventContext$1().getCtx().event.type;
168
+ }
169
+ getIgnoreSpan() {
170
+ const { getMethodMeta, getControllerMeta } = useControllerContext();
171
+ const cMeta = getControllerMeta();
172
+ const mMeta = getMethodMeta();
173
+ return cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan;
174
+ }
175
+ getControllerHandlerMeta() {
176
+ const { getMethod, getMethodMeta, getController, getControllerMeta, getRoute } = useControllerContext();
177
+ const methodName = getMethod();
178
+ const cMeta = getControllerMeta();
179
+ const mMeta = getMethodMeta();
180
+ return {
181
+ ignoreMeter: cMeta?.otelIgnoreMeter || mMeta?.otelIgnoreMeter,
182
+ ignoreSpan: cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
183
+ attrs: {
184
+ 'moost.controller': getConstructor$2(getController()).name,
185
+ 'moost.handler': methodName,
186
+ 'moost.handler_description': mMeta?.description,
187
+ 'moost.handler_label': mMeta?.label,
188
+ 'moost.handler_id': mMeta?.id,
189
+ 'moost.ignore': cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
190
+ 'moost.route': getRoute(),
191
+ 'moost.event_type': this.getEventType(),
192
+ },
193
+ };
194
+ }
195
+ hook(name, route) {
196
+ useAsyncEventContext$1().store('otel').set('route', route);
197
+ const chm = this.getControllerHandlerMeta();
198
+ if (!chm.ignoreMeter) {
199
+ this.startEventMetrics(chm.attrs, route);
77
200
  }
78
201
  const { getSpan } = useOtelContext();
79
202
  const span = getSpan();
80
203
  if (span) {
81
- const { getMethod, getMethodMeta, getController } = useControllerContext();
82
- const methodName = getMethod();
83
- const methodMeta = getMethodMeta();
84
- span.setAttributes({
85
- 'custom.event_type': eventType,
86
- 'custom.event_description': methodMeta?.description || methodMeta?.label || methodName,
87
- 'moost.controller': getConstructor(getController()).name,
88
- 'moost.handler': methodName,
89
- });
90
- if (abortReason) {
91
- span.recordException(new Error(abortReason));
92
- span.setStatus({ code: SpanStatusCode.ERROR, message: abortReason });
93
- }
94
- if (eventType === 'HTTP') {
95
- const req = useAsyncEventContext()
96
- .store('event')
97
- .get('req');
98
- span.updateName(`${req?.method || ''} ${methodMeta?.id || methodMeta?.label || methodName}`);
204
+ span.setAttributes(chm.attrs);
205
+ if (chm.attrs['moost.event_type'] === 'HTTP') {
206
+ span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
99
207
  }
100
208
  else {
101
- span.updateName(`${methodMeta?.label || methodName}`);
209
+ span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
210
+ }
211
+ }
212
+ }
213
+ withSpan(span, cb, opts) {
214
+ let result;
215
+ let exception;
216
+ const endSpan = (error) => {
217
+ if (error) {
218
+ span.recordException(error);
219
+ }
220
+ if (opts.rootSpan) {
221
+ const chm = this.getControllerHandlerMeta();
222
+ if (!chm.ignoreMeter) {
223
+ this.endEventMetrics(chm.attrs, error);
224
+ }
225
+ }
226
+ if (opts.endSpan) {
102
227
  span.end();
103
228
  }
229
+ };
230
+ context.with(trace.setSpan(context.active(), span), () => {
231
+ try {
232
+ result = cb();
233
+ }
234
+ catch (error) {
235
+ exception = error;
236
+ endSpan(exception);
237
+ }
238
+ });
239
+ const ret = result;
240
+ if (!exception) {
241
+ if (ret && ret instanceof Promise) {
242
+ ret
243
+ .then(r => {
244
+ endSpan();
245
+ return r;
246
+ })
247
+ .catch(error => {
248
+ endSpan(error);
249
+ });
250
+ }
251
+ else {
252
+ endSpan();
253
+ }
254
+ }
255
+ return ret;
256
+ }
257
+ startEventMetrics(a, route) {
258
+ if (a['moost.event_type'] === 'HTTP') {
259
+ const req = this.getRequest();
260
+ const attrs = {
261
+ ...a,
262
+ route,
263
+ url: req?.url,
264
+ };
265
+ moostMetrics.httpRequestCount.add(1, attrs);
266
+ moostMetrics.httpActiveRequests.add(1, attrs);
104
267
  }
268
+ const attrs = {
269
+ ...a,
270
+ route,
271
+ };
272
+ moostMetrics.moostEventCount.add(1, attrs);
273
+ moostMetrics.moostActiveEvents.add(1, attrs);
274
+ }
275
+ endEventMetrics(a, error) {
276
+ const route = useAsyncEventContext$1().store('otel').get('route');
277
+ if (a['moost.event_type'] === 'HTTP') {
278
+ const req = this.getRequest();
279
+ const res = this.getResponse();
280
+ const attrs = {
281
+ ...a,
282
+ route,
283
+ url: req?.url,
284
+ };
285
+ moostMetrics.httpActiveRequests.add(-1, attrs);
286
+ if (error) {
287
+ moostMetrics.httpErrorCount.add(1, attrs);
288
+ }
289
+ moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
290
+ moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
291
+ moostMetrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
292
+ }
293
+ const attrs = {
294
+ ...a,
295
+ route,
296
+ };
297
+ moostMetrics.moostActiveEvents.add(-1, attrs);
298
+ if (error) {
299
+ moostMetrics.moostErrorCount.add(1, attrs);
300
+ }
301
+ }
302
+ getRequest() {
303
+ return useAsyncEventContext$1().store('event').get('req');
304
+ }
305
+ getResponse() {
306
+ return useAsyncEventContext$1()
307
+ .store('event')
308
+ .get('res');
309
+ }
310
+ }
311
+
312
+ function enableOtelForMoost() {
313
+ replaceContextInjector(new SpanInjector());
314
+ }
315
+
316
+ function getConstructor$1(instance) {
317
+ return isConstructor(instance) ?
318
+ instance : instance.constructor ?
319
+ instance.constructor : Object.getPrototypeOf(instance).constructor;
320
+ }
321
+ function isConstructor(v) {
322
+ return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
323
+ }
324
+
325
+ const classMetadata = new WeakMap();
326
+ const paramMetadata = new WeakMap();
327
+ const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
328
+ function getMetaObject(target, prop) {
329
+ const isParam = typeof prop !== 'undefined';
330
+ const metadata = isParam ? paramMetadata : classMetadata;
331
+ const targetKey = getConstructor$1(target);
332
+ let meta = metadata.get(targetKey);
333
+ if (!meta) {
334
+ meta = {};
335
+ metadata.set(targetKey, meta);
336
+ }
337
+ if (isParam) {
338
+ meta = (meta[prop] =
339
+ meta[prop] || {});
340
+ }
341
+ return meta;
342
+ }
343
+ const _reflect = {
344
+ getOwnMetadata(key, target, prop) {
345
+ return getMetaObject(target, prop)[key];
346
+ },
347
+ defineMetadata(key, data, target, prop) {
348
+ const meta = getMetaObject(target, prop);
349
+ meta[key] = data;
350
+ },
351
+ metadata(key, data) {
352
+ return ((target, propKey) => {
353
+ Reflect$1.defineMetadata(key, data, target, propKey);
354
+ });
355
+ },
356
+ };
357
+ if (!root.Reflect) {
358
+ root.Reflect = _reflect;
359
+ }
360
+ else {
361
+ const funcs = [
362
+ 'getOwnMetadata',
363
+ 'defineMetadata',
364
+ 'metadata',
365
+ ];
366
+ const target = root.Reflect;
367
+ for (const func of funcs) {
368
+ if (typeof target[func] !== 'function') {
369
+ Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
370
+ }
371
+ }
372
+ }
373
+ const Reflect$1 = _reflect;
374
+
375
+ const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
376
+ class Mate {
377
+ constructor(workspace, options = {}) {
378
+ this.workspace = workspace;
379
+ this.options = options;
380
+ this.logger = options.logger || console;
381
+ }
382
+ set(args, key, value, isArray) {
383
+ var _a;
384
+ let level = 'CLASS';
385
+ const newArgs = args.level === 'CLASS' ? { target: args.target }
386
+ : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
387
+ : args;
388
+ let meta = (Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {});
389
+ if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
390
+ meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
391
+ }
392
+ if (newArgs.propKey && this.options.readType && !meta.type) {
393
+ meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
394
+ }
395
+ const { index } = newArgs;
396
+ const cb = typeof key === 'function' ? key : undefined;
397
+ let data = meta;
398
+ if (!data.params) {
399
+ data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
400
+ }
401
+ if (typeof index === 'number') {
402
+ level = 'PARAM';
403
+ data.params = data.params || [];
404
+ data.params[index] = data.params[index] || {
405
+ type: undefined,
406
+ };
407
+ if (cb) {
408
+ data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
409
+ }
410
+ else {
411
+ data = data.params[index];
412
+ }
413
+ }
414
+ else if (!index && !args.descriptor && args.propKey && this.options.collectPropKeys && args.level !== 'CLASS') {
415
+ this.set({ ...args, level: 'CLASS' }, (meta) => {
416
+ if (!meta.properties) {
417
+ meta.properties = [args.propKey];
418
+ }
419
+ else if (!meta.properties.includes(args.propKey)) {
420
+ meta.properties.push(args.propKey);
421
+ }
422
+ return meta;
423
+ });
424
+ }
425
+ level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
426
+ if (typeof key !== 'function') {
427
+ if (isArray) {
428
+ const newArray = (data[key] || []);
429
+ if (!Array.isArray(newArray)) {
430
+ this.logger.error('Mate.add (isArray=true) called for non-array metadata');
431
+ }
432
+ newArray.unshift(value);
433
+ data[key] = newArray;
434
+ }
435
+ else {
436
+ data[key] = value;
437
+ }
438
+ }
439
+ else if (cb && typeof index !== 'number') {
440
+ meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
441
+ }
442
+ Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
443
+ }
444
+ read(target, propKey) {
445
+ var _a;
446
+ const isConstr = isConstructor(target);
447
+ const constructor = isConstr ? target : getConstructor$1(target);
448
+ const proto = constructor.prototype;
449
+ let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
450
+ if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
451
+ const parent = Object.getPrototypeOf(constructor);
452
+ if (typeof parent === 'function' &&
453
+ parent !== fnProto &&
454
+ parent !== constructor) {
455
+ ownMeta.params = (_a = this.read(parent)) === null || _a === void 0 ? void 0 : _a.params;
456
+ }
457
+ }
458
+ if (this.options.inherit) {
459
+ const inheritFn = typeof this.options.inherit === 'function'
460
+ ? this.options.inherit
461
+ : undefined;
462
+ let shouldInherit = this.options.inherit;
463
+ if (inheritFn) {
464
+ if (typeof propKey === 'string') {
465
+ const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
466
+ shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
467
+ }
468
+ else {
469
+ shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
470
+ }
471
+ }
472
+ if (shouldInherit) {
473
+ const parent = Object.getPrototypeOf(constructor);
474
+ if (typeof parent === 'function' &&
475
+ parent !== fnProto &&
476
+ parent !== constructor) {
477
+ const inheritedMeta = (this.read(parent, propKey) ||
478
+ {});
479
+ const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
480
+ ownMeta = { ...inheritedMeta, ...ownMeta };
481
+ if (typeof propKey === 'string' &&
482
+ ownParams &&
483
+ (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
484
+ for (let i = 0; i < ownParams.length; i++) {
485
+ if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !==
486
+ 'undefined') {
487
+ const ownParam = ownParams[i];
488
+ if (ownMeta.params &&
489
+ inheritFn &&
490
+ inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string'
491
+ ? propKey
492
+ : undefined)) {
493
+ ownMeta.params[i] = {
494
+ ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
495
+ ...ownParams[i],
496
+ };
497
+ }
498
+ }
499
+ }
500
+ }
501
+ }
502
+ }
503
+ }
504
+ return ownMeta;
505
+ }
506
+ apply(...decorators) {
507
+ return ((target, propKey, descriptor) => {
508
+ for (const d of decorators) {
509
+ d(target, propKey, descriptor);
510
+ }
511
+ });
512
+ }
513
+ decorate(key, value, isArray, level) {
514
+ return ((target, propKey, descriptor) => {
515
+ const args = {
516
+ target,
517
+ propKey,
518
+ descriptor: typeof descriptor === 'number' ? undefined : descriptor,
519
+ index: typeof descriptor === 'number' ? descriptor : undefined,
520
+ level,
521
+ };
522
+ this.set(args, key, value, isArray);
523
+ });
524
+ }
525
+ decorateConditional(ccb) {
526
+ return ((target, propKey, descriptor) => {
527
+ const hasIndex = typeof descriptor === 'number';
528
+ const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
529
+ const d = ccb(decoratorLevel);
530
+ if (d) {
531
+ d(target, propKey, descriptor);
532
+ }
533
+ });
534
+ }
535
+ decorateClass(key, value, isArray) {
536
+ return this.decorate(key, value, isArray, 'CLASS');
537
+ }
538
+ }
539
+ const fnProto = Object.getPrototypeOf(Function);
540
+
541
+ const defaultLevels = [
542
+ 'fatal',
543
+ 'error',
544
+ 'warn',
545
+ 'log',
546
+ 'info',
547
+ 'debug',
548
+ 'trace',
549
+ ];
550
+ const defaultMappedLevels = new Map();
551
+ defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
552
+
553
+ function attachHook(target, opts, name) {
554
+ Object.defineProperty(target, name || 'value', {
555
+ get: opts.get,
556
+ set: opts.set,
105
557
  });
558
+ return target;
559
+ }
560
+
561
+ const asyncStorage = new AsyncLocalStorage();
562
+ function useAsyncEventContext(expectedTypes) {
563
+ let cc = asyncStorage.getStore();
564
+ if (!cc) {
565
+ throw new Error('Event context does not exist at this point.');
566
+ }
567
+ if (expectedTypes || typeof expectedTypes === 'string') {
568
+ const type = cc.event.type;
569
+ const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
570
+ if (!types.includes(type)) {
571
+ if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) {
572
+ cc = cc.parentCtx;
573
+ }
574
+ else {
575
+ throw new Error(`Event context type mismatch: expected ${types
576
+ .map(t => `"${t}"`)
577
+ .join(', ')}, received "${type}"`);
578
+ }
579
+ }
580
+ }
581
+ return _getCtxHelpers(cc);
582
+ }
583
+ function _getCtxHelpers(cc) {
584
+ function store(key) {
585
+ const obj = {
586
+ value: null,
587
+ hook,
588
+ init,
589
+ set: setNested,
590
+ get: getNested,
591
+ has: hasNested,
592
+ del: delNested,
593
+ entries,
594
+ clear,
595
+ };
596
+ attachHook(obj, {
597
+ set: v => {
598
+ set(key, v);
599
+ },
600
+ get: () => get(key),
601
+ });
602
+ function init(key2, getter) {
603
+ if (hasNested(key2)) {
604
+ return getNested(key2);
605
+ }
606
+ return setNested(key2, getter());
607
+ }
608
+ function hook(key2) {
609
+ const obj = {
610
+ value: null,
611
+ isDefined: null,
612
+ };
613
+ attachHook(obj, {
614
+ set: v => setNested(key2, v),
615
+ get: () => getNested(key2),
616
+ });
617
+ attachHook(obj, {
618
+ get: () => hasNested(key2),
619
+ }, 'isDefined');
620
+ return obj;
621
+ }
622
+ function setNested(key2, v) {
623
+ if (obj.value === undefined) {
624
+ obj.value = {};
625
+ }
626
+ obj.value[key2] = v;
627
+ return v;
628
+ }
629
+ function delNested(key2) {
630
+ setNested(key2, undefined);
631
+ }
632
+ function getNested(key2) {
633
+ return (obj.value || {})[key2];
634
+ }
635
+ function hasNested(key2) {
636
+ return (obj.value || {})[key2] !== undefined;
637
+ }
638
+ function entries() {
639
+ return Object.entries(obj.value || {});
640
+ }
641
+ function clear() {
642
+ obj.value = {};
643
+ }
644
+ return obj;
645
+ }
646
+ function getCtx() {
647
+ return cc;
648
+ }
649
+ function get(key) {
650
+ return getCtx()[key];
651
+ }
652
+ function set(key, v) {
653
+ getCtx()[key] = v;
654
+ }
655
+ const hasParentCtx = () => !!cc.parentCtx;
656
+ return {
657
+ getCtx,
658
+ store,
659
+ getStore: get,
660
+ setStore: set,
661
+ setParentCtx: (parentCtx) => {
662
+ cc.parentCtx = parentCtx;
663
+ },
664
+ hasParentCtx,
665
+ getParentCtx: () => {
666
+ if (!hasParentCtx()) {
667
+ throw new Error('Parent context is not available');
668
+ }
669
+ return _getCtxHelpers(cc.parentCtx);
670
+ },
671
+ };
672
+ }
673
+
674
+ function useEventId() {
675
+ const { store } = useAsyncEventContext();
676
+ const { init } = store('event');
677
+ const getId = () => init('id', () => randomUUID());
678
+ return { getId };
679
+ }
680
+
681
+ function getConstructor(instance) {
682
+ return Object.getPrototypeOf(instance).constructor;
683
+ }
684
+
685
+ const globalRegistry = {};
686
+ const UNDEFINED = Symbol('undefined');
687
+ class Infact {
688
+ constructor(options) {
689
+ this.options = options;
690
+ this.registry = {};
691
+ this.instanceRegistries = new WeakMap();
692
+ this.scopes = {};
693
+ this._silent = false;
694
+ this.logger = options.logger || console;
695
+ }
696
+ setLogger(logger) {
697
+ this.logger = logger;
698
+ }
699
+ silent(value = 'logs') {
700
+ this._silent = value;
701
+ }
702
+ registerScope(scopeId) {
703
+ if (!this.scopes[scopeId]) {
704
+ this.scopes[scopeId] = {};
705
+ }
706
+ }
707
+ unregisterScope(scopeId) {
708
+ delete this.scopes[scopeId];
709
+ }
710
+ getForInstance(instance, classConstructor, opts) {
711
+ const registries = this.getInstanceRegistries(instance);
712
+ return this.get(classConstructor, {
713
+ ...opts,
714
+ provide: registries.provide || {},
715
+ replace: registries.replace,
716
+ customData: registries.customData,
717
+ });
718
+ }
719
+ async get(classConstructor, opts, optional = false) {
720
+ const result = await this._get(classConstructor, opts, optional);
721
+ if (result) {
722
+ const { instance, mergedProvide, replace } = result;
723
+ if (this.options.storeProvideRegByInstance) {
724
+ this.setInstanceRegistries(instance, mergedProvide, replace, opts === null || opts === void 0 ? void 0 : opts.customData);
725
+ }
726
+ return instance;
727
+ }
728
+ return undefined;
729
+ }
730
+ setInstanceRegistries(instance, provide, replace, customData) {
731
+ this.instanceRegistries.set(instance, { provide, replace, customData });
732
+ }
733
+ getInstanceRegistries(instance) {
734
+ return this.instanceRegistries.get(instance) || {};
735
+ }
736
+ async _get(classConstructor, opts, optional) {
737
+ const hierarchy = (opts === null || opts === void 0 ? void 0 : opts.hierarchy) || [];
738
+ const provide = opts === null || opts === void 0 ? void 0 : opts.provide;
739
+ const replace = opts === null || opts === void 0 ? void 0 : opts.replace;
740
+ const syncContextFn = opts === null || opts === void 0 ? void 0 : opts.syncContextFn;
741
+ hierarchy.push(classConstructor.name);
742
+ let classMeta;
743
+ let instanceKey = Symbol.for(classConstructor);
744
+ if (replace && replace[instanceKey]) {
745
+ classConstructor = replace === null || replace === void 0 ? void 0 : replace[instanceKey];
746
+ instanceKey = Symbol.for(classConstructor);
747
+ }
748
+ try {
749
+ classMeta = this.options.describeClass(classConstructor);
750
+ }
751
+ catch (e) {
752
+ throw this.panicOwnError(`Could not instantiate "${classConstructor.name}". ` +
753
+ `An error occored on "describeClass" function.\n${e.message}`, hierarchy);
754
+ }
755
+ if (!classMeta || !classMeta.injectable) {
756
+ if (provide && provide[instanceKey]) {
757
+ syncContextFn && syncContextFn(classMeta);
758
+ return {
759
+ instance: await getProvidedValue(provide[instanceKey]),
760
+ mergedProvide: provide,
761
+ replace,
762
+ };
763
+ }
764
+ if (!optional) {
765
+ throw this.panicOwnError(`Could not instantiate Injectable "${classConstructor.name}". ` +
766
+ 'Please check if the class is injectable or if you properly typed arguments.', hierarchy);
767
+ }
768
+ else {
769
+ return undefined;
770
+ }
771
+ }
772
+ if (classMeta.scopeId && classMeta.global) {
773
+ throw this.panicOwnError(`Could not instantiate scoped Injectable "${classConstructor.name}" for scope "${classMeta.scopeId}". ` +
774
+ 'The scoped Injectable is not supported for Global scope.', hierarchy);
775
+ }
776
+ if (classMeta.scopeId && !this.scopes[classMeta.scopeId]) {
777
+ throw this.panicOwnError(`Could not instantiate scoped Injectable "${classConstructor.name}" for scope "${classMeta.scopeId}". ` +
778
+ 'The requested scope isn\'t registered.', hierarchy);
779
+ }
780
+ const scope = classMeta.scopeId
781
+ ? this.scopes[classMeta.scopeId]
782
+ : {};
783
+ const mergedProvide = {
784
+ ...(provide || {}),
785
+ ...(classMeta.provide || {}),
786
+ };
787
+ if (mergedProvide[instanceKey]) {
788
+ syncContextFn && syncContextFn(classMeta);
789
+ return {
790
+ instance: await getProvidedValue(mergedProvide[instanceKey]),
791
+ mergedProvide,
792
+ replace,
793
+ };
794
+ }
795
+ if (!this.registry[instanceKey] &&
796
+ !globalRegistry[instanceKey] &&
797
+ !scope[instanceKey]) {
798
+ const registry = classMeta.scopeId
799
+ ? scope
800
+ : classMeta.global
801
+ ? globalRegistry
802
+ : this.registry;
803
+ const params = classMeta.constructorParams || [];
804
+ const isCircular = !!params.find((p) => !!p.circular);
805
+ if (isCircular) {
806
+ registry[instanceKey] = Object.create(classConstructor.prototype);
807
+ }
808
+ const resolvedParams = [];
809
+ for (let i = 0; i < params.length; i++) {
810
+ const param = params[i];
811
+ if (param.inject) {
812
+ if (mergedProvide && mergedProvide[param.inject]) {
813
+ resolvedParams[i] = getProvidedValue(mergedProvide[param.inject]);
814
+ }
815
+ else if (param.nullable || param.optional) {
816
+ resolvedParams[i] = UNDEFINED;
817
+ }
818
+ else {
819
+ throw this.panicOwnError(`Could not inject ${JSON.stringify(param.inject)} to "${classConstructor.name}" to argument ${param.label
820
+ ? `labeled as "${param.label}"`
821
+ : `with index ${i}`}`, hierarchy);
822
+ }
823
+ }
824
+ else if (this.options.resolveParam) {
825
+ resolvedParams[i] = this.options.resolveParam({
826
+ classMeta,
827
+ classConstructor,
828
+ index: i,
829
+ paramMeta: param,
830
+ customData: opts === null || opts === void 0 ? void 0 : opts.customData,
831
+ });
832
+ }
833
+ }
834
+ for (let i = 0; i < resolvedParams.length; i++) {
835
+ const rp = resolvedParams[i];
836
+ if (rp &&
837
+ rp !== UNDEFINED &&
838
+ typeof rp.then === 'function') {
839
+ try {
840
+ syncContextFn && syncContextFn(classMeta);
841
+ resolvedParams[i] = await rp;
842
+ }
843
+ catch (e) {
844
+ const param = params[i];
845
+ throw this.panic(e, `Could not inject "${param.type.name}" to "${classConstructor.name}" ` +
846
+ `constructor at index ${i}${param.label ? ` (${param.label})` : ''}. An exception occured.`, hierarchy);
847
+ }
848
+ }
849
+ }
850
+ for (let i = 0; i < params.length; i++) {
851
+ const param = params[i];
852
+ if (typeof resolvedParams[i] === 'undefined') {
853
+ if (param.type === undefined && !param.circular) {
854
+ if (this._silent === false) {
855
+ this.logger.warn(`${classConstructor.name}.constructor() expects argument ${param.label
856
+ ? `labeled as "${param.label}"`
857
+ : `#${i}`} that is undefined. This might happen when Circular Dependency occurs. To handle Circular Dependencies please specify circular meta for param.`);
858
+ }
859
+ }
860
+ else if (param.type === undefined && param.circular) {
861
+ param.type = param.circular();
862
+ }
863
+ if (typeof param.type === 'function') {
864
+ if ([String, Number, Date, Array].includes(param.type)) {
865
+ if (!param.nullable && !param.optional) {
866
+ throw this.panicOwnError(`Could not inject "${param.type
867
+ .name}" to "${classConstructor.name}" ` +
868
+ `constructor at index ${i}${param.label
869
+ ? ` (${param.label})`
870
+ : ''}. The param was not resolved to a value.`, hierarchy);
871
+ }
872
+ }
873
+ resolvedParams[i] = this.get(param.type, {
874
+ provide: mergedProvide,
875
+ replace,
876
+ hierarchy,
877
+ syncContextFn,
878
+ customData: opts === null || opts === void 0 ? void 0 : opts.customData,
879
+ }, param.optional || param.nullable);
880
+ }
881
+ }
882
+ if (resolvedParams[i] === UNDEFINED) {
883
+ resolvedParams[i] = undefined;
884
+ }
885
+ }
886
+ for (let i = 0; i < resolvedParams.length; i++) {
887
+ const rp = resolvedParams[i];
888
+ if (rp && typeof rp.then === 'function') {
889
+ try {
890
+ syncContextFn && syncContextFn(classMeta);
891
+ resolvedParams[i] = await rp;
892
+ }
893
+ catch (e) {
894
+ const param = params[i];
895
+ throw this.panic(e, `Could not inject "${param.type.name}" to "${classConstructor.name}" ` +
896
+ `constructor at index ${i}${param.label ? ` (${param.label})` : ''}. An exception occured.`, hierarchy);
897
+ }
898
+ }
899
+ }
900
+ const instance = new classConstructor(...resolvedParams);
901
+ if (isCircular) {
902
+ Object.assign(registry[instanceKey], instance);
903
+ }
904
+ else {
905
+ registry[instanceKey] = instance;
906
+ }
907
+ if (this.options.describeProp &&
908
+ this.options.resolveProp &&
909
+ classMeta.properties &&
910
+ classMeta.properties.length) {
911
+ const resolvedProps = {};
912
+ for (const prop of classMeta.properties) {
913
+ const initialValue = instance[prop];
914
+ let propMeta;
915
+ try {
916
+ propMeta = this.options.describeProp(classConstructor, prop);
917
+ }
918
+ catch (e) {
919
+ throw this.panic(e, `Could not process prop "${prop}" of "${classConstructor.name}". ` +
920
+ `An error occored on "describeProp" function.\n${e.message}`, hierarchy);
921
+ }
922
+ if (propMeta) {
923
+ try {
924
+ resolvedProps[prop] = this.options.resolveProp({
925
+ classMeta,
926
+ classConstructor,
927
+ initialValue,
928
+ key: prop,
929
+ instance,
930
+ propMeta,
931
+ customData: opts === null || opts === void 0 ? void 0 : opts.customData,
932
+ });
933
+ }
934
+ catch (e) {
935
+ throw this.panic(e, `Could not inject prop "${prop}" to "${classConstructor.name}". ` +
936
+ 'An exception occured: ' +
937
+ e.message, hierarchy);
938
+ }
939
+ }
940
+ }
941
+ for (const [prop, value] of Object.entries(resolvedProps)) {
942
+ try {
943
+ syncContextFn && syncContextFn(classMeta);
944
+ resolvedProps[prop] = value
945
+ ? await value
946
+ : value;
947
+ }
948
+ catch (e) {
949
+ throw this.panic(e, `Could not inject prop "${prop}" to "${classConstructor.name}". ` +
950
+ 'An exception occured: ' +
951
+ e.message, hierarchy);
952
+ }
953
+ }
954
+ Object.assign(instance, resolvedProps);
955
+ }
956
+ if (this._silent === false) {
957
+ this.logger.info(`Class "${'' +
958
+ classConstructor.name +
959
+ '' +
960
+ ''}" instantiated with: ${''}[${resolvedParams
961
+ .map((p) => {
962
+ switch (typeof p) {
963
+ case 'number':
964
+ case 'boolean':
965
+ return p;
966
+ case 'string':
967
+ return `"${''}...${''}"`;
968
+ case 'object':
969
+ if (getConstructor(p))
970
+ return getConstructor(p).name;
971
+ return '{}';
972
+ default:
973
+ return '*';
974
+ }
975
+ })
976
+ .join(', ')}]`);
977
+ }
978
+ }
979
+ hierarchy.pop();
980
+ syncContextFn && syncContextFn(classMeta);
981
+ return {
982
+ instance: await (scope[instanceKey] ||
983
+ this.registry[instanceKey] ||
984
+ globalRegistry[instanceKey]),
985
+ mergedProvide,
986
+ replace,
987
+ };
988
+ }
989
+ panic(origError, text, hierarchy) {
990
+ if (this._silent === true) ;
991
+ else {
992
+ this.logger.error(text +
993
+ (hierarchy
994
+ ? '\nHierarchy:\n' + hierarchy.join(' -> ')
995
+ : ''));
996
+ }
997
+ return origError;
998
+ }
999
+ panicOwnError(text, hierarchy) {
1000
+ const e = new Error(text + (hierarchy ? '\nHierarchy:\n' + hierarchy.join(' -> ') : ''));
1001
+ if (this._silent === true) {
1002
+ return e;
1003
+ }
1004
+ else {
1005
+ this.logger.error(e);
1006
+ return e;
1007
+ }
1008
+ }
1009
+ }
1010
+ function getProvidedValue(meta) {
1011
+ if (!meta.resolved) {
1012
+ meta.resolved = true;
1013
+ meta.value = meta.fn();
1014
+ }
1015
+ return meta.value;
1016
+ }
1017
+
1018
+ async function runPipes(pipes, initialValue, metas, level) {
1019
+ let v = initialValue;
1020
+ for (const pipe of pipes) {
1021
+ v = await pipe.handler(v, metas, level);
1022
+ }
1023
+ return v;
1024
+ }
1025
+
1026
+ const METADATA_WORKSPACE = 'moost';
1027
+ const moostMate = new Mate(METADATA_WORKSPACE, {
1028
+ readType: true,
1029
+ readReturnType: true,
1030
+ collectPropKeys: true,
1031
+ inherit(classMeta, targetMeta, level) {
1032
+ if (level === 'CLASS') {
1033
+ return !!classMeta?.inherit;
1034
+ }
1035
+ if (level === 'PROP') {
1036
+ return !!targetMeta?.inherit || !!(classMeta?.inherit && !targetMeta);
1037
+ }
1038
+ return !!targetMeta?.inherit;
1039
+ },
1040
+ });
1041
+ function getMoostMate() {
1042
+ return moostMate;
1043
+ }
1044
+
1045
+ getNewMoostInfact();
1046
+ function getNewMoostInfact() {
1047
+ return new Infact({
1048
+ describeClass(classConstructor) {
1049
+ const meta = getMoostMate().read(classConstructor);
1050
+ return {
1051
+ injectable: !!meta?.injectable,
1052
+ global: false,
1053
+ constructorParams: meta?.params || [],
1054
+ provide: meta?.provide,
1055
+ properties: meta?.properties || [],
1056
+ scopeId: meta?.injectable === 'FOR_EVENT' ? useEventId().getId() : undefined,
1057
+ };
1058
+ },
1059
+ resolveParam({ paramMeta, classMeta, customData, classConstructor, index }) {
1060
+ if (paramMeta && customData?.pipes) {
1061
+ return runPipes(customData.pipes, undefined, {
1062
+ paramMeta,
1063
+ type: classConstructor,
1064
+ key: 'constructor',
1065
+ classMeta: classMeta,
1066
+ index,
1067
+ targetMeta: paramMeta,
1068
+ }, 'PARAM');
1069
+ }
1070
+ },
1071
+ describeProp(classConstructor, key) {
1072
+ const meta = getMoostMate().read(classConstructor, key);
1073
+ return meta;
1074
+ },
1075
+ resolveProp({ instance, key, initialValue, propMeta, classMeta, customData, classConstructor, }) {
1076
+ if (propMeta && customData?.pipes) {
1077
+ return runPipes(customData.pipes, initialValue, {
1078
+ instance,
1079
+ type: classConstructor,
1080
+ key,
1081
+ propMeta,
1082
+ targetMeta: propMeta,
1083
+ classMeta: classMeta,
1084
+ }, 'PROP');
1085
+ }
1086
+ },
1087
+ storeProvideRegByInstance: true,
1088
+ });
1089
+ }
1090
+
1091
+ getMoostMate().decorate(meta => {
1092
+ if (!meta.injectable) {
1093
+ meta.injectable = true;
1094
+ }
1095
+ return meta;
1096
+ });
1097
+
1098
+ var TInterceptorPriority;
1099
+ (function (TInterceptorPriority) {
1100
+ TInterceptorPriority[TInterceptorPriority["BEFORE_ALL"] = 0] = "BEFORE_ALL";
1101
+ TInterceptorPriority[TInterceptorPriority["BEFORE_GUARD"] = 1] = "BEFORE_GUARD";
1102
+ TInterceptorPriority[TInterceptorPriority["GUARD"] = 2] = "GUARD";
1103
+ TInterceptorPriority[TInterceptorPriority["AFTER_GUARD"] = 3] = "AFTER_GUARD";
1104
+ TInterceptorPriority[TInterceptorPriority["INTERCEPTOR"] = 4] = "INTERCEPTOR";
1105
+ TInterceptorPriority[TInterceptorPriority["CATCH_ERROR"] = 5] = "CATCH_ERROR";
1106
+ TInterceptorPriority[TInterceptorPriority["AFTER_ALL"] = 6] = "AFTER_ALL";
1107
+ })(TInterceptorPriority || (TInterceptorPriority = {}));
1108
+
1109
+ var TPipePriority;
1110
+ (function (TPipePriority) {
1111
+ TPipePriority[TPipePriority["BEFORE_RESOLVE"] = 0] = "BEFORE_RESOLVE";
1112
+ TPipePriority[TPipePriority["RESOLVE"] = 1] = "RESOLVE";
1113
+ TPipePriority[TPipePriority["AFTER_RESOLVE"] = 2] = "AFTER_RESOLVE";
1114
+ TPipePriority[TPipePriority["BEFORE_TRANSFORM"] = 3] = "BEFORE_TRANSFORM";
1115
+ TPipePriority[TPipePriority["TRANSFORM"] = 4] = "TRANSFORM";
1116
+ TPipePriority[TPipePriority["AFTER_TRANSFORM"] = 5] = "AFTER_TRANSFORM";
1117
+ TPipePriority[TPipePriority["BEFORE_VALIDATE"] = 6] = "BEFORE_VALIDATE";
1118
+ TPipePriority[TPipePriority["VALIDATE"] = 7] = "VALIDATE";
1119
+ TPipePriority[TPipePriority["AFTER_VALIDATE"] = 8] = "AFTER_VALIDATE";
1120
+ })(TPipePriority || (TPipePriority = {}));
1121
+
1122
+ function definePipeFn(fn, priority = TPipePriority.TRANSFORM) {
1123
+ fn.priority = priority;
1124
+ return fn;
1125
+ }
1126
+
1127
+ const resolvePipe = definePipeFn((_value, metas, level) => {
1128
+ const resolver = metas.targetMeta?.resolver;
1129
+ if (resolver) {
1130
+ return resolver(metas, level);
1131
+ }
1132
+ return undefined;
1133
+ }, TPipePriority.RESOLVE);
1134
+
1135
+ [
1136
+ {
1137
+ handler: resolvePipe,
1138
+ priority: TPipePriority.RESOLVE,
1139
+ },
1140
+ ];
1141
+
1142
+ function getOtelMate() {
1143
+ return getMoostMate();
1144
+ }
1145
+
1146
+ const mate = getOtelMate();
1147
+ const OtelIgnoreSpan = () => mate.decorate('otelIgnoreSpan', true);
1148
+ const OtelIgnoreMeter = () => mate.decorate('otelIgnoreMeter', true);
1149
+
1150
+ function shouldSpanBeIgnored(span) {
1151
+ return span.attributes['moost.ignore'] === true;
1152
+ }
1153
+
1154
+ class MoostBatchSpanProcessor extends BatchSpanProcessor {
1155
+ onEnd(span) {
1156
+ if (shouldSpanBeIgnored(span)) {
1157
+ return;
1158
+ }
1159
+ super.onEnd(span);
1160
+ }
1161
+ }
1162
+
1163
+ class MoostSimpleSpanProcessor extends SimpleSpanProcessor {
1164
+ onEnd(span) {
1165
+ if (shouldSpanBeIgnored(span)) {
1166
+ return;
1167
+ }
1168
+ super.onEnd(span);
1169
+ }
106
1170
  }
107
1171
 
108
- export { enableOtelForMoost, useOtelContext, useOtelPropagation, useSpan, useTrace };
1172
+ export { MoostBatchSpanProcessor, MoostSimpleSpanProcessor, OtelIgnoreMeter, OtelIgnoreSpan, SpanInjector, enableOtelForMoost, getOtelMate, shouldSpanBeIgnored, useOtelContext, useOtelPropagation, useSpan, useTrace };