@moostjs/otel 0.4.7 → 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.cjs CHANGED
@@ -6,6 +6,7 @@ var crypto = require('crypto');
6
6
  var node_async_hooks = require('node:async_hooks');
7
7
  var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
8
8
 
9
+ const spanStack = [];
9
10
  function useOtelContext() {
10
11
  const store = moost.useAsyncEventContext().store('otel');
11
12
  const getSpan = () => store.get('span');
@@ -25,23 +26,42 @@ function useOtelContext() {
25
26
  }
26
27
  return {};
27
28
  };
29
+ const pushSpan = (span) => {
30
+ const activeSpan = api.trace.getActiveSpan();
31
+ if (activeSpan) {
32
+ const origEnd = activeSpan.end;
33
+ Object.assign(activeSpan, {
34
+ end: (t) => {
35
+ popSpan();
36
+ const i = spanStack.indexOf(activeSpan);
37
+ if (i >= 0) {
38
+ spanStack.splice(i, 1);
39
+ }
40
+ origEnd.apply(span, [t]);
41
+ },
42
+ });
43
+ spanStack.push(activeSpan);
44
+ }
45
+ api.trace.setSpan(api.context.active(), span);
46
+ };
47
+ const popSpan = () => {
48
+ const span = spanStack.pop();
49
+ if (span) {
50
+ api.trace.setSpan(api.context.active(), span);
51
+ }
52
+ };
28
53
  return {
29
54
  trace: api.trace,
30
55
  withChildSpan: (name, cb, opts) => {
31
- const parentSpan = getSpan();
32
- if (parentSpan) {
33
- const tracer = api.trace.getTracer('moost-tracer');
34
- const span = tracer.startSpan(name, opts);
35
- return () => api.context.with(api.trace.setSpan(api.context.active(), span), () => cb());
36
- }
37
- else {
38
- return cb();
39
- }
56
+ const tracer = api.trace.getTracer('moost-tracer');
57
+ const span = tracer.startSpan(name, opts);
58
+ return () => api.context.with(api.trace.setSpan(api.context.active(), span), cb);
40
59
  },
41
60
  getSpan,
42
61
  getSpanContext,
43
62
  getPropagationHeaders,
44
63
  registerSpan: (span) => store.set('span', span),
64
+ pushSpan,
45
65
  };
46
66
  }
47
67
  function useTrace() {
@@ -58,60 +78,468 @@ function useOtelPropagation() {
58
78
  };
59
79
  }
60
80
 
