@moostjs/otel 0.4.7 → 0.4.9

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,472 @@ function useOtelPropagation() {
58
78
  };
59
79
  }
60
80
 
61
- function enableOtelForMoost() {
62
- moost.eventContextHooks.onStartEvent((eventType) => {
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(attrs.eventType, fn);
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(eventType, cb) {
63
151
  if (eventType === 'init') {
64
- return;
152
+ return cb();
65
153
  }
66
154
  const { registerSpan } = useOtelContext();
67
- let span;
155
+ let span = api.trace.getActiveSpan();
68
156
  if (eventType === 'HTTP') {
69
- span = api.trace.getSpan(api.context.active());
157
+ this.patchRsponse();
70
158
  }
71
159
  else {
72
- const tracer = api.trace.getTracer('moost-tracer');
73
160
  span = tracer.startSpan(`${eventType} Event`);
74
161
  }
75
162
  if (span) {
76
163
  registerSpan(span);
164
+ return this.withSpan(span, cb, {
165
+ rootSpan: true,
166
+ endSpan: eventType !== 'HTTP',
167
+ });
77
168
  }
78
- });
79
- moost.eventContextHooks.onEndEvent((eventType, abortReason) => {
80
- if (eventType === 'init') {
81
- return;
169
+ return cb();
170
+ }
171
+ getEventType() {
172
+ return moost.useAsyncEventContext().getCtx().event.type;
173
+ }
174
+ getIgnoreSpan() {
175
+ const { getMethodMeta, getControllerMeta } = moost.useControllerContext();
176
+ const cMeta = getControllerMeta();
177
+ const mMeta = getMethodMeta();
178
+ return cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan;
179
+ }
180
+ getControllerHandlerMeta() {
181
+ const { getMethod, getMethodMeta, getController, getControllerMeta, getRoute } = moost.useControllerContext();
182
+ const methodName = getMethod();
183
+ const controller = getController();
184
+ const cMeta = controller ? getControllerMeta() : undefined;
185
+ const mMeta = controller ? getMethodMeta() : undefined;
186
+ return {
187
+ ignoreMeter: cMeta?.otelIgnoreMeter || mMeta?.otelIgnoreMeter,
188
+ ignoreSpan: cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
189
+ attrs: {
190
+ 'moost.controller': controller ? moost.getConstructor(controller).name : undefined,
191
+ 'moost.handler': methodName,
192
+ 'moost.handler_description': mMeta?.description,
193
+ 'moost.handler_label': mMeta?.label,
194
+ 'moost.handler_id': mMeta?.id,
195
+ 'moost.ignore': cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
196
+ 'moost.route': getRoute(),
197
+ 'moost.event_type': this.getEventType(),
198
+ },
199
+ };
200
+ }
201
+ hook(name, route) {
202
+ moost.useAsyncEventContext().store('otel').set('route', route);
203
+ const chm = this.getControllerHandlerMeta();
204
+ if (!chm.ignoreMeter) {
205
+ this.startEventMetrics(chm.attrs, route);
82
206
  }
83
207
  const { getSpan } = useOtelContext();
84
208
  const span = getSpan();
85
209
  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}`);
210
+ span.setAttributes(chm.attrs);
211
+ if (chm.attrs['moost.event_type'] === 'HTTP') {
212
+ span.updateName(`${this.getRequest()?.method || ''} ${route || '<unresolved>'}`);
106
213
  }
107
214
  else {
108
- span.updateName(`${methodMeta?.label || methodName}`);
215
+ span.updateName(`${chm.attrs['moost.event_type']} ${route || '<unresolved>'}`);
216
+ }
217
+ }
218
+ }
219
+ withSpan(span, cb, opts) {
220
+ let result;
221
+ let exception;
222
+ const endSpan = (error) => {
223
+ if (error) {
224
+ span.recordException(error);
225
+ }
226
+ if (opts.rootSpan) {
227
+ const chm = this.getControllerHandlerMeta();
228
+ if (!chm.ignoreMeter) {
229
+ this.endEventMetrics(chm.attrs, error);
230
+ }
231
+ }
232
+ if (opts.endSpan) {
109
233
  span.end();
110
234
  }
235
+ };
236
+ api.context.with(api.trace.setSpan(api.context.active(), span), () => {
237
+ try {
238
+ result = cb();
239
+ }
240
+ catch (error) {
241
+ exception = error;
242
+ endSpan(exception);
243
+ }
244
+ });
245
+ const ret = result;
246
+ if (!exception) {
247
+ if (ret && ret instanceof Promise) {
248
+ ret
249
+ .then(r => {
250
+ endSpan();
251
+ return r;
252
+ })
253
+ .catch(error => {
254
+ endSpan(error);
255
+ });
256
+ }
257
+ else {
258
+ endSpan();
259
+ }
111
260
  }
112
- });
261
+ return ret;
262
+ }
263
+ startEventMetrics(a, route) {
264
+ if (a['moost.event_type'] === 'HTTP') {
265
+ const req = this.getRequest();
266
+ const attrs = {
267
+ ...a,
268
+ route,
269
+ url: req?.url,
270
+ };
271
+ moostMetrics.httpRequestCount.add(1, attrs);
272
+ moostMetrics.httpActiveRequests.add(1, attrs);
273
+ }
274
+ const attrs = {
275
+ ...a,
276
+ route,
277
+ };
278
+ moostMetrics.moostEventCount.add(1, attrs);
279
+ moostMetrics.moostActiveEvents.add(1, attrs);
280
+ }
281
+ endEventMetrics(a, error) {
282
+ const route = moost.useAsyncEventContext().store('otel').get('route');
283
+ if (a['moost.event_type'] === 'HTTP') {
284
+ const req = this.getRequest();
285
+ const res = this.getResponse();
286
+ const attrs = {
287
+ ...a,
288
+ route,
289
+ url: req?.url,
290
+ };
291
+ moostMetrics.httpActiveRequests.add(-1, attrs);
292
+ if (error) {
293
+ moostMetrics.httpErrorCount.add(1, attrs);
294
+ }
295
+ moostMetrics.httpRequestSize.record(req?.socket.bytesRead || 0, attrs);
296
+ moostMetrics.httpResponseSize.record(res?._contentLength || 0, attrs);
297
+ moostMetrics.httpResponseCount.add(1, { ...attrs, status: res?._statusCode });
298
+ }
299
+ const attrs = {
300
+ ...a,
301
+ route,
302
+ };
303
+ moostMetrics.moostActiveEvents.add(-1, attrs);
304
+ if (error) {
305
+ moostMetrics.moostErrorCount.add(1, attrs);
306
+ }
307
+ }
308
+ getRequest() {
309
+ return moost.useAsyncEventContext().store('event').get('req');
310
+ }
311
+ getResponse() {
312
+ return moost.useAsyncEventContext()
313
+ .store('event')
314
+ .get('res');
315
+ }
316
+ }
317
+
318
+ function enableOtelForMoost() {
319
+ moost.replaceContextInjector(new SpanInjector());
320
+ }
321
+
322
+ function getConstructor$1(instance) {
323
+ return isConstructor(instance) ?
324
+ instance : instance.constructor ?
325
+ instance.constructor : Object.getPrototypeOf(instance).constructor;
326
+ }
327
+ function isConstructor(v) {
328
+ return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
113
329
  }
114
330
 
331
+ const classMetadata = new WeakMap();
332
+ const paramMetadata = new WeakMap();
333
+ const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
334
+ function getMetaObject(target, prop) {
335
+ const isParam = typeof prop !== 'undefined';
336
+ const metadata = isParam ? paramMetadata : classMetadata;
337
+ const targetKey = getConstructor$1(target);
338
+ let meta = metadata.get(targetKey);
339
+ if (!meta) {
340
+ meta = {};
341
+ metadata.set(targetKey, meta);
342
+ }
343
+ if (isParam) {
344
+ meta = (meta[prop] =
345
+ meta[prop] || {});
346
+ }
347
+ return meta;
348
+ }
349
+ const _reflect = {
350
+ getOwnMetadata(key, target, prop) {
351
+ return getMetaObject(target, prop)[key];
352
+ },
353
+ defineMetadata(key, data, target, prop) {
354
+ const meta = getMetaObject(target, prop);
355
+ meta[key] = data;
356
+ },
357
+ metadata(key, data) {
358
+ return ((target, propKey) => {
359
+ Reflect$1.defineMetadata(key, data, target, propKey);
360
+ });
361
+ },
362
+ };
363
+ if (!root.Reflect) {
364
+ root.Reflect = _reflect;
365
+ }
366
+ else {
367
+ const funcs = [
368
+ 'getOwnMetadata',
369
+ 'defineMetadata',
370
+ 'metadata',
371
+ ];
372
+ const target = root.Reflect;
373
+ for (const func of funcs) {
374
+ if (typeof target[func] !== 'function') {
375
+ Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
376
+ }
377
+ }
378
+ }
379
+ const Reflect$1 = _reflect;
380
+
381
+ const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
382
+ class Mate {
383
+ constructor(workspace, options = {}) {
384
+ this.workspace = workspace;
385
+ this.options = options;
386
+ this.logger = options.logger || console;
387
+ }
388
+ set(args, key, value, isArray) {
389
+ var _a;
390
+ let level = 'CLASS';
391
+ const newArgs = args.level === 'CLASS' ? { target: args.target }
392
+ : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
393
+ : args;
394
+ let meta = (Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {});
395
+ if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
396
+ meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
397
+ }
398
+ if (newArgs.propKey && this.options.readType && !meta.type) {
399
+ meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
400
+ }
401
+ const { index } = newArgs;
402
+ const cb = typeof key === 'function' ? key : undefined;
403
+ let data = meta;
404
+ if (!data.params) {
405
+ data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
406
+ }
407
+ if (typeof index === 'number') {
408
+ level = 'PARAM';
409
+ data.params = data.params || [];
410
+ data.params[index] = data.params[index] || {
411
+ type: undefined,
412
+ };
413
+ if (cb) {
414
+ data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
415
+ }
416
+ else {
417
+ data = data.params[index];
418
+ }
419
+ }
420
+ else if (!index && !args.descriptor && args.propKey && this.options.collectPropKeys && args.level !== 'CLASS') {
421
+ this.set({ ...args, level: 'CLASS' }, (meta) => {
422
+ if (!meta.properties) {
423
+ meta.properties = [args.propKey];
424
+ }
425
+ else if (!meta.properties.includes(args.propKey)) {
426
+ meta.properties.push(args.propKey);
427
+ }
428
+ return meta;
429
+ });
430
+ }
431
+ level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
432
+ if (typeof key !== 'function') {
433
+ if (isArray) {
434
+ const newArray = (data[key] || []);
435
+ if (!Array.isArray(newArray)) {
436
+ this.logger.error('Mate.add (isArray=true) called for non-array metadata');
437
+ }
438
+ newArray.unshift(value);
439
+ data[key] = newArray;
440
+ }
441
+ else {
442
+ data[key] = value;
443
+ }
444
+ }
445
+ else if (cb && typeof index !== 'number') {
446
+ meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
447
+ }
448
+ Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
449
+ }
450
+ read(target, propKey) {
451
+ var _a;
452
+ const isConstr = isConstructor(target);
453
+ const constructor = isConstr ? target : getConstructor$1(target);
454
+ const proto = constructor.prototype;
455
+ let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
456
+ if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
457
+ const parent = Object.getPrototypeOf(constructor);
458
+ if (typeof parent === 'function' &&
459
+ parent !== fnProto &&
460
+ parent !== constructor) {
461
+ ownMeta.params = (_a = this.read(parent)) === null || _a === void 0 ? void 0 : _a.params;
462
+ }
463
+ }
464
+ if (this.options.inherit) {
465
+ const inheritFn = typeof this.options.inherit === 'function'
466
+ ? this.options.inherit
467
+ : undefined;
468
+ let shouldInherit = this.options.inherit;
469
+ if (inheritFn) {
470
+ if (typeof propKey === 'string') {
471
+ const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
472
+ shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
473
+ }
474
+ else {
475
+ shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
476
+ }
477
+ }
478
+ if (shouldInherit) {
479
+ const parent = Object.getPrototypeOf(constructor);
480
+ if (typeof parent === 'function' &&
481
+ parent !== fnProto &&
482
+ parent !== constructor) {
483
+ const inheritedMeta = (this.read(parent, propKey) ||
484
+ {});
485
+ const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
486
+ ownMeta = { ...inheritedMeta, ...ownMeta };
487
+ if (typeof propKey === 'string' &&
488
+ ownParams &&
489
+ (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
490
+ for (let i = 0; i < ownParams.length; i++) {
491
+ if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !==
492
+ 'undefined') {
493
+ const ownParam = ownParams[i];
494
+ if (ownMeta.params &&
495
+ inheritFn &&
496
+ inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string'
497
+ ? propKey
498
+ : undefined)) {
499
+ ownMeta.params[i] = {
500
+ ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
501
+ ...ownParams[i],
502
+ };
503
+ }
504
+ }
505
+ }
506
+ }
507
+ }
508
+ }
509
+ }
510
+ return ownMeta;
511
+ }
512
+ apply(...decorators) {
513
+ return ((target, propKey, descriptor) => {
514
+ for (const d of decorators) {
515
+ d(target, propKey, descriptor);
516
+ }
517
+ });
518
+ }
519
+ decorate(key, value, isArray, level) {
520
+ return ((target, propKey, descriptor) => {
521
+ const args = {
522
+ target,
523
+ propKey,
524
+ descriptor: typeof descriptor === 'number' ? undefined : descriptor,
525
+ index: typeof descriptor === 'number' ? descriptor : undefined,
526
+ level,
527
+ };
528
+ this.set(args, key, value, isArray);
529
+ });
530
+ }
531
+ decorateConditional(ccb) {
532
+ return ((target, propKey, descriptor) => {
533
+ const hasIndex = typeof descriptor === 'number';
534
+ const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
535
+ const d = ccb(decoratorLevel);
536
+ if (d) {
537
+ d(target, propKey, descriptor);
538
+ }
539
+ });
540
+ }
541
+ decorateClass(key, value, isArray) {
542
+ return this.decorate(key, value, isArray, 'CLASS');
543
+ }
544
+ }
545
+ const fnProto = Object.getPrototypeOf(Function);
546
+
115
547
  const defaultLevels = [
116
548
  'fatal',
117
549
  'error',
@@ -124,54 +556,6 @@ const defaultLevels = [
124
556
  const defaultMappedLevels = new Map();
125
557
  defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
126
558
 
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
559
  function attachHook(target, opts, name) {
176
560
  Object.defineProperty(target, name || 'value', {
177
561
  get: opts.get,
@@ -277,12 +661,6 @@ function _getCtxHelpers(cc) {
277
661
  const hasParentCtx = () => !!cc.parentCtx;
278
662
  return {
279
663
  getCtx,
280
- endEvent: (abortReason) => {
281
- if (cc && !cc._ended) {
282
- cc._ended = true;
283
- eventContextHooks.fireEndEvent(cc.event.type, abortReason);
284
- }
285
- },
286
664
  store,
287
665
  getStore: get,
288
666
  setStore: set,
@@ -306,7 +684,7 @@ function useEventId() {
306
684
  return { getId };
307
685
  }
308
686
 
309
- function getConstructor$1(instance) {
687
+ function getConstructor(instance) {
310
688
  return Object.getPrototypeOf(instance).constructor;
311
689
  }
312
690
 
@@ -594,8 +972,8 @@ class Infact {
594
972
  case 'string':
595
973
  return `"${''}...${''}"`;
596
974
  case 'object':
597
- if (getConstructor$1(p))
598
- return getConstructor$1(p).name;
975
+ if (getConstructor(p))
976
+ return getConstructor(p).name;
599
977
  return '{}';
600
978
  default:
601
979
  return '*';
@@ -651,231 +1029,6 @@ async function runPipes(pipes, initialValue, metas, level) {
651
1029
  return v;
652
1030
  }
653
1031
 
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
1032
  const METADATA_WORKSPACE = 'moost';
880
1033
  const moostMate = new Mate(METADATA_WORKSPACE, {
881
1034
  readType: true,
@@ -998,6 +1151,7 @@ function getOtelMate() {
998
1151
 
999
1152
  const mate = getOtelMate();
1000
1153
  const OtelIgnoreSpan = () => mate.decorate('otelIgnoreSpan', true);
1154
+ const OtelIgnoreMeter = () => mate.decorate('otelIgnoreMeter', true);
1001
1155
 
1002
1156
  function shouldSpanBeIgnored(span) {
1003
1157
  return span.attributes['moost.ignore'] === true;
@@ -1023,7 +1177,9 @@ class MoostSimpleSpanProcessor extends sdkTraceBase.SimpleSpanProcessor {
1023
1177
 
1024
1178
  exports.MoostBatchSpanProcessor = MoostBatchSpanProcessor;
1025
1179
  exports.MoostSimpleSpanProcessor = MoostSimpleSpanProcessor;
1180
+ exports.OtelIgnoreMeter = OtelIgnoreMeter;
1026
1181
  exports.OtelIgnoreSpan = OtelIgnoreSpan;
1182
+ exports.SpanInjector = SpanInjector;
1027
1183
  exports.enableOtelForMoost = enableOtelForMoost;
1028
1184
  exports.getOtelMate = getOtelMate;
1029
1185
  exports.shouldSpanBeIgnored = shouldSpanBeIgnored;