61
- function enableOtelForMoost() {
62
- moost.eventContextHooks.onStartEvent((eventType) => {
63
- if (eventType === 'init') {
64
- return;
81
+ const meter = api.metrics.getMeter('moost-meter');
82
+ const moostMetrics = {
83
+ httpRequestCount: meter.createCounter('http.server.requests', {
84
+ description: 'The number of HTTP requests received',
85
+ }),
86
+ httpResponseCount: meter.createCounter('http.server.responses', {
87
+ description: 'The number of HTTP responses sent',
88
+ }),
89
+ httpActiveRequests: meter.createUpDownCounter('http.server.active_requests', {
90
+ description: 'The number of active HTTP requests',
91
+ }),
92
+ httpRequestSize: meter.createHistogram('http.server.request.size', {
93
+ description: 'The size of HTTP request bodies',
94
+ }),
95
+ httpResponseSize: meter.createHistogram('http.server.response.size', {
96
+ description: 'The size of HTTP response bodies',
97
+ }),
98
+ httpErrorCount: meter.createCounter('http.server.errors', {
99
+ description: 'The number of HTTP requests that resulted in an error',
100
+ }),
101
+ moostEventCount: meter.createCounter('moost.events.count', {
102
+ description: 'The number of HTTP requests received',
103
+ }),
104
+ moostActiveEvents: meter.createUpDownCounter('moost.events.active', {
105
+ description: 'The number of active HTTP requests',
106
+ }),
107
+ moostErrorCount: meter.createCounter('moost.events.errors', {
108
+ description: 'The number of HTTP requests that resulted in an error',
109
+ }),
110
+ };
111
+
112
+ const tracer = api.trace.getTracer('moost-tracer');
113
+ class SpanInjector extends moost.ContextInjector {
114
+ with(name, attributes, cb) {
115
+ const fn = typeof attributes === 'function' ? attributes : cb;
116
+ const attrs = typeof attributes === 'object' ? attributes : undefined;
117
+ if (name === 'Event:start' && attrs?.eventType) {
118
+ return this.startEvent(name, attrs.eventType, fn);
65
119
  }
120
+ else if (name !== 'Event:start') {
121
+ if (this.getIgnoreSpan()) {
122
+ return fn();
123
+ }
124
+ else {
125
+ const span = tracer.startSpan(name, {
126
+ attributes: attrs,
127
+ });
128
+ return this.withSpan(span, fn, {
129
+ rootSpan: false,
130
+ endSpan: true,
131
+ });
132
+ }
133
+ }
134
+ return fn();
135
+ }
136
+ patchRsponse() {
137
+ const res = this.getResponse();
138
+ if (res) {
139
+ const originalWriteHead = res.writeHead;
140
+ Object.assign(res, {
141
+ writeHead: (arg0, arg1, arg2) => {
142
+ res._statusCode = arg0;
143
+ const headers = typeof arg2 === 'object' ? arg2 : typeof arg1 === 'object' ? arg1 : undefined;
144
+ res._contentLength = headers?.['content-length'] ? Number(headers['content-length']) : 0;
145
+ return originalWriteHead.apply(res, [arg0, arg1, arg2]);
146
+ },
147
+ });
148
+ }
149
+ }
150
+ startEvent(name, eventType, cb) {
66
151
  const { registerSpan } = useOtelContext();
67
- let span;
152
+ let span = api.trace.getActiveSpan();
68
153
  if (eventType === 'HTTP') {
69
- span = api.trace.getSpan(api.context.active());
154
+ this.patchRsponse();
70
155
  }
71
156
  else {
72
- const tracer = api.trace.getTracer('moost-tracer');
73
157
  span = tracer.startSpan(`${eventType} Event`);
74
158
  }
75
159
  if (span) {
76
160
  registerSpan(span);
161
+ return this.withSpan(span, cb, {
162
+ rootSpan: true,
163
+ endSpan: eventType !== 'HTTP',
164
+ });
77
165
  }
78
- });
79
- moost.eventContextHooks.onEndEvent((eventType, abortReason) => {
80
- if (eventType === 'init') {
81
- return;
166
+ return cb();
167
+ }
168
+ getEventType() {
169
+ return moost.useAsyncEventContext().getCtx().event.type;
170
+ }
171
+ getIgnoreSpan() {
172
+ const { getMethodMeta, getControllerMeta } = moost.useControllerContext();
173
+ const cMeta = getControllerMeta();
174
+ const mMeta = getMethodMeta();
175
+ return cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan;
176
+ }
177
+ getControllerHandlerMeta() {
178
+ const { getMethod, getMethodMeta, getController, getControllerMeta, getRoute } = moost.useControllerContext();
179
+ const methodName = getMethod();
180
+ const cMeta = getControllerMeta();
181
+ const mMeta = getMethodMeta();
182
+ return {
183
+ ignoreMeter: cMeta?.otelIgnoreMeter || mMeta?.otelIgnoreMeter,
184
+ ignoreSpan: cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
185
+ attrs: {
186
+ 'moost.controller': moost.getConstructor(getController()).name,
187
+ 'moost.handler': methodName,
188
+ 'moost.handler_description': mMeta?.description,
189
+ 'moost.handler_label': mMeta?.label,
190
+ 'moost.handler_id': mMeta?.id,
191
+ 'moost.ignore': cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
192
+ 'moost.route': getRoute(),
193
+ 'moost.event_type': this.getEventType(),
194
+ },
195
+ };
196
+ }
197
+ hook(name, route) {
198
+ moost.useAsyncEventContext().store('otel').set('route', route);
199
+ const chm = this.getControllerHandlerMeta();
200
+ if (!chm.ignoreMeter) {
201
+ this.startEventMetrics(chm.attrs, route);
82
202
  }
83
203
  const { getSpan } = useOtelContext();
84
204
  const span = getSpan();
85
205
  if (span) {
86
- const { getMethod, getMethodMeta, getController, getControllerMeta } = moost.useControllerContext();
87
- const methodName = getMethod();
88
- const methodMeta = getMethodMeta();
89
- const cMeta = getControllerMeta();
90
- span.setAttributes({
91
- 'custom.event_type': eventType,
92
- 'custom.event_description': methodMeta?.description || methodMeta?.label || methodName,
93
- 'moost.controller': moost.getConstructor(getController()).name,
94
- 'moost.handler': methodName,
95
- 'moost.ignore': cMeta?.otelIgnoreSpan || methodMeta?.otelIgnoreSpan,
96
- });
97
- if (abortReason) {
98
- span.recordException(new Error(abortReason));
99
- span.setStatus({ code: api.SpanStatusCode.ERROR, message: abortReason });
100
- }
101
- if (eventType === 'HTTP') {
102
- const req = moost.useAsyncEventContext()
103
- .store('event')
104
- .get('req');
105
- span.updateName(`${req?.method || ''} ${methodMeta?.id || methodMeta?.label || methodName}`);
206
+ span.setAttributes(chm.attrs);
207
+ if (chm.attrs['moost.event_type'] === 'HTTP') {
208
+ span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
106
209
  }
107
210
  else {
108
- span.updateName(`${methodMeta?.label || methodName}`);
211
+ span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
212
+ }
213
+ }
214
+ }
215
+ withSpan(span, cb, opts) {
216
+ let result;
217
+ let exception;
218
+ const endSpan = (error) => {
219
+ if (error) {
220
+ span.recordException(error);
221
+ }
222
+ if (opts.rootSpan) {
223
+ const chm = this.getControllerHandlerMeta();
224
+ if (!chm.ignoreMeter) {
225
+ this.endEventMetrics(chm.attrs, error);
226
+ }
227
+ }
228
+ if (opts.endSpan) {
109
229
  span.end();
110
230
  }
231
+ };
232
+ api.context.with(api.trace.setSpan(api.context.active(), span), () => {
233
+ try {
234
+ result = cb();
235
+ }
236
+ catch (error) {
237
+ exception = error;
238
+ endSpan(exception);
239
+ }
240
+ });
241
+ const ret = result;
242
+ if (!exception) {
243
+ if (ret && ret instanceof Promise) {
244
+ ret
245
+ .then(r => {
246
+ endSpan();
247
+ return r;
248
+ })
249
+ .catch(error => {
250
+ endSpan(error);
251
+ });
252
+ }
253
+ else {
254
+ endSpan();
255
+ }
111
256
  }
112
- });
257
+ return ret;
258
+ }
259
+ startEventMetrics(a, route) {
260
+ if (a['moost.event_type'] === 'HTTP') {
261
+ const req = this.getRequest();
262
+ const attrs = {
263
+ ...a,
264
+ route,
265
+ url: req?.url,
266
+ };
267
+ moostMetrics.httpRequestCount.add(1, attrs);
268
+ moostMetrics.httpActiveRequests.add(1, attrs);
269
+ }
270
+ const attrs = {
271
+ ...a,
272
+ route,
273
+ };
274
+ moostMetrics.moostEventCount.add(1, attrs);
275
+ moostMetrics.moostActiveEvents.add(1, attrs);
276
+ }
277
+ endEventMetrics(a, error) {
278
+ const route = moost.useAsyncEventContext().store('otel').get('route');
279
+ if (a['moost.event_type'] === 'HTTP') {
280
+ const req = this.getRequest();
281
+ const res = this.getResponse();
282
+ const attrs = {
283
+ ...a,
284
+ route,
285
+ url: req?.url,
286
+ };
287
+ moostMetrics.httpActiveRequests.add(-1, attrs);
288
+ if (error) {
289
+ moostMetrics.httpErrorCount.add(1, attrs);
290
+ }
291
+ moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
292
+ moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
293
+ moostMetrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
294
+ }
295
+ const attrs = {
296
+ ...a,
297
+ route,
298
+ };
299
+ moostMetrics.moostActiveEvents.add(-1, attrs);
300
+ if (error) {
301
+ moostMetrics.moostErrorCount.add(1, attrs);
302
+ }
303
+ }
304
+ getRequest() {
305
+ return moost.useAsyncEventContext().store('event').get('req');
306
+ }
307
+ getResponse() {
308
+ return moost.useAsyncEventContext()
309
+ .store('event')
310
+ .get('res');
311
+ }
312
+ }
313
+
314
+ function enableOtelForMoost() {
315
+ moost.replaceContextInjector(new SpanInjector());
316
+ }
317
+
318
+ function getConstructor$1(instance) {
319
+ return isConstructor(instance) ?
320
+ instance : instance.constructor ?
321
+ instance.constructor : Object.getPrototypeOf(instance).constructor;
322
+ }
323
+ function isConstructor(v) {
324
+ return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
113
325
  }
114
326
 
327
+ const classMetadata = new WeakMap();
328
+ const paramMetadata = new WeakMap();
329
+ const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
330
+ function getMetaObject(target, prop) {
331
+ const isParam = typeof prop !== 'undefined';
332
+ const metadata = isParam ? paramMetadata : classMetadata;
333
+ const targetKey = getConstructor$1(target);
334
+ let meta = metadata.get(targetKey);
335
+ if (!meta) {
336
+ meta = {};
337
+ metadata.set(targetKey, meta);
338
+ }
339
+ if (isParam) {
340
+ meta = (meta[prop] =
341
+ meta[prop] || {});
342
+ }
343
+ return meta;
344
+ }
345
+ const _reflect = {
346
+ getOwnMetadata(key, target, prop) {
347
+ return getMetaObject(target, prop)[key];
348
+ },
349
+ defineMetadata(key, data, target, prop) {
350
+ const meta = getMetaObject(target, prop);
351
+ meta[key] = data;
352
+ },
353
+ metadata(key, data) {
354
+ return ((target, propKey) => {
355
+ Reflect$1.defineMetadata(key, data, target, propKey);
356
+ });
357
+ },
358
+ };
359
+ if (!root.Reflect) {
360
+ root.Reflect = _reflect;
361
+ }
362
+ else {
363
+ const funcs = [
364
+ 'getOwnMetadata',
365
+ 'defineMetadata',
366
+ 'metadata',
367
+ ];
368
+ const target = root.Reflect;
369
+ for (const func of funcs) {
370
+ if (typeof target[func] !== 'function') {
371
+ Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
372
+ }
373
+ }
374
+ }
375
+ const Reflect$1 = _reflect;
376
+
377
+ const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
378
+ class Mate {
379
+ constructor(workspace, options = {}) {
380
+ this.workspace = workspace;
381
+ this.options = options;
382
+ this.logger = options.logger || console;
383
+ }
384
+ set(args, key, value, isArray) {
385
+ var _a;
386
+ let level = 'CLASS';
387
+ const newArgs = args.level === 'CLASS' ? { target: args.target }
388
+ : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
389
+ : args;
390
+ let meta = (Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {});
391
+ if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
392
+ meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
393
+ }
394
+ if (newArgs.propKey && this.options.readType && !meta.type) {
395
+ meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
396
+ }
397
+ const { index } = newArgs;
398
+ const cb = typeof key === 'function' ? key : undefined;
399
+ let data = meta;
400
+ if (!data.params) {
401
+ data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
402
+ }
403
+ if (typeof index === 'number') {
404
+ level = 'PARAM';
405
+ data.params = data.params || [];
406
+ data.params[index] = data.params[index] || {
407
+ type: undefined,
408
+ };
409
+ if (cb) {
410
+ data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
411
+ }
412
+ else {
413
+ data = data.params[index];
414
+ }
415
+ }
416
+ else if (!index && !args.descriptor && args.propKey && this.options.collectPropKeys && args.level !== 'CLASS') {
417
+ this.set({ ...args, level: 'CLASS' }, (meta) => {
418
+ if (!meta.properties) {
419
+ meta.properties = [args.propKey];
420
+ }
421
+ else if (!meta.properties.includes(args.propKey)) {
422
+ meta.properties.push(args.propKey);
423
+ }
424
+ return meta;
425
+ });
426
+ }
427
+ level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
428
+ if (typeof key !== 'function') {
429
+ if (isArray) {
430
+ const newArray = (data[key] || []);
431
+ if (!Array.isArray(newArray)) {
432
+ this.logger.error('Mate.add (isArray=true) called for non-array metadata');
433
+ }
434
+ newArray.unshift(value);
435
+ data[key] = newArray;
436
+ }
437
+ else {
438
+ data[key] = value;
439
+ }
440
+ }
441
+ else if (cb && typeof index !== 'number') {
442
+ meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
443
+ }
444
+ Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
445
+ }
446
+ read(target, propKey) {
447
+ var _a;
448
+ const isConstr = isConstructor(target);
449
+ const constructor = isConstr ? target : getConstructor$1(target);
450
+ const proto = constructor.prototype;
451
+ let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
452
+ if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
453
+ const parent = Object.getPrototypeOf(constructor);
454
+ if (typeof parent === 'function' &&
455
+ parent !== fnProto &&
456
+ parent !== constructor) {
457
+ ownMeta.params = (_a = this.read(parent)) === null || _a === void 0 ? void 0 : _a.params;
458
+ }
459
+ }
460
+ if (this.options.inherit) {
461
+ const inheritFn = typeof this.options.inherit === 'function'
462
+ ? this.options.inherit
463
+ : undefined;
464
+ let shouldInherit = this.options.inherit;
465
+ if (inheritFn) {
466
+ if (typeof propKey === 'string') {
467
+ const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
468
+ shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
469
+ }
470
+ else {
471
+ shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
472
+ }
473
+ }
474
+ if (shouldInherit) {
475
+ const parent = Object.getPrototypeOf(constructor);
476
+ if (typeof parent === 'function' &&
477
+ parent !== fnProto &&
478
+ parent !== constructor) {
479
+ const inheritedMeta = (this.read(parent, propKey) ||
480
+ {});
481
+ const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
482
+ ownMeta = { ...inheritedMeta, ...ownMeta };
483
+ if (typeof propKey === 'string' &&
484
+ ownParams &&
485
+ (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
486
+ for (let i = 0; i < ownParams.length; i++) {
487
+ if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !==
488
+ 'undefined') {
489
+ const ownParam = ownParams[i];
490
+ if (ownMeta.params &&
491
+ inheritFn &&
492
+ inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string'
493
+ ? propKey
494
+ : undefined)) {
495
+ ownMeta.params[i] = {
496
+ ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
497
+ ...ownParams[i],
498
+ };
499
+ }
500
+ }
501
+ }
502
+ }
503
+ }
504
+ }
505
+ }
506
+ return ownMeta;
507
+ }
508
+ apply(...decorators) {
509
+ return ((target, propKey, descriptor) => {
510
+ for (const d of decorators) {
511
+ d(target, propKey, descriptor);
512
+ }
513
+ });
514
+ }
515
+ decorate(key, value, isArray, level) {
516
+ return ((target, propKey, descriptor) => {
517
+ const args = {
518
+ target,
519
+ propKey,
520
+ descriptor: typeof descriptor === 'number' ? undefined : descriptor,
521
+ index: typeof descriptor === 'number' ? descriptor : undefined,
522
+ level,
523
+ };
524
+ this.set(args, key, value, isArray);
525
+ });
526
+ }
527
+ decorateConditional(ccb) {
528
+ return ((target, propKey, descriptor) => {
529
+ const hasIndex = typeof descriptor === 'number';
530
+ const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
531
+ const d = ccb(decoratorLevel);
532
+ if (d) {
533
+ d(target, propKey, descriptor);
534
+ }
535
+ });
536
+ }
537
+ decorateClass(key, value, isArray) {
538
+ return this.decorate(key, value, isArray, 'CLASS');
539
+ }
540
+ }
541
+ const fnProto = Object.getPrototypeOf(Function);
542
+
115
543
  const defaultLevels = [
116
544
  'fatal',
117
545
  'error',
@@ -124,54 +552,6 @@ const defaultLevels = [
124
552
  const defaultMappedLevels = new Map();
125
553
  defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
126
554
 
127
- class Hookable {
128
- constructor() {
129
- this.hooks = {};
130
- }
131
- hook(name, cb) {
132
- if (!this.hooks[name]) {
133
- this.hooks[name] = [];
134
- }
135
- this.hooks[name].push(cb);
136
- return () => {
137
- this.unhook(name, cb);
138
- };
139
- }
140
- callHook(name, ...args) {
141
- if (this.hooks[name]) {
142
- for (const cb of this.hooks[name]) {
143
- try {
144
- cb(...args);
145
- }
146
- catch (error) {
147
- console.error(`Error in hook ${name}:`, error);
148
- }
149
- }
150
- }
151
- }
152
- unhook(name, cb) {
153
- if (this.hooks[name]) {
154
- this.hooks[name] = this.hooks[name].filter(hookCb => hookCb !== cb);
155
- }
156
- }
157
- }
158
-
159
- class EventContextHooks extends Hookable {
160
- fireStartEvent(eventType) {
161
- this.callHook('start-event', eventType);
162
- }
163
- fireEndEvent(eventType, abortReason) {
164
- this.callHook('end-event', eventType, abortReason);
165
- }
166
- onStartEvent(cb) {
167
- return this.hook('start-event', cb);
168
- }
169
- onEndEvent(cb) {
170
- return this.hook('end-event', cb);
171
- }
172
- }
173
- const eventContextHooks = new EventContextHooks();
174
-
175
555
  function attachHook(target, opts, name) {
176
556
  Object.defineProperty(target, name || 'value', {
177
557
  get: opts.get,
@@ -277,12 +657,6 @@ function _getCtxHelpers(cc) {
277
657
  const hasParentCtx = () => !!cc.parentCtx;
278
658
  return {
279
659
  getCtx,
280
- endEvent: (abortReason) => {
281
- if (cc && !cc._ended) {
282
- cc._ended = true;
283
- eventContextHooks.fireEndEvent(cc.event.type, abortReason);
284
- }
285
- },
286
660
  store,
287
661
  getStore: get,
288
662
  setStore: set,
@@ -306,7 +680,7 @@ function useEventId() {
306
680
  return { getId };
307
681
  }
308
682
 
309
- function getConstructor$1(instance) {
683
+ function getConstructor(instance) {
310
684
  return Object.getPrototypeOf(instance).constructor;
311
685
  }
312
686
 
@@ -594,8 +968,8 @@ class Infact {
594
968
  case 'string':
595
969
  return `"${''}...${''}"`;
596
970
  case 'object':
597
- if (getConstructor$1(p))
598
- return getConstructor$1(p).name;
971
+ if (getConstructor(p))
972
+ return getConstructor(p).name;
599
973
  return '{}';
600
974
  default:
601
975
  return '*';
@@ -651,231 +1025,6 @@ async function runPipes(pipes, initialValue, metas, level) {
651
1025
  return v;
652
1026
  }
653
1027
 
654
- function getConstructor(instance) {
655
- return isConstructor(instance) ?
656
- instance : instance.constructor ?
657
- instance.constructor : Object.getPrototypeOf(instance).constructor;
658
- }
659
- function isConstructor(v) {
660
- return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
661
- }
662
-
663
- const classMetadata = new WeakMap();
664
- const paramMetadata = new WeakMap();
665
- const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
666
- function getMetaObject(target, prop) {
667
- const isParam = typeof prop !== 'undefined';
668
- const metadata = isParam ? paramMetadata : classMetadata;
669
- const targetKey = getConstructor(target);
670
- let meta = metadata.get(targetKey);
671
- if (!meta) {
672
- meta = {};
673
- metadata.set(targetKey, meta);
674
- }
675
- if (isParam) {
676
- meta = (meta[prop] =
677
- meta[prop] || {});
678
- }
679
- return meta;
680
- }
681
- const _reflect = {
682
- getOwnMetadata(key, target, prop) {
683
- return getMetaObject(target, prop)[key];
684
- },
685
- defineMetadata(key, data, target, prop) {
686
- const meta = getMetaObject(target, prop);
687
- meta[key] = data;
688
- },
689
- metadata(key, data) {
690
- return ((target, propKey) => {
691
- Reflect$1.defineMetadata(key, data, target, propKey);
692
- });
693
- },
694
- };
695
- if (!root.Reflect) {
696
- root.Reflect = _reflect;
697
- }
698
- else {
699
- const funcs = [
700
- 'getOwnMetadata',
701
- 'defineMetadata',
702
- 'metadata',
703
- ];
704
- const target = root.Reflect;
705
- for (const func of funcs) {
706
- if (typeof target[func] !== 'function') {
707
- Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
708
- }
709
- }
710
- }
711
- const Reflect$1 = _reflect;
712
-
713
- const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
714
- class Mate {
715
- constructor(workspace, options = {}) {
716
- this.workspace = workspace;
717
- this.options = options;
718
- this.logger = options.logger || console;
719
- }
720
- set(args, key, value, isArray) {
721
- var _a;
722
- let level = 'CLASS';
723
- const newArgs = args.level === 'CLASS' ? { target: args.target }
724
- : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
725
- : args;
726
- let meta = (Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {});
727
- if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
728
- meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
729
- }
730
- if (newArgs.propKey && this.options.readType && !meta.type) {
731
- meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
732
- }
733
- const { index } = newArgs;
734
- const cb = typeof key === 'function' ? key : undefined;
735
- let data = meta;
736
- if (!data.params) {
737
- data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
738
- }
739
- if (typeof index === 'number') {
740
- level = 'PARAM';
741
- data.params = data.params || [];
742
- data.params[index] = data.params[index] || {
743
- type: undefined,
744
- };
745
- if (cb) {
746
- data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
747
- }
748
- else {
749
- data = data.params[index];
750
- }
751
- }
752
- else if (!index && !args.descriptor && args.propKey && this.options.collectPropKeys && args.level !== 'CLASS') {
753
- this.set({ ...args, level: 'CLASS' }, (meta) => {
754
- if (!meta.properties) {
755
- meta.properties = [args.propKey];
756
- }
757
- else if (!meta.properties.includes(args.propKey)) {
758
- meta.properties.push(args.propKey);
759
- }
760
- return meta;
761
- });
762
- }
763
- level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
764
- if (typeof key !== 'function') {
765
- if (isArray) {
766
- const newArray = (data[key] || []);
767
- if (!Array.isArray(newArray)) {
768
- this.logger.error('Mate.add (isArray=true) called for non-array metadata');
769
- }
770
- newArray.unshift(value);
771
- data[key] = newArray;
772
- }
773
- else {
774
- data[key] = value;
775
- }
776
- }
777
- else if (cb && typeof index !== 'number') {
778
- meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
779
- }
780
- Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
781
- }
782
- read(target, propKey) {
783
- var _a;
784
- const isConstr = isConstructor(target);
785
- const constructor = isConstr ? target : getConstructor(target);
786
- const proto = constructor.prototype;
787
- let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
788
- if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
789
- const parent = Object.getPrototypeOf(constructor);
790
- if (typeof parent === 'function' &&
791
- parent !== fnProto &&
792
- parent !== constructor) {
793
- ownMeta.params = (_a = this.read(parent)) === null || _a === void 0 ? void 0 : _a.params;
794
- }
795
- }
796
- if (this.options.inherit) {
797
- const inheritFn = typeof this.options.inherit === 'function'
798
- ? this.options.inherit
799
- : undefined;
800
- let shouldInherit = this.options.inherit;
801
- if (inheritFn) {
802
- if (typeof propKey === 'string') {
803
- const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
804
- shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
805
- }
806
- else {
807
- shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
808
- }
809
- }
810
- if (shouldInherit) {
811
- const parent = Object.getPrototypeOf(constructor);
812
- if (typeof parent === 'function' &&
813
- parent !== fnProto &&
814
- parent !== constructor) {
815
- const inheritedMeta = (this.read(parent, propKey) ||
816
- {});
817
- const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
818
- ownMeta = { ...inheritedMeta, ...ownMeta };
819
- if (typeof propKey === 'string' &&
820
- ownParams &&
821
- (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
822
- for (let i = 0; i < ownParams.length; i++) {
823
- if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !==
824
- 'undefined') {
825
- const ownParam = ownParams[i];
826
- if (ownMeta.params &&
827
- inheritFn &&
828
- inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string'
829
- ? propKey
830
- : undefined)) {
831
- ownMeta.params[i] = {
832
- ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
833
- ...ownParams[i],
834
- };
835
- }
836
- }
837
- }
838
- }
839
- }
840
- }
841
- }
842
- return ownMeta;
843
- }
844
- apply(...decorators) {
845
- return ((target, propKey, descriptor) => {
846
- for (const d of decorators) {
847
- d(target, propKey, descriptor);
848
- }
849
- });
850
- }
851
- decorate(key, value, isArray, level) {
852
- return ((target, propKey, descriptor) => {
853
- const args = {
854
- target,
855
- propKey,
856
- descriptor: typeof descriptor === 'number' ? undefined : descriptor,
857
- index: typeof descriptor === 'number' ? descriptor : undefined,
858
- level,
859
- };
860
- this.set(args, key, value, isArray);
861
- });
862
- }
863
- decorateConditional(ccb) {
864
- return ((target, propKey, descriptor) => {
865
- const hasIndex = typeof descriptor === 'number';
866
- const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
867
- const d = ccb(decoratorLevel);
868
- if (d) {
869
- d(target, propKey, descriptor);
870
- }
871
- });
872
- }
873
- decorateClass(key, value, isArray) {
874
- return this.decorate(key, value, isArray, 'CLASS');
875
- }
876
- }
877
- const fnProto = Object.getPrototypeOf(Function);
878
-
879
1028
  const METADATA_WORKSPACE = 'moost';
880
1029
  const moostMate = new Mate(METADATA_WORKSPACE, {
881
1030
  readType: true,
@@ -998,6 +1147,7 @@ function getOtelMate() {
998
1147
 
999
1148
  const mate = getOtelMate();
1000
1149
  const OtelIgnoreSpan = () => mate.decorate('otelIgnoreSpan', true);
1150
+ const OtelIgnoreMeter = () => mate.decorate('otelIgnoreMeter', true);
1001
1151
 
1002
1152
  function shouldSpanBeIgnored(span) {
1003
1153
  return span.attributes['moost.ignore'] === true;
@@ -1023,7 +1173,9 @@ class MoostSimpleSpanProcessor extends sdkTraceBase.SimpleSpanProcessor {
1023
1173
 
1024
1174
  exports.MoostBatchSpanProcessor = MoostBatchSpanProcessor;
1025
1175
  exports.MoostSimpleSpanProcessor = MoostSimpleSpanProcessor;
1176
+ exports.OtelIgnoreMeter = OtelIgnoreMeter;
1026
1177
  exports.OtelIgnoreSpan = OtelIgnoreSpan;
1178
+ exports.SpanInjector = SpanInjector;
1027
1179
  exports.enableOtelForMoost = enableOtelForMoost;
1028
1180
  exports.getOtelMate = getOtelMate;
1029
1181
  exports.shouldSpanBeIgnored = shouldSpanBeIgnored